-
Notifications
You must be signed in to change notification settings - Fork 4
/
DynamicArray.cs
232 lines (204 loc) · 4.57 KB
/
DynamicArray.cs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections;
using CutTheRope.ios;
internal class DynamicArray : NSObject, IEnumerable
{
public const int DEFAULT_CAPACITY = 10;
public NSObject[] map;
public int size;
public int highestIndex;
public int overRealloc;
public ulong mutationsCount;
public NSObject this[int key]
{
get
{
return map[key];
}
}
public IEnumerator GetEnumerator()
{
return new DynamicArrayEnumerator(map, highestIndex);
}
public override NSObject init()
{
initWithCapacityandOverReallocValue(10, 10);
return this;
}
public virtual NSObject initWithCapacity(int c)
{
if (base.init() != null)
{
size = c;
highestIndex = -1;
overRealloc = 0;
mutationsCount = 0uL;
map = new NSObject[size];
}
return this;
}
public virtual NSObject initWithCapacityandOverReallocValue(int c, int v)
{
if (initWithCapacity(c) != null)
{
overRealloc = v;
}
return this;
}
public virtual int count()
{
return highestIndex + 1;
}
public virtual int capacity()
{
return size;
}
public virtual void setNewSize(int k)
{
int num = k + overRealloc;
NSObject[] array = new NSObject[num];
Array.Copy(map, array, Math.Min(map.Length, array.Length));
map = array;
size = num;
}
public virtual int addObject(NSObject obj)
{
int num = highestIndex + 1;
setObjectAt(obj, num);
return num;
}
public virtual void setObjectAt(NSObject obj, int k)
{
if (k >= size)
{
setNewSize(k + 1);
}
if (map[k] != null)
{
map[k].release();
map[k] = null;
}
if (highestIndex < k)
{
highestIndex = k;
}
map[k] = obj;
map[k].retain();
mutationsCount++;
}
public virtual NSObject firstObject()
{
return objectAtIndex(0);
}
public virtual NSObject lastObject()
{
if (highestIndex == -1)
{
return null;
}
return objectAtIndex(highestIndex);
}
public virtual NSObject objectAtIndex(int k)
{
return map[k];
}
public virtual void unsetAll()
{
for (int i = 0; i <= highestIndex; i++)
{
if (map[i] != null)
{
unsetObjectAtIndex(i);
}
}
}
public virtual void unsetObjectAtIndex(int k)
{
map[k].release();
map[k] = null;
mutationsCount++;
}
public virtual void insertObjectatIndex(NSObject obj, int k)
{
if (k >= size || highestIndex + 1 >= size)
{
setNewSize(size + 1);
}
highestIndex++;
for (int num = highestIndex; num > k; num--)
{
map[num] = map[num - 1];
}
map[k] = obj;
map[k].retain();
mutationsCount++;
}
public virtual void removeObjectAtIndex(int k)
{
NSObject nSObject = map[k];
if (nSObject != null)
{
nSObject.release();
nSObject = null;
}
for (int i = k; i < highestIndex; i++)
{
map[i] = map[i + 1];
}
map[highestIndex] = null;
highestIndex--;
mutationsCount++;
}
public virtual void removeAllObjects()
{
unsetAll();
highestIndex = -1;
}
public virtual void removeObject(NSObject obj)
{
for (int i = 0; i <= highestIndex; i++)
{
if (map[i] == obj)
{
removeObjectAtIndex(i);
break;
}
}
}
public virtual int getFirstEmptyIndex()
{
for (int i = 0; i < size; i++)
{
if (map[i] == null)
{
return i;
}
}
return size;
}
public virtual int getObjectIndex(NSObject obj)
{
for (int i = 0; i < size; i++)
{
if (map[i] == obj)
{
return i;
}
}
return -1;
}
public override void dealloc()
{
for (int i = 0; i <= highestIndex; i++)
{
if (map[i] != null)
{
map[i].release();
map[i] = null;
}
}
free(map);
map = null;
base.dealloc();
}
}