libamxc
1.10.3
C Generic Data Containers
|
The Ambiorix set is an abstract data type that can store unique values. More...
Data Structures | |
struct | _flag |
The flag structure. More... | |
struct | _set |
The set structure. More... | |
Typedefs | |
typedef struct _flag | amxc_flag_t |
The flag structure. More... | |
Functions | |
int | amxc_set_new (amxc_set_t **set, bool counted) |
Allocates a set. More... | |
void | amxc_set_delete (amxc_set_t **set) |
Frees a set. More... | |
int | amxc_set_init (amxc_set_t *const set, bool counted) |
Initializes a set. More... | |
void | amxc_set_clean (amxc_set_t *const set) |
Cleans a set. More... | |
amxc_set_t * | amxc_set_copy (const amxc_set_t *const set) |
Copies a set. More... | |
void | amxc_set_reset (amxc_set_t *set) |
Reset or empty a set, i.e. clear all flags. More... | |
int | amxc_set_parse (amxc_set_t *set, const char *str) |
Parses a space-separated string of flags and adds them to the set. More... | |
char * | amxc_set_to_string (const amxc_set_t *const set) |
Converts a set to a space-separated string of flags. More... | |
char * | amxc_set_to_string_sep (const amxc_set_t *const set, const char *sep) |
Converts a set to a separated string of flags. More... | |
void | amxc_set_add_flag (amxc_set_t *set, const char *flag) |
Adds a flag to a set, or increases the flag counter. More... | |
void | amxc_set_remove_flag (amxc_set_t *set, const char *flag) |
Removes a flag from a set or decreases the flag counter. More... | |
bool | amxc_set_has_flag (const amxc_set_t *const set, const char *flag) |
Check if a set contains a flag. More... | |
uint32_t | amxc_set_get_count (const amxc_set_t *const set, const char *flag) |
Get a flag counter. More... | |
void | amxc_set_union (amxc_set_t *const set, const amxc_set_t *const operand) |
Joins two sets. More... | |
void | amxc_set_intersect (amxc_set_t *const set, const amxc_set_t *const operand) |
Intersect a set with another set. More... | |
void | amxc_set_subtract (amxc_set_t *const set, const amxc_set_t *const operand) |
Subtract a set from another set. More... | |
bool | amxc_set_is_equal (const amxc_set_t *const set1, const amxc_set_t *const set2) |
Compare two sets. More... | |
void | amxc_set_alert_cb (amxc_set_t *set, amxc_set_alert_t handler, void *priv) |
Install a set alert callback function. More... | |
void | amxc_set_symmetric_difference (amxc_set_t *const set, const amxc_set_t *const operand) |
Calculates the symmetric difference of two sets. More... | |
The Ambiorix set is an abstract data type that can store unique values.
A set is an abstract data type that can store unique values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.
typedef struct _flag amxc_flag_t |
The flag structure.
Flags can be added to a set.
void amxc_set_add_flag | ( | amxc_set_t * | set, |
const char * | flag | ||
) |
Adds a flag to a set, or increases the flag counter.
Adds a flag to a set if it does not contain it already. If the set already contains the flag, increase its counter if the flag set is a counted set or do nothing at all for normal flag sets.
set | The flag set on which a flag needs to be added. |
flag | The flag to add. |
Definition at line 305 of file amxc_set.c.
void amxc_set_alert_cb | ( | amxc_set_t * | set, |
amxc_set_alert_t | handler, | ||
void * | priv | ||
) |
Install a set alert callback function.
For normal sets, this handler is called every time a flag is added or removed from the set.
For counted sets, this handler is called only whenever a flag is really added to or removed from the flag set, i.e. not when setting or clearing a flag only causes a flag counter to be incremented or decremented.
set | The set on which an alert handler needs to be installed. |
handler | The callback function to be called whenever a flag is set or cleared. |
priv | A void pointer to be passed to the callback function. |
Definition at line 453 of file amxc_set.c.
void amxc_set_clean | ( | amxc_set_t *const | set | ) |
Cleans a set.
clean-up a set, which was previously initialized with amxc_set_init.
set | a pointer to an amxc_set_t structure |
Definition at line 177 of file amxc_set.c.
amxc_set_t* amxc_set_copy | ( | const amxc_set_t *const | set | ) |
Copies a set.
Allocates a new set and copies the flags of the given set into this new set. The set alert handler and private data are not copied.
set | a pointer to an amxc_set_t structure |
Definition at line 187 of file amxc_set.c.
void amxc_set_delete | ( | amxc_set_t ** | set | ) |
Frees a set.
Releases any allocated memory for the flag set.
set | The set to destroy. When this function returns, the pointer is set to NULL. |
Definition at line 151 of file amxc_set.c.
uint32_t amxc_set_get_count | ( | const amxc_set_t *const | set, |
const char * | flag | ||
) |
Get a flag counter.
Get the counter of a specific flag if flag is non-NULL or the global flag set counter if flag is NULL. The global counter is the sum of all flag counters. For counted sets, the counter of a flag is the number of times the flag was set. For normal flag sets, the counter equals 1 if the flag is set.
set | The set for which a counter needs to be returned. NULL is considered to be the empty flag set. |
flag | The flag to count. If it equals NULL, the global flag set counter is returned. |
Definition at line 349 of file amxc_set.c.
bool amxc_set_has_flag | ( | const amxc_set_t *const | set, |
const char * | flag | ||
) |
Check if a set contains a flag.
set | The set on which the flag needs to be checked. NULL is considered to be the empty flag set. |
flag | The flag to check. |
Definition at line 337 of file amxc_set.c.
int amxc_set_init | ( | amxc_set_t *const | set, |
bool | counted | ||
) |
Initializes a set.
Initializes a set that is declared on the stack. When the set is not needed anymore use amxc_set_clean to clean it up.
set | a pointer to an amxc_set_t structure |
counted | If set to true, the initialized set will keep a counter for each flag. Each time a specific flag is set, the counter is incremented and when the flag is cleared, the counter is decremented. Only when the counter reaches zero, the flag is really removed from the flag set. If set to false, the flag set will have default behaviour, i.e. when clearing a flag, it is removed immediately no matter how many times it has been set. |
Definition at line 162 of file amxc_set.c.
void amxc_set_intersect | ( | amxc_set_t *const | set, |
const amxc_set_t *const | operand | ||
) |
Intersect a set with another set.
All flags that are set in the set referenced by set, but not in the set referenced by operand, are removed from set. Additionally, for counted sets, the counter of each flag of set will be set to the minimum of itself and the counter of the corresponding flag in operand.
set | Left hand operand of the intersect operator, as well as the target to store the result into. |
operand | Right hand operand of the intersect operator. NULL is considered to be the empty set. |
Definition at line 384 of file amxc_set.c.
bool amxc_set_is_equal | ( | const amxc_set_t *const | set1, |
const amxc_set_t *const | set2 | ||
) |
Compare two sets.
Normal sets are considered to be equal if they contain the exact same set of flags. For counted sets, the flag counters have to match as well in order to be considered as equal.
set1 | Left hand operand of the equals operator. NULL is considered to be the empty set. |
set2 | Right hand operand of the equals operator. NULL is considered to be the empty set. |
Definition at line 430 of file amxc_set.c.
int amxc_set_new | ( | amxc_set_t ** | set, |
bool | counted | ||
) |
Allocates a set.
Allocates and initializes memory to store a set. This function allocates memory from the heap, if a set is on the stack, it can be initialized using function amxc_set_init
set | a pointer to the location where the pointer to the new set can be stored |
counted | If set to true, the created set will keep a counter for each flag. Each time a specific flag is set, the counter is incremented and when the flag is cleared, the counter is decremented. Only when the counter reaches zero, the flag is really removed from the flag set. If set to false, the flag set will have default behaviour, i.e. when clearing a flag, it is removed immediately no matter how many times it has been set. |
Definition at line 138 of file amxc_set.c.
int amxc_set_parse | ( | amxc_set_t * | set, |
const char * | str | ||
) |
Parses a space-separated string of flags and adds them to the set.
This function will iterate over the given string, uses space characters as delimiter (uses function isspace). Multiple consecutive spaces are considered as one single space.
When the set is a counted set, a flag can be followed by a : sign and a number, that number is used as the initial count value. If the set is not a counted set, the number is ignored.
The : sign can not be used in flag names.
set | The flag set to store the parsed flags into. |
str | The string that holds the space separated string of flags. |
Definition at line 210 of file amxc_set.c.
void amxc_set_remove_flag | ( | amxc_set_t * | set, |
const char * | flag | ||
) |
Removes a flag from a set or decreases the flag counter.
If the flag set contains the flag, decrease its counter if it is a counted set. If it is a normal flag set, or the flag's counter reaches zero, removes the flag from the set. If the set does not contain the flag, doesn't do anything at all.
set | The set on which a flag needs to be removed. |
flag | The flag to clear. |
Definition at line 315 of file amxc_set.c.
void amxc_set_reset | ( | amxc_set_t * | set | ) |
Reset or empty a set, i.e. clear all flags.
set | The set to reset. |
Definition at line 199 of file amxc_set.c.
void amxc_set_subtract | ( | amxc_set_t *const | set, |
const amxc_set_t *const | operand | ||
) |
Subtract a set from another set.
For normal sets, all flags that are set in both the set referenced by set and the set referenced by operand, are removed from set.
For counted sets, the counter of each flag of set will be decremented by the counter of the corresponding flag in operand. If this results in a counter equal to or less than zero, the flag is removed from set.
set | Left hand operand of the subtract operator, as well as the target to store the result into. |
operand | Right hand operand of the subtract operator. NULL is considered to be the empty set. |
Definition at line 408 of file amxc_set.c.
void amxc_set_symmetric_difference | ( | amxc_set_t *const | set, |
const amxc_set_t *const | operand | ||
) |
Calculates the symmetric difference of two sets.
The symmetric difference of two sets is the set of elements which are in either of the sets, but not in both. E.g. the symmetric difference of the sets {1,2,3} and {3,4} is {1,2,4}.
set | Left hand operand of the operator, as well as the target to store the result into. |
operand | Right hand operand of the operator. NULL is considered to be the empty flag set. |
Definition at line 463 of file amxc_set.c.
char* amxc_set_to_string | ( | const amxc_set_t *const | set | ) |
Converts a set to a space-separated string of flags.
set | The flag set to convert. |
Definition at line 301 of file amxc_set.c.
char* amxc_set_to_string_sep | ( | const amxc_set_t *const | set, |
const char * | sep | ||
) |
Converts a set to a separated string of flags.
set | The flag set to convert. |
sep | The separation string. |
Definition at line 264 of file amxc_set.c.
void amxc_set_union | ( | amxc_set_t *const | set, |
const amxc_set_t *const | operand | ||
) |
Joins two sets.
All flags that are set in the set referenced by operand, but are not in the flag set referenced by set, are added to set. Additionally, for counted sets, the counter of each flag of set will be incremented by the counter of the corresponding flag in operand.
set | Left hand operand of the union operator, as well as the target to store the result into. |
operand | Right hand operand of the union operator. NULL is considered to be the empty flag set. |
Definition at line 365 of file amxc_set.c.