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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
/* linkedlist.vala
*
* Copyright (C) 2004-2005 Novell, Inc
* Copyright (C) 2005 David Waite
* Copyright (C) 2007-2008 Jürg Billeter
* Copyright (C) 2009 Mark Lee, 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:
* Mark Lee <marklee@src.gnome.org>
* Didier 'Ptitjes Villevalois <ptitjes@free.fr>
*/
/**
* Doubly-linked list implementation of the {@link List} interface.
*
* This implementation is pretty well designed for highly mutable data. When
* indexed access is privileged prefer using {@link ArrayList}.
*
* @see ArrayList
*/
public class Gee.LinkedList<G> : AbstractBidirList<G>, Queue<G>, Deque<G> {
private int _size = 0;
private int _stamp = 0;
private Node<G>? _head = null;
private weak Node<G>? _tail = null;
/**
* The elements' equality testing function.
*/
[CCode (notify = false)]
public EqualDataFunc<G> equal_func { private set; get; }
/**
* Constructs a new, empty linked 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 LinkedList (owned EqualDataFunc<G>? equal_func = null) {
if (equal_func == null) {
equal_func = Functions.get_equal_func_for (typeof (G));
}
this.equal_func = equal_func;
}
~LinkedList () {
this.clear ();
}
/**
* {@inheritDoc}
*/
public override bool foreach(ForallFunc<G> f) {
for (weak Node<G>? node = _head; node != null; node = node.next) {
if (!f (node.data)) {
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 int size {
get { return this._size; }
}
/**
* {@inheritDoc}
*/
public override bool read_only {
get { return false; }
}
/**
* {@inheritDoc}
*/
public override bool contains (G item) {
return this.index_of (item) != -1;
}
/**
* {@inheritDoc}
*/
public override bool add (G item) {
Node<G> n = new Node<G> (item);
if (this._head == null && this._tail == null) {
this._tail = n;
this._head = (owned) n;
} else {
n.prev = this._tail;
this._tail.next = (owned) n;
this._tail = this._tail.next;
}
// Adding items to the list during iterations is allowed.
//++this._stamp;
this._size++;
return true;
}
/**
* {@inheritDoc}
*/
public override bool remove (G item) { // Should remove only the first occurence (a test should be added)
for (weak Node<G> n = this._head; n != null; n = n.next) {
if (this.equal_func (item, n.data)) {
this._remove_node (n);
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
public override void clear () {
while (_head != null) {
_remove_node (_head);
}
++this._stamp;
this._head = null;
this._tail = null;
this._size = 0;
}
/**
* {@inheritDoc}
*/
public override G get (int index) {
assert (index >= 0);
assert (index < this._size);
unowned Node<G>? n = this._get_node_at (index);
assert (n != null);
return n.data;
}
/**
* {@inheritDoc}
*/
public override void set (int index, G item) {
assert (index >= 0);
assert (index < this._size);
unowned Node<G>? n = this._get_node_at (index);
return_if_fail (n != null);
n.data = item;
}
/**
* {@inheritDoc}
*/
public override int index_of (G item) {
int idx = 0;
for (weak Node<G>? node = _head; node != null; node = node.next, idx++) {
if (this.equal_func (item, node.data)) {
return idx;
}
}
return -1;
}
/**
* {@inheritDoc}
*/
public override void insert (int index, G item) {
assert (index >= 0);
assert (index <= this._size);
if (index == this._size) {
this.add (item);
} else {
Node<G> n = new Node<G> (item);
if (index == 0) {
n.next = (owned) this._head;
n.next.prev = n;
this._head = (owned)n;
} else {
weak Node prev = this._head;
for (int i = 0; i < index - 1; i++) {
prev = prev.next;
}
n.prev = prev;
n.next = (owned) prev.next;
n.next.prev = n;
prev.next = (owned) n;
}
// Adding items to the list during iterations is allowed.
//++this._stamp;
this._size++;
}
}
/**
* {@inheritDoc}
*/
public override G remove_at (int index) {
assert (index >= 0);
assert (index < this._size);
unowned Node<G>? n = this._get_node_at (index);
assert (n != null);
G element = n.data;
this._remove_node (n);
return element;
}
/**
* {@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 <= this._size, null);
List<G> slice = new LinkedList<G> (this.equal_func);
weak Node<G> n = this._get_node_at (start);
for (int i = start; i < stop; i++) {
slice.add (n.data);
n = n.next;
}
return slice;
}
/**
* {@inheritDoc}
*/
public G first () {
assert (_size > 0);
return _head.data;
}
/**
* {@inheritDoc}
*/
public G last () {
assert (_size > 0);
return _tail.data;
}
/**
* {@inheritDoc}
*/
public int capacity {
get { return UNBOUNDED_CAPACITY; }
}
/**
* {@inheritDoc}
*/
public int remaining_capacity {
get { return UNBOUNDED_CAPACITY; }
}
/**
* {@inheritDoc}
*/
public bool is_full {
get { return false; }
}
/**
* {@inheritDoc}
*/
public bool offer (G element) {
return offer_tail (element);
}
/**
* {@inheritDoc}
*/
public G? peek () {
return peek_head ();
}
/**
* {@inheritDoc}
*/
public G? poll () {
return poll_head ();
}
/**
* {@inheritDoc}
*/
public int drain (Collection<G> recipient, int amount = -1) {
return drain_head (recipient, amount);
}
/**
* {@inheritDoc}
*/
public bool offer_head (G element) {
insert (0, element);
return true;
}
/**
* {@inheritDoc}
*/
public G? peek_head () {
if (this._size == 0) {
return null;
}
return get (0);
}
/**
* {@inheritDoc}
*/
public G? poll_head () {
if (this._size == 0) {
return null;
}
return remove_at (0);
}
/**
* {@inheritDoc}
*/
public int drain_head (Collection<G> recipient, int amount = -1) {
if (amount == -1) {
amount = this._size;
}
for (int i = 0; i < amount; i++) {
if (this._size == 0) {
return i;
}
recipient.add (remove_at (0));
}
return amount;
}
/**
* {@inheritDoc}
*/
public bool offer_tail (G element) {
return add (element);
}
/**
* {@inheritDoc}
*/
public G? peek_tail () {
if (this._size == 0) {
return null;
}
return get (_size - 1);
}
/**
* {@inheritDoc}
*/
public G? poll_tail () {
if (this._size == 0) {
return null;
}
return remove_at (_size - 1);
}
/**
* {@inheritDoc}
*/
public int drain_tail (Collection<G> recipient, int amount = -1) {
if (amount == -1) {
amount = this._size;
}
for (int i = 0; i < amount; i++) {
if (this._size == 0) {
return i;
}
recipient.add (remove_at (this._size - 1));
}
return amount;
}
[Compact]
private class Node<G> { // Maybe a compact class should be used?
public G data;
public weak Node<G>? prev = null;
public Node<G>? next = null;
public Node (owned G data) {
this.data = data;
}
}
private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G>, BidirIterator<G>, ListIterator<G>, BidirListIterator<G> {
private bool started = false;
private bool removed = false;
private unowned Node<G>? position;
private int _stamp;
private LinkedList<G> _list;
private int _index;
public Iterator (LinkedList<G> list) {
this._list = list;
this.position = null;
this._index = -1;
this._stamp = list._stamp;
}
public bool next () {
assert (this._stamp == this._list._stamp);
if (this.removed) {
if (this.position != null) {
this.removed = false;
return true;
} else {
return false;
}
} else if (!this.started) {
if (this._list._head != null) {
this.started = true;
this.position = this._list._head;
this._index++;
return true;
} else {
return false;
}
} else if (this.position != null) {
if (this.position.next != null) {
this.position = this.position.next;
this._index++;
return true;
} else {
return false;
}
}
return false;
}
public bool has_next () {
assert (this._stamp == this._list._stamp);
if (this.removed) {
return this.position != null;
} else if (!this.started) {
return this._list._head != null;
} else if (this.position != null) {
return this.position.next != null;
}
return false;
}
public bool first () {
assert (this._stamp == this._list._stamp);
if (this._list.size == 0) {
return false;
}
this.position = this._list._head;
this.started = true;
this._index = 0;
this.removed = false;
return this.position != null;
}
public new G get () {
assert (this._stamp == this._list._stamp);
assert (this.position != null);
return this.position.data;
}
public void remove () {
assert (this._stamp == this._list._stamp);
assert (this.position != null);
unowned Node<G>? new_position = this.position.next;
if (new_position == null) {
started = false;
}
_list._remove_node (this.position);
this.position = new_position;
this.removed = true;
this._stamp = this._list._stamp;
}
public bool previous () {
assert (this._stamp == this._list._stamp);
if (!this.started) {
this.position = null;
return false;
} else if (this.position != null && this.position.prev != null) {
this.position = this.position.prev;
this._index--;
return true;
}
return false;
}
public bool has_previous () {
assert (this._stamp == this._list._stamp);
if (!this.started) {
return false;
} else if (this.position != null) {
return this.position.prev != null;
}
return false;
}
public bool last () {
assert (this._stamp == this._list._stamp);
if (this._list.size == 0) {
return false;
}
this.position = this._list._tail;
this.started = true;
this._index = this._list._size - 1;
return this.position != null;
}
public new void set (G item) {
assert (this._stamp == this._list._stamp);
assert (this.position != null);
this.position.data = item;
}
public void insert (G item) {
assert (this._stamp == this._list._stamp);
assert (this.position != null);
Node<G> n = new Node<G> (item);
if (this.position.prev != null) {
Node<G> position = (owned) this.position.prev.next;
n.prev = position.prev;
position.prev = n;
n.next = (owned) position;
weak Node<G> _n = n;
_n.prev.next = (owned) n;
} else {
Node<G> position = (owned) this._list._head;
position.prev = n;
n.next = (owned) position;
this._list._head = (owned) n;
}
this._list._size++;
this._index++;
_stamp = _list._stamp;
}
public void add (G item) {
assert (this._stamp == this._list._stamp);
assert (this.position != null);
Node<G> n = new Node<G> (item);
if (this.position.next != null) {
this.position.next.prev = n;
n.next = (owned) this.position.next;
} else {
this._list._tail = n;
}
this.position.next = (owned) n;
this.position.next.prev = this.position;
this.position = this.position.next;
this._list._size++;
this._index++;
_stamp = _list._stamp;
}
public int index () {
assert (this._stamp == this._list._stamp);
assert (this.position != null);
return this._index;
}
public bool read_only {
get {
return false;
}
}
public bool valid {
get {
return !this.removed && this.position != null;
}
}
public bool foreach (ForallFunc<G> f) {
assert (_stamp == _list._stamp);
if (!started) {
position = _list._head;
if (position != null)
started = true;
}
removed = false;
while (position != null) {
if (!f (position.data)) {
return false;
}
position = position.next;
}
position = _list._tail;
return true;
}
}
private unowned Node<G>? _get_node_at (int index) {
unowned Node<G>? n = null;;
if (index == 0) {
n = this._head;
} else if (index == this._size - 1) {
n = this._tail;
} else if (index <= this._size / 2) {
n = this._head;
for (int i = 0; index != i; i++) {
n = n.next;
}
} else {
n = this._tail;
for (int i = this._size - 1; index != i; i--) {
n = n.prev;
}
}
return n;
}
private void _remove_node (Node<G> _n) {
Node<G> n;
weak Node<G> next;
if (_n == this._head) {
n = (owned) this._head;
next = this._head = (owned) n.next;
} else {
n = (owned) _n.prev.next;
next = n.prev.next = (owned) n.next;
}
if (n == this._tail) {
this._tail = n.prev;
} else {
next.prev = n.prev;
}
n.prev = null;
n.next = null;
n.data = null;
++this._stamp;
this._size--;
}
}
|