9 #ifndef CPPCOMMON_CONTAINERS_FLATMAP_H
10 #define CPPCOMMON_CONTAINERS_FLATMAP_H
31 template <
typename TKey,
typename TValue,
typename TCompare = std::less<TKey>,
typename TAllocator = std::allocator<std::pair<TKey, TValue>>>
45 typedef typename std::vector<value_type, TAllocator>::iterator
iterator;
46 typedef typename std::vector<value_type, TAllocator>::const_iterator
const_iterator;
47 typedef typename std::vector<value_type, TAllocator>::reverse_iterator
reverse_iterator;
56 explicit FlatMap(
size_t capacity = 128,
const TCompare&
compare = TCompare(),
const TAllocator& allocator = TAllocator());
57 template <
class InputIterator>
58 FlatMap(InputIterator first, InputIterator last,
bool unused,
size_t capacity = 128,
const TCompare&
compare = TCompare(),
const TAllocator& allocator = TAllocator());
68 explicit operator
bool() const noexcept {
return !
empty(); }
74 bool empty() const noexcept {
return _container.empty(); }
77 size_t capacity() const noexcept {
return _container.capacity(); }
79 size_t size() const noexcept {
return _container.size(); }
81 size_t max_size() const noexcept {
return _container.max_size(); }
84 bool compare(
const TKey& key1,
const TKey& key2)
const noexcept {
return _compare(key1, key2); }
85 bool compare(
const TKey& key1,
const value_type& key2)
const noexcept {
return _compare(key1, key2.first); }
86 bool compare(
const value_type& key1,
const TKey& key2)
const noexcept {
return _compare(key1.first, key2); }
119 std::pair<iterator, iterator>
equal_range(
const TKey& key) noexcept;
120 std::pair<const_iterator, const_iterator>
equal_range(
const TKey& key)
const noexcept;
123 size_t count(
const TKey& key)
const noexcept {
return (
find(key) ==
end()) ? 0 : 1; }
169 template <
class InputIterator>
170 void insert(InputIterator first, InputIterator last);
177 template <
typename... Args>
178 std::pair<iterator, bool>
emplace(Args&&... args);
185 template <
typename... Args>
193 size_t erase(
const TKey& key);
220 void clear() noexcept { _container.clear(); }
224 template <
typename UKey,
typename UValue,
typename UCompare,
typename UAllocator>
229 std::vector<value_type, TAllocator> _container;
231 template <
typename... Args>
232 std::pair<iterator, bool> emplace_internal(
const TKey& key, Args&&... args);
233 template <
typename... Args>
mapped_type & operator[](const TKey &key)
Access to the item with the given key or insert a new one.
reverse_iterator rend() noexcept
Get the reverse end flat map iterator.
iterator begin() noexcept
Get the begin flat map iterator.
FlatMap(size_t capacity=128, const TCompare &compare=TCompare(), const TAllocator &allocator=TAllocator())
Initialize the flat map with a given capacity.
const value_type & const_reference
void swap(FlatMap &flatmap) noexcept
Swap two instances.
std::pair< TKey, TValue > value_type
void clear() noexcept
Clear the flat map.
const_reverse_iterator rbegin() const noexcept
friend void swap(FlatMap< UKey, UValue, UCompare, UAllocator > &flatmap1, FlatMap< UKey, UValue, UCompare, UAllocator > &flatmap2) noexcept
std::vector< value_type, TAllocator >::const_iterator const_iterator
const_iterator cbegin() const noexcept
const_iterator end() const noexcept
std::pair< iterator, bool > insert(const value_type &item)
Insert a new item into the flat map.
bool empty() const noexcept
Is the flat map empty?
void reserve(size_t count)
Reserve the flat map capacity to fit the given count of items.
std::vector< value_type, TAllocator >::iterator iterator
size_t max_size() const noexcept
Get the flat map maximum size.
const_reverse_iterator rend() const noexcept
const_reverse_iterator crend() const noexcept
iterator find(const TKey &key) noexcept
Find the iterator which points to the first item with the given key in the flat map or return end ite...
bool compare(const value_type &key1, const value_type &key2) const noexcept
const_iterator cend() const noexcept
const_iterator begin() const noexcept
void shrink_to_fit()
Requests the flat map to reduce its capacity to fit its size.
iterator emplace_hint(const const_iterator &position, Args &&... args)
Emplace a new item into the flat map with a position hint.
ptrdiff_t difference_type
bool compare(const value_type &key1, const TKey &key2) const noexcept
mapped_type & at(const TKey &key) noexcept
Access to the item with the given key or throw std::out_of_range exception.
size_t capacity() const noexcept
Get the flat map capacity.
std::vector< value_type, TAllocator >::const_reverse_iterator const_reverse_iterator
std::pair< iterator, iterator > equal_range(const TKey &key) noexcept
Find the bounds of a range that includes all the elements in the hash map with the given key.
size_t count(const TKey &key) const noexcept
Find the count of items with the given key.
bool compare(const TKey &key1, const TKey &key2) const noexcept
Compare two items: if the first key is less than the second one?
size_t erase(const TKey &key)
Erase the item with the given key from the flat map.
const value_type * const_pointer
std::pair< iterator, bool > emplace(Args &&... args)
Emplace a new item into the flat map.
bool compare(const TKey &key1, const value_type &key2) const noexcept
iterator end() noexcept
Get the end flat map iterator.
iterator upper_bound(const TKey &key) noexcept
Find the iterator which points to the first item with the given key that greater than the given key i...
size_t size() const noexcept
Get the flat map size.
iterator lower_bound(const TKey &key) noexcept
Find the iterator which points to the first item with the given key that not less than the given key ...
std::vector< value_type, TAllocator >::reverse_iterator reverse_iterator
const_reverse_iterator crbegin() const noexcept
FlatMap(FlatMap &&) noexcept=default
reverse_iterator rbegin() noexcept
Get the reverse begin flat map iterator.
Flat map container inline implementation.
C++ Common project definitions.