dynamiC  0.1
Macros | Functions
DynamicDictionary

Macros

#define DYN_DICT_LEN(dyn)   dyn->data.dict->value.data.list->length
 Return number of elements within a dictionary.
 
#define DYN_DICT_GET_I_REF(dyn, i)   &(dyn)->data.dict->value.data.list->container[i]
 Return a reference to the ith element stored within a dictionary.
 
#define DYN_DICT_GET_I_KEY(dyn, i)   (dyn)->data.dict->key[i]
 Return a reference to the ith key stored within a dictionary.
 
#define DYN_DICT_SPACE(dyn)   dyn->value.data.list->space
 Return the maximal usable number of elements of a dictionary.
 
#define DYN_DICT_LENGTH(dyn)   dyn->value.data.list->length
 Return the number of elements stored within a dictionary.
 

Functions

trilean dyn_set_dict (dyn_c *dyn, const dyn_ushort length)
 Set dyn to a dictionary with a max. length of elements. More...
 
trilean dyn_dict_change (dyn_c *dict, const dyn_ushort i, const dyn_c *value)
 Replace the ith element in a dictionary with a new value.
 
dyn_cdyn_dict_insert (dyn_c *dict, dyn_const_str key, dyn_c *value)
 Insert a new key-value pair into the dictionary. More...
 
trilean dyn_dict_remove (dyn_c *dict, dyn_const_str key)
 Remove key-value pair from dictionary. More...
 
dyn_cdyn_dict_get (const dyn_c *dict, dyn_const_str key)
 Get the reference to value stored at key. More...
 
trilean dyn_dict_resize (dyn_c *dict, const dyn_ushort size)
 Set the available space for elements. More...
 
dyn_cdyn_dict_get_i_ref (const dyn_c *dict, const dyn_ushort i)
 Get the reference to ith value in dict. More...
 
dyn_str dyn_dict_get_i_key (const dyn_c *dict, const dyn_ushort i)
 Get the reference to ith key in dict. More...
 
dyn_ushort dyn_dict_has_key (const dyn_c *dict, dyn_const_str key)
 Check if dict has key and return its position - 1 (returns 0 if not found) More...
 
void dyn_dict_empty (dyn_c *dict)
 todo More...
 
void dyn_dict_free (dyn_c *dict)
 Free all allocated memory. More...
 
trilean dyn_dict_copy (const dyn_c *dict, dyn_c *copy)
 Copy the entire dict.
 
dyn_ushort dyn_dict_string_len (const dyn_c *dict)
 Calculate the required string length.
 
void dyn_dict_string_add (const dyn_c *dict, dyn_str string)
 Add the dict-string representation to string.
 

Detailed Description

Function Documentation

◆ dyn_dict_empty()

void dyn_dict_empty ( dyn_c dict)

todo

Parameters
dictto be freed has to be of type DICT
Return values
DYN_TRUEif the key value pair was found and removed

Definition at line 229 of file dynamic_dict.c.

Referenced by dyn_dict_free().

230 {
231  dyn_dict* ptr = dict->data.dict;
232 
234  while (i--) {
235  free(ptr->key[i]);
236  ptr->key[i] = NULL;
237  dyn_free(DYN_DICT_GET_I_REF(dict, i));
238  }
239  ptr->value.data.list->length = 0;
240 }
dyn_list * list
pointer to dynamic list
dyn_int i
basic integer
Definition: dynamic_types.h:59
void * ptr
pointer to function
Definition: dynamic_types.h:57
#define DYN_DICT_GET_I_REF(dyn, i)
Return a reference to the ith element stored within a dictionary.
Definition: dynamic.h:191
Basic container for dictionaries.
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_ushort length
elements in use
dyn_dict * dict
pointer to dynamic dictionary
dyn_c value
dynamic element of type dyn_list
dyn_str * key
array to C strings used as identifiers
#define DYN_DICT_LENGTH(dyn)
Return the number of elements stored within a dictionary.
Definition: dynamic.h:198
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30

◆ dyn_dict_free()

void dyn_dict_free ( dyn_c dict)

Free all allocated memory.

Parameters
dicthas to be of type DICT

Definition at line 245 of file dynamic_dict.c.

Referenced by dyn_free().

246 {
247  dyn_dict_empty(dict);
248  dyn_free(&dict->data.dict->value);
249  free(dict->data.dict->key);
250  free(dict->data.dict);
251 }
void dyn_dict_empty(dyn_c *dict)
todo
Definition: dynamic_dict.c:229
dyn_dict * dict
pointer to dynamic dictionary
dyn_c value
dynamic element of type dyn_list
dyn_str * key
array to C strings used as identifiers
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30

◆ dyn_dict_get()

dyn_c* dyn_dict_get ( const dyn_c dict,
dyn_const_str  key 
)

Get the reference to value stored at key.

Searches the dictionary for a certain key and removes the key value pair. The last element is moved to the freed position to spare space, such that there are no free positions within the linear dictionary representation.

Parameters
dicthas to be of type DICT
keyof the key-value pair to remove
Returns
reference to the value stored under the given key, if exists, otherwise NULL

Definition at line 264 of file dynamic_dict.c.

265 {
266  dyn_ushort pos = dyn_dict_has_key(dict, key);
267 
268  if (pos)
269  return DYN_DICT_GET_I_REF(dict, --pos);
270 
271  return NULL;
272 }
dyn_ushort dyn_dict_has_key(const dyn_c *dict, dyn_const_str key)
Check if dict has key and return its position - 1 (returns 0 if not found)
Definition: dynamic_dict.c:145
#define DYN_DICT_GET_I_REF(dyn, i)
Return a reference to the ith element stored within a dictionary.
Definition: dynamic.h:191
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_str * key
array to C strings used as identifiers
Definition: dynamic_types.h:56

◆ dyn_dict_get_i_key()

dyn_str dyn_dict_get_i_key ( const dyn_c dict,
const dyn_ushort  i 
)

Get the reference to ith key in dict.

This function is only used to offer an interface, such that values of the library can also be accsessed externally. Internally it is recommended to use the macro DYN_DICT_GET_I_KEY

Parameters
dynhas to be of type DICT
iposition of the key
Returns
reference to the ith key (C-string)

Definition at line 183 of file dynamic_dict.c.

184 {
185  return dict->data.dict->key[i];
186 }
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_dict * dict
pointer to dynamic dictionary
dyn_str * key
array to C strings used as identifiers

◆ dyn_dict_get_i_ref()

dyn_c* dyn_dict_get_i_ref ( const dyn_c dict,
const dyn_ushort  i 
)

Get the reference to ith value in dict.

This function is only used to offer an interface, such that values of the library can also be accsessed externally. Internally it is recommended to use the macro DYN_DICT_GET_I_REF

Parameters
dicthas to be of type DICT
iposition of the dynamic value
Returns
reference to the ith value in dyn

Definition at line 168 of file dynamic_dict.c.

169 {
170  return dyn_list_get_ref(&dict->data.dict->value, i);
171 }
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_c * dyn_list_get_ref(const dyn_c *list, const dyn_short i)
Return a reference to the ith element within list, negative values are allowed.
Definition: dynamic_list.c:289
dyn_dict * dict
pointer to dynamic dictionary
dyn_c value
dynamic element of type dyn_list

◆ dyn_dict_has_key()

dyn_ushort dyn_dict_has_key ( const dyn_c dict,
dyn_const_str  key 
)

Check if dict has key and return its position - 1 (returns 0 if not found)

Searches the dictionary for a certain key, if this key could be found, then its position plus 1 is returned, to indicate that the key was found, even if it is on position 0, otherwise 0 is returned. Thus, the returned value has to be decreased by one if it is larger than 0.

Parameters
dictto be searched has to be of type DICT
keyC-string to search for
Return values
0if the key was not found
position+1otherwise

Definition at line 145 of file dynamic_dict.c.

Referenced by dyn_dict_get(), dyn_dict_insert(), and dyn_dict_remove().

146 {
147  dyn_char** s_key = dict->data.dict->key;
148  dyn_ushort length = DYN_DICT_LENGTH(dict->data.dict);
149  dyn_ushort i;
150  for (i=0; i<length; ++i, ++s_key) {
151  if (!dyn_strcmp(*s_key, key))
152  return i+1;
153  }
154 
155  return 0;
156 }
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_ushort length
elements in use
Definition: dynamic_types.h:56
char dyn_char
Basic 8bit signed integer.
Definition: dynamic_types.h:30
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_dict * dict
pointer to dynamic dictionary
dyn_str * key
array to C strings used as identifiers
Definition: dynamic_types.h:56
dyn_str * key
array to C strings used as identifiers
dyn_char dyn_strcmp(dyn_const_str a, dyn_const_str b)
Compares the string a to the string b.
#define DYN_DICT_LENGTH(dyn)
Return the number of elements stored within a dictionary.
Definition: dynamic.h:198

◆ dyn_dict_insert()

dyn_c* dyn_dict_insert ( dyn_c dict,
dyn_const_str  key,
dyn_c value 
)

Insert a new key-value pair into the dictionary.

If the key is already contained within the dictionary, then the current value is overwritten, if not then the new value is added to the end of the list as well as a new key. If the maximal space is exceeded, then new memory is attached as defined in DICT_DEFAULT.

Parameters
[in,out]dicthas to be of type DICT
[in]key
[in]valueto be coppied
Returns
reference to the newly inserted value, if insertion was not possible, NULL is returned

Definition at line 65 of file dynamic_dict.c.

Referenced by dyn_dict_copy().

66 {
67  dyn_dict* ptr = dict->data.dict;
70  if (i--)
71  goto GOTO__CHANGE; //return dyn_dict_change(dyn, i-1, value);
72 
73  if (DYN_DICT_LENGTH(ptr) == space) {
74  dyn_dict_resize(dict, space + DICT_DEFAULT);
75  /*if (dyn_list_resize(&dyn->data.dict->value, space)) {
76  dyn->data.dict->key = (dyn_str*) realloc(dyn->data.dict->key, space * sizeof(dyn_str*));
77  if (dyn->data.dict->key) {
78  for (i=space - DICT_DEFAULT; i<space; ++i)
79  dyn->data.dict->key[i] = NULL;
80  }
81  }*/
82  }
83 
84  i = DYN_DICT_LENGTH(ptr);
85  ptr->key[i] = (dyn_str) malloc(dyn_strlen(key)+1);
86  if (ptr->key[i]) {
87  dyn_strcpy(ptr->key[i], key);
88  DYN_DICT_LENGTH(ptr)++;
89 
90 GOTO__CHANGE:
91  dyn_dict_change(dict, i, value);
92  return dyn_dict_get_i_ref(dict, i);
93  }
94 
95  return NULL;
96 }
dyn_ushort dyn_strlen(dyn_const_str str)
Returns the length of an string.
void dyn_strcpy(dyn_str destination, dyn_const_str source)
Copy string.
trilean dyn_dict_change(dyn_c *dict, const dyn_ushort i, const dyn_c *value)
Replace the ith element in a dictionary with a new value.
Definition: dynamic_dict.c:128
dyn_ushort dyn_dict_has_key(const dyn_c *dict, dyn_const_str key)
Check if dict has key and return its position - 1 (returns 0 if not found)
Definition: dynamic_dict.c:145
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_ushort space
elements available
Definition: dynamic_types.h:57
dyn_c * dyn_dict_get_i_ref(const dyn_c *dict, const dyn_ushort i)
Get the reference to ith value in dict.
Definition: dynamic_dict.c:168
void * ptr
pointer to function
Definition: dynamic_types.h:57
Basic container for dictionaries.
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_dict * dict
pointer to dynamic dictionary
dyn_str * key
array to C strings used as identifiers
Definition: dynamic_types.h:56
dyn_str * key
array to C strings used as identifiers
trilean dyn_dict_resize(dyn_c *dict, dyn_ushort size)
Set the available space for elements.
Definition: dynamic_dict.c:108
#define DYN_DICT_SPACE(dyn)
Return the maximal usable number of elements of a dictionary.
Definition: dynamic.h:196
#define DYN_DICT_LENGTH(dyn)
Return the number of elements stored within a dictionary.
Definition: dynamic.h:198
char * dyn_str
Standard dynamic C string.
Definition: dynamic_types.h:36

◆ dyn_dict_remove()

trilean dyn_dict_remove ( dyn_c dict,
dyn_const_str  key 
)

Remove key-value pair from dictionary.

Searches the dictionary for a certain key and removes the key value pair. The last element is moved to the freed position to spare space, such that there are no free positions within the linear dictionary representation.

Parameters
dicthas to be of type DICT
keyof the key-value pair to remove
Return values
DYN_TRUEif the key value pair was found and removed
DYN_FALSEotherwise

Definition at line 199 of file dynamic_dict.c.

200 {
201  dyn_dict* ptr = dict->data.dict;
202  dyn_ushort i = dyn_dict_has_key(dict, key);
203 
204  if(i) {
205  free(ptr->key[--i]);
206  ptr->key[i] = NULL;
207  dyn_free(DYN_DICT_GET_I_REF(dict, i));
208  ptr->value.data.list->length--;
209 
210  // if not last element
211  if (i != ptr->value.data.list->length && ptr->value.data.list->length) {
212  ptr->key[i] = ptr->key[ptr->value.data.list->length];
213  ptr->key[ptr->value.data.list->length] = NULL;
214  dyn_move(DYN_DICT_GET_I_REF(dict, ptr->value.data.list->length),
215  DYN_DICT_GET_I_REF(dict, i));
216  }
217 
218  return DYN_TRUE;
219  }
220 
221  return DYN_FALSE;
222 }
represents boolean false
Definition: dynamic_types.h:23
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
dyn_ushort dyn_dict_has_key(const dyn_c *dict, dyn_const_str key)
Check if dict has key and return its position - 1 (returns 0 if not found)
Definition: dynamic_dict.c:145
dyn_int i
basic integer
Definition: dynamic_types.h:59
void * ptr
pointer to function
Definition: dynamic_types.h:57
#define DYN_DICT_GET_I_REF(dyn, i)
Return a reference to the ith element stored within a dictionary.
Definition: dynamic.h:191
Basic container for dictionaries.
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_ushort length
elements in use
void dyn_move(dyn_c *from, dyn_c *to)
Move dynamic element to new reference, from is of type NONE afterwards.
dyn_dict * dict
pointer to dynamic dictionary
dyn_c value
dynamic element of type dyn_list
dyn_str * key
array to C strings used as identifiers
Definition: dynamic_types.h:56
dyn_str * key
array to C strings used as identifiers
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30

◆ dyn_dict_resize()

trilean dyn_dict_resize ( dyn_c dict,
dyn_ushort  size 
)

Set the available space for elements.

Increase or decrease the size of the dictionary to a new size, removed elements have to be of type NONE.

Parameters
[in,out]dicthas to be of type DICT
[in]sizenew
Return values
DYN_TRUEif operation could be performed
DYN_FALSEotherwise

Definition at line 108 of file dynamic_dict.c.

Referenced by dyn_dict_insert().

109 {
110  dyn_dict* ptr = dict->data.dict;
111 
113 
114  if (size > space)
115  if (dyn_list_resize(&ptr->value, size)) {
116  ptr->key = (dyn_str*) realloc(ptr->key, size * sizeof(dyn_str*));
117  if (ptr->key) {
118  for (; space<size; ++space)
119  ptr->key[space] = NULL;
120  return DYN_TRUE;
121  }
122  }
123 
124  return DYN_FALSE;
125 }
represents boolean false
Definition: dynamic_types.h:23
represents boolean true
Definition: dynamic_types.h:24
dyn_ushort space
elements available
Definition: dynamic_types.h:57
void * ptr
pointer to function
Definition: dynamic_types.h:57
Basic container for dictionaries.
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_dict * dict
pointer to dynamic dictionary
dyn_c value
dynamic element of type dyn_list
dyn_str * key
array to C strings used as identifiers
#define DYN_DICT_SPACE(dyn)
Return the maximal usable number of elements of a dictionary.
Definition: dynamic.h:196
trilean dyn_list_resize(dyn_c *list, const dyn_ushort size)
Change the maximal space of a list.
Definition: dynamic_list.c:91
char * dyn_str
Standard dynamic C string.
Definition: dynamic_types.h:36

◆ dyn_set_dict()

trilean dyn_set_dict ( dyn_c dyn,
dyn_ushort  length 
)

Set dyn to a dictionary with a max. length of elements.

Parameters
[in,out]dynelement which is initialized as a dictionary
[in]lengthof preallocated memory
Return values
DYN_TRUEif memory for the DICT could be allocated
DYN_FALSEotherwise

Definition at line 25 of file dynamic_dict.c.

Referenced by dyn_dict_copy().

26 {
27  dyn_free(dyn);
28 
29  dyn_dict *dict = (dyn_dict*) malloc(sizeof(dyn_dict));
30 
31  if (dict) {
32  DYN_INIT(&dict->value);
33  if (dyn_set_list_len(&dict->value, length)) {
34  dict->key = (dyn_str*) malloc(length * sizeof(dyn_str*));
35  if (dict->key) {
36  dyn_ushort i;
37  for (i=0; i<length; ++i)
38  dict->key[i] = NULL;
39 
40  dyn->type = DICT;
41  dyn->data.dict = dict;
42  return DYN_TRUE;
43  }
44  dyn_free(&dict->value);
45  }
46  free(dict);
47  }
48 
49  return DYN_FALSE;
50 }
represents boolean false
Definition: dynamic_types.h:23
represents boolean true
Definition: dynamic_types.h:24
dictionary of type dyn_dict
Definition: dynamic_types.h:69
dyn_dict * dict
pointer to dynamic dictionary
Definition: dynamic_types.h:63
dyn_int i
basic integer
Definition: dynamic_types.h:59
#define DYN_INIT(dyn)
Mandatory initialization for dynamic elements (NONE)
Definition: dynamic.h:38
dyn_ushort length
elements in use
Definition: dynamic_types.h:56
Basic container for dictionaries.
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_dict * dict
pointer to dynamic dictionary
dyn_c value
dynamic element of type dyn_list
dyn_str * key
array to C strings used as identifiers
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30
char * dyn_str
Standard dynamic C string.
Definition: dynamic_types.h:36
char type
type definition
trilean dyn_set_list_len(dyn_c *dyn, dyn_ushort len)
Set dynamic element to list with maximal length.
Definition: dynamic_list.c:37