1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# STC [csset](../include/stc/csset.h): Sorted Set

A **csset** is an associative container that contains a sorted set of unique objects of type *i_key*. Sorting is done using the key comparison function *keyCompare*. Search, removal, and insertion operations have logarithmic complexity. **csset** is implemented as an AA-tree.
See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) for a functional description.
## Header file and declaration
```c
#define i_type // full typename of the container
#define i_key // key: REQUIRED
#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type
#define i_keydrop // destroy key func - defaults to empty destruct
#define i_keyclone // REQUIRED IF i_keydrop defined
#define i_keyraw // convertion "raw" type - defaults to i_key
#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy
#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy
#define i_tag // alternative typename: csset_{i_tag}. i_tag defaults to i_key
#include <stc/csset.h>
```
`X` should be replaced by the value of `i_tag` in all of the following documentation.
## Methods
```c
csset_X csset_X_init(void);
csset_X csset_X_with_capacity(intptr_t cap);
bool csset_X_reserve(csset_X* self, intptr_t cap);
void csset_X_shrink_to_fit(csset_X* self);
csset_X csset_X_clone(csset_x set);
void csset_X_clear(csset_X* self);
void csset_X_copy(csset_X* self, const csset_X* other);
void csset_X_drop(csset_X* self); // destructor
bool csset_X_empty(const csset_X* self);
intptr_t csset_X_size(const csset_X* self);
intptr_t csset_X_capacity(const csset_X* self);
const csset_X_value* csset_X_get(const csset_X* self, i_keyraw rkey); // const get
csset_X_value* csset_X_get_mut(csset_X* self, i_keyraw rkey); // return NULL if not found
bool csset_X_contains(const csset_X* self, i_keyraw rkey);
csset_X_iter csset_X_find(const csset_X* self, i_keyraw rkey);
csset_X_value* csset_X_find_it(const csset_X* self, i_keyraw rkey, csset_X_iter* out); // return NULL if not found
csset_X_iter csset_X_lower_bound(const csset_X* self, i_keyraw rkey); // find closest entry >= rkey
csset_X_result csset_X_insert(csset_X* self, i_key key);
csset_X_result csset_X_push(csset_X* self, i_key key); // alias for insert()
csset_X_result csset_X_emplace(csset_X* self, i_keyraw rkey);
int csset_X_erase(csset_X* self, i_keyraw rkey);
csset_X_iter csset_X_erase_at(csset_X* self, csset_X_iter it); // return iter after it
csset_X_iter csset_X_erase_range(csset_X* self, csset_X_iter it1, csset_X_iter it2); // return updated it2
csset_X_iter csset_X_begin(const csset_X* self);
csset_X_iter csset_X_end(const csset_X* self);
void csset_X_next(csset_X_iter* it);
csset_X_value csset_X_value_clone(csset_X_value val);
```
## Types
| Type name | Type definition | Used to represent... |
|:-------------------|:--------------------------------------------------|:----------------------------|
| `csset_X` | `struct { ... }` | The csset type |
| `csset_X_key` | `i_key` | The key type |
| `csset_X_value` | `i_key` | The key type (alias) |
| `csset_X_keyraw` | `i_keyraw` | The raw key type |
| `csset_X_raw` | `i_keyraw` | The raw key type (alias) |
| `csset_X_result` | `struct { csset_X_value* ref; bool inserted; }` | Result of insert/emplace |
| `csset_X_iter` | `struct { csset_X_value *ref; ... }` | Iterator type |
## Example
```c
#define i_implement
#include <stc/cstr.h>
#define i_type SSet
#define i_key_str
#include <stc/csset.h>
int main(void)
{
SSet second={0}, third={0}, fourth={0}, fifth={0};
second = c_init(SSet, {"red", "green", "blue"});
c_forlist (i, const char*, {"orange", "pink", "yellow"})
SSet_emplace(&third, *i.ref);
SSet_emplace(&fourth, "potatoes");
SSet_emplace(&fourth, "milk");
SSet_emplace(&fourth, "flour");
// Copy all to fifth:
fifth = SSet_clone(second);
c_foreach (i, SSet, third)
SSet_emplace(&fifth, cstr_str(i.ref));
c_foreach (i, SSet, fourth)
SSet_emplace(&fifth, cstr_str(i.ref));
printf("fifth contains:\n\n");
c_foreach (i, SSet, fifth)
printf("%s\n", cstr_str(i.ref));
c_drop(SSet, &second, &third, &fourth, &fifth);
}
```
Output:
```
fifth contains:
blue
flour
green
milk
orange
pink
potatoes
red
yellow
```
|