-
Notifications
You must be signed in to change notification settings - Fork 584
/
BooleanHashBag.java
955 lines (864 loc) · 24.4 KB
/
BooleanHashBag.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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/*
* Copyright (c) 2018 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.bag.mutable.primitive;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.LazyBooleanIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.BooleanBag;
import org.eclipse.collections.api.bag.primitive.ImmutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.block.function.primitive.BooleanToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectBooleanToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.BooleanPredicate;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.primitive.BooleanIntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.BooleanProcedure;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.iterator.BooleanIterator;
import org.eclipse.collections.api.iterator.MutableBooleanIterator;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
import org.eclipse.collections.api.set.primitive.BooleanSet;
import org.eclipse.collections.api.set.primitive.MutableBooleanSet;
import org.eclipse.collections.api.tuple.primitive.BooleanIntPair;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.factory.primitive.BooleanBags;
import org.eclipse.collections.impl.factory.primitive.BooleanSets;
import org.eclipse.collections.impl.lazy.primitive.LazyBooleanIterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.BooleanArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.BooleanHashSet;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
/**
* BooleanHashBag is similar to {@link HashBag}, and is memory-optimized for boolean primitives.
*
* @since 3.0.
*/
public final class BooleanHashBag implements MutableBooleanBag, Externalizable
{
private static final long serialVersionUID = 1L;
private int falseCount;
private int trueCount;
public BooleanHashBag()
{
}
public BooleanHashBag(BooleanIterable iterable)
{
this();
this.addAll(iterable);
}
public BooleanHashBag(boolean... elements)
{
this();
this.addAll(elements);
}
public BooleanHashBag(BooleanHashBag bag)
{
this.falseCount = bag.falseCount;
this.trueCount = bag.trueCount;
}
public static BooleanHashBag newBagWith(boolean... source)
{
BooleanHashBag result = new BooleanHashBag();
result.addAll(source);
return result;
}
public static BooleanHashBag newBag(BooleanIterable source)
{
if (source instanceof BooleanHashBag)
{
return new BooleanHashBag((BooleanHashBag) source);
}
return new BooleanHashBag(source);
}
private boolean containsFalse()
{
return this.falseCount > 0;
}
private boolean containsTrue()
{
return this.trueCount > 0;
}
@Override
public boolean isEmpty()
{
return !this.containsFalse() && !this.containsTrue();
}
@Override
public boolean notEmpty()
{
return this.containsFalse() || this.containsTrue();
}
@Override
public int size()
{
return this.falseCount + this.trueCount;
}
@Override
public int sizeDistinct()
{
return (this.containsFalse() ? 1 : 0) + (this.containsTrue() ? 1 : 0);
}
@Override
public void clear()
{
this.falseCount = 0;
this.trueCount = 0;
}
@Override
public BooleanHashBag with(boolean element)
{
this.add(element);
return this;
}
public BooleanHashBag with(boolean element1, boolean element2)
{
this.add(element1);
this.add(element2);
return this;
}
public BooleanHashBag with(boolean element1, boolean element2, boolean element3)
{
this.add(element1);
this.add(element2);
this.add(element3);
return this;
}
@Override
public BooleanHashBag withAll(BooleanIterable iterable)
{
this.addAll(iterable);
return this;
}
@Override
public BooleanHashBag without(boolean element)
{
this.remove(element);
return this;
}
@Override
public BooleanHashBag withoutAll(BooleanIterable iterable)
{
this.removeAll(iterable);
return this;
}
@Override
public MutableBooleanBag asUnmodifiable()
{
return new UnmodifiableBooleanBag(this);
}
@Override
public MutableBooleanBag asSynchronized()
{
return new SynchronizedBooleanBag(this);
}
@Override
public ImmutableBooleanBag toImmutable()
{
return BooleanBags.immutable.withAll(this);
}
@Override
public boolean contains(boolean value)
{
return value ? this.containsTrue() : this.containsFalse();
}
@Override
public boolean containsAll(boolean... source)
{
for (boolean each : source)
{
if (!this.contains(each))
{
return false;
}
}
return true;
}
@Override
public boolean containsAll(BooleanIterable source)
{
return source.allSatisfy(this::contains);
}
@Override
public int occurrencesOf(boolean item)
{
return item ? this.trueCount : this.falseCount;
}
@Override
public void forEachWithOccurrences(BooleanIntProcedure procedure)
{
if (this.containsFalse())
{
procedure.value(false, this.falseCount);
}
if (this.containsTrue())
{
procedure.value(true, this.trueCount);
}
}
@Override
public boolean add(boolean item)
{
if (item)
{
this.trueCount++;
}
else
{
this.falseCount++;
}
return true;
}
@Override
public boolean remove(boolean item)
{
if (item)
{
if (!this.containsTrue())
{
return false;
}
this.trueCount--;
}
else
{
if (!this.containsFalse())
{
return false;
}
this.falseCount--;
}
return true;
}
@Override
public boolean addAll(boolean... source)
{
if (source.length < 1)
{
return false;
}
for (boolean each : source)
{
this.add(each);
}
return true;
}
@Override
public boolean addAll(BooleanIterable source)
{
if (source.isEmpty())
{
return false;
}
if (source instanceof BooleanBag)
{
BooleanBag otherBag = (BooleanBag) source;
otherBag.forEachWithOccurrences(this::addOccurrences);
}
else
{
BooleanIterator iterator = source.booleanIterator();
while (iterator.hasNext())
{
boolean each = iterator.next();
this.add(each);
}
}
return true;
}
@Override
public boolean removeAll(boolean... source)
{
if (source.length == 0)
{
return false;
}
int oldSize = this.size();
for (boolean each : source)
{
if (each)
{
this.trueCount = 0;
}
else
{
this.falseCount = 0;
}
}
return this.size() != oldSize;
}
@Override
public boolean removeAll(BooleanIterable source)
{
if (source.isEmpty())
{
return false;
}
int oldSize = this.size();
if (source instanceof BooleanBag)
{
BooleanBag otherBag = (BooleanBag) source;
otherBag.forEachWithOccurrences((each, occurrences) ->
{
if (each)
{
this.trueCount = 0;
}
else
{
this.falseCount = 0;
}
});
}
else
{
BooleanIterator iterator = source.booleanIterator();
while (iterator.hasNext())
{
boolean each = iterator.next();
if (each)
{
this.trueCount = 0;
}
else
{
this.falseCount = 0;
}
}
}
return this.size() != oldSize;
}
@Override
public boolean retainAll(BooleanIterable elements)
{
int oldSize = this.size();
BooleanSet set = elements instanceof BooleanSet ? (BooleanSet) elements : elements.toSet();
if (!set.contains(true) && this.containsTrue())
{
this.trueCount = 0;
}
if (!set.contains(false) && this.containsFalse())
{
this.falseCount = 0;
}
return this.size() != oldSize;
}
@Override
public boolean retainAll(boolean... source)
{
return this.retainAll(BooleanHashSet.newSetWith(source));
}
@Override
public void addOccurrences(boolean item, int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot add a negative number of occurrences");
}
if (occurrences > 0)
{
if (item)
{
this.trueCount += occurrences;
}
else
{
this.falseCount += occurrences;
}
}
}
@Override
public boolean removeOccurrences(boolean item, int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot remove a negative number of occurrences");
}
if (occurrences == 0)
{
return false;
}
if (item)
{
if (!this.containsTrue())
{
return false;
}
this.trueCount -= occurrences;
if (this.trueCount < 0)
{
this.trueCount = 0;
}
}
else
{
if (!this.containsFalse())
{
return false;
}
this.falseCount -= occurrences;
if (this.falseCount < 0)
{
this.falseCount = 0;
}
}
return true;
}
@Override
public void forEach(BooleanProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
@Override
public void each(BooleanProcedure procedure)
{
for (int i = 0; i < this.falseCount; i++)
{
procedure.value(false);
}
for (int i = 0; i < this.trueCount; i++)
{
procedure.value(true);
}
}
@Override
public MutableBooleanBag select(BooleanPredicate predicate)
{
MutableBooleanBag result = new BooleanHashBag();
this.forEachWithOccurrences((each, occurrences) ->
{
if (predicate.accept(each))
{
result.addOccurrences(each, occurrences);
}
});
return result;
}
@Override
public MutableBooleanBag selectByOccurrences(IntPredicate predicate)
{
MutableBooleanBag result = new BooleanHashBag();
this.forEachWithOccurrences((each, occurrences) ->
{
if (predicate.accept(occurrences))
{
result.addOccurrences(each, occurrences);
}
});
return result;
}
@Override
public MutableList<BooleanIntPair> topOccurrences(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Cannot use a value of count < 0");
}
if (this.size() > 0)
{
if (count == 1)
{
if (this.trueCount == this.falseCount)
{
return Lists.mutable.with(PrimitiveTuples.pair(true, this.trueCount), PrimitiveTuples.pair(false, this.falseCount));
}
if (this.trueCount > this.falseCount)
{
return Lists.mutable.with(PrimitiveTuples.pair(true, this.trueCount));
}
return Lists.mutable.with(PrimitiveTuples.pair(false, this.falseCount));
}
if (count > 1)
{
if (this.trueCount >= this.falseCount)
{
return Lists.mutable.with(
PrimitiveTuples.pair(true, this.trueCount),
PrimitiveTuples.pair(false, this.falseCount));
}
return Lists.mutable.with(
PrimitiveTuples.pair(false, this.falseCount),
PrimitiveTuples.pair(true, this.trueCount));
}
}
return Lists.mutable.empty();
}
@Override
public MutableList<BooleanIntPair> bottomOccurrences(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Cannot use a value of count < 0");
}
if (this.size() > 0)
{
if (count == 1)
{
if (this.trueCount == this.falseCount)
{
return Lists.mutable.with(PrimitiveTuples.pair(true, this.trueCount), PrimitiveTuples.pair(false, this.falseCount));
}
if (this.trueCount < this.falseCount)
{
return Lists.mutable.with(PrimitiveTuples.pair(true, this.trueCount));
}
return Lists.mutable.with(PrimitiveTuples.pair(false, this.falseCount));
}
if (count > 1)
{
if (this.trueCount <= this.falseCount)
{
return Lists.mutable.with(
PrimitiveTuples.pair(true, this.trueCount),
PrimitiveTuples.pair(false, this.falseCount));
}
return Lists.mutable.with(
PrimitiveTuples.pair(false, this.falseCount),
PrimitiveTuples.pair(true, this.trueCount));
}
}
return Lists.mutable.empty();
}
@Override
public MutableBooleanBag reject(BooleanPredicate predicate)
{
MutableBooleanBag result = new BooleanHashBag();
this.forEachWithOccurrences((each, occurrences) ->
{
if (!predicate.accept(each))
{
result.addOccurrences(each, occurrences);
}
});
return result;
}
@Override
public <T> T injectInto(T injectedValue, ObjectBooleanToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
BooleanIterator it = this.booleanIterator();
while (it.hasNext())
{
result = function.valueOf(result, it.next());
}
return result;
}
@Override
public RichIterable<BooleanIterable> chunk(int size)
{
if (size <= 0)
{
throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
}
MutableList<BooleanIterable> result = Lists.mutable.empty();
if (this.notEmpty())
{
if (this.size() <= size)
{
result.add(BooleanBags.mutable.withAll(this));
}
else
{
BooleanIterator iterator = this.booleanIterator();
while (iterator.hasNext())
{
MutableBooleanBag batch = BooleanBags.mutable.empty();
for (int i = 0; i < size && iterator.hasNext(); i++)
{
batch.add(iterator.next());
}
result.add(batch);
}
}
}
return result;
}
@Override
public boolean equals(Object otherBag)
{
if (otherBag == this)
{
return true;
}
if (!(otherBag instanceof BooleanBag))
{
return false;
}
BooleanBag bag = (BooleanBag) otherBag;
if (this.sizeDistinct() != bag.sizeDistinct())
{
return false;
}
return this.falseCount == bag.occurrencesOf(false) && this.trueCount == bag.occurrencesOf(true);
}
@Override
public int hashCode()
{
int result = 0;
if (this.containsFalse())
{
result = 1237 ^ this.falseCount;
}
if (this.containsTrue())
{
result += 1231 ^ this.trueCount;
}
return result;
}
@Override
public String toString()
{
return this.makeString("[", ", ", "]");
}
@Override
public String makeString()
{
return this.makeString(", ");
}
@Override
public String makeString(String separator)
{
return this.makeString("", separator, "");
}
@Override
public String makeString(String start, String separator, String end)
{
Appendable stringBuilder = new StringBuilder();
this.appendString(stringBuilder, start, separator, end);
return stringBuilder.toString();
}
@Override
public void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
}
@Override
public void appendString(Appendable appendable, String separator)
{
this.appendString(appendable, "", separator, "");
}
@Override
public void appendString(
Appendable appendable,
String start,
String separator,
String end)
{
try
{
appendable.append(start);
boolean firstItem = true;
for (int i = 0; i < this.falseCount; i++)
{
if (!firstItem)
{
appendable.append(separator);
}
appendable.append("false");
firstItem = false;
}
for (int i = 0; i < this.trueCount; i++)
{
if (!firstItem)
{
appendable.append(separator);
}
appendable.append("true");
firstItem = false;
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public int count(BooleanPredicate predicate)
{
int result = 0;
if (this.containsFalse() && predicate.accept(false))
{
result = this.falseCount;
}
if (this.containsTrue() && predicate.accept(true))
{
result += this.trueCount;
}
return result;
}
@Override
public boolean anySatisfy(BooleanPredicate predicate)
{
return this.containsFalse() && predicate.accept(false) || this.containsTrue() && predicate.accept(true);
}
@Override
public boolean allSatisfy(BooleanPredicate predicate)
{
return (!this.containsFalse() || predicate.accept(false)) && (!this.containsTrue() || predicate.accept(true));
}
@Override
public boolean noneSatisfy(BooleanPredicate predicate)
{
return (!this.containsFalse() || !predicate.accept(false)) && (!this.containsTrue() || !predicate.accept(true));
}
@Override
public boolean detectIfNone(BooleanPredicate predicate, boolean ifNone)
{
if (this.containsFalse() && predicate.accept(false))
{
return false;
}
if (this.containsTrue() && predicate.accept(true))
{
return true;
}
return ifNone;
}
@Override
public <V> MutableBag<V> collect(BooleanToObjectFunction<? extends V> function)
{
HashBag<V> result = HashBag.newBag();
if (this.containsFalse())
{
result.addOccurrences(function.valueOf(false), this.falseCount);
}
if (this.containsTrue())
{
result.addOccurrences(function.valueOf(true), this.trueCount);
}
return result;
}
@Override
public boolean[] toArray()
{
boolean[] array = new boolean[this.size()];
int[] index = {0};
this.forEachWithOccurrences((each, occurrences) ->
{
for (int i = 0; i < occurrences; i++)
{
array[index[0]] = each;
index[0]++;
}
});
return array;
}
@Override
public MutableBooleanList toList()
{
return BooleanArrayList.newList(this);
}
@Override
public MutableBooleanSet toSet()
{
return BooleanHashSet.newSet(this);
}
@Override
public MutableBooleanBag toBag()
{
return BooleanHashBag.newBag(this);
}
@Override
public LazyBooleanIterable asLazy()
{
return new LazyBooleanIterableAdapter(this);
}
@Override
public MutableBooleanIterator booleanIterator()
{
return new InternalIterator(this.falseCount, this.trueCount);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeInt(this.falseCount);
out.writeInt(this.trueCount);
}
@Override
public void readExternal(ObjectInput in) throws IOException
{
this.falseCount = in.readInt();
this.trueCount = in.readInt();
}
private final class InternalIterator implements MutableBooleanIterator
{
private int internalFalseCount;
private int internalTrueCount;
private boolean lastBooleanValue;
private boolean removedAlready = true;
private InternalIterator(int falseCount, int trueCount)
{
this.internalFalseCount = falseCount;
this.internalTrueCount = trueCount;
}
@Override
public boolean hasNext()
{
return this.internalFalseCount > 0 || this.internalTrueCount > 0;
}
@Override
public boolean next()
{
this.removedAlready = false;
if (this.internalFalseCount > 0)
{
this.internalFalseCount--;
this.lastBooleanValue = false;
return false;
}
if (this.internalTrueCount > 0)
{
this.internalTrueCount--;
this.lastBooleanValue = true;
return true;
}
throw new NoSuchElementException("next() called, but the iterator is exhausted");
}
@Override
public void remove()
{
if (this.removedAlready)
{
throw new IllegalStateException();
}
if (this.lastBooleanValue)
{
BooleanHashBag.this.trueCount--;
}
else
{
BooleanHashBag.this.falseCount--;
}
this.removedAlready = true;
}
}
@Override
public MutableBooleanSet selectUnique()
{
MutableBooleanSet result = BooleanSets.mutable.empty();
this.forEachWithOccurrences((each, occurrences) ->
{
if (occurrences == 1)
{
result.add(each);
}
});
return result;
}
}