dynamiC  0.1
Macros | Functions
DynamicList

All list related functions. More...

Macros

#define DYN_SET_LIST(dyn)   dyn_set_list_len(dyn, LIST_DEFAULT)
 Initialize dyn as list with default length.
 
#define DYN_LIST_LEN(dyn)   (dyn)->data.list->length
 Return list length.
 
#define DYN_LIST_GET_REF(dyn, i)   &(dyn)->data.list->container[i]
 Return the reference to the ith element within a dynamic list.
 
#define DYN_LIST_GET_END(dyn)   &(dyn)->data.list->container[DYN_LIST_LEN(dyn)-1]
 Return the reference to the last element within a list.
 
#define DYN_LIST_GET_REF_END(dyn, i)   &(dyn)->data.list->container[DYN_LIST_LEN(dyn)-i]
 Return the reference to the ith element starting from the last.
 

Functions

trilean dyn_set_list_len (dyn_c *dyn, dyn_ushort len)
 Set dynamic element to list with maximal length. More...
 
dyn_cdyn_list_push (dyn_c *list, const dyn_c *element)
 Push new element to the end of a list. More...
 
dyn_cdyn_list_push_none (dyn_c *list)
 Push NONE element to the end of a list. More...
 
trilean dyn_list_pop (dyn_c *list, dyn_c *element)
 Pop the last element from the list and move it to param element. More...
 
trilean dyn_list_get (const dyn_c *list, dyn_c *element, const dyn_short i)
 Copy the ith element of a list to param element. More...
 
dyn_cdyn_list_get_ref (const dyn_c *list, const dyn_short i)
 Return a reference to the ith element within list, negative values are allowed. More...
 
trilean dyn_list_popi (dyn_c *list, const dyn_short i)
 Pop i elements from the end of a list. More...
 
void dyn_list_free (dyn_c *list)
 Free the allocated memory of the entire list and set it to NONE. More...
 
trilean dyn_list_copy (const dyn_c *list, dyn_c *copy)
 Make a deep copy of the entire list. More...
 
trilean dyn_list_remove (dyn_c *list, dyn_ushort i)
 Delete the ith element from a list. More...
 
trilean dyn_list_insert (dyn_c *list, dyn_c *element, const dyn_ushort i)
 Insert a new element at the ith position into a list. More...
 
trilean dyn_list_resize (dyn_c *list, const dyn_ushort size)
 Change the maximal space of a list. More...
 
dyn_ushort dyn_list_string_len (const dyn_c *list)
 Return the length of the string representation of a list. More...
 
void dyn_list_string_add (const dyn_c *list, dyn_str str)
 Add string representation of a list to str. More...
 

Detailed Description

All list related functions.

Function Documentation

◆ dyn_list_copy()

trilean dyn_list_copy ( const dyn_c list,
dyn_c copy 
)

Make a deep copy of the entire list.

See also
dyn_copy
Parameters
[in]listoriginal
[in,out]copynew list
Return values
DYN_TRUEif the element was found and coppied
DYN_FALSEotherwise

Definition at line 308 of file dynamic_list.c.

Referenced by dyn_get_bool().

309 {
310  dyn_ushort len = DYN_LIST_LEN(list);
311 
312  if (dyn_set_list_len(copy, len)) {
313  list = list->data.list->container;
314  while (len--) {
315  if (!dyn_list_push(copy, list++)) {
316  dyn_free(copy);
317  return DYN_FALSE;
318  }
319  }
320  return DYN_TRUE;
321  }
322  return DYN_FALSE;
323 }
represents boolean false
Definition: dynamic_types.h:23
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
#define DYN_LIST_LEN(dyn)
Return list length.
Definition: dynamic.h:123
dyn_c * container
pointer to an array of dynamic elements
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_c * dyn_list_push(dyn_c *list, const dyn_c *element)
Push new element to the end of a list.
Definition: dynamic_list.c:124
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30
trilean dyn_set_list_len(dyn_c *dyn, dyn_ushort len)
Set dynamic element to list with maximal length.
Definition: dynamic_list.c:37

◆ dyn_list_free()

void dyn_list_free ( dyn_c dyn)

Free the allocated memory of the entire list and set it to NONE.

Parameters
[in,out]listinput put has to be a list

Definition at line 67 of file dynamic_list.c.

Referenced by dyn_free().

68 {
69  dyn_ushort len = DYN_LIST_LEN(dyn);
70 
71  // free all elements within the allocated container element
72  dyn_c *ptr = dyn->data.list->container;
73  while (len--) {
74  dyn_free(ptr++);
75  }
76 
77  free(dyn->data.list->container);
78  free(dyn->data.list);
79 }
dyn_list * list
pointer to dynamic list
#define DYN_LIST_LEN(dyn)
Return list length.
Definition: dynamic.h:123
dyn_c * container
pointer to an array of dynamic elements
void * ptr
pointer to function
Definition: dynamic_types.h:57
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30
Basic container for dynamic data types.
Definition: dynamic_types.h:98

◆ dyn_list_get()

trilean dyn_list_get ( const dyn_c list,
dyn_c element,
const dyn_short  i 
)

Copy the ith element of a list to param element.

Copies the i value in the list to element. If the i value is negative, then the i value is relative to the last value (-1). If only the reference to the value in the list is required, use dyn_list_get_ref

See also
dyn_list_get_ref
Parameters
[in]listinput has to be of type LIST
[in,out]elementto copy to
[in]iposition in list
Return values
DYN_TRUEif the element was found and coppied
DYN_FALSEotherwise

Definition at line 264 of file dynamic_list.c.

265 {
266  dyn_free(element);
267 
268  dyn_c* ptr = dyn_list_get_ref(list, i);
269 
270  if (ptr) {
271  return dyn_copy(ptr, element);
272  }
273 
274  return DYN_FALSE;
275 }
represents boolean false
Definition: dynamic_types.h:23
dyn_int i
basic integer
Definition: dynamic_types.h:59
trilean dyn_copy(const dyn_c *dyn, dyn_c *copy)
Deep copy dynamic element.
void * ptr
pointer to function
Definition: dynamic_types.h:57
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
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30
Basic container for dynamic data types.
Definition: dynamic_types.h:98

◆ dyn_list_get_ref()

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.

Returns the reference of the i value in list, if a negative position value is used, then the position is calculated from the end. And if the i position exceeds the length of the list, NULL is returned.

See also
dyn_list_get
Parameters
listinput has to be of type LIST
iposition in list
Returns
reference to the ith value

Definition at line 289 of file dynamic_list.c.

Referenced by dyn_dict_get_i_ref(), and dyn_list_get().

290 {
291  dyn_list *ptr = list->data.list;
292  if (i >= 0 && i<= ptr->length)
293  return &ptr->container[i];
294  else if (i < 0 && -i <= ptr->length)
295  return &ptr->container[ ptr->length+i ];
296  return NULL;
297 }
dyn_list * list
pointer to dynamic list
dyn_c * container
pointer to an array of dynamic elements
dyn_int i
basic integer
Definition: dynamic_types.h:59
void * ptr
pointer to function
Definition: dynamic_types.h:57
dyn_ushort length
elements in use
Definition: dynamic_types.h:56
dyn_ushort length
elements in use
Basic container for lists.

◆ dyn_list_insert()

trilean dyn_list_insert ( dyn_c list,
dyn_c element,
const dyn_ushort  i 
)

Insert a new element at the ith position into a list.

Insert a new element at position i to a list, all other elements with pos>i are shifted "to the left" befor the element gets inserted (moved).

// pseudo code
dyn_list_insert([0,1,3,4], "22", 2) == [0,1,"22",3,4]
Parameters
[in,out]listinput has to be of type LIST
[in]elementto be inserted
[in]iposition
Return values
DYN_TRUEif the required memory could be allocated

Definition at line 199 of file dynamic_list.c.

200 {
201  dyn_ushort n = DYN_LIST_LEN(list);
202  if (n >= i) {
203  dyn_list_push_none(list);
204 
205  dyn_c *ptr = list->data.list->container;
206 
207  ++n;
208  while(--n > i)
209  dyn_move(&ptr[n-1], &ptr[n]);
210 
211  dyn_move(element, &ptr[i]);
212  }
213  return DYN_TRUE;
214 }
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
#define DYN_LIST_LEN(dyn)
Return list length.
Definition: dynamic.h:123
dyn_c * container
pointer to an array of dynamic elements
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_c * dyn_list_push_none(dyn_c *list)
Push NONE element to the end of a list.
Definition: dynamic_list.c:146
void * ptr
pointer to function
Definition: dynamic_types.h:57
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
void dyn_move(dyn_c *from, dyn_c *to)
Move dynamic element to new reference, from is of type NONE afterwards.
Basic container for dynamic data types.
Definition: dynamic_types.h:98

◆ dyn_list_pop()

trilean dyn_list_pop ( dyn_c list,
dyn_c element 
)

Pop the last element from the list and move it to param element.

Pops the last element from the list and moves its content to parameter element. The length of the list decreases by one.

Parameters
[in,out]listinput has to be of type LIST
[in,out]elementwhere last dynamic value is moved to
Return values
DYN_TRUEif worked properly

Definition at line 225 of file dynamic_list.c.

226 {
227  dyn_list *ptr = list->data.list;
228 
229  dyn_move(&ptr->container[--ptr->length], element);
230 
231  if (ptr->space - ptr->length > LIST_DEFAULT)
232  if (!dyn_list_resize(list, ptr->space - LIST_DEFAULT))
233  return DYN_FALSE;
234 
235  return DYN_TRUE;
236 }
represents boolean false
Definition: dynamic_types.h:23
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
dyn_c * container
pointer to an array of dynamic elements
dyn_ushort space
elements available
void * ptr
pointer to function
Definition: dynamic_types.h:57
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.
Basic container for lists.
trilean dyn_list_resize(dyn_c *list, dyn_ushort size)
Change the maximal space of a list.
Definition: dynamic_list.c:91

◆ dyn_list_popi()

trilean dyn_list_popi ( dyn_c list,
dyn_short  i 
)

Pop i elements from the end of a list.

Parameters
[in,out]listinput has to be of type LIST
[in]inumber of elements to be popped

Definition at line 242 of file dynamic_list.c.

Referenced by dyn_list_remove().

243 {
244  while(i--)
245  dyn_free(&list->data.list->container[ --list->data.list->length ]);
246 
247  return DYN_TRUE;
248 }
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
dyn_c * container
pointer to an array of dynamic elements
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_ushort length
elements in use
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30

◆ dyn_list_push()

dyn_c* dyn_list_push ( dyn_c list,
const dyn_c element 
)

Push new element to the end of a list.

Pushes (copies) an additional element to the end of a list and increases the length value by one. If the maximal available space is reached, then new memory is allocated and the maximal space value gets increased, this additional size value is defined in LIST_DEFAULT.

Parameters
[in,out]listinput has to be of type LIST
[in]elementto be pushed
Returns
a reference to the element at the end of the list

Definition at line 124 of file dynamic_list.c.

Referenced by dyn_list_copy(), and dyn_set_insert().

125 {
126  dyn_list *ptr = list->data.list;
127 
128  if (ptr->length == ptr->space)
129  if (!dyn_list_resize(list, ptr->space + LIST_DEFAULT))
130  return NULL;
131 
132  dyn_copy(element, &ptr->container[ ptr->length++ ]);
133 
134  return &ptr->container[ ptr->length-1 ];
135 }
dyn_list * list
pointer to dynamic list
dyn_c * container
pointer to an array of dynamic elements
trilean dyn_copy(const dyn_c *dyn, dyn_c *copy)
Deep copy dynamic element.
dyn_ushort space
elements available
void * ptr
pointer to function
Definition: dynamic_types.h:57
dyn_ushort length
elements in use
Basic container for lists.
trilean dyn_list_resize(dyn_c *list, dyn_ushort size)
Change the maximal space of a list.
Definition: dynamic_list.c:91

◆ dyn_list_push_none()

dyn_c* dyn_list_push_none ( dyn_c list)

Push NONE element to the end of a list.

Increses the length value of the list by one and returns a reference to the last NONE element of the list. This reference can be used to move a larger element to the end of a list, without copying.

Parameters
[in,out]listinput has to be of type LIST
Returns
reference to the pushed NONE value

Definition at line 146 of file dynamic_list.c.

Referenced by dyn_list_insert().

147 {
148  dyn_list *ptr = list->data.list;
149  if (ptr->length == ptr->space)
150  if (!dyn_list_resize(list, ptr->space + LIST_DEFAULT))
151  return NULL;
152 
153  return &ptr->container[ ptr->length++ ];
154 }
dyn_list * list
pointer to dynamic list
dyn_c * container
pointer to an array of dynamic elements
dyn_ushort space
elements available
void * ptr
pointer to function
Definition: dynamic_types.h:57
dyn_ushort length
elements in use
Basic container for lists.
trilean dyn_list_resize(dyn_c *list, dyn_ushort size)
Change the maximal space of a list.
Definition: dynamic_list.c:91

◆ dyn_list_remove()

trilean dyn_list_remove ( dyn_c list,
dyn_ushort  i 
)

Delete the ith element from a list.

Delete an element from the list at position i, successive elements are moved to close this gap. This function can be interpeted as the opposite to dyp_list_insert

// pseudo code
dyn_list_remove([0,1,2,3,4], 2) == [0,1,3,4]
Parameters
[in,out]listinput has to be of type LIST
[in]iposition to remove
Return values
DYN_TRUEif the required memory could be allocated
DYN_FALSEotherwise

Definition at line 172 of file dynamic_list.c.

173 {
174  dyn_list *ptr = list->data.list;
175  if (ptr->length > i) {
176  for(; i<ptr->length-1; ++i) {
177  dyn_move(&ptr->container[i+1], &ptr->container[i]);
178  }
179  dyn_list_popi(list, 1);
180  }
181  return DYN_TRUE;
182 }
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
dyn_c * container
pointer to an array of dynamic elements
dyn_int i
basic integer
Definition: dynamic_types.h:59
trilean dyn_list_popi(dyn_c *list, dyn_short i)
Pop i elements from the end of a list.
Definition: dynamic_list.c:242
void * ptr
pointer to function
Definition: dynamic_types.h:57
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.
Basic container for lists.

◆ dyn_list_resize()

trilean dyn_list_resize ( dyn_c list,
dyn_ushort  size 
)

Change the maximal space of a list.

Resize the maximal usable space, if the size decreases then the removed elements have to be removed previously.

Parameters
[in,out]listinput put has to be a list
[in]sizenew maximal available space
Return values
DYN_TRUEif the required memory could be allocated
DYN_FALSEotherwise

Definition at line 91 of file dynamic_list.c.

Referenced by dyn_dict_resize(), dyn_list_push(), and dyn_list_push_none().

92 {
93  dyn_list *ptr = list->data.list;
94 
95  dyn_c* new_list = (dyn_c*) realloc(ptr->container, size * sizeof(dyn_c));
96 
97  if (new_list) {
98  ptr->container = new_list;
99 
100  if (ptr->space < size) {
101  dyn_ushort i = ptr->length;
102  for(; i<size; ++i)
103  DYN_INIT(&ptr->container[i]);
104  }
105 
106  ptr->space = size;
107  return DYN_TRUE;
108  }
109 
110  return DYN_FALSE;
111 }
represents boolean false
Definition: dynamic_types.h:23
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
dyn_c * container
pointer to an array of dynamic elements
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_ushort space
elements available
#define DYN_INIT(dyn)
Mandatory initialization for dynamic elements (NONE)
Definition: dynamic.h:38
void * ptr
pointer to function
Definition: dynamic_types.h:57
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_ushort length
elements in use
Basic container for lists.
Basic container for dynamic data types.
Definition: dynamic_types.h:98

◆ dyn_list_string_add()

void dyn_list_string_add ( const dyn_c list,
dyn_str  str 
)

Add string representation of a list to str.

Generates a string representation of the list and all included element, segregated by commas, and attaches it to the end of the string.

Parameters
[in]listinput has to be of type LIST
[in,out]strwith added list representation

Definition at line 351 of file dynamic_list.c.

Referenced by dyn_get_bool().

352 {
353  dyn_strcat(str, "[");
354  dyn_ushort len = DYN_LIST_LEN(list);
355 
356  if (len == 0) {
357  dyn_strcat(str, "]");
358  return;
359  }
360 
361  dyn_ushort i;
362  for (i=0; i<len; i++) {
364  dyn_strcat(str, ",");
365  }
366  str[dyn_strlen(str)-1] = ']';
367 }
dyn_ushort dyn_strlen(dyn_const_str str)
Returns the length of an string.
#define DYN_LIST_LEN(dyn)
Return list length.
Definition: dynamic.h:123
dyn_int i
basic integer
Definition: dynamic_types.h:59
#define DYN_LIST_GET_REF(dyn, i)
Return the reference to the ith element within a dynamic list.
Definition: dynamic.h:125
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
void dyn_string_add(const dyn_c *dyn, dyn_str string)
Add string representation of dynamic element to string.
dyn_str str
pointer to character-array
Definition: dynamic_types.h:61
void dyn_strcat(dyn_str destination, dyn_const_str source)
Concatenate strings.

◆ dyn_list_string_len()

dyn_ushort dyn_list_string_len ( const dyn_c list)

Return the length of the string representation of a list.

The length of the list string representation is calculated by the length of all included elemens plus 2 brackets and the number of commas.

Parameters
listinput has to be of type LIST
Returns
length of string

Definition at line 333 of file dynamic_list.c.

Referenced by dyn_get_bool().

334 {
335  dyn_ushort size = DYN_LIST_LEN(list)+1;
336  dyn_ushort len = 2 + size;
337 
338  while (--size)
339  len += dyn_string_len(DYN_LIST_GET_REF(list, size-1));
340 
341  return len;
342 }
#define DYN_LIST_LEN(dyn)
Return list length.
Definition: dynamic.h:123
#define DYN_LIST_GET_REF(dyn, i)
Return the reference to the ith element within a dynamic list.
Definition: dynamic.h:125
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_ushort dyn_string_len(const dyn_c *dyn)
Calculate length of string representation of dynamic element.

◆ dyn_set_list_len()

trilean dyn_set_list_len ( dyn_c dyn,
dyn_ushort  len 
)

Set dynamic element to list with maximal length.

Takes in any kind of dynamic paramter, frees all allocated memory and allocates a new array of dynamic elemens with a length defined in paramter len. The len paramter us used to denote the max space available space, initially the lenght of a list is marked as empty. Every application of of dyn_list_push increases the internal counter of dyn->data.list->len, until the max value dyn->data.list->space is reached, if so, new memory is allocated automatically. Every popped value by applying dyn_list_pop decreases the internal counter.

Parameters
[in,out]dyninput any, output LIST
[in]len
Return values
DYN_TRUEif the required memory could be allocated
DYN_FALSEotherwise

Definition at line 37 of file dynamic_list.c.

Referenced by dyn_list_copy(), dyn_set_dict(), and dyn_set_set_len().

38 {
39  dyn_free(dyn);
40 
41  dyn_list *list = (dyn_list*) malloc(sizeof(dyn_list));
42 
43  if (list) {
44 
45  list->container = (dyn_c*) malloc(len * sizeof(dyn_c));
46 
47  if (list->container) {
48  list->space = len;
49  list->length = 0;
50 
51  // Initialize all elements in list with NONE
52  while (len--)
53  DYN_INIT(&list->container[len]);
54 
55  dyn->type = LIST;
56  dyn->data.list = list;
57  return DYN_TRUE;
58  }
59  free(list);
60  }
61  return DYN_FALSE;
62 }
represents boolean false
Definition: dynamic_types.h:23
represents boolean true
Definition: dynamic_types.h:24
dyn_list * list
pointer to dynamic list
dyn_list * list
pointer to dynamic list
Definition: dynamic_types.h:62
dyn_c * container
pointer to an array of dynamic elements
dyn_ushort space
elements available
#define DYN_INIT(dyn)
Mandatory initialization for dynamic elements (NONE)
Definition: dynamic.h:38
list of type dyn_list
Definition: dynamic_types.h:67
dyn_ushort length
elements in use
Basic container for lists.
void dyn_free(dyn_c *dyn)
free allocated memory
Definition: dynamic.c:30
char type
type definition
Basic container for dynamic data types.
Definition: dynamic_types.h:98