libamxc  1.10.3
C Generic Data Containers
amxc_set.c File Reference
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <amxc/amxc_set.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static void amxc_set_flag_free (amxc_llist_it_t *it)
 
static amxc_flag_tamxc_set_flag_find (const amxc_set_t *const set, const char *flag)
 
static void amxc_set_flag_add (amxc_set_t *set, const char *flag, int flaglen, int count)
 
static void amxc_set_flag_delete (amxc_set_t *set, amxc_llist_it_t *it)
 
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_tamxc_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_sep (const amxc_set_t *const set, const char *sep)
 Converts a set to a separated string of flags. More...
 
char * amxc_set_to_string (const amxc_set_t *const set)
 Converts a set to a space-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...
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 55 of file amxc_set.c.

Function Documentation

◆ amxc_set_flag_add()

static void amxc_set_flag_add ( amxc_set_t set,
const char *  flag,
int  flaglen,
int  count 
)
static

Definition at line 84 of file amxc_set.c.

87  {
88  amxc_flag_t* f = NULL;
89  char* str = NULL;
90 
91  if(flaglen != 0) {
92  str = (char*) calloc(flaglen + 1, 1);
93  when_null(str, exit);
94  strncpy(str, flag, flaglen);
95  } else {
96  str = strdup(flag);
97  when_null(str, exit);
98  }
99 
100  f = amxc_set_flag_find(set, str);
101  if(f != NULL) {
102  if(set->counted) {
103  f->count += count;
104  set->count += count;
105  }
106  free(str);
107  goto exit;
108  }
109 
110  f = (amxc_flag_t*) calloc(1, sizeof(amxc_flag_t));
111  if(f == NULL) {
112  free(str);
113  goto exit;
114  }
115  f->flag = str;
116 
117  amxc_llist_append(&set->list, &f->it);
118  f->count = count;
119  set->count += count;
120  if(set->alert_handler) {
121  set->alert_handler(set, f->flag, true, set->priv);
122  }
123 
124 exit:
125  return;
126 }
#define when_null(x, l)
Definition: amxc_macros.h:126
static amxc_flag_t * amxc_set_flag_find(const amxc_set_t *const set, const char *flag)
Definition: amxc_set.c:72
int amxc_llist_append(amxc_llist_t *const llist, amxc_llist_it_t *const it)
Adds an item to the end of the linked list.
Definition: amxc_llist.c:169
The flag structure.
Definition: amxc_set.h:118
uint32_t count
Definition: amxc_set.h:121
char * flag
Definition: amxc_set.h:120
amxc_llist_it_t it
Definition: amxc_set.h:119
bool counted
Definition: amxc_set.h:134
amxc_set_alert_t alert_handler
Definition: amxc_set.h:131
int count
Definition: amxc_set.h:135
amxc_llist_t list
Definition: amxc_set.h:130
void * priv
Definition: amxc_set.h:133

◆ amxc_set_flag_delete()

static void amxc_set_flag_delete ( amxc_set_t set,
amxc_llist_it_t it 
)
static

Definition at line 128 of file amxc_set.c.

128  {
131  set->count -= flag->count;
132  if(set->alert_handler != NULL) {
133  set->alert_handler(set, flag->flag, false, set->priv);
134  }
136 }
static void amxc_set_flag_free(amxc_llist_it_t *it)
Definition: amxc_set.c:66
#define amxc_container_of(addr, type, member)
Calculates the address of the containing structure.
Definition: amxc_common.h:83
void amxc_llist_it_take(amxc_llist_it_t *const it)
Removes the iterator from the list.
static amxc_htable_it_t it[2000]

◆ amxc_set_flag_find()

static amxc_flag_t* amxc_set_flag_find ( const amxc_set_t *const  set,
const char *  flag 
)
static

Definition at line 72 of file amxc_set.c.

72  {
73  amxc_flag_t* f = NULL;
74  amxc_llist_iterate(it, &set->list) {
76  if(strcmp(f->flag, flag) == 0) {
77  break;
78  }
79  f = NULL;
80  }
81  return f;
82 }
#define amxc_llist_iterate(it, list)
Loops over the list from head to tail.
Definition: amxc_llist.h:270

◆ amxc_set_flag_free()

static void amxc_set_flag_free ( amxc_llist_it_t it)
static

Definition at line 66 of file amxc_set.c.

66  {
68  free(flag->flag);
69  free(flag);
70 }