14 #include <type_traits> 43 using iterator_category = std::bidirectional_iterator_tag;
45 using const_pointer =
const value_type*;
46 using const_reference =
const value_type&;
53 const_reference operator*()
const {
return *this->operator->(); }
54 bool operator!=(
const iterator &other)
const {
return !(*
this == other); }
56 const_pointer operator->()
const {
57 return static_cast<const_pointer
>(internal_iterator->key);
61 assert(internal_iterator);
73 assert(internal_iterator);
84 bool operator==(
const iterator &other)
const {
85 return this->internal_iterator == other.internal_iterator;
91 static_assert(std::is_copy_assignable<T>(),
"value type must be copy-assignable");
92 static_assert(std::is_copy_constructible<T>(),
"value type must be copy-constructible");
95 static int key_comparator(
const void *void_key1,
const void *void_key2) {
96 const value_type &key1 = *
static_cast<const value_type *
>(void_key1);
97 const value_type &key2 = *
static_cast<const value_type *
>(void_key2);
100 }
else if (key1 > key2) {
108 : set_handle(
CMAGIC_SET_NEW(value_type, key_comparator, alloc_packet)) {}
110 template <
typename URef>
111 std::pair<iterator, bool> insert_template(URef &&val) {
116 value_type {std::forward<URef>(val)};
118 const bool insert_unique_success =
138 set &operator=(
const set &x) {
146 auto insert_result =
insert(val);
147 assert(insert_result.first ==
end() || insert_result.second);
148 if (insert_result.first ==
end()) {
151 set_handle =
nullptr;
164 set &operator=(
set &&x) {
168 set_handle = x.set_handle;
174 set(
set &&x) : set_handle(x.set_handle) {
193 operator bool()
const {
194 return static_cast<bool>(set_handle);
243 return insert_template(val);
250 return insert_template(std::move(val));
iterator end() const
Return iterator to end.
Definition: set.hpp:213
size_t size_type
Type used to measure element size.
Definition: set.hpp:38
A container that stores unique elements following a specific order.
Definition: set.hpp:27
#define CMAGIC_SET_FREE(cmagic_set)
Frees the resources allocated by the set before.
Definition: set.h:132
Set of allocation functions. Used in some CMagic structures to specify a desired memory pool...
Definition: memory.h:187
Implementation of a set container.
#define CMAGIC_SET_FIRST(cmagic_set)
Return iterator to the first element in set.
Definition: set.h:201
Set insertion result.
Definition: set.h:60
#define CMAGIC_SET_NEW(key_type, key_comparator, alloc_packet)
Allocates and returns an address of a newly created empty set.
Definition: set.h:124
#define CMAGIC_SET_GET_ALLOC_PACKET(cmagic_set)
Retrieves cmagic_memory_alloc_packet_t associated with the set.
Definition: set.h:251
#define CMAGIC_SET_FIND(cmagic_set, key)
Searches the container for an element equivalent to key and returns an iterator to it if found...
Definition: set.h:233
size_type size() const
Returns the number of elements in the set.
Definition: set.hpp:269
bool already_exists
true if the element already exists in the set and the set was not modified, false if a new element ha...
Definition: set.h:72
#define CMAGIC_SET_CLEAR(cmagic_set)
Removes all elements from the set.
Definition: set.h:185
cmagic_set_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: set.h:66
static set custom_allocation_set()
Constructs an empty set using custom CMagic memory allocation from memory.h.
Definition: set.hpp:134
T value_type
Type of set elements.
Definition: set.hpp:33
#define CMAGIC_SET_ITERATOR_PREV(iterator)
Returns iterator to the previous element in container.
Definition: set.h:225
#define CMAGIC_SET_ERASE_EXT(cmagic_set, key, destructor)
Extended version of CMAGIC_SET_ERASE.
Definition: set.h:168
#define CMAGIC_SET(key_type)
Convenient alias for type*. Returned type of CMAGIC_SET_NEW.
Definition: set.h:113
iterator find(const value_type &val) const
Searches the container for an element equivalent to val and returns an iterator to it if found...
Definition: set.hpp:290
#define CMAGIC_SET_ALLOCATE(cmagic_set, key)
Allocates space for a new element but does not initialize it.
Definition: set.h:145
void clear()
Removes all elements from the set, leaving the container with a size of 0.
Definition: set.hpp:221
iterator begin() const
Return iterator to beginning.
Definition: set.hpp:203
#define CMAGIC_SET_ITERATOR_NEXT(iterator)
Returns iterator to the next element in container.
Definition: set.h:217
#define CMAGIC_SET_SIZE(cmagic_set)
Returns the number of elements in the set.
Definition: set.h:192
void erase(const value_type &val)
Removes a single element from the set.
Definition: set.hpp:258
static const cmagic_memory_alloc_packet_t CMAGIC_MEMORY_ALLOC_PACKET_STD
Allocation from the standard library.
Definition: memory.h:196
std::pair< iterator, bool > insert(value_type &&val)
Inserts a new element to the set if it is not equivalent to any element already contained in the set...
Definition: set.hpp:249
static const cmagic_memory_alloc_packet_t CMAGIC_MEMORY_ALLOC_PACKET_CUSTOM_CMAGIC
Custom allocation from the CMagic library.
Definition: memory.h:203
std::pair< iterator, bool > insert(const value_type &val)
Inserts a new element to the set if it is not equivalent to any element already contained in the set...
Definition: set.hpp:242
bool empty() const
Returns whether the set is empty (i.e. whether its size is 0).
Definition: set.hpp:280