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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/* arraylist.vala
*
* Copyright (C) 2004-2005 Novell, Inc
* Copyright (C) 2005 David Waite
* Copyright (C) 2007-2008 Jürg Billeter
* Copyright (C) 2009 Didier Villevalois
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
* Didier 'Ptitjes Villevalois <ptitjes@free.fr>
*/
using GLib;
/**
* Resizable array implementation of the {@link List} interface.
*
* The storage array grows automatically when needed.
*
* This implementation is pretty good for rarely modified data. Because they are
* stored in an array this structure does not fit for highly mutable data. For an
* alternative implementation see {@link LinkedList}.
*
* @see LinkedList
*/
public class Gee.ArrayList<G> : AbstractBidirList<G> {
/**
* {@inheritDoc}
*/
public override int size {
get { return _size; }
}
/**
* {@inheritDoc}
*/
public override bool read_only {
get { return false; }
}
/**
* The elements' equality testing function.
*/
[CCode (notify = false)]
public EqualDataFunc<G> equal_func { private set; get; }
internal G[] _items;
internal int _size;
// concurrent modification protection
private int _stamp = 0;
/**
* Constructs a new, empty array list.
*
* If not provided, the function parameter is requested to the
* {@link Functions} function factory methods.
*
* @param equal_func an optional element equality testing function
*/
public ArrayList (owned EqualDataFunc<G>? equal_func = null) {
if (equal_func == null) {
equal_func = Functions.get_equal_func_for (typeof (G));
}
this.equal_func = equal_func;
_items = new G[4];
_size = 0;
}
/**
* Constructs a new array list based on provided array.
*
* If not provided, the function parameter is requested to the
* {@link Functions} function factory methods.
*
* @param items initial items to be put into array
* @param equal_func an optional element equality testing function
*/
public ArrayList.wrap (owned G[] items, owned EqualDataFunc<G>? equal_func = null) {
if (equal_func == null) {
equal_func = Functions.get_equal_func_for (typeof (G));
}
this.equal_func = equal_func;
_items = items;
_size = items.length;
}
/**
* {@inheritDoc}
*/
public override bool foreach(ForallFunc<G> f) {
for (int i = 0; i < _size; i++) {
if (!f (_items[i])) {
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*/
public override Gee.Iterator<G> iterator () {
return new Iterator<G> (this);
}
/**
* {@inheritDoc}
*/
public override ListIterator<G> list_iterator () {
return new Iterator<G> (this);
}
/**
* {@inheritDoc}
*/
public override BidirListIterator<G> bidir_list_iterator () {
return new Iterator<G> (this);
}
/**
* {@inheritDoc}
*/
public override bool contains (G item) {
return (index_of (item) != -1);
}
/**
* {@inheritDoc}
*/
public override int index_of (G item) {
for (int index = 0; index < _size; index++) {
if (equal_func (_items[index], item)) {
return index;
}
}
return -1;
}
/**
* {@inheritDoc}
*/
public override G get (int index) {
assert (index >= 0);
assert (index < _size);
return _items[index];
}
/**
* {@inheritDoc}
*/
public override void set (int index, G item) {
assert (index >= 0);
assert (index < _size);
_items[index] = item;
}
/**
* {@inheritDoc}
*/
public override bool add (G item) {
if (_size == _items.length) {
grow_if_needed (1);
}
_items[_size++] = item;
_stamp++;
return true;
}
/**
* {@inheritDoc}
*/
public override void insert (int index, G item) {
assert (index >= 0);
assert (index <= _size);
if (_size == _items.length) {
grow_if_needed (1);
}
shift (index, 1);
_items[index] = item;
_stamp++;
}
/**
* {@inheritDoc}
*/
public override bool remove (G item) {
for (int index = 0; index < _size; index++) {
if (equal_func (_items[index], item)) {
remove_at (index);
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
public override G remove_at (int index) {
assert (index >= 0);
assert (index < _size);
G item = _items[index];
_items[index] = null;
shift (index + 1, -1);
_stamp++;
return item;
}
/**
* {@inheritDoc}
*/
public override void clear () {
for (int index = 0; index < _size; index++) {
_items[index] = null;
}
_size = 0;
_stamp++;
}
/**
* {@inheritDoc}
*/
public override List<G>? slice (int start, int stop) {
return_val_if_fail (start <= stop, null);
return_val_if_fail (start >= 0, null);
return_val_if_fail (stop <= _size, null);
var slice = new ArrayList<G> (this.equal_func);
for (int i = start; i < stop; i++) {
slice.add (this[i]);
}
return slice;
}
/**
* {@inheritDoc}
*/
public bool add_all (Collection<G> collection) {
if (collection.is_empty) {
return false;
}
grow_if_needed (collection.size);
collection.foreach ((item) => {_items[_size++] = item; return true;});
_stamp++;
return true;
}
private void shift (int start, int delta) {
assert (start >= 0);
assert (start <= _size);
assert (start >= -delta);
_items.move (start, start + delta, _size - start);
_size += delta;
}
private void grow_if_needed (int new_count) {
assert (new_count >= 0);
int minimum_size = _size + new_count;
if (minimum_size > _items.length) {
// double the capacity unless we add even more items at this time
set_capacity (new_count > _items.length ? minimum_size : 2 * _items.length);
}
}
private void set_capacity (int value) {
assert (value >= _size);
_items.resize (value);
}
private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G>, BidirIterator<G>, ListIterator<G>, BidirListIterator<G> {
private ArrayList<G> _list;
private int _index = -1;
private bool _removed = false;
// concurrent modification protection
private int _stamp = 0;
public Iterator (ArrayList list) {
_list = list;
_stamp = _list._stamp;
}
public bool next () {
assert (_stamp == _list._stamp);
if (_index + 1 < _list._size) {
_index++;
_removed = false;
return true;
}
return false;
}
public bool has_next () {
assert (_stamp == _list._stamp);
return (_index + 1 < _list._size);
}
public bool first () {
assert (_stamp == _list._stamp);
if (_list.size == 0) {
return false;
}
_index = 0;
_removed = false;
return true;
}
public new G get () {
assert (_stamp == _list._stamp);
assert (_index >= 0);
assert (_index < _list._size);
assert (! _removed);
return _list._items[_index];
}
public void remove () {
assert (_stamp == _list._stamp);
assert (_index >= 0);
assert (_index < _list._size);
assert (! _removed);
_list.remove_at (_index);
_index--;
_removed = true;
_stamp = _list._stamp;
}
public bool previous () {
assert (_stamp == _list._stamp);
if (_index > 0) {
_index--;
return true;
}
return false;
}
public bool has_previous () {
assert (_stamp == _list._stamp);
return (_index - 1 >= 0);
}
public bool last () {
assert (_stamp == _list._stamp);
if (_list.size == 0) {
return false;
}
_index = _list._size - 1;
return true;
}
public new void set (G item) {
assert (_stamp == _list._stamp);
assert (_index >= 0);
assert (_index < _list._size);
_list._items[_index] = item;
_stamp = ++_list._stamp;
}
public void insert (G item) {
assert (_stamp == _list._stamp);
assert (_index >= 0);
assert (_index < _list._size);
_list.insert (_index, item);
_index++;
_stamp = _list._stamp;
}
public void add (G item) {
assert (_stamp == _list._stamp);
assert (_index >= 0);
assert (_index < _list._size);
_list.insert (_index + 1, item);
_index++;
_stamp = _list._stamp;
}
public int index () {
assert (_stamp == _list._stamp);
assert (_index >= 0);
assert (_index < _list._size);
return _index;
}
public bool read_only {
get {
return false;
}
}
public bool valid {
get {
return _index >= 0 && _index < _list._size && ! _removed;
}
}
public bool foreach (ForallFunc<G> f) {
assert (_stamp == _list._stamp);
if (_index < 0 || _removed) {
_index++;
}
while (_index < _list._size) {
if (!f (_list._items[_index])) {
return false;
}
_index++;
}
_index = _list._size - 1;
return true;
}
}
}
|