-
Notifications
You must be signed in to change notification settings - Fork 278
/
CompositeFastList.java
819 lines (748 loc) · 23.7 KB
/
CompositeFastList.java
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
* Copyright 2015 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.impl.list.mutable;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutorService;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.list.ParallelListIterable;
import com.gs.collections.impl.block.factory.Predicates;
import com.gs.collections.impl.lazy.parallel.list.NonParallelListIterable;
import com.gs.collections.impl.parallel.BatchIterable;
import com.gs.collections.impl.parallel.ParallelIterate;
import com.gs.collections.impl.utility.Iterate;
/**
* CompositeFastList behaves like a list, but is composed of at least one list.
* It is useful where you don't want the additional expense of appending several lists or allocating memory
* for a super list to add multiple sublists to.<p>
* <b>Note:</b> mutation operations (e.g. add and remove, sorting) will change the underlying
* lists - so be sure to only use a composite list where it will be the only reference to the sublists
* (for example, a composite list which contains multiple query results is OK as long
* as it is the only thing that references the lists)
*/
public final class CompositeFastList<E>
extends AbstractMutableList<E>
implements BatchIterable<E>, Serializable
{
private static final Predicate2<FastList<?>, Object> REMOVE_PREDICATE = new Predicate2<FastList<?>, Object>()
{
public boolean accept(FastList<?> list, Object toRemove)
{
return list.remove(toRemove);
}
};
private static final Procedure<FastList<?>> REVERSE_LIST_PROCEDURE = new Procedure<FastList<?>>()
{
public void value(FastList<?> each)
{
each.reverseThis();
}
};
private static final long serialVersionUID = 2L;
private final FastList<FastList<E>> lists = FastList.newList();
private int size;
@Override
public MutableList<E> clone()
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".clone() not implemented yet");
}
public int size()
{
return this.size;
}
public void resetSize()
{
int newSize = 0;
for (int i = this.lists.size() - 1; i >= 0; i--)
{
newSize += this.lists.get(i).size();
}
this.size = newSize;
}
public void batchForEach(Procedure<? super E> procedure, int sectionIndex, int sectionCount)
{
if (this.lists.size() == 1)
{
this.lists.get(0).batchForEach(procedure, sectionIndex, sectionCount);
}
else
{
this.lists.get(sectionIndex).batchForEach(procedure, 0, 1);
}
}
public int getBatchCount(int batchSize)
{
if (this.lists.size() == 1)
{
return this.lists.get(0).getBatchCount(batchSize);
}
return this.lists.size();
}
@Override
public CompositeFastList<E> reverseThis()
{
ParallelIterate.forEach(this.lists, REVERSE_LIST_PROCEDURE);
this.lists.reverseThis();
return this;
}
@Override
public void each(final Procedure<? super E> procedure)
{
this.lists.forEach(new Procedure<FastList<E>>()
{
public void value(FastList<E> list)
{
list.forEach(procedure);
}
});
}
@Override
public <IV> IV injectInto(IV injectedValue, final Function2<? super IV, ? super E, ? extends IV> function)
{
return this.lists.injectInto(injectedValue, new Function2<IV, FastList<E>, IV>()
{
public IV value(IV inject, FastList<E> list)
{
return list.injectInto(inject, function);
}
});
}
@Override
public int injectInto(int injectedValue, final IntObjectToIntFunction<? super E> function)
{
return this.lists.injectInto(injectedValue, new IntObjectToIntFunction<FastList<E>>()
{
public int intValueOf(int inject, FastList<E> list)
{
return list.injectInto(inject, function);
}
});
}
@Override
public float injectInto(float injectedValue, final FloatObjectToFloatFunction<? super E> function)
{
return this.lists.injectInto(injectedValue, new FloatObjectToFloatFunction<FastList<E>>()
{
public float floatValueOf(float inject, FastList<E> list)
{
return list.injectInto(inject, function);
}
});
}
@Override
public long injectInto(long injectedValue, final LongObjectToLongFunction<? super E> function)
{
return this.lists.injectInto(injectedValue, new LongObjectToLongFunction<FastList<E>>()
{
public long longValueOf(long inject, FastList<E> list)
{
return list.injectInto(inject, function);
}
});
}
@Override
public double injectInto(double injectedValue, final DoubleObjectToDoubleFunction<? super E> function)
{
return this.lists.injectInto(injectedValue, new DoubleObjectToDoubleFunction<FastList<E>>()
{
public double doubleValueOf(double inject, FastList<E> list)
{
return list.injectInto(inject, function);
}
});
}
@Override
public void forEachWithIndex(ObjectIntProcedure<? super E> objectIntProcedure)
{
this.lists.forEach(new ProcedureToInnerListObjectIntProcedure<E>(objectIntProcedure));
}
@Override
public void reverseForEach(final Procedure<? super E> procedure)
{
this.lists.reverseForEach(new Procedure<FastList<E>>()
{
public void value(FastList<E> each)
{
each.reverseForEach(procedure);
}
});
}
@Override
public <P> void forEachWith(
final Procedure2<? super E, ? super P> procedure2,
final P parameter)
{
this.lists.forEach(new Procedure<FastList<E>>()
{
public void value(FastList<E> list)
{
list.forEachWith(procedure2, parameter);
}
});
}
@Override
public boolean isEmpty()
{
return this.lists.allSatisfy(new Predicate<FastList<E>>()
{
public boolean accept(FastList<E> list)
{
return list.isEmpty();
}
});
}
@Override
public boolean contains(final Object object)
{
return this.lists.anySatisfy(new Predicate<FastList<E>>()
{
public boolean accept(FastList<E> list)
{
return list.contains(object);
}
});
}
@Override
public Iterator<E> iterator()
{
if (this.lists.isEmpty())
{
return Collections.<E>emptyList().iterator();
}
return new CompositeIterator(this.lists);
}
@Override
public Object[] toArray()
{
final Object[] result = new Object[this.size()];
this.forEachWithIndex(new ObjectIntProcedure<E>()
{
public void value(E each, int index)
{
result[index] = each;
}
});
return result;
}
@Override
public boolean add(E object)
{
if (this.lists.isEmpty())
{
this.addComposited(FastList.<E>newList());
}
Collection<E> list = this.lists.getLast();
this.size++;
return list.add(object);
}
@Override
public boolean remove(Object object)
{
boolean removed = this.lists.anySatisfyWith(REMOVE_PREDICATE, object);
if (removed)
{
this.size--;
}
return removed;
}
@Override
public boolean addAll(Collection<? extends E> collection)
{
if (!collection.isEmpty())
{
Collection<? extends E> collectionToAdd = collection instanceof FastList ? collection : new FastList<E>(collection);
this.addComposited(collectionToAdd);
}
return true;
}
@Override
public boolean containsAll(Collection<?> collection)
{
return Iterate.allSatisfy(collection, Predicates.in(this));
}
@Override
public Object[] toArray(Object[] array)
{
int size = this.size();
final Object[] result = array.length >= size
? array
: (Object[]) Array.newInstance(array.getClass().getComponentType(), size);
this.forEachWithIndex(new ObjectIntProcedure<E>()
{
public void value(E each, int index)
{
result[index] = each;
}
});
if (result.length > size)
{
result[size] = null;
}
return result;
}
public void addComposited(Collection<? extends E> collection)
{
if (!(collection instanceof FastList))
{
throw new IllegalArgumentException("CompositeFastList can only add FastLists");
}
this.size += collection.size();
this.lists.add((FastList<E>) collection);
}
public boolean addAll(int index, Collection<? extends E> collection)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".addAll(index, collection) not implemented yet");
}
public void clear()
{
this.lists.forEach(new Procedure<FastList<E>>()
{
public void value(FastList<E> object)
{
object.clear();
}
});
this.size = 0;
}
@Override
public boolean retainAll(Collection<?> collection)
{
boolean changed = false;
for (int i = this.lists.size() - 1; i >= 0; i--)
{
changed = this.lists.get(i).retainAll(collection) || changed;
}
if (changed)
{
this.resetSize();
}
return changed;
}
@Override
public boolean removeAll(Collection<?> collection)
{
if (collection.isEmpty())
{
return false;
}
boolean changed = false;
for (int i = this.lists.size() - 1; i >= 0; i--)
{
changed = this.lists.get(i).removeAll(collection) || changed;
}
if (changed)
{
this.resetSize();
}
return changed;
}
public E get(int index)
{
this.rangeCheck(index);
int p = 0;
int currentSize = this.lists.getFirst().size();
while (index >= currentSize)
{
index -= currentSize;
currentSize = this.lists.get(++p).size();
}
return this.lists.get(p).items[index];
}
private void rangeCheck(int index)
{
if (index >= this.size())
{
throw new IndexOutOfBoundsException("No such element " + index + " size: " + this.size());
}
}
public E set(int index, E element)
{
this.rangeCheck(index);
int p = 0;
int currentSize = this.lists.getFirst().size();
while (index >= currentSize)
{
index -= currentSize;
currentSize = this.lists.get(++p).size();
}
return this.lists.get(p).set(index, element);
}
public void add(int index, E element)
{
int localSize = this.size();
if (index > localSize || index < 0)
{
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + localSize);
}
int max = 0;
for (int i = 0; i < this.lists.size(); i++)
{
List<E> list = this.lists.get(i);
int previousMax = max;
max += list.size();
if (index <= max)
{
list.add(index - previousMax, element);
this.size++;
return;
}
}
}
public E remove(int index)
{
this.rangeCheck(index);
int p = 0;
int currentSize = this.lists.getFirst().size();
while (index >= currentSize)
{
index -= currentSize;
currentSize = this.lists.get(++p).size();
}
this.size--;
return this.lists.get(p).remove(index);
}
@Override
public int indexOf(Object o)
{
int offset = 0;
int listsSize = this.lists.size();
for (int i = 0; i < listsSize; i++)
{
MutableList<E> list = this.lists.get(i);
int index = list.indexOf(o);
if (index > -1)
{
return index + offset;
}
offset += list.size();
}
return -1;
}
@Override
public int lastIndexOf(Object o)
{
int offset = this.size();
for (int i = this.lists.size() - 1; i >= 0; i--)
{
MutableList<E> list = this.lists.get(i);
offset -= list.size();
int index = list.lastIndexOf(o);
if (index > -1)
{
return index + offset;
}
}
return -1;
}
/**
* a list iterator is a problem for a composite list as going back in the order of the list is an issue,
* as are the other methods like set() and add() (and especially, remove).
* Convert the internal lists to one list (if not already just one list)
* and return that list's list iterator.
* <p>
* AFAIK list iterator is only commonly used in sorting.
*
* @return a ListIterator for this, with internal state convertedto one list if needed.
*/
@Override
public ListIterator<E> listIterator()
{
return this.listIterator(0);
}
/**
* a list iterator is a problem for a composite list as going back in the order of the list is an issue,
* as are the other methods like set() and add() (and especially, remove).
* Convert the internal lists to one list (if not already just one list)
* and return that list's list iterator.
* <p>
* AFAIK list iterator is only commonly used in sorting.
*
* @return a ListIterator for this, with internal state convertedto one list if needed.
*/
@Override
public ListIterator<E> listIterator(int index)
{
if (this.lists.size() > 1 || this.lists.isEmpty())
{
this.flattenLists();
}
return super.listIterator(index);
}
@Override
public int count(Predicate<? super E> predicate)
{
int count = 0;
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
count += this.lists.get(i).count(predicate);
}
return count;
}
@Override
public <P> int countWith(Predicate2<? super E, ? super P> predicate, P parameter)
{
int count = 0;
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
count += this.lists.get(i).countWith(predicate, parameter);
}
return count;
}
@Override
public boolean anySatisfy(final Predicate<? super E> predicate)
{
return this.lists.anySatisfy(new Predicate<FastList<E>>()
{
public boolean accept(FastList<E> each)
{
return each.anySatisfy(predicate);
}
});
}
@Override
public <R extends Collection<E>> R select(Predicate<? super E> predicate, R target)
{
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
this.lists.get(i).select(predicate, target);
}
return target;
}
@Override
public <P, R extends Collection<E>> R selectWith(Predicate2<? super E, ? super P> predicate, P parameter, R target)
{
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
this.lists.get(i).selectWith(predicate, parameter, target);
}
return target;
}
@Override
public <R extends Collection<E>> R reject(Predicate<? super E> predicate, R target)
{
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
this.lists.get(i).reject(predicate, target);
}
return target;
}
@Override
public <P, R extends Collection<E>> R rejectWith(Predicate2<? super E, ? super P> predicate, P parameter, R target)
{
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
this.lists.get(i).rejectWith(predicate, parameter, target);
}
return target;
}
@Override
public <V, R extends Collection<V>> R collect(Function<? super E, ? extends V> function, R target)
{
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
this.lists.get(i).collect(function, target);
}
return target;
}
@Override
public <P, A, R extends Collection<A>> R collectWith(Function2<? super E, ? super P, ? extends A> function, P parameter, R target)
{
int localSize = this.lists.size();
for (int i = 0; i < localSize; i++)
{
this.lists.get(i).collectWith(function, parameter, target);
}
return target;
}
@Override
public <P> boolean anySatisfyWith(final Predicate2<? super E, ? super P> predicate, P parameter)
{
return this.lists.anySatisfyWith(new Predicate2<FastList<E>, P>()
{
public boolean accept(FastList<E> each, P parm)
{
return each.anySatisfyWith(predicate, parm);
}
}, parameter);
}
@Override
public boolean allSatisfy(final Predicate<? super E> predicate)
{
return this.lists.allSatisfy(new Predicate<FastList<E>>()
{
public boolean accept(FastList<E> each)
{
return each.allSatisfy(predicate);
}
});
}
@Override
public <P> boolean allSatisfyWith(final Predicate2<? super E, ? super P> predicate, P parameter)
{
return this.lists.allSatisfyWith(new Predicate2<FastList<E>, P>()
{
public boolean accept(FastList<E> each, P param)
{
return each.allSatisfyWith(predicate, param);
}
}, parameter);
}
@Override
public boolean noneSatisfy(final Predicate<? super E> predicate)
{
return this.lists.allSatisfy(new Predicate<FastList<E>>()
{
public boolean accept(FastList<E> each)
{
return each.noneSatisfy(predicate);
}
});
}
@Override
public <P> boolean noneSatisfyWith(final Predicate2<? super E, ? super P> predicate, P parameter)
{
return this.lists.allSatisfyWith(new Predicate2<FastList<E>, P>()
{
public boolean accept(FastList<E> each, P param)
{
return each.noneSatisfyWith(predicate, param);
}
}, parameter);
}
/**
* convert multiple contained lists into one list and replace the contained lists with that list.
* Synchronize to prevent changes to this list whilst this process is happening
*/
private void flattenLists()
{
FastList<E> list = (FastList<E>) this.toList();
this.lists.clear();
this.lists.add(list);
}
/**
* Override in subclasses where it can be optimized.
*/
@Override
protected void defaultSort(Comparator<? super E> comparator)
{
FastList<E> list = comparator == null
? (FastList<E>) this.toSortedList()
: (FastList<E>) this.toSortedList(comparator);
this.lists.clear();
this.lists.add(list);
}
private final class CompositeIterator
implements Iterator<E>
{
private final Iterator<E>[] iterators;
private Iterator<E> currentIterator;
private int currentIndex;
private CompositeIterator(FastList<FastList<E>> newLists)
{
this.iterators = new Iterator[newLists.size()];
for (int i = 0; i < newLists.size(); ++i)
{
this.iterators[i] = newLists.get(i).iterator();
}
this.currentIterator = this.iterators[0];
this.currentIndex = 0;
}
public boolean hasNext()
{
if (this.currentIterator.hasNext())
{
return true;
}
if (this.currentIndex < this.iterators.length - 1)
{
this.currentIterator = this.iterators[++this.currentIndex];
return this.hasNext();
}
return false;
}
public E next()
{
if (this.currentIterator.hasNext())
{
return this.currentIterator.next();
}
if (this.currentIndex < this.iterators.length - 1)
{
this.currentIterator = this.iterators[++this.currentIndex];
return this.next();
}
throw new NoSuchElementException();
}
public void remove()
{
CompositeFastList.this.size--;
this.currentIterator.remove();
}
}
private static final class ProcedureToInnerListObjectIntProcedure<E> implements Procedure<FastList<E>>
{
private static final long serialVersionUID = 1L;
private int index;
private final ObjectIntProcedure<? super E> objectIntProcedure;
private ProcedureToInnerListObjectIntProcedure(ObjectIntProcedure<? super E> objectIntProcedure)
{
this.objectIntProcedure = objectIntProcedure;
}
public void value(FastList<E> list)
{
list.forEach(new Procedure<E>()
{
public void value(E object)
{
ProcedureToInnerListObjectIntProcedure.this.objectIntProcedure.value(
object,
ProcedureToInnerListObjectIntProcedure.this.index);
ProcedureToInnerListObjectIntProcedure.this.index++;
}
});
}
}
@Override
public ParallelListIterable<E> asParallel(ExecutorService executorService, int batchSize)
{
return new NonParallelListIterable<E>(this);
}
}