[1fa077]: ArrayList.c Maximize Restore History

Download this file

ArrayList.c    186 lines (164 with data), 4.6 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "AutoPanoSift.h"
/* This is a minimal subset of the C# Container Class ArrayList */
void ArrayList_init(ArrayList* self, void* deletefn)
{
self->count = 0;
self->reserved = 0;
self->items = NULL;
self->deletefn = deletefn;
}
ArrayList* ArrayList_new0(void* deletefn)
{
ArrayList* self = (ArrayList*)malloc(sizeof(ArrayList));
ArrayList_init(self, deletefn);
return self;
}
ArrayList* ArrayList_new(int dim, void* deletefn)
{
ArrayList* self = ArrayList_new0(deletefn);
self->count = dim;
self->reserved = dim;
self->items = (void**)malloc(self->reserved * sizeof(void*));
int index;
for(index=0; index<self->reserved; index++) {
self->items[index] = NULL;
}
return self;
}
ArrayList* ArrayList_clone(ArrayList* self)
{
ArrayList* cl = ArrayList_new0(self->deletefn);
cl->count = self->count;
cl->reserved = self->reserved;
cl->items = (void**)malloc(cl->reserved * sizeof(void*));
memcpy(cl->items, self->items, self->count * sizeof(void*));
return cl;
}
void ArrayList_delete(ArrayList* self)
{
if (self != NULL) {
if (self->items != NULL) {
if (self->deletefn != NULL) {
int index=0;
void (*_deletefn)(void*) = (void ( *)(void *)) self->deletefn;
for(index=0; index<self->count; index++) {
if (self->items[index]) {
_deletefn(self->items[index]);
self->items[index] = NULL;
}
}
}
free(self->items);
self->items = NULL;
}
self->count = 0;
self->reserved = 0;
free(self);
}
}
int ArrayList_Count(ArrayList* self)
{
return self->count;
}
void* ArrayList_GetItem(ArrayList* self, int index)
{
if (index < 0 || index >= self->count) {
FatalError("Array out of bounds");
return NULL;
}
return self->items[index];
}
void ArrayList_SetItem(ArrayList* self, int index, void* item)
{
if (index < 0 || index >= self->count) {
WriteError("Array out of bounds");
return;
}
self->items[index] = item;
}
int ArrayList_IndexOf(ArrayList* self, void* item)
{
int index;
for(index=0; index<self->count; index++) {
if( self->items[index] == item) {
return index;
}
}
return -1;
}
bool ArrayList_Contains(ArrayList* self, void* item)
{
return ArrayList_IndexOf(self, item) >= 0;
}
void ArrayList_AddItem(ArrayList* self, void* item)
{
if (self->count+1 > self->reserved) {
void* tmp = self->items;
self->reserved = self->reserved * 5 + 10;
self->items = (void**)malloc(self->reserved * sizeof(void*));
if (tmp != NULL) {
memcpy(self->items, tmp, self->count * sizeof(void*));
free(tmp);
}
}
self->items[self->count] = item;
self->count++;
}
void ArrayList_AddRange(ArrayList* self, ArrayList* list)
{
if (self->count+list->count > self->reserved) {
void* tmp = self->items;
self->reserved = self->count + list->count + 10;
self->items = (void**)malloc(self->reserved * sizeof(void*));
if (tmp != NULL) {
memcpy(self->items, tmp, self->count * sizeof(void*));
free(tmp);
}
}
memcpy(self->items+self->count, list->items, list->count * sizeof(void*));
self->count += list->count;
}
void ArrayList_Copy(ArrayList* self, int start, ArrayList* dest, int offset, int len)
{
if (dest->reserved < len) {
if (dest->reserved>0) {
free(dest->items);
}
dest->reserved = len+10;
dest->items = (void**)malloc(dest->reserved * sizeof(void*));
}
memcpy(dest->items, self->items+start, len*sizeof(void*));
}
void ArrayList_RemoveItem(ArrayList* self, void* value)
{
int index;
for(index=0; index<self->count; index++) {
if (self->items[index] == value) {
memmove(self->items+index, self->items+index+1, (self->count-(index+1))*sizeof(void*));
self->count--;
return;
}
}
}
void ArrayList_RemoveAt(ArrayList* self, int index)
{
memmove(self->items+index, self->items+index+1, (self->count-(index+1))*sizeof(void*));
self->count--;
}
void ArrayList_RemoveRange(ArrayList* self, int start, int len)
{
memmove(self->items+start, self->items+start+len, (self->count-(start+len))*sizeof(void*));
self->count-=len;
}
IComparator* ArrayList_comparator = NULL;
int ArrayList_compareTo(const void** obj1, const void** obj2) {
return ArrayList_comparator->compareTo(ArrayList_comparator, *obj1, *obj2);
}
void ArrayList_Sort(ArrayList* self, IComparator* cmp) {
if (ArrayList_comparator != NULL) {
FatalError("ArrayList_Sort called recursively");
}
ArrayList_comparator = cmp;
qsort(self->items, self->count, sizeof(void*), (int ( *)(const void *,const void *)) ArrayList_compareTo);
ArrayList_comparator = NULL;
}