Dinamic Array
 
Loading...
Searching...
No Matches
darr.h File Reference
#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Classes

struct  darr_t
 

Macros

#define DARR_DEFAULT_CAPACITY   2
 
#define DARR_DEFAULT_GROWING_FACTOR   1.25f
 

Typedefs

typedef bool compfun_t(void const *curr, void const *value)
 
typedef struct darr_t darr_t
 

Functions

darr_tdarr_new (void)
 
size_t darr_get_size (darr_t *da)
 
size_t darr_get_capacity (darr_t *da)
 
void * darr_at (darr_t *da, size_t index)
 
int darr_push (darr_t *da, void *value, size_t size)
 
int darr_pop (darr_t *da)
 
int darr_insert_at (darr_t *da, size_t index, void *value, size_t size)
 
int darr_delete_at (darr_t *da, size_t index)
 
int darr_find (darr_t *da, void *value, compfun_t compare)
 
void darr_destroy (darr_t *da)
 

Macro Definition Documentation

◆ 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.

Typedef Documentation

◆ 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.

Function Documentation

◆ 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}
void ** _arr
Definition darr.h:52
size_t _size
Definition darr.h:54

◆ 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
115 if (!_shrink_darr(da))
116 return -1;
117
118 --da->_size;
119
120 return index;
121}
bool _shrink_darr(darr_t *da)
private method form darr_t
Definition darr.c:169

◆ 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)
137 free(da->_arr[i]);
138
139 if (da->_arr != NULL)
140 free(da->_arr);
141
142 free(da);
143}

◆ darr_find()

int darr_find ( darr_t * da,
void * value,
compfun_t compare )

Definition at line 123 of file darr.c.

123 {
124 for (size_t i = 0; i < da->_size; ++i)
125 if (compare(da->_arr[i], value))
126 return i;
127
128 return -1;
129}
bool compare(void const *curr, void const *value)
Definition main.c:111

◆ 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.

45{ return da->_capacity; }
size_t _capacity
Definition darr.h:56

◆ 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.

37{ return da->_size; }

◆ 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
90 if (!_increase_darr(da))
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;
101 ++da->_size;
102
103 return index;
104}
bool _increase_darr(darr_t *da)
private method form darr_t
Definition darr.c:145

◆ darr_new()

darr_t * darr_new ( void )

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 {
22 darr_t *da = malloc(sizeof(darr_t));
23
24 da->_size = 0;
26 da->_arr = malloc(da->_capacity * sizeof(void *));
27
28 return da;
29}
#define DARR_DEFAULT_CAPACITY
Definition darr.h:13
Definition darr.h:50

◆ darr_pop()

int darr_pop ( darr_t * da)

Definition at line 70 of file darr.c.

70 {
71 if (da->_size == 0)
72 return -1;
73
74 size_t const index = da->_size - 1;
75
76 free(da->_arr[index]);
77
78 --da->_size;
79
80 if (!_shrink_darr(da))
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
57 if (!_increase_darr(da))
58 return -1;
59
60 void *copy = malloc(size);
61
62 memcpy(copy, value, size);
63
64 da->_arr[index] = copy;
65 ++da->_size;
66
67 return index;
68}