|
1 | | -## A collection of algorithms and augmentations for STL-compatible containers |
| 1 | +# Container utilities |
2 | 2 |
|
3 | | -### algorithms.hpp |
| 3 | +Algorithms, adapters, and containers for STL-compatible code. |
4 | 4 |
|
5 | | -Defines two functions in `ContainerAlgorithms` namespace that are constructed upon the standard algorithms: |
6 | | -* `void erase_all_occurrences(ContainerType& container, const ArgumentType& item)` deletes every occurrence of the `item` in `container` using the remove-erase idiom. |
7 | | -* `void void erase_if(ContainerType& container, std::function<bool(const ItemType&)>` deletes every occurrence of the `item` in `container` using the remove-erase idiom. |
| 5 | +## Facilities |
8 | 6 |
|
9 | | -### flat_map.hpp |
| 7 | +| Header | Facility | |
| 8 | +|---|---| |
| 9 | +| `algorithms.hpp` | `ContainerAlgorithms::erase_if()` applies the remove/erase idiom to sequence containers. | |
| 10 | +| `flat_map.hpp` | Vector-backed sorted `flat_map` and `flat_set` with heterogeneous lookup, random-access iteration, ordinary insertion/erasure, sorted-range merging, and batched unsorted insertion. | |
| 11 | +| `iterator_helpers.hpp` | `const_forward_iterator_wrapper` retains both an iterator and its parent container, allowing validity and end checks; factories provide wrapped `cbegin`/`cend`. | |
| 12 | +| `multi_index.hpp` | `MultiIndexSet` owns values uniquely by one member and maintains a non-unique secondary-member index with exact and range lookup. | |
| 13 | +| `multimap_helpers.hpp` | `multimap_value_iterator` adapts a multimap iterator to expose only its mapped value while retaining access to the native iterator. | |
| 14 | +| `ordered_containers.hpp` | `ordered_container` adds explicit sorting, lower-bound lookup, and unique sorted insertion to sequence containers. | |
| 15 | +| `set_operations.hpp` | Common-prefix, deduplication, difference, three-way diff, and order-insensitive equality algorithms. | |
| 16 | +| `std_container_helpers.hpp` | Begin/end pair macros, transparent `std::set`, and helpers selecting `push_back`/`insert` or member/linear lookup according to container capabilities. | |
| 17 | +| `tracking_allocator.hpp` | Standard allocator wrapper that reports the bytes currently allocated through that allocator instance. | |
| 18 | +| `vector2d.hpp` | Rectangular `vector<vector<T>>` helper with two-dimensional resize, row fill, width, and height. | |
10 | 19 |
|
11 | | -Defines `flat_map` and `flat_set`, sorted associative containers backed by vectors. `flat_map` keeps keys and mapped |
12 | | -values in separate vectors and exposes pair-like proxy iterators with `first`/`second` and `key()`/`value()` access. |
13 | | -Both containers support ordinary insertion, merging from a sorted range, and batched unsorted appends followed |
14 | | -by tail sorting and merging. Existing entries and the first newly inserted entry win duplicate keys. Key equality uses |
15 | | -`operator==` when the compared types provide it, otherwise comparator equivalence. When both operations are available, |
16 | | -they must define the same equivalence. Batch entries are added with `append_unsorted()`; ordered operations and |
17 | | -iteration must not be used between `begin_batch()` and `end_batch()`. |
18 | | -Map dereference returns its proxy by value, so `auto entry` and `const auto& entry` work in range loops but `auto& entry` |
19 | | -does not. Read-only standard algorithms and construction of ordinary pair containers are supported; algorithms that |
20 | | -reorder entries are intentionally ill-formed because keys are immutable. |
| 20 | +## Flat associative containers |
21 | 21 |
|
22 | | -### iterator_helpers.hpp |
| 22 | +`flat_map` stores keys and mapped values in separate vectors and exposes pair-like proxy iterators with `first`/`second` and `key()`/`value()` access. Map dereference returns its proxy by value: `auto entry` and `const auto& entry` work in range loops, but `auto& entry` does not. Keys remain immutable, so read-only standard algorithms and construction of ordinary pair containers work while algorithms that reorder entries are intentionally ill-formed. |
23 | 23 |
|
24 | | -Defines two classes `const_forward_iterator_wrapper` and `forward_iterator_wrapper` that encapsulate an std (or std-compatible) iterator together with a reference to the container this iterator points to. This allows using these iterators as any other normal iterator while also being able to get the parent container from them. |
| 24 | +Both flat containers support ordinary insertion, merging a sorted range, `append_sorted_unique()`, and batched unsorted appends. Between `begin_batch()` and `end_batch()`, ordered operations and iteration are invalid. Finalization sorts only the appended tail and merges it with the existing prefix. Existing entries win conflicts with a batch, and the first batch entry wins duplicates within that batch. |
25 | 25 |
|
26 | | -### ordered_containers.hpp |
| 26 | +Key equality uses `operator==` when the compared types provide it and comparator equivalence otherwise. When both are available, they must describe the same equivalence relation. |
27 | 27 |
|
28 | | -Defines `ordered_container` class that wraps an STL-compatible container. It is intended for use with containers that aren't sorted by nature (e. g. vector or list as opposed to map or set), and provides three extra methods: `sort()`, `find(value)` and `insert_into_sorted(value)`. The `find` and `insert_into_sorted` methods require that container is sorted, and for such a container they provide optimized implementation using `std::lower_bound`. The `insert_into_sorted` method returns `std::pair<iterator, bool>` similar to the standard ordered containers. |
| 28 | +## Set operations |
29 | 29 |
|
30 | | -### set_operations.hpp |
31 | | - |
32 | | -Defines a number of algorithms on containers in `SetOperations` namespace: |
33 | | -* `OrderedSetType longestCommonStart(SupersetType<OrderedSetType> const & superset)` takes a set of ordered containers and returns the longest common starting sequence of items between all of these ordered containers. |
34 | | -Example 1: `longestCommonStart(std::vector{std::vector<int>{1, 2, 3, 4, 5}, std::vector<int>{1, 2, 3, 10, 20}})` -> `std::vector{1, 2}` |
35 | | -Example 2: `longestCommonStart(std::vector{std::string("Hello"), std::string("Heat"), std::string("Home")})` -> `std::string("H")` |
36 | | -* `template <class ContainerType> ContainerType uniqueElements(const ContainerType& c)` returns only the unique items from `c`. This function is stable (item order is preserved). Has no-op overloads for `set` and `map` which may only contain unique items by definition. |
37 | | -* `setTheoreticDifference` takes two containers `a` and `b` and an optional comparator, and returns a container of all the elements from `a` that are not in `b`. |
38 | | -Example: `setTheoreticDifference<std::list>(std::vector<int> {1, 2, 3}, std::deque<int> {3, 1})` -> `std::list<int> {2}` |
39 | | -It is assumed that the containers are unordered because for ordered containers `std::set_difference` can be called directly. |
40 | | -* `calculateDiff` takes two containers `a` and `b` and an optional template argument specifying the output container type. It returns the following structure: |
41 | | - ```template <class OutputContainerType> |
42 | | - struct Diff |
43 | | - { |
44 | | - OutputContainerType common_elements; |
45 | | - OutputContainerType elements_from_a_not_in_b; |
46 | | - OutputContainerType elements_from_b_not_in_a; |
47 | | - }; |
48 | | -
|
49 | | -### std_container_helpers.hpp |
50 | | -
|
51 | | -Defines two functions that behave differently depending on what container they're called with: |
52 | | -* `void add_item(Container& container, const ItemType& item)` calls `push_back(item)` for containers that have push_back (ordered containers), and `insert(item)` for other (unordered) containers. |
53 | | -* `auto container_aware_find(Container& container, const ItemType& item)` calls `container.find(item)` for containers that have a member function `find`, calls `std::find` otherwise. |
54 | | -
|
55 | | -### string_helpers.hpp |
56 | | -
|
57 | | -Defines `bool operator==(const std::string str, const char ch)` and `bool operator==(const char ch, const std::string str)` for comparing a string with to a single character. |
| 30 | +- `longestCommonStart()` returns the longest shared prefix of a container of ordered containers: `std::vector<std::string>{"Hello", "Heat", "Home"}` produces `"H"`. |
| 31 | +- `uniqueElements<ItemOrder>()` removes duplicates, optionally retaining the first or last occurrence order. The `std::set` overload is a no-op reference return. |
| 32 | +- `setTheoreticDifference<OutputContainer>()` sorts copies of two unordered inputs and returns the elements present only in the first. |
| 33 | +- `calculateDiff()` returns `common_elements`, `elements_from_a_not_in_b`, and `elements_from_b_not_in_a`. |
| 34 | +- `is_equal_sets()` compares compatible containers without regard to order; it sorts non-`std::set` inputs in place. |
0 commit comments