Skip to content
Reini Urban edited this page Jan 4, 2021 · 1 revision

set - CTL - C Container Template library

Defined in header <ctl/set.h>, CTL prefix set, parent for map.

SYNOPSIS

static inline int
int_cmp(int *a, int *b) {
  return *a < *b;
}

#undef POD
#define T int
#include <ctl/set.h>

int i = 0;
set_int a = set_int_init (int_cmp);

for (i=0; i<1000; i++) {
  set_int_insert (&a, i);
}

foreach(set_int, &a, it) { i = it.ref->key); }
printf("last key %d, ", i);

printf("min {\"%s\", %d} ", set_int_min (a));
printf("max {\"%s\", %d}\n", set_int_max (a));
set_int_free(&a);

DESCRIPTION

set is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the custom comparison function T_cmp. Search, removal, and insertion operations have logarithmic complexity. set is implemented as red-black tree.

The function names are composed of the prefix set_, the user-defined type T and the method name. E.g set_int with #define T int.

Everywhere the CTL uses the Compare requirements, uniqueness is determined by using the equivalence relation. In imprecise terms, two objects a and b are considered equivalent (not unique) if neither compares less than the other: !comp(a, b) && !comp(b, a).

Member types

T value type

A being set_T container type

B being set_T_node node type

I being set_T_it iterator type

Member functions

init (T_compare(T*, T*))

constructs the set.

free (A* self)

destructs the set.

assign (A* self, A* other)

replaces the contents of the container.

copy (A* self)

returns a copy of the container.

Element access

at (A* self, size_t index)

access specified element with bounds checking

Iterators

begin (A* self)

returns an iterator to the beginning

end (A* self)

returns an iterator to the end

Capacity

empty (A* self)

checks whether the container is empty

size (A* self)

returns the number of elements

max_size ()

returns the maximum possible number of elements

Modifiers

clear (A* self)

clears the contents

insert (A* self, T key)

inserts the element (C++17)

emplace (A* self, T values...)

constructs elements in-place. (NYI)

emplace_hint (A* self, I* pos, T values...)

constructs elements in-place at position. (NYI)

erase (A* self, T key)

erases the element by value

erase_it (A* self, I* pos)

erases the element at pos

erase_range (A* self, I* first, I* last)

erases elements

swap (A* self, A* other)

swaps the contents

extract (A* self, T key)

extracts a node from the container. NYI

extract_it (A* self, I* pos)

extracts nodes from the container. NYI

merge (A* self)

splices nodes from another container

Lookup

count (A* self)

returns the number of elements matching specific key

find (A* self, T key)

finds element with specific key

contains (A* self, T key)

checks if the container contains element with specific key. (C++20)

equal_range (A* self)

returns range of elements matching a specific key. (NYI)

lower_bound (A* self)

returns an iterator to the first element not less than the given key. (NYI)

upper_bound (A* self)

returns an iterator to the first element greater than the given key. (NYI)

Observers

value_comp (A* self)

Returns the function that compares keys in objects of type value_type T. (NYI)

Non-member functions

swap (A* self)

specializes the swap algorithm

remove_if (A* self, int T_match(T*))

Removes all elements satisfying specific criteria.

erase_if (A* self, int T_match(T*))

erases all elements satisfying specific criteria (C++20)

intersection (A* self, A* other)

union (A* self, A* other)

difference (A* self, A* other)

symmetric_difference (A* self, A* other)

Clone this wiki locally