Finished the Basic Unit test frame work to ensure stablity.

-Added: 150k Unit tests.
-Fixed: Reworked the NavigableSet/Map implementations of RBTree/AVLTree/Array Sets/Maps
This commit is contained in:
Speiger 2021-12-13 10:41:20 +01:00
parent 865966db55
commit caf2f22e9f
13 changed files with 4790 additions and 4452 deletions

View File

@ -1,5 +1,9 @@
# Changelog of versions
### Version 0.5.1
- Fixed: Reworked the NavigableSet/Map implementations of RBTree/AVLTree/Array Sets/Maps so they are now deemed stable.
- Added: Another 150k Unit tests.
### Version 0.5.0
- Added: 2 Helper functions to find out how many bits are required to store a Number.
- Added: pour function directly into Iterable which allows to collect all elements in the Iterable directly.

View File

@ -46,7 +46,6 @@ dependencies {
runtimeOnly 'de.speiger:Simple-Code-Generator:1.0.5'
testImplementation 'junit:junit:4.12'
testImplementation 'com.google.guava:guava-testlib:31.0.1-jre'
}
task generateSource(type: JavaExec) {

View File

@ -207,13 +207,13 @@ public interface NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE extends SORTED_MAP KEY_VAL
default NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { return tailMap(OBJ_TO_KEY(fromKey), true); }
#else
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(CLASS_TYPE key) { return lowerEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(CLASS_TYPE key);
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(CLASS_TYPE key) { return floorEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(CLASS_TYPE key);
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(CLASS_TYPE key) { return ceilingEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(CLASS_TYPE key);
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(CLASS_TYPE key) { return higherEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(CLASS_TYPE key);
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, boolean fromInclusive, CLASS_TYPE toKey, boolean toInclusive);

View File

@ -1,6 +1,7 @@
package speiger.src.collections.PACKAGE.sets;
import java.util.Collection;
import java.util.Collections;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
@ -19,7 +20,6 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#endif
@ -557,7 +557,11 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
@Override
public KEY_TYPE POLL_FIRST_KEY() {
if(tree == null) throw new NoSuchElementException();
#if TYPE_OBJECT
if(tree == null) return null;
#else
if(tree == null) return getDefaultMinValue();
#endif
KEY_TYPE value = first.key;
removeNode(first);
return value;
@ -565,7 +569,11 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
@Override
public KEY_TYPE POLL_LAST_KEY() {
if(tree == null) throw new NoSuchElementException();
#if TYPE_OBJECT
if(tree == null) return null;
#else
if(tree == null) return getDefaultMaxValue();
#endif
KEY_TYPE value = last.key;
removeNode(last);
return value;
@ -601,16 +609,16 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SetIterator(false); }
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new AscendingSetIterator(first); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
Entry KEY_GENERIC_TYPE entry = findNode(fromElement);
return entry == null ? null : new SetIterator(entry);
return entry == null ? null : new AscendingSetIterator(entry);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SetIterator(true); }
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSetIterator(last); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
@ -743,158 +751,174 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
}
}
private static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
{
AscendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
public AscendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive)
{
super(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range");
public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.comparator(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive)
{
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement out of range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of range");
return new AscendingSubSetBRACES(set, false, fromElement, fromInclusive, false, toElement, toInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) {
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range");
return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, false, toElement, inclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) {
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range");
return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, end, hiInclusive);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SubSetIterator(findLowest()); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement);
return entry == null || !inClosedRange(fromElement) ? null : new SubSetIterator(entry);
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive)
{
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range");
return new AscendingSubSetBRACES(set, fromStart, lo, loInclusive, false, toElement, inclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive)
{
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement out of range");
return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, hi, hiInclusive);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SubSetIterator(findHighest()); }
public BI_ITERATOR KEY_GENERIC_TYPE iterator()
{
return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence());
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement)
{
return new AscendingSubSetIterator(absLower(fromElement), absHighFence(), absLowFence());
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator()
{
return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence());
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() {
return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet()
{
if(inverse == null) inverse = new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
return inverse;
}
@Override
protected Entry KEY_GENERIC_TYPE subLowest() { return absLowest(); }
@Override
protected Entry KEY_GENERIC_TYPE subHighest() { return absHighest(); }
@Override
protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absCeiling(key); }
@Override
protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absHigher(key); }
@Override
protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absFloor(key); }
@Override
protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absLower(key); }
@Override
protected Entry KEY_GENERIC_TYPE start() { return absLowest(); }
@Override
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); }
}
private static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
{
COMPARATOR KEY_GENERIC_TYPE comparator;
DescendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
comparator = set.comparator();
if(comparator != null) comparator = comparator.reversed();
public DescendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive)
{
super(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
#if TYPE_OBJECT
comparator = Collections.reverseOrder(set.comparator());
#else
comparator = set.comparator() == null ? COMPARATOR.of(Collections.reverseOrder()) : set.comparator().reversed();
#endif
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
public KEY_TYPE getDefaultMaxValue() { return super.getDefaultMinValue(); }
public KEY_TYPE getDefaultMinValue() { return super.getDefaultMaxValue(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range");
return new DescendingSubSetBRACES(set, false, toElement, toInclusive, false, fromElement, fromInclusive);
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement out of range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of range");
return new DescendingSubSetBRACES(set, false, fromElement, fromInclusive, false, toElement, toInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) {
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range");
return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, end, hiInclusive);
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive)
{
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range");
return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, hi, hiInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) {
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range");
return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, false, fromElement, inclusive);
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement out of range");
return new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, false, fromElement, inclusive);
}
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); }
protected Entry KEY_GENERIC_TYPE start() { return findHighest(); }
@Override
public KEY_TYPE FIRST_KEY() { return super.LAST_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_KEY() { return super.POLL_LAST_KEY(); }
@Override
public KEY_TYPE LAST_KEY() { return super.FIRST_KEY(); }
@Override
public KEY_TYPE POLL_LAST_KEY() { return super.POLL_FIRST_KEY(); }
@Override
public KEY_TYPE lower(KEY_TYPE e) { return super.higher(e); }
@Override
public KEY_TYPE floor(KEY_TYPE e) { return super.ceiling(e); }
@Override
public KEY_TYPE ceiling(KEY_TYPE e) { return super.floor(e); }
@Override
public KEY_TYPE higher(KEY_TYPE e) { return super.lower(e); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new DescendingSubIterator(findHighest()); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement);
return entry == null || !inClosedRange(fromElement) ? null : new DescendingSubIterator(entry);
public BI_ITERATOR KEY_GENERIC_TYPE iterator() {
return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence());
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSubIterator(findLowest()); }
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() {
return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence());
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() {
return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
if(inverse == null) inverse = new AscendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
return inverse;
}
private class DescendingSubIterator extends SubSetIterator
{
public DescendingSubIterator(Entry KEY_GENERIC_TYPE entry) {
super(entry);
}
@Override
protected void updateNext() {
next = current.previous();
if(!toEnd && next != null && bottomReached(next)) next = null;
}
@Override
protected void updatePrevious() {
previous = current.next();
if(!fromStart && previous != null && topReached(previous)) previous = null;
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
return new DescendingSubSetIterator(absHigher(fromElement), absLowFence(), absHighFence());
}
@Override
protected Entry KEY_GENERIC_TYPE subLowest() { return absHighest(); }
@Override
protected Entry KEY_GENERIC_TYPE subHighest() { return absLowest(); }
@Override
protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absFloor(key); }
@Override
protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absLower(key); }
@Override
protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absCeiling(key); }
@Override
protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absHigher(key); }
@Override
protected Entry KEY_GENERIC_TYPE start() { return absHighest(); }
@Override
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); }
}
private static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
{
AVL_TREE_SET KEY_GENERIC_TYPE set;
KEY_TYPE start;
KEY_TYPE end;
boolean fromStart;
boolean toEnd;
boolean loInclusive;
boolean hiInclusive;
final AVL_TREE_SET KEY_GENERIC_TYPE set;
final KEY_TYPE lo, hi;
final boolean fromStart, toEnd;
final boolean loInclusive, hiInclusive;
NAVIGABLE_SET KEY_GENERIC_TYPE inverse;
SubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
public SubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive)
{
this.set = set;
this.start = start;
this.end = end;
this.lo = lo;
this.hi = hi;
this.fromStart = fromStart;
this.toEnd = toEnd;
this.loInclusive = loInclusive;
@ -903,13 +927,13 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#if !TYPE_OBJECT
@Override
public void setDefaultMaxValue(KEY_TYPE value) { throw new UnsupportedOperationException(); }
public void setDefaultMaxValue(KEY_TYPE value) { set.setDefaultMaxValue(value); }
@Override
public KEY_TYPE getDefaultMaxValue() { return set.getDefaultMaxValue(); }
@Override
public void setDefaultMinValue(KEY_TYPE value) { throw new UnsupportedOperationException(); }
public void setDefaultMinValue(KEY_TYPE value) { set.setDefaultMinValue(value); }
@Override
public KEY_TYPE getDefaultMinValue() { return set.getDefaultMinValue(); }
@ -920,80 +944,75 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
public KEY_TYPE getDefaultMinValue() { return null; }
#endif
boolean tooLow(KEY_TYPE key) { return !fromStart && (loInclusive ? set.compare(key, start) < 0 : set.compare(key, start) <= 0); }
boolean tooHigh(KEY_TYPE key) { return !toEnd && (hiInclusive ? set.compare(key, end) > 0 : set.compare(key, end) >= 0); }
boolean tooLow(KEY_TYPE key) {
if (!fromStart) {
int c = set.compare(key, lo);
if (c < 0 || (c == 0 && !loInclusive)) return true;
}
return false;
}
boolean tooHigh(KEY_TYPE key) {
if (!toEnd) {
int c = set.compare(key, hi);
if (c > 0 || (c == 0 && !hiInclusive)) return true;
}
return false;
}
boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); }
boolean inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, start) >= 0) && (toEnd || set.compare(end, key) >= 0); }
boolean inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, lo) >= 0) && (toEnd || set.compare(hi, key) >= 0); }
boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); }
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); }
protected Entry KEY_GENERIC_TYPE start() { return findLowest(); }
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
protected abstract Entry KEY_GENERIC_TYPE subLowest();
protected abstract Entry KEY_GENERIC_TYPE subHighest();
protected abstract Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key);
protected abstract Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key);
protected abstract Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key);
protected abstract Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key);
protected KEY_TYPE lowKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMinValue() : entry.key; }
protected KEY_TYPE highKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMaxValue() : entry.key; }
protected abstract Entry KEY_GENERIC_TYPE start();
protected abstract Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry);
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.comparator(); }
@Override
public abstract BI_ITERATOR KEY_GENERIC_TYPE iterator();
@Override
public KEY_TYPE FIRST_KEY() {
Entry KEY_GENERIC_TYPE entry = findLowest();
return entry == null ? getDefaultMaxValue() : entry.key;
final Entry KEY_GENERIC_TYPE absLowest() {
Entry KEY_GENERIC_TYPE e = (fromStart ? set.first : (loInclusive ? set.findCeilingNode(lo) : set.findHigherNode(lo)));
return (e == null || tooHigh(e.key)) ? null : e;
}
protected Entry KEY_GENERIC_TYPE findLowest() {
if(fromStart) return set.first;
Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start);
return entry == null || tooHigh(entry.key) ? null : entry;
final Entry KEY_GENERIC_TYPE absHighest() {
Entry KEY_GENERIC_TYPE e = (toEnd ? set.last : (hiInclusive ? set.findFloorNode(hi) : set.findLowerNode(hi)));
return (e == null || tooLow(e.key)) ? null : e;
}
@Override
public KEY_TYPE POLL_FIRST_KEY() {
if(fromStart) return set.POLL_FIRST_KEY();
Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start);
if(entry != null && !tooHigh(entry.key)) {
KEY_TYPE value = entry.key;
set.removeNode(entry);
return value;
}
return getDefaultMaxValue();
}
@Override
public KEY_TYPE LAST_KEY() {
Entry KEY_GENERIC_TYPE entry = findHighest();
return entry == null ? getDefaultMinValue() : entry.key;
final Entry KEY_GENERIC_TYPE absCeiling(KEY_TYPE key) {
if (tooLow(key)) return absLowest();
Entry KEY_GENERIC_TYPE e = set.findCeilingNode(key);
return (e == null || tooHigh(e.key)) ? null : e;
}
protected Entry KEY_GENERIC_TYPE findHighest() {
if(toEnd) return set.last;
Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end);
return entry == null || tooLow(entry.key) ? null : entry;
final Entry KEY_GENERIC_TYPE absHigher(KEY_TYPE key) {
if (tooLow(key)) return absLowest();
Entry KEY_GENERIC_TYPE e = set.findHigherNode(key);
return (e == null || tooHigh(e.key)) ? null : e;
}
@Override
public KEY_TYPE POLL_LAST_KEY() {
if(toEnd) return set.POLL_LAST_KEY();
Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end);
if(entry != null && !tooLow(entry.key)) {
KEY_TYPE value = entry.key;
set.removeNode(entry);
return value;
}
return getDefaultMinValue();
final Entry KEY_GENERIC_TYPE absFloor(KEY_TYPE key) {
if (tooHigh(key)) return absHighest();
Entry KEY_GENERIC_TYPE e = set.findFloorNode(key);
return (e == null || tooLow(e.key)) ? null : e;
}
final Entry KEY_GENERIC_TYPE absLower(KEY_TYPE key) {
if (tooHigh(key)) return absHighest();
Entry KEY_GENERIC_TYPE e = set.findLowerNode(key);
return (e == null || tooLow(e.key)) ? null : e;
}
final Entry KEY_GENERIC_TYPE absHighFence() { return (toEnd ? null : (hiInclusive ? set.findHigherNode(hi) : set.findCeilingNode(hi))); }
final Entry KEY_GENERIC_TYPE absLowFence() { return (fromStart ? null : (loInclusive ? set.findLowerNode(lo) : set.findFloorNode(lo))); }
@Override
public boolean add(KEY_TYPE o) {
if(!inRange(o)) throw new IllegalArgumentException("Key is out of range");
@ -1017,7 +1036,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
KEY_TYPE o = OBJ_TO_KEY(((CLASS_TYPE)e));
return inRange(o) && set.contains(o);
}
@Override
public boolean remove(Object e) {
KEY_TYPE o = OBJ_TO_KEY(((CLASS_TYPE)e));
@ -1025,50 +1044,85 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
}
@Override
public KEY_TYPE lower(KEY_TYPE e) {
if(tooHigh(e)) {
Entry KEY_GENERIC_TYPE entry = findHighest();
return entry == null ? getDefaultMinValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findLowerNode(e);
return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key;
}
@Override
public KEY_TYPE floor(KEY_TYPE e) {
if(tooHigh(e)) {
Entry KEY_GENERIC_TYPE entry = findHighest();
return entry == null ? getDefaultMinValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findFloorNode(e);
return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key;
}
@Override
public KEY_TYPE ceiling(KEY_TYPE e) {
if(tooLow(e)) {
Entry KEY_GENERIC_TYPE entry = findLowest();
return entry == null ? getDefaultMaxValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findCeilingNode(e);
return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key;
}
@Override
public KEY_TYPE higher(KEY_TYPE e) {
if(tooLow(e)) {
Entry KEY_GENERIC_TYPE entry = findLowest();
return entry == null ? getDefaultMaxValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findHigherNode(e);
return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key;
public boolean isEmpty() {
if(fromStart && toEnd) return set.isEmpty();
Entry KEY_GENERIC_TYPE n = absLowest();
return n == null || tooHigh(n.key);
}
@Override
public int size() {
return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE);
if(fromStart && toEnd) return set.size();
int i = 0;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();iter.NEXT(),i++);
return i;
}
@Override
public KEY_TYPE lower(KEY_TYPE e) {
return lowKeyOrNull(subLower(e));
}
@Override
public KEY_TYPE floor(KEY_TYPE e) {
return lowKeyOrNull(subFloor(e));
}
@Override
public KEY_TYPE ceiling(KEY_TYPE e) {
return highKeyOrNull(subCeiling(e));
}
@Override
public KEY_TYPE higher(KEY_TYPE e) {
return highKeyOrNull(subHigher(e));
}
@Override
public KEY_TYPE POLL_FIRST_KEY() {
Entry KEY_GENERIC_TYPE entry = subLowest();
if(entry != null) {
KEY_TYPE result = entry.key;
set.removeNode(entry);
return result;
}
return getDefaultMinValue();
}
@Override
public KEY_TYPE POLL_LAST_KEY() {
Entry KEY_GENERIC_TYPE entry = subHighest();
if(entry != null) {
KEY_TYPE result = entry.key;
set.removeNode(entry);
return result;
}
return getDefaultMaxValue();
}
@Override
public KEY_TYPE FIRST_KEY() {
Entry KEY_GENERIC_TYPE entry = subLowest();
if(entry == null) throw new NoSuchElementException();
return entry.key;
}
@Override
public KEY_TYPE LAST_KEY() {
Entry KEY_GENERIC_TYPE entry = subHighest();
if(entry == null) throw new NoSuchElementException();
return entry.key;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public SubSet KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@ -1172,171 +1226,207 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
return result;
}
class SubSetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE
class AscendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE previous;
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE current;
int index = 0;
boolean unboundForwardFence;
boolean unboundBackwardFence;
KEY_TYPE forwardFence;
KEY_TYPE backwardFence;
public SubSetIterator(Entry KEY_GENERIC_TYPE entry) {
next = entry;
previous = entry == null ? null : entry.previous();
public AscendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence)
{
next = first;
this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key;
this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key;
unboundForwardFence = forwardFence == null;
unboundBackwardFence = backwardFence == null;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public boolean hasPrevious() {
return previous != null;
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index - 1;
}
boolean topReached(Entry KEY_GENERIC_TYPE entry) {
return hiInclusive ? set.compare(entry.key, end) >= 0 : set.compare(entry.key, end) > 0;
}
boolean bottomReached(Entry KEY_GENERIC_TYPE entry) {
return loInclusive ? set.compare(entry.key, start) <= 0 : set.compare(entry.key, start) < 0;
}
protected void updateNext() {
next = current.next();
if(!toEnd && next != null && topReached(next)) next = null;
}
protected void updatePrevious() {
previous = current.previous();
if(!fromStart && previous != null && bottomReached(previous)) previous = null;
return next != null && (unboundForwardFence || next.key != forwardFence);
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
current = previous = next;
updateNext();
index++;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public boolean hasPrevious() {
return next != null && (unboundBackwardFence || next.key != backwardFence);
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
current = next = previous;
updatePrevious();
index--;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public void remove() {
if(current == null) throw new IllegalStateException();
if(current == previous) index--;
updateNext();
updatePrevious();
set.removeNode(current);
current = null;
if(lastReturned == null) throw new IllegalStateException();
if(lastReturned.needsSuccessor()) next = lastReturned;
set.removeNode(lastReturned);
lastReturned = null;
}
}
class DescendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
boolean unboundForwardFence;
boolean unboundBackwardFence;
KEY_TYPE forwardFence;
KEY_TYPE backwardFence;
public DescendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence)
{
next = first;
this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key;
this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key;
unboundForwardFence = forwardFence == null;
unboundBackwardFence = backwardFence == null;
}
@Override
public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); }
public boolean hasNext() {
return next != null && (unboundForwardFence || next.key != forwardFence);
}
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public boolean hasPrevious() {
return next != null && (unboundBackwardFence || next.key != backwardFence);
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
set.removeNode(lastReturned);
lastReturned = null;
}
}
}
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE
class AscendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE previous;
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE current;
int index = 0;
public SetIterator(boolean descending) {
if(descending) previous = last;
else next = first;
}
public SetIterator(Entry KEY_GENERIC_TYPE entry) {
next = entry;
previous = entry.previous();
public AscendingSetIterator(Entry KEY_GENERIC_TYPE first)
{
next = first;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public boolean hasPrevious() {
return previous != null;
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index - 1;
}
protected void updateNext() {
next = current.next();
}
protected void updatePrevious() {
previous = current.previous();
return next != null;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
current = previous = next;
updateNext();
index++;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public boolean hasPrevious() {
return next != null;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
current = next = previous;
updatePrevious();
index--;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public void remove() {
if(current == null) throw new IllegalStateException();
if(current == previous) index--;
updateNext();
updatePrevious();
if(current.needsSuccessor()) next = current;
removeNode(current);
current = null;
if(lastReturned == null) throw new IllegalStateException();
if(lastReturned.needsSuccessor()) next = lastReturned;
removeNode(lastReturned);
lastReturned = null;
}
}
class DescendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
public DescendingSetIterator(Entry KEY_GENERIC_TYPE first)
{
next = first;
}
@Override
public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
public boolean hasNext() {
return next != null;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public boolean hasPrevious() {
return next != null;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
removeNode(lastReturned);
lastReturned = null;
}
}
private static final class Entry KEY_GENERIC_TYPE

View File

@ -499,27 +499,30 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
throw new NoSuchElementException();
}
/**
* Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions.
* It is a Unsorted Sorted Set. Thats why the SubSet implementation will be disabled until a better solution is found.
* To give a simple reason: LinkedHashSets are also not SortedSets even so they could be.
* @throws UnsupportedOperationException
*/
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) {
int fromIndex = findIndex(fromElement);
int toIndex = findIndex(toElement);
if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, toIndex - fromIndex + 1);
}
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
/**
* Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions.
* It is a Unsorted Sorted Set. Thats why the SubSet implementation will be disabled until a better solution is found.
* To give a simple reason: LinkedHashSets are also not SortedSets even so they could be.
* @throws UnsupportedOperationException
*/
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) {
int toIndex = findIndex(toElement);
if(toIndex == -1) throw new NoSuchElementException();
return new SubSet(0, toIndex+1);
}
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
/**
* Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions.
* It is a Unsorted Sorted Set. Thats why the SubSet implementation will be disabled until a better solution is found.
* To give a simple reason: LinkedHashSets are also not SortedSets even so they could be.
* @throws UnsupportedOperationException
*/
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) {
int fromIndex = findIndex(fromElement);
if(fromIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, size - fromIndex);
}
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
public ARRAY_SET KEY_GENERIC_TYPE copy() {
ARRAY_SET KEY_GENERIC_TYPE set = new ARRAY_SETBRACES();
@ -573,345 +576,346 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
return a;
}
private class SubSet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
int offset;
int length;
SubSet(int offset, int length) {
this.offset = offset;
this.length = length;
}
int end() { return offset+length; }
@Override
public boolean add(KEY_TYPE o) {
int index = findIndex(o);
if(index == -1) {
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
if(end() != size) System.arraycopy(data, end(), data, end()+1, size-(offset+length));
data[end()] = o;
size++;
length++;
return true;
}
return false;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) {
int index = findIndex(o);
if(index == -1) {
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
System.arraycopy(data, offset, data, offset+1, size-offset);
data[offset] = o;
size++;
length++;
return true;
}
else if(index != 0) {
o = data[index];
System.arraycopy(data, offset, data, offset+1, index-offset);
data[offset] = o;
}
return false;
}
@Override
public boolean addAndMoveToLast(KEY_TYPE o) {
int index = findIndex(o);
if(index == -1) {
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
System.arraycopy(data, end()+1, data, end(), size-end());
data[end()] = o;
size++;
length++;
return true;
}
else if(index != 0) {
o = data[index];
System.arraycopy(data, offset+1, data, offset, index-offset);
data[offset+length] = o;
}
return false;
}
@Override
public boolean moveToFirst(KEY_TYPE o) {
int index = findIndex(o);
if(index > offset) {
o = data[index];
System.arraycopy(data, offset, data, offset+1, index-offset);
data[offset] = o;
return true;
}
return false;
}
@Override
public boolean moveToLast(KEY_TYPE o) {
int index = findIndex(o);
if(index != -1 && index < end()-1) {
o = data[index];
System.arraycopy(data, index+1, data, index, end()-index-1);
data[end()-1] = o;
return true;
}
return false;
}
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE e) {
return findIndex(e) != -1;
}
#endif
@Override
public boolean contains(Object e) {
return findIndex(e) != -1;
}
@Override
public KEY_TYPE FIRST_KEY() {
if(length == 0) throw new NoSuchElementException();
return data[offset];
}
@Override
public KEY_TYPE LAST_KEY() {
if(length == 0) throw new NoSuchElementException();
return data[end()-1];
}
#if !TYPE_OBJECT
@Override
public boolean remove(KEY_TYPE o) {
int index = findIndex(o);
if(index != -1) {
size--;
length--;
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
return true;
}
return false;
}
#endif
@Override
public boolean remove(Object o) {
int index = findIndex(o);
if(index != -1) {
size--;
length--;
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
#if TYPE_OBJECT
data[size] = EMPTY_KEY_VALUE;
#endif
return true;
}
return false;
}
@Override
public KEY_TYPE POLL_FIRST_KEY() {
if(length == 0) throw new NoSuchElementException();
size--;
length--;
KEY_TYPE result = data[offset];
System.arraycopy(data, offset+1, data, offset, size-offset);
#if TYPE_OBJECT
data[size] = EMPTY_KEY_VALUE;
#endif
return result;
}
@Override
public KEY_TYPE POLL_LAST_KEY() {
if(length == 0) throw new NoSuchElementException();
KEY_TYPE result = data[offset+length];
size--;
length--;
System.arraycopy(data, end()+1, data, end(), size-end());
#if TYPE_OBJECT
data[size] = EMPTY_KEY_VALUE;
#endif
return result;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() {
return new SetIterator(offset);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
int index = findIndex(fromElement);
if(index != -1) return new SetIterator(index);
throw new NoSuchElementException();
}
@Override
public SubSet copy() { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) {
int fromIndex = findIndex(fromElement);
int toIndex = findIndex(toElement);
if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, toIndex - fromIndex + 1);
}
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) {
int toIndex = findIndex(toElement);
if(toIndex == -1) throw new NoSuchElementException();
return new SubSet(0, toIndex+1);
}
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) {
int fromIndex = findIndex(fromElement);
if(fromIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, size - fromIndex);
}
@Override
public int size() {
return length;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) return Arrays.copyOfRange(data, offset, end());
System.arraycopy(data, offset, a, 0, size());
return a;
}
#endif
@Override
@Deprecated
public Object[] toArray() {
Object[] obj = new Object[size()];
for(int i = 0;i<size();i++)
obj[i] = KEY_TO_OBJ(data[offset+i]);
return obj;
}
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size()];
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
for(int i = 0;i<size();i++)
a[i] = (E)KEY_TO_OBJ(data[offset+i]);
return a;
}
#if !TYPE_OBJECT
protected int findIndex(KEY_TYPE o) {
for(int i = length-1;i>=0;i--)
if(KEY_EQUALS(data[offset+i], o)) return i + offset;
return -1;
}
#endif
protected int findIndex(Object o) {
for(int i = length-1;i>=0;i--)
if(EQUALS_KEY_TYPE(data[offset+i], o)) return i + offset;
return -1;
}
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
int index;
int lastReturned = -1;
public SetIterator(int index) {
this.index = index;
}
@Override
public boolean hasNext() {
return index < size();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
lastReturned = index;
return data[index++];
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
lastReturned = index;
return data[index--];
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index-1;
}
@Override
public void remove() {
if(lastReturned == -1)
throw new IllegalStateException();
SubSet.this.remove(data[lastReturned]);
if(lastReturned < index)
index--;
lastReturned = -1;
}
#if TYPE_OBJECT
@Override
public void set(Object e) { throw new UnsupportedOperationException(); }
@Override
public void add(Object e) { throw new UnsupportedOperationException(); }
#else
@Override
public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
#endif
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, (size() - 1) - index);
index += steps;
return steps;
}
@Override
public int back(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
return steps;
}
}
}
// Disabled until a Proper Implementation can be thought out or it is decided that the interface is thrown out.
// private class SubSet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
// int offset;
// int length;
//
// SubSet(int offset, int length) {
// this.offset = offset;
// this.length = length;
// }
//
// int end() { return offset+length; }
//
// @Override
// public boolean add(KEY_TYPE o) {
// int index = findIndex(o);
// if(index == -1) {
// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
// if(end() != size) System.arraycopy(data, end(), data, end()+1, size-(offset+length));
// data[end()] = o;
// size++;
// length++;
// return true;
// }
// return false;
// }
//
// @Override
// public boolean addAndMoveToFirst(KEY_TYPE o) {
// int index = findIndex(o);
// if(index == -1) {
// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
// System.arraycopy(data, offset, data, offset+1, size-offset);
// data[offset] = o;
// size++;
// length++;
// return true;
// }
// else if(index != 0) {
// o = data[index];
// System.arraycopy(data, offset, data, offset+1, index-offset);
// data[offset] = o;
// }
// return false;
// }
//
// @Override
// public boolean addAndMoveToLast(KEY_TYPE o) {
// int index = findIndex(o);
// if(index == -1) {
// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
// System.arraycopy(data, end()+1, data, end(), size-end());
// data[end()] = o;
// size++;
// length++;
// return true;
// }
// else if(index != 0) {
// o = data[index];
// System.arraycopy(data, offset+1, data, offset, index-offset);
// data[offset+length] = o;
// }
// return false;
// }
//
// @Override
// public boolean moveToFirst(KEY_TYPE o) {
// int index = findIndex(o);
// if(index > offset) {
// o = data[index];
// System.arraycopy(data, offset, data, offset+1, index-offset);
// data[offset] = o;
// return true;
// }
// return false;
// }
//
// @Override
// public boolean moveToLast(KEY_TYPE o) {
// int index = findIndex(o);
// if(index != -1 && index < end()-1) {
// o = data[index];
// System.arraycopy(data, index+1, data, index, end()-index-1);
// data[end()-1] = o;
// return true;
// }
// return false;
// }
//
//#if !TYPE_OBJECT
// @Override
// public boolean contains(KEY_TYPE e) {
// return findIndex(e) != -1;
// }
//
//#endif
// @Override
// public boolean contains(Object e) {
// return findIndex(e) != -1;
// }
//
// @Override
// public KEY_TYPE FIRST_KEY() {
// if(length == 0) throw new NoSuchElementException();
// return data[offset];
// }
//
// @Override
// public KEY_TYPE LAST_KEY() {
// if(length == 0) throw new NoSuchElementException();
// return data[end()-1];
// }
//
//#if !TYPE_OBJECT
// @Override
// public boolean remove(KEY_TYPE o) {
// int index = findIndex(o);
// if(index != -1) {
// size--;
// length--;
// if(index != size) System.arraycopy(data, index+1, data, index, size - index);
// return true;
// }
// return false;
// }
//
//#endif
// @Override
// public boolean remove(Object o) {
// int index = findIndex(o);
// if(index != -1) {
// size--;
// length--;
// if(index != size) System.arraycopy(data, index+1, data, index, size - index);
//#if TYPE_OBJECT
// data[size] = EMPTY_KEY_VALUE;
//#endif
// return true;
// }
// return false;
// }
//
// @Override
// public KEY_TYPE POLL_FIRST_KEY() {
// if(length == 0) throw new NoSuchElementException();
// size--;
// length--;
// KEY_TYPE result = data[offset];
// System.arraycopy(data, offset+1, data, offset, size-offset);
//#if TYPE_OBJECT
// data[size] = EMPTY_KEY_VALUE;
//#endif
// return result;
// }
//
// @Override
// public KEY_TYPE POLL_LAST_KEY() {
// if(length == 0) throw new NoSuchElementException();
// KEY_TYPE result = data[offset+length];
// size--;
// length--;
// System.arraycopy(data, end()+1, data, end(), size-end());
//#if TYPE_OBJECT
// data[size] = EMPTY_KEY_VALUE;
//#endif
// return result;
// }
//
// @Override
// public COMPARATOR KEY_GENERIC_TYPE comparator() {
// return null;
// }
//
// @Override
// public BI_ITERATOR KEY_GENERIC_TYPE iterator() {
// return new SetIterator(offset);
// }
//
// @Override
// public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
// int index = findIndex(fromElement);
// if(index != -1) return new SetIterator(index);
// throw new NoSuchElementException();
// }
//
// @Override
// public SubSet copy() { throw new UnsupportedOperationException(); }
//
// @Override
// public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) {
// int fromIndex = findIndex(fromElement);
// int toIndex = findIndex(toElement);
// if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException();
// return new SubSet(fromIndex, toIndex - fromIndex + 1);
// }
//
// @Override
// public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) {
// int toIndex = findIndex(toElement);
// if(toIndex == -1) throw new NoSuchElementException();
// return new SubSet(0, toIndex+1);
// }
//
// @Override
// public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) {
// int fromIndex = findIndex(fromElement);
// if(fromIndex == -1) throw new NoSuchElementException();
// return new SubSet(fromIndex, size - fromIndex);
// }
//
// @Override
// public int size() {
// return length;
// }
//
//#if !TYPE_OBJECT
// @Override
// public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
// if(a == null || a.length < size()) return Arrays.copyOfRange(data, offset, end());
// System.arraycopy(data, offset, a, 0, size());
// return a;
// }
//
//#endif
// @Override
// @Deprecated
// public Object[] toArray() {
// Object[] obj = new Object[size()];
// for(int i = 0;i<size();i++)
// obj[i] = KEY_TO_OBJ(data[offset+i]);
// return obj;
// }
//
// @Override
// @Primitive
// public <E> E[] toArray(E[] a) {
// if(a == null) a = (E[])new Object[size()];
// else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
// for(int i = 0;i<size();i++)
// a[i] = (E)KEY_TO_OBJ(data[offset+i]);
// return a;
// }
//
//#if !TYPE_OBJECT
// protected int findIndex(KEY_TYPE o) {
// for(int i = length-1;i>=0;i--)
// if(KEY_EQUALS(data[offset+i], o)) return i + offset;
// return -1;
// }
//
//#endif
// protected int findIndex(Object o) {
// for(int i = length-1;i>=0;i--)
// if(EQUALS_KEY_TYPE(data[offset+i], o)) return i + offset;
// return -1;
// }
//
// private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
// int index;
// int lastReturned = -1;
//
// public SetIterator(int index) {
// this.index = index;
// }
//
// @Override
// public boolean hasNext() {
// return index < size();
// }
//
// @Override
// public KEY_TYPE NEXT() {
// if(!hasNext()) throw new NoSuchElementException();
// lastReturned = index;
// return data[index++];
// }
//
// @Override
// public boolean hasPrevious() {
// return index > 0;
// }
//
// @Override
// public KEY_TYPE PREVIOUS() {
// if(!hasPrevious()) throw new NoSuchElementException();
// lastReturned = index;
// return data[index--];
// }
//
// @Override
// public int nextIndex() {
// return index;
// }
//
// @Override
// public int previousIndex() {
// return index-1;
// }
//
// @Override
// public void remove() {
// if(lastReturned == -1)
// throw new IllegalStateException();
// SubSet.this.remove(data[lastReturned]);
// if(lastReturned < index)
// index--;
// lastReturned = -1;
// }
//
// #if TYPE_OBJECT
// @Override
// public void set(Object e) { throw new UnsupportedOperationException(); }
//
// @Override
// public void add(Object e) { throw new UnsupportedOperationException(); }
//
// #else
// @Override
// public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
//
// @Override
// public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
//
// #endif
// @Override
// public int skip(int amount) {
// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
// int steps = Math.min(amount, (size() - 1) - index);
// index += steps;
// return steps;
// }
//
// @Override
// public int back(int amount) {
// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
// int steps = Math.min(amount, index);
// index -= steps;
// return steps;
// }
// }
// }
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
int index;

View File

@ -1,6 +1,7 @@
package speiger.src.collections.PACKAGE.sets;
import java.util.Collection;
import java.util.Collections;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
@ -19,7 +20,6 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#endif
@ -559,7 +559,11 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
@Override
public KEY_TYPE POLL_FIRST_KEY() {
if(tree == null) throw new NoSuchElementException();
#if TYPE_OBJECT
if(tree == null) return null;
#else
if(tree == null) return getDefaultMinValue();
#endif
KEY_TYPE value = first.key;
removeNode(first);
return value;
@ -567,7 +571,11 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
@Override
public KEY_TYPE POLL_LAST_KEY() {
if(tree == null) throw new NoSuchElementException();
#if TYPE_OBJECT
if(tree == null) return null;
#else
if(tree == null) return getDefaultMaxValue();
#endif
KEY_TYPE value = last.key;
removeNode(last);
return value;
@ -603,16 +611,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SetIterator(false); }
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new AscendingSetIterator(first); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
Entry KEY_GENERIC_TYPE entry = findNode(fromElement);
return entry == null ? null : new SetIterator(entry);
return entry == null ? null : new AscendingSetIterator(entry);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SetIterator(true); }
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSetIterator(last); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
@ -804,158 +812,174 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
setBlack(entry, true);
}
private static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
{
AscendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
public AscendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive)
{
super(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range");
public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.comparator(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive)
{
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement out of range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of range");
return new AscendingSubSetBRACES(set, false, fromElement, fromInclusive, false, toElement, toInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) {
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range");
return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, false, toElement, inclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) {
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range");
return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, end, hiInclusive);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SubSetIterator(findLowest()); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement);
return entry == null || !inClosedRange(fromElement) ? null : new SubSetIterator(entry);
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive)
{
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range");
return new AscendingSubSetBRACES(set, fromStart, lo, loInclusive, false, toElement, inclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive)
{
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement out of range");
return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, hi, hiInclusive);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SubSetIterator(findHighest()); }
public BI_ITERATOR KEY_GENERIC_TYPE iterator()
{
return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence());
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement)
{
return new AscendingSubSetIterator(absLower(fromElement), absHighFence(), absLowFence());
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator()
{
return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence());
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() {
return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet()
{
if(inverse == null) inverse = new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
return inverse;
}
@Override
protected Entry KEY_GENERIC_TYPE subLowest() { return absLowest(); }
@Override
protected Entry KEY_GENERIC_TYPE subHighest() { return absHighest(); }
@Override
protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absCeiling(key); }
@Override
protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absHigher(key); }
@Override
protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absFloor(key); }
@Override
protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absLower(key); }
@Override
protected Entry KEY_GENERIC_TYPE start() { return absLowest(); }
@Override
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); }
}
private static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE
{
COMPARATOR KEY_GENERIC_TYPE comparator;
DescendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
comparator = set.comparator();
if(comparator != null) comparator = comparator.reversed();
public DescendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive)
{
super(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
#if TYPE_OBJECT
comparator = Collections.reverseOrder(set.comparator());
#else
comparator = set.comparator() == null ? COMPARATOR.of(Collections.reverseOrder()) : set.comparator().reversed();
#endif
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
public KEY_TYPE getDefaultMaxValue() { return super.getDefaultMinValue(); }
public KEY_TYPE getDefaultMinValue() { return super.getDefaultMaxValue(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range");
return new DescendingSubSetBRACES(set, false, toElement, toInclusive, false, fromElement, fromInclusive);
if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement out of range");
if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of range");
return new DescendingSubSetBRACES(set, false, fromElement, fromInclusive, false, toElement, toInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) {
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range");
return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, end, hiInclusive);
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive)
{
if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range");
return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, hi, hiInclusive);
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) {
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range");
return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, false, fromElement, inclusive);
if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement out of range");
return new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, false, fromElement, inclusive);
}
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); }
protected Entry KEY_GENERIC_TYPE start() { return findHighest(); }
@Override
public KEY_TYPE FIRST_KEY() { return super.LAST_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_KEY() { return super.POLL_LAST_KEY(); }
@Override
public KEY_TYPE LAST_KEY() { return super.FIRST_KEY(); }
@Override
public KEY_TYPE POLL_LAST_KEY() { return super.POLL_FIRST_KEY(); }
@Override
public KEY_TYPE lower(KEY_TYPE e) { return super.higher(e); }
@Override
public KEY_TYPE floor(KEY_TYPE e) { return super.ceiling(e); }
@Override
public KEY_TYPE ceiling(KEY_TYPE e) { return super.floor(e); }
@Override
public KEY_TYPE higher(KEY_TYPE e) { return super.lower(e); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new DescendingSubIterator(findHighest()); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement);
return entry == null || !inClosedRange(fromElement) ? null : new DescendingSubIterator(entry);
public BI_ITERATOR KEY_GENERIC_TYPE iterator() {
return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence());
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSubIterator(findLowest()); }
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() {
return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence());
}
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() {
return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
if(inverse == null) inverse = new AscendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
return inverse;
}
private class DescendingSubIterator extends SubSetIterator
{
public DescendingSubIterator(Entry KEY_GENERIC_TYPE entry) {
super(entry);
}
@Override
protected void updateNext() {
next = current.previous();
if(!toEnd && next != null && bottomReached(next)) next = null;
}
@Override
protected void updatePrevious() {
previous = current.next();
if(!fromStart && previous != null && topReached(previous)) previous = null;
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
return new DescendingSubSetIterator(absHigher(fromElement), absLowFence(), absHighFence());
}
@Override
protected Entry KEY_GENERIC_TYPE subLowest() { return absHighest(); }
@Override
protected Entry KEY_GENERIC_TYPE subHighest() { return absLowest(); }
@Override
protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absFloor(key); }
@Override
protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absLower(key); }
@Override
protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absCeiling(key); }
@Override
protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absHigher(key); }
@Override
protected Entry KEY_GENERIC_TYPE start() { return absHighest(); }
@Override
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); }
}
private static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
{
RB_TREE_SET KEY_GENERIC_TYPE set;
KEY_TYPE start;
KEY_TYPE end;
boolean fromStart;
boolean toEnd;
boolean loInclusive;
boolean hiInclusive;
final RB_TREE_SET KEY_GENERIC_TYPE set;
final KEY_TYPE lo, hi;
final boolean fromStart, toEnd;
final boolean loInclusive, hiInclusive;
NAVIGABLE_SET KEY_GENERIC_TYPE inverse;
SubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
public SubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive)
{
this.set = set;
this.start = start;
this.end = end;
this.lo = lo;
this.hi = hi;
this.fromStart = fromStart;
this.toEnd = toEnd;
this.loInclusive = loInclusive;
@ -981,80 +1005,75 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
public KEY_TYPE getDefaultMinValue() { return null; }
#endif
boolean tooLow(KEY_TYPE key) { return !fromStart && (loInclusive ? set.compare(key, start) < 0 : set.compare(key, start) <= 0); }
boolean tooHigh(KEY_TYPE key) { return !toEnd && (hiInclusive ? set.compare(key, end) > 0 : set.compare(key, end) >= 0); }
boolean tooLow(KEY_TYPE key) {
if (!fromStart) {
int c = set.compare(key, lo);
if (c < 0 || (c == 0 && !loInclusive)) return true;
}
return false;
}
boolean tooHigh(KEY_TYPE key) {
if (!toEnd) {
int c = set.compare(key, hi);
if (c > 0 || (c == 0 && !hiInclusive)) return true;
}
return false;
}
boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); }
boolean inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, start) >= 0) && (toEnd || set.compare(end, key) >= 0); }
boolean inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, lo) >= 0) && (toEnd || set.compare(hi, key) >= 0); }
boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); }
protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); }
protected Entry KEY_GENERIC_TYPE start() { return findLowest(); }
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
protected abstract Entry KEY_GENERIC_TYPE subLowest();
protected abstract Entry KEY_GENERIC_TYPE subHighest();
protected abstract Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key);
protected abstract Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key);
protected abstract Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key);
protected abstract Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key);
protected KEY_TYPE lowKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMinValue() : entry.key; }
protected KEY_TYPE highKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMaxValue() : entry.key; }
protected abstract Entry KEY_GENERIC_TYPE start();
protected abstract Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry);
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.comparator(); }
@Override
public abstract BI_ITERATOR KEY_GENERIC_TYPE iterator();
@Override
public KEY_TYPE FIRST_KEY() {
Entry KEY_GENERIC_TYPE entry = findLowest();
return entry == null ? getDefaultMaxValue() : entry.key;
final Entry KEY_GENERIC_TYPE absLowest() {
Entry KEY_GENERIC_TYPE e = (fromStart ? set.first : (loInclusive ? set.findCeilingNode(lo) : set.findHigherNode(lo)));
return (e == null || tooHigh(e.key)) ? null : e;
}
protected Entry KEY_GENERIC_TYPE findLowest() {
if(fromStart) return set.first;
Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start);
return entry == null || tooHigh(entry.key) ? null : entry;
final Entry KEY_GENERIC_TYPE absHighest() {
Entry KEY_GENERIC_TYPE e = (toEnd ? set.last : (hiInclusive ? set.findFloorNode(hi) : set.findLowerNode(hi)));
return (e == null || tooLow(e.key)) ? null : e;
}
@Override
public KEY_TYPE POLL_FIRST_KEY() {
if(fromStart) return set.POLL_FIRST_KEY();
Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start);
if(entry != null && !tooHigh(entry.key)) {
KEY_TYPE value = entry.key;
set.removeNode(entry);
return value;
}
return getDefaultMaxValue();
}
@Override
public KEY_TYPE LAST_KEY() {
Entry KEY_GENERIC_TYPE entry = findHighest();
return entry == null ? getDefaultMinValue() : entry.key;
final Entry KEY_GENERIC_TYPE absCeiling(KEY_TYPE key) {
if (tooLow(key)) return absLowest();
Entry KEY_GENERIC_TYPE e = set.findCeilingNode(key);
return (e == null || tooHigh(e.key)) ? null : e;
}
protected Entry KEY_GENERIC_TYPE findHighest() {
if(toEnd) return set.last;
Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end);
return entry == null || tooLow(entry.key) ? null : entry;
final Entry KEY_GENERIC_TYPE absHigher(KEY_TYPE key) {
if (tooLow(key)) return absLowest();
Entry KEY_GENERIC_TYPE e = set.findHigherNode(key);
return (e == null || tooHigh(e.key)) ? null : e;
}
@Override
public KEY_TYPE POLL_LAST_KEY() {
if(toEnd) return set.POLL_LAST_KEY();
Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end);
if(entry != null && !tooLow(entry.key)) {
KEY_TYPE value = entry.key;
set.removeNode(entry);
return value;
}
return getDefaultMinValue();
final Entry KEY_GENERIC_TYPE absFloor(KEY_TYPE key) {
if (tooHigh(key)) return absHighest();
Entry KEY_GENERIC_TYPE e = set.findFloorNode(key);
return (e == null || tooLow(e.key)) ? null : e;
}
final Entry KEY_GENERIC_TYPE absLower(KEY_TYPE key) {
if (tooHigh(key)) return absHighest();
Entry KEY_GENERIC_TYPE e = set.findLowerNode(key);
return (e == null || tooLow(e.key)) ? null : e;
}
final Entry KEY_GENERIC_TYPE absHighFence() { return (toEnd ? null : (hiInclusive ? set.findHigherNode(hi) : set.findCeilingNode(hi))); }
final Entry KEY_GENERIC_TYPE absLowFence() { return (fromStart ? null : (loInclusive ? set.findLowerNode(lo) : set.findFloorNode(lo))); }
@Override
public boolean add(KEY_TYPE o) {
if(!inRange(o)) throw new IllegalArgumentException("Key is out of range");
@ -1086,50 +1105,85 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
}
@Override
public KEY_TYPE lower(KEY_TYPE e) {
if(tooHigh(e)) {
Entry KEY_GENERIC_TYPE entry = findHighest();
return entry == null ? getDefaultMinValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findLowerNode(e);
return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key;
}
@Override
public KEY_TYPE floor(KEY_TYPE e) {
if(tooHigh(e)) {
Entry KEY_GENERIC_TYPE entry = findHighest();
return entry == null ? getDefaultMinValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findFloorNode(e);
return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key;
}
@Override
public KEY_TYPE ceiling(KEY_TYPE e) {
if(tooLow(e)) {
Entry KEY_GENERIC_TYPE entry = findLowest();
return entry == null ? getDefaultMaxValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findCeilingNode(e);
return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key;
}
@Override
public KEY_TYPE higher(KEY_TYPE e) {
if(tooLow(e)) {
Entry KEY_GENERIC_TYPE entry = findLowest();
return entry == null ? getDefaultMaxValue() : entry.key;
}
Entry KEY_GENERIC_TYPE entry = set.findHigherNode(e);
return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key;
public boolean isEmpty() {
if(fromStart && toEnd) return set.isEmpty();
Entry KEY_GENERIC_TYPE n = absLowest();
return n == null || tooHigh(n.key);
}
@Override
public int size() {
return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE);
if(fromStart && toEnd) return set.size();
int i = 0;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();iter.NEXT(),i++);
return i;
}
@Override
public KEY_TYPE lower(KEY_TYPE e) {
return lowKeyOrNull(subLower(e));
}
@Override
public KEY_TYPE floor(KEY_TYPE e) {
return lowKeyOrNull(subFloor(e));
}
@Override
public KEY_TYPE ceiling(KEY_TYPE e) {
return highKeyOrNull(subCeiling(e));
}
@Override
public KEY_TYPE higher(KEY_TYPE e) {
return highKeyOrNull(subHigher(e));
}
@Override
public KEY_TYPE POLL_FIRST_KEY() {
Entry KEY_GENERIC_TYPE entry = subLowest();
if(entry != null) {
KEY_TYPE result = entry.key;
set.removeNode(entry);
return result;
}
return getDefaultMinValue();
}
@Override
public KEY_TYPE POLL_LAST_KEY() {
Entry KEY_GENERIC_TYPE entry = subHighest();
if(entry != null) {
KEY_TYPE result = entry.key;
set.removeNode(entry);
return result;
}
return getDefaultMaxValue();
}
@Override
public KEY_TYPE FIRST_KEY() {
Entry KEY_GENERIC_TYPE entry = subLowest();
if(entry == null) throw new NoSuchElementException();
return entry.key;
}
@Override
public KEY_TYPE LAST_KEY() {
Entry KEY_GENERIC_TYPE entry = subHighest();
if(entry == null) throw new NoSuchElementException();
return entry.key;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public SubSet KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@ -1233,172 +1287,207 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
return result;
}
class SubSetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE
class AscendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE previous;
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE current;
int index = 0;
boolean unboundForwardFence;
boolean unboundBackwardFence;
KEY_TYPE forwardFence;
KEY_TYPE backwardFence;
public SubSetIterator(Entry KEY_GENERIC_TYPE entry) {
next = entry;
previous = entry == null ? null : entry.previous();
public AscendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence)
{
next = first;
this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key;
this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key;
unboundForwardFence = forwardFence == null;
unboundBackwardFence = backwardFence == null;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public boolean hasPrevious() {
return previous != null;
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index - 1;
}
boolean topReached(Entry KEY_GENERIC_TYPE entry) {
return hiInclusive ? set.compare(entry.key, end) >= 0 : set.compare(entry.key, end) > 0;
}
boolean bottomReached(Entry KEY_GENERIC_TYPE entry) {
return loInclusive ? set.compare(entry.key, start) <= 0 : set.compare(entry.key, start) < 0;
}
protected void updateNext() {
next = current.next();
if(!toEnd && next != null && topReached(next)) next = null;
}
protected void updatePrevious() {
previous = current.previous();
if(!fromStart && previous != null && bottomReached(previous)) previous = null;
return next != null && (unboundForwardFence || next.key != forwardFence);
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
current = previous = next;
updateNext();
index++;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public boolean hasPrevious() {
return next != null && (unboundBackwardFence || next.key != backwardFence);
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
current = next = previous;
updatePrevious();
index--;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public void remove() {
if(current == null) throw new IllegalStateException();
if(current == previous) index--;
updateNext();
updatePrevious();
if(current.needsSuccessor()) next = current;
set.removeNode(current);
current = null;
if(lastReturned == null) throw new IllegalStateException();
if(lastReturned.needsSuccessor()) next = lastReturned;
set.removeNode(lastReturned);
lastReturned = null;
}
}
class DescendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
boolean unboundForwardFence;
boolean unboundBackwardFence;
KEY_TYPE forwardFence;
KEY_TYPE backwardFence;
public DescendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence)
{
next = first;
this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key;
this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key;
unboundForwardFence = forwardFence == null;
unboundBackwardFence = backwardFence == null;
}
@Override
public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); }
public boolean hasNext() {
return next != null && (unboundForwardFence || next.key != forwardFence);
}
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public boolean hasPrevious() {
return next != null && (unboundBackwardFence || next.key != backwardFence);
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
set.removeNode(lastReturned);
lastReturned = null;
}
}
}
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE
class AscendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE previous;
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE current;
int index = 0;
public SetIterator(boolean descending) {
if(descending) previous = last;
else next = first;
}
public SetIterator(Entry KEY_GENERIC_TYPE entry) {
next = entry;
previous = entry.previous();
public AscendingSetIterator(Entry KEY_GENERIC_TYPE first)
{
next = first;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public boolean hasPrevious() {
return previous != null;
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index - 1;
}
protected void updateNext() {
next = current.next();
}
protected void updatePrevious() {
previous = current.previous();
return next != null;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
current = previous = next;
updateNext();
index++;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public boolean hasPrevious() {
return next != null;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
current = next = previous;
updatePrevious();
index--;
return current.key;
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public void remove() {
if(current == null) throw new IllegalStateException();
if(current == previous) index--;
updateNext();
updatePrevious();
if(current.needsSuccessor()) next = current;
removeNode(current);
current = null;
if(lastReturned == null) throw new IllegalStateException();
if(lastReturned.needsSuccessor()) next = lastReturned;
removeNode(lastReturned);
lastReturned = null;
}
}
class DescendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE lastReturned;
Entry KEY_GENERIC_TYPE next;
public DescendingSetIterator(Entry KEY_GENERIC_TYPE first)
{
next = first;
}
@Override
public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
public boolean hasNext() {
return next != null;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.previous();
return result;
}
@Override
public boolean hasPrevious() {
return next != null;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
lastReturned = next;
KEY_TYPE result = next.key;
next = next.next();
return result;
}
@Override
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
removeNode(lastReturned);
lastReturned = null;
}
}
private static final class Entry KEY_GENERIC_TYPE

View File

@ -1,12 +1,18 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest;
import speiger.src.collections.ints.maps.impl.misc.Int2IntArrayMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
import speiger.src.collections.tests.SortedMapTests;
@SuppressWarnings("javadoc")
public class Int2IntArrayMapTest extends BaseInt2IntSortedMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.SUB_MAP, SortedMapTests.HEAD_MAP, SortedMapTests.TAIL_MAP)); }
@Override
public Int2IntSortedMap createMap(int[] keys, int[] values)
{

View File

@ -1,10 +1,15 @@
package speiger.src.collections.ints.sets;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseIntSortedSetTest;
import speiger.src.collections.tests.SortedSetTest;
@SuppressWarnings("javadoc")
public class IntArraySetTests extends BaseIntSortedSetTest
{
@Override
protected EnumSet<SortedSetTest> getValidSortedSetTests() { return EnumSet.complementOf(EnumSet.of(SortedSetTest.SUB_SET, SortedSetTest.HEAD_SET, SortedSetTest.TAIL_SET)); }
@Override
protected IntSortedSet create(int[] data) { return new IntArraySet(data.clone()); }
}

View File

@ -2,14 +2,17 @@ package speiger.src.collections.objects.map;
import java.util.Comparator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import com.google.common.collect.testing.AnEnum;
import com.google.common.collect.testing.MapTestSuiteBuilder;
import com.google.common.collect.testing.NavigableMapTestSuiteBuilder;
import com.google.common.collect.testing.TestEnumMapGenerator;
import com.google.common.collect.testing.TestStringMapGenerator;
import com.google.common.collect.testing.TestStringSortedMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
@ -39,10 +42,10 @@ public class ObjectMapTests extends TestCase
suite.addTest(suite("LinkedHashMap", Object2ObjectLinkedOpenHashMap::new, true));
suite.addTest(suite("CustomHashMap", () -> new Object2ObjectOpenCustomHashMap<>(Strategy.INSTANCE), true));
suite.addTest(suite("LinkedCustomHashMap", () -> new Object2ObjectLinkedOpenCustomHashMap<>(Strategy.INSTANCE), true));
suite.addTest(suite("RBTreeMap_NonNull", Object2ObjectRBTreeMap::new, false));
suite.addTest(suite("AVLTreeMap_NonNull", Object2ObjectAVLTreeMap::new, false));
suite.addTest(suite("RBTreeMap_Null", () -> new Object2ObjectRBTreeMap<String, String>(Comparator.nullsFirst(Comparator.naturalOrder())), true));
suite.addTest(suite("AVLTreeMap_Null", () -> new Object2ObjectAVLTreeMap<String, String>(Comparator.nullsFirst(Comparator.naturalOrder())), true));
suite.addTest(navigableSuite("RBTreeMap_NonNull", Object2ObjectRBTreeMap::new, false));
suite.addTest(navigableSuite("AVLTreeMap_NonNull", Object2ObjectAVLTreeMap::new, false));
suite.addTest(navigableSuite("RBTreeMap_Null", () -> new Object2ObjectRBTreeMap<>(new NullFriendlyComparator()), true));
suite.addTest(navigableSuite("AVLTreeMap_Null", () -> new Object2ObjectAVLTreeMap<>(new NullFriendlyComparator()), true));
suite.addTest(immutableSuit("ImmutableMap", ImmutableObject2ObjectOpenHashMap::new));
suite.addTest(suite("ArrayMap", Object2ObjectArrayMap::new, true));
suite.addTest(enumSuite("EnumMap", () -> new Enum2ObjectMap<>(AnEnum.class)));
@ -66,6 +69,22 @@ public class ObjectMapTests extends TestCase
return builder.createTestSuite();
}
public static Test navigableSuite(String name, Supplier<NavigableMap<String, String>> factory, boolean allowNull)
{
MapTestSuiteBuilder<String, String> builder = NavigableMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() {
@Override
protected NavigableMap<String, String> create(Map.Entry<String, String>[] entries) {
NavigableMap<String, String> map = factory.get();
for(Map.Entry<String, String> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
}).named(name).withFeatures(CollectionSize.ANY, MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE);
if(allowNull) builder.withFeatures(MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_ANY_NULL_QUERIES);
return builder.createTestSuite();
}
public static Test immutableSuit(String name, BiFunction<String[], String[], Map<String, String>> factory) {
MapTestSuiteBuilder<String, String> builder = MapTestSuiteBuilder.using(new TestStringMapGenerator() {
@Override
@ -97,6 +116,14 @@ public class ObjectMapTests extends TestCase
return builder.createTestSuite();
}
private static final class NullFriendlyComparator implements Comparator<String>
{
@Override
public int compare(String left, String right) {
return String.valueOf(left).compareTo(String.valueOf(right));
}
}
private static class Strategy implements ObjectStrategy<String>
{
static final Strategy INSTANCE = new Strategy();

View File

@ -1,12 +1,15 @@
package speiger.src.collections.objects.set;
import java.util.Comparator;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import com.google.common.collect.testing.NavigableSetTestSuiteBuilder;
import com.google.common.collect.testing.SetTestSuiteBuilder;
import com.google.common.collect.testing.TestStringSetGenerator;
import com.google.common.collect.testing.TestStringSortedSetGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.SetFeature;
@ -36,15 +39,14 @@ public class ObjectSetTests extends TestCase
suite.addTest(suite("LinkedCustomHashSet", T -> new ObjectLinkedOpenCustomHashSet<>(T, Strategy.INSTANCE), true));
suite.addTest(immutableSuite("ImmutableHashSet", ImmutableObjectOpenHashSet::new));
suite.addTest(suite("ArraySet", ObjectArraySet::new, true));
suite.addTest(suite("RBTreeSet_NonNull", ObjectRBTreeSet::new, false));
suite.addTest(suite("AVLTreeSet_NonNull", ObjectAVLTreeSet::new, false));
suite.addTest(suite("RBTreeSet_Null", T -> new ObjectRBTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true));
suite.addTest(suite("AVLTreeSet_Null", T -> new ObjectAVLTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true));
suite.addTest(navigableSuite("RBTreeSet_NonNull", ObjectRBTreeSet::new, false));
suite.addTest(navigableSuite("AVLTreeSet_NonNull", ObjectAVLTreeSet::new, false));
suite.addTest(navigableSuite("RBTreeSet_Null", T -> new ObjectRBTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true));
suite.addTest(navigableSuite("AVLTreeSet_Null", T -> new ObjectAVLTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true));
return suite;
}
public static Test suite(String name, Function<String[], Set<String>> factory, boolean allowNull)
{
public static Test suite(String name, Function<String[], Set<String>> factory, boolean allowNull) {
SetTestSuiteBuilder<String> generator = SetTestSuiteBuilder.using(new TestStringSetGenerator() {
@Override
protected Set<String> create(String[] elements) { return factory.apply(elements); }
@ -53,17 +55,23 @@ public class ObjectSetTests extends TestCase
return generator.createTestSuite();
}
public static Test navigableSuite(String name, Function<String[], NavigableSet<String>> factory, boolean allowNull) {
SetTestSuiteBuilder<String> generator = NavigableSetTestSuiteBuilder.using(new TestStringSortedSetGenerator() {
@Override
protected NavigableSet<String> create(String[] elements) { return factory.apply(elements); }
}).named(name).withFeatures(CollectionSize.ANY, SetFeature.GENERAL_PURPOSE);
if(allowNull) generator.withFeatures(CollectionFeature.ALLOWS_NULL_VALUES);
return generator.createTestSuite();
}
public static Test immutableSuite(String name, Function<String[], Set<String>> factory)
{
public static Test immutableSuite(String name, Function<String[], Set<String>> factory) {
return SetTestSuiteBuilder.using(new TestStringSetGenerator() {
@Override
protected Set<String> create(String[] elements) { return factory.apply(elements); }
}).named(name).withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES).createTestSuite();
}
private static class Strategy implements ObjectStrategy<String>
{
private static class Strategy implements ObjectStrategy<String> {
static final Strategy INSTANCE = new Strategy();
@Override
@ -75,6 +83,5 @@ public class ObjectSetTests extends TestCase
public boolean equals(String key, String value) {
return Objects.equals(key, value);
}
}
}
}