#include <stdbool.h>
#include <stddef.h>
Go to the source code of this file.
|
typedef bool | compfun_t(void const *curr, void const *value) |
|
typedef struct darr_t | darr_t |
|
◆ DARR_DEFAULT_CAPACITY
#define DARR_DEFAULT_CAPACITY 2 |
Initial array capacity, that's just an arbitrary number and it can shrank as the items is removed. But this value can be changed to avoid resizing the array too soon.
Definition at line 13 of file darr.h.
◆ DARR_DEFAULT_GROWING_FACTOR
#define DARR_DEFAULT_GROWING_FACTOR 1.25f |
The growing factor will define how much the array should be resized when needed. Between 1 and 2 is a good choice. By the way, this value should be a float.
Definition at line 22 of file darr.h.
◆ compfun_t
typedef bool compfun_t(void const *curr, void const *value) |
Type for the function that the darr_find()
method will use to iterate over all items in the array to compare with the target item that needs to be found. The user isn't meant to use this type, it's useful to make the definition of the find paramter easier to write – but it can also be used as reference to build the comparation function in the main code.
Definition at line 32 of file darr.h.
◆ darr_t
typedef struct darr_t darr_t |
Struct that will hold the actual dinamic array and other informations to search, insert and remove elements from within. Since C doesn't support generic types, the array will be stored as an array that points to a sequence of pointers (I know complicated).
Since all the items is a generic pointer (void*
) to something, it's due to the developer to cast that item into a type that makes sence, for an example: darr_at(mydarr, 1)
will be a void*
, cast this void
to a int
with (int *)darr_at(mydarr, 1)
to interprete this as a pointer to an integer, and now access the value that it's pointing to with an extra *
, like *(int *)darr_at(mydarr, 1)
.
Check the detailed darr_t
description for more information about it's attributes.
◆ darr_at()
void * darr_at |
( |
darr_t * | da, |
|
|
size_t | index ) |
Definition at line 50 of file darr.c.
50 {
51 return index >= da->
_size ? NULL : da->
_arr[index];
52}
◆ darr_delete_at()
int darr_delete_at |
( |
darr_t * | da, |
|
|
size_t | index ) |
Definition at line 106 of file darr.c.
106 {
107 if (index >= da->
_size)
108 return -1;
109
110 free(da->
_arr[index]);
111
112 memmove(&da->
_arr[index], &da->
_arr[index + 1],
113 sizeof(
void *) * (da->
_size - index));
114
116 return -1;
117
119
120 return index;
121}
bool _shrink_darr(darr_t *da)
private method form darr_t
◆ darr_destroy()
void darr_destroy |
( |
darr_t * | da | ) |
|
Definition at line 131 of file darr.c.
131 {
132 if (da == NULL)
133 return;
134
135 for (
size_t i = 0; i < da->
_size; ++i)
136 if (da->
_arr[i] != NULL)
138
139 if (da->
_arr != NULL)
141
142 free(da);
143}
◆ darr_find()
Definition at line 123 of file darr.c.
123 {
124 for (
size_t i = 0; i < da->
_size; ++i)
126 return i;
127
128 return -1;
129}
bool compare(void const *curr, void const *value)
◆ darr_get_capacity()
size_t darr_get_capacity |
( |
darr_t * | da | ) |
|
Simple getter, this one was intended to be used for debugging propurses, but it can be in handy when needing to have fine control over the memory usage.
- Returns
- The
darr_t._capacity
attribute.
Definition at line 45 of file darr.c.
◆ darr_get_size()
size_t darr_get_size |
( |
darr_t * | da | ) |
|
Simple getter, intended to be used in for
/while
loops to interact with the array itself.
- Returns
- The
darr_t._size
attribute.
Definition at line 37 of file darr.c.
◆ darr_insert_at()
int darr_insert_at |
( |
darr_t * | da, |
|
|
size_t | index, |
|
|
void * | value, |
|
|
size_t | size ) |
Definition at line 86 of file darr.c.
86 {
87 if (index > da->
_size)
88 return -1;
89
91 return -1;
92
93 memmove(&da->
_arr[index + 1], &da->
_arr[index],
94 sizeof(
void *) * (da->
_size - index));
95
96 void *copy = malloc(size);
97
98 memcpy(copy, value, size);
99
100 da->
_arr[index] = copy;
102
103 return index;
104}
bool _increase_darr(darr_t *da)
private method form darr_t
◆ darr_new()
Creates a darr_t
instance in the heap and allocates the initial size that the array will occupe based on the DARR_DEFAULT_CAPACITY
value. The array will be a pointer [array] to other pointers with void
(generic) type, this means that the items should be castet to some type when using, e.g.: *(float *)da->_arr[1]
or, even better, *(float *)darr_at(da, 1)
.
- Returns
- A pointer to the created
darr_t
instance, it should be used in the other darr_*()
methods to interact with the array itself.
Definition at line 21 of file darr.c.
21 {
23
27
28 return da;
29}
#define DARR_DEFAULT_CAPACITY
◆ darr_pop()
Definition at line 70 of file darr.c.
70 {
72 return -1;
73
74 size_t const index = da->
_size - 1;
75
76 free(da->
_arr[index]);
77
79
81 return -1;
82
83 return index;
84}
◆ darr_push()
int darr_push |
( |
darr_t * | da, |
|
|
void * | value, |
|
|
size_t | size ) |
Definition at line 54 of file darr.c.
54 {
55 size_t const index = da->
_size;
56
58 return -1;
59
60 void *copy = malloc(size);
61
62 memcpy(copy, value, size);
63
64 da->
_arr[index] = copy;
66
67 return index;
68}