14 #include <type_traits> 21 template<
typename Key,
typename Value>
48 using iterator_category = std::bidirectional_iterator_tag;
57 assert(internal_iterator);
58 return std::make_pair(*static_cast<const key_type *>(internal_iterator->key),
59 *static_cast<mapped_type *>(internal_iterator->value));
62 void update_adapter() {
63 if (internal_iterator) {
64 adapter = make_adapter();
69 assert(internal_iterator);
75 assert(internal_iterator);
81 const_pointer operator->()
const {
return &adapter; }
82 const_reference operator*()
const {
return *this->operator->(); }
83 bool operator!=(
const iterator &other)
const {
return !(*
this == other); }
111 bool operator==(
const iterator &other)
const {
112 return this->internal_iterator == other.internal_iterator;
118 static_assert(std::is_copy_constructible<key_type>(),
"key type must be copy-constructible");
119 static_assert(std::is_copy_constructible<mapped_type>(),
120 "mapped type must be copy-constructible");
121 static_assert(std::is_copy_assignable<mapped_type>(),
"mapped type must be copy-assignable");
125 static int key_comparator(
const void *void_key1,
const void *void_key2) {
130 }
else if (key1 > key2) {
140 template <
typename Key_URef,
typename Val_URef>
141 std::pair<iterator, bool> insert_template(Key_URef &&key, Val_URef &&value) {
146 key_type {std::forward<Key_URef>(key)};
150 const bool insert_unique_success =
170 map &operator=(
const map &x) {
178 auto insert_result =
insert(val);
179 assert(insert_result.first ==
end() || insert_result.second);
180 if (insert_result.first ==
end()) {
183 map_handle =
nullptr;
200 map_handle = x.map_handle;
206 map(
map &&x) : map_handle(x.map_handle) {
225 operator bool()
const {
226 return static_cast<bool>(map_handle);
260 key_ptr->~key_type();
261 value_ptr->~mapped_type();
280 return insert_template(val.first, val.second);
287 return insert_template(std::move(val.first), std::move(val.second));
void erase(const key_type &key)
Removes a single element from the map.
Definition: map.hpp:295
std::pair< iterator, bool > insert(const value_type &val)
Inserts a new element to the map if its key is not equivalent to any element already contained in the...
Definition: map.hpp:279
size_type size() const
Returns the number of elements in the map.
Definition: map.hpp:307
Value mapped_type
Type of map values.
Definition: map.hpp:33
iterator find(const key_type &key) const
Searches the container for an element with a key equivalent to key and returns an iterator to it if f...
Definition: map.hpp:328
Set of allocation functions. Used in some CMagic structures to specify a desired memory pool...
Definition: memory.h:187
void clear()
Removes all elements from the map, leaving the container with a size of 0.
Definition: map.hpp:253
Key key_type
Type of map keys.
Definition: map.hpp:28
#define CMAGIC_MAP_ERASE_EXT(cmagic_map, key, destructor)
Extended version of CMAGIC_MAP_ERASE.
Definition: map.h:175
bool empty() const
Returns whether the map is empty (i.e. whether its size is 0).
Definition: map.hpp:318
#define CMAGIC_MAP_FIND(cmagic_map, key)
Searches the container for an element with a key equivalent to key and returns an iterator to it if f...
Definition: map.h:240
#define CMAGIC_MAP_FREE(cmagic_map)
Frees the resources allocated by the map before.
Definition: map.h:138
bool already_exists
true if the element already exists in the map and the map was not modified, false if a new element ha...
Definition: map.h:74
#define CMAGIC_MAP(key_type)
Convenient alias for type*. Returned type of CMAGIC_MAP_NEW.
Definition: map.h:118
#define CMAGIC_MAP_GET_ALLOC_PACKET(cmagic_map)
Retrieves cmagic_memory_alloc_packet_t associated with the map.
Definition: map.h:268
#define CMAGIC_MAP_ITERATOR_PREV(iterator)
Returns iterator to the previous element in container.
Definition: map.h:232
map()
Constructs an empty map with standard memory allocation.
Definition: map.hpp:160
#define CMAGIC_MAP_CLEAR(cmagic_map)
Removes all elements from the map.
Definition: map.h:192
std::pair< iterator, bool > insert(value_type &&val)
Inserts a new element to the map if its key is not equivalent to any element already contained in the...
Definition: map.hpp:286
iterator begin() const
Return iterator to beginning.
Definition: map.hpp:235
#define CMAGIC_MAP_NEW(key_type, value_type, key_comparator, alloc_packet)
Allocates and returns an address of a newly created empty map.
Definition: map.h:130
#define CMAGIC_MAP_ITERATOR_NEXT(iterator)
Returns iterator to the next element in container.
Definition: map.h:224
static map custom_allocation_map()
Constructs an empty map using custom CMagic memory allocation from memory.h.
Definition: map.hpp:166
iterator end() const
Return iterator to end.
Definition: map.hpp:245
cmagic_map_iterator_t inserted_or_existing
iterator pointing to a new or already existing element or NULL if the allocation of a new element has...
Definition: map.h:68
Implementation of a map container.
static const cmagic_memory_alloc_packet_t CMAGIC_MEMORY_ALLOC_PACKET_STD
Allocation from the standard library.
Definition: memory.h:196
size_t size_type
Type used to measure element size.
Definition: map.hpp:43
static const cmagic_memory_alloc_packet_t CMAGIC_MEMORY_ALLOC_PACKET_CUSTOM_CMAGIC
Custom allocation from the CMagic library.
Definition: memory.h:203
#define CMAGIC_MAP_FIRST(cmagic_map)
Return iterator to the first element in map.
Definition: map.h:208
#define CMAGIC_MAP_SIZE(cmagic_map)
Returns the number of elements in the map.
Definition: map.h:199
Map insertion result.
Definition: map.h:62
#define CMAGIC_MAP_ALLOCATE(cmagic_map, key)
Allocates space for a new element (key-value pair) but does not initialize it.
Definition: map.h:151
std::pair< key_type, mapped_type > value_type
Type of map elements.
Definition: map.hpp:38