-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutablePointerArray.c
More file actions
163 lines (130 loc) · 4.49 KB
/
MutablePointerArray.c
File metadata and controls
163 lines (130 loc) · 4.49 KB
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
/*****************************************************************************
GOOS - Duck-typing for C
Written in 2012 by Ryan Balsdon ryanbalsdon@gmail.com
To the extent possible under law, the author(s) have dedicated
all copyright and related and neighboring rights to this software
to the public domain worldwide. This software is distributed
without any warranty.
You should have received a copy of the CC0 Public Domain Dedication
along with this software. If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
FYI: This source was released originally as GPLv3 for the SAND project. If
you happen to have an older version, you're welcome to use whichever license
is least restrictive.
*****************************************************************************/
/*
MutablePointerArray
Mutable Array of Pointers
This class is meant to be a simple C replacement of NSMutableArray. It is far from as-capable but does the job.
Right now, this is an array that re-sizes itself as-needed. It was intended to be an array-list hybrid but this
works pretty well.
*/
#include "MutablePointerArray.h"
#include <stdlib.h>
#include <stdio.h>
void InitMutablePointerArray(MutablePointerArray* self) {
//default to 128 entries
self->Array = (void**)malloc(128*sizeof(void*));
self->ArrayAllocationSize = 128;
self->ArraySize = 0;
}
void FreeMutablePointerArray(MutablePointerArray* self) {
//Should this also free the managed pointers?
//No--they may need custom free routines
free(self->Array);
self->ArrayAllocationSize = 0;
self->ArraySize = 0;
}
inline int MPA_GetSize(MutablePointerArray* self) {
return self->ArraySize;
}
inline int MPA_Length(MutablePointerArray* self) {
return self->ArraySize;
}
static void MPA_IncreaseAllocation(MutablePointerArray* self) {
//double allocation
void ** newArray = (void**)malloc(2*self->ArrayAllocationSize*sizeof(void*));
for (int i=0; i<self->ArrayAllocationSize; i++) {
newArray[i] = self->Array[i];
}
free(self->Array);
self->Array = newArray;
self->ArrayAllocationSize *= 2;
}
int MPA_AddPointer(MutablePointerArray* self, void* pointer) {
if (self->ArrayAllocationSize <= self->ArraySize)
MPA_IncreaseAllocation(self);
self->Array[self->ArraySize] = pointer;
self->ArraySize++;
return self->ArraySize-1;
}
inline void* MPA_PointerAtIndex(MutablePointerArray* self, int index) {
return self->Array[index];
}
void* MPA_LastPointer(MutablePointerArray* self) {
return MPA_PointerAtIndex(self, MPA_GetSize(self)-1);
}
int MPA_RemoveIndex(MutablePointerArray* self, int index) {
//This does not free the memory!
for (int i=index; i<self->ArraySize-1; i++) {
self->Array[i] = self->Array[i+1];
}
self->ArraySize--;
self->Array[self->ArraySize] = 0;
return 1;
}
inline void MPA_FreeIndex(MutablePointerArray* self, int index) {
void* pointer = MPA_PointerAtIndex(self, index);
MPA_RemoveIndex(self, index);
free(pointer);
}
void MPA_FreeAll(MutablePointerArray* self) {
for (int i=MPA_GetSize(self)-1; i>=0; i--) {
MPA_FreeIndex(self, i);
}
}
inline void MPA_RemoveAll(MutablePointerArray* self) {
while (MPA_GetSize(self) > 0) {
MPA_Pop(self);
}
}
int MPA_IndexOfPointer(MutablePointerArray* self, void* pointer) {
for (int i=0; i<self->ArraySize; i++) {
if (self->Array[i] == pointer)
return i;
}
return -1;
}
int MPA_RemovePointer(MutablePointerArray* self, void* pointer) {
//This does not free the memory!
int i;
for (i=0; i<self->ArraySize; i++) {
if (self->Array[i] == pointer)
break;
}
if (i >= self->ArraySize) return 0; //No pointer found
MPA_RemoveIndex(self, i);
//Recurse incase there are more copies
MPA_RemovePointer(self, pointer);
return 1;
}
int MPA_FreePointer(MutablePointerArray* self, void* pointer) {
int index = MPA_IndexOfPointer(self, pointer);
if (index < 0) {
printf("ERROR: Pointer not found in Array\n");
return 0;
}
MPA_FreeIndex(self, index);
return 1;
}
void MPA_Pop(MutablePointerArray* self) {
MPA_RemoveIndex(self, MPA_GetSize(self)-1);
}
int MPA_Swap(MutablePointerArray* self, int indexA, int indexB) {
if (indexA > self->ArraySize || indexB > self->ArraySize)
return 0;
void* temp = self->Array[indexA];
self->Array[indexA] = self->Array[indexB];
self->Array[indexB] = temp;
return 1;
}