-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynarray.c
175 lines (137 loc) · 4.1 KB
/
dynarray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* [c] 2011 by tm512 (Kyle Davis)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "dynarray.h"
// Create the array on the heap, initialize.
dynarray_t *dynarray_create (unsigned int max, unsigned int size, unsigned char resize)
{
dynarray_t *ret = (dynarray_t *) malloc (sizeof (dynarray_t));
if (!ret)
return NULL;
memset (ret, 0, sizeof (dynarray_t));
ret->e_size = size;
ret->e_max = max;
ret->resize = resize;
ret->index = (void **) malloc (max * sizeof (void *));
ret->elements = malloc (size * max);
if (!ret->index || !ret->elements)
{
dynarray_delete (&ret);
return NULL;
}
memset (ret->index, 0, max * sizeof (void *));
memset (ret->elements, 0, size * max);
return ret;
}
// Destroy dynarray
void dynarray_delete (dynarray_t **arr)
{
if (!arr || !*arr)
return;
if ((*arr)->index)
free ((*arr)->index);
if ((*arr)->elements)
free ((*arr)->elements);
free (*arr);
*arr = NULL;
return;
}
// Resize array
// returns arr on success, NULL on failure
dynarray_t *dynarray_resize (dynarray_t *arr, unsigned int new_size)
{
void *tmp;
if (!(tmp = realloc (arr->index, new_size * sizeof (void *))))
return NULL;
else
arr->index = tmp;
if (!(tmp = realloc (arr->elements, new_size * arr->e_size)))
return NULL;
// Adjust index if necessary
void *oldel = arr->elements;
arr->elements = tmp;
if (arr->elements != oldel)
{
int i;
for (i = 0; i < arr->e_max; i++)
if (arr->index [i])
arr->index [i] = ((char*)arr->elements) + arr->e_size * i;
}
// Initialize new memory
if (new_size > arr->e_max)
{
memset (arr->index + arr->e_max, 0, (new_size - arr->e_max) * sizeof (void *));
memset ((char*)arr->elements + arr->e_max * arr->e_size, 0, (new_size - arr->e_max) * arr->e_size);
}
arr->e_max = new_size;
return arr;
}
// Add element to array, returns pointer to new element or NULL on failure
void *dynarray_insert (dynarray_t *arr, void *new, unsigned int pos)
{
if (pos > arr->e_max || !new || !arr)
return NULL;
return arr->index [pos] = memcpy ((char*)arr->elements + arr->e_size * pos, new, arr->e_size);
}
// Push element
void *dynarray_push (dynarray_t *arr, void *new)
{
// Valid?
if (!arr || !new)
return NULL;
// Find empty slot
int i;
for (i = 0; i < arr->e_max; i++)
if (arr->index [i] == NULL)
break;
if (i == arr->e_max) // Out of room, resize!
{
if (arr->resize)
{
return dynarray_push (dynarray_resize (arr, arr->e_max + arr->resize), new);
}
else
return NULL;
}
return dynarray_insert (arr, new, i);
}
// Clear element from array
void dynarray_clear (dynarray_t *arr, unsigned int ele)
{
if (!arr || ele > arr->e_max)
return;
arr->index [ele] = NULL;
memset ((char*)arr->elements + ele * arr->e_size, 0, arr->e_size);
return;
}
// Pop top element from array, write it to a pointer
void dynarray_pop (dynarray_t *arr, void *dest)
{
if (!arr)
return;
int i;
for (i = arr->e_max - 1; i >= 0; i--)
if (arr->index [i])
break;
if (dest)
memcpy (dest, arr->index [i], arr->e_size);
dynarray_clear (arr, i);
return;
}