You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2017/01/13 22:36:03 UTC
svn commit: r1778675 - in
/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util:
CopyOnWriteObjHashSet.java CopyOnWriteOrderedFsSet_array.java
OrderedFsSet_array.java
Author: schor
Date: Fri Jan 13 22:36:03 2017
New Revision: 1778675
URL: http://svn.apache.org/viewvc?rev=1778675&view=rev
Log:
[UIMA-5250] make iterators and other things use the copy on write object
Modified:
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteObjHashSet.java
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteOrderedFsSet_array.java
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/OrderedFsSet_array.java
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteObjHashSet.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteObjHashSet.java?rev=1778675&r1=1778674&r2=1778675&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteObjHashSet.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteObjHashSet.java Fri Jan 13 22:36:03 2017
@@ -19,6 +19,7 @@
package org.apache.uima.internal.util;
import java.util.Iterator;
+import java.util.NoSuchElementException;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.impl.CopyOnWriteIndexPart;
@@ -91,7 +92,45 @@ public class CopyOnWriteObjHashSet<T> im
}
public Iterator<T> iterator() {
- return ohs.iterator();
+ return new Iterator<T>() {
+ /**
+ * Keep this always pointing to a non-0 entry, or
+ * if not valid, outside the range
+ */
+ protected int curPosition = moveToNextFilled(0);
+
+ @Override
+ public final boolean hasNext() {
+ return curPosition < getCapacity();
+ }
+
+ @Override
+ public final T next() {
+ if (curPosition >= getCapacity()) {
+ throw new NoSuchElementException();
+ }
+ try {
+ T r = get(curPosition);
+ curPosition = moveToNextFilled(curPosition + 1);
+ return r;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new NoSuchElementException();
+ }
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ private int moveToPrevious(int position) {
+ if (position >= getCapacity()) {
+ return -1;
+ }
+ return moveToPreviousFilled(position - 1);
+ }
+
+ };
}
/**
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteOrderedFsSet_array.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteOrderedFsSet_array.java?rev=1778675&r1=1778674&r2=1778675&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteOrderedFsSet_array.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/CopyOnWriteOrderedFsSet_array.java Fri Jan 13 22:36:03 2017
@@ -19,21 +19,24 @@
package org.apache.uima.internal.util;
import java.util.Collection;
+import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
+import java.util.NoSuchElementException;
import java.util.SortedSet;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
+import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.uima.cas.impl.CopyOnWriteIndexPart;
+import org.apache.uima.internal.util.OrderedFsSet_array.SubSet;
import org.apache.uima.jcas.cas.TOP;
/**
* implements OrderedFsSet_array partially, for iterator use
- * Delegates generated by Eclipse
*/
public class CopyOnWriteOrderedFsSet_array implements NavigableSet<TOP>, CopyOnWriteIndexPart {
@@ -42,6 +45,7 @@ public class CopyOnWriteOrderedFsSet_arr
final public Comparator<TOP> comparatorWithoutID;
+ final Supplier<OrderedFsSet_array> sosa = () -> set;
public CopyOnWriteOrderedFsSet_array(OrderedFsSet_array original) {
this.set = original;
@@ -321,25 +325,107 @@ public class CopyOnWriteOrderedFsSet_arr
/**
* @see OrderedFsSet_array#iterator()
*/
+// @Override
+// public Iterator<TOP> iterator() {
+// return set.iterator();
+// }
+ /**
+ * @see Iterable#iterator()
+ */
@Override
public Iterator<TOP> iterator() {
- return set.iterator();
- }
+ if (set.isEmpty()) {
+ return Collections.emptyIterator();
+ }
+
+ return new Iterator<TOP>() {
+ private int pos = set.a_firstUsedslot;
+ { incrToNext(); } // non-static initializer code
+
+ @Override
+ public boolean hasNext() {
+ set.processBatch();
+ return pos < set.a_nextFreeslot;
+ }
+
+ @Override
+ public TOP next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+
+ TOP r = set.a[pos++];
+ incrToNext();
+ return r;
+ }
+
+ private void incrToNext() {
+ while (pos < set.a_nextFreeslot) {
+ if (set.a[pos] != null) {
+ break;
+ }
+ pos ++;
+ }
+ }
+ };
+ }
+
/**
* @see OrderedFsSet_array#descendingSet()
*/
@Override
public NavigableSet<TOP> descendingSet() {
- return set.descendingSet();
+ throw new UnsupportedOperationException(); // unused
}
/**
* @see OrderedFsSet_array#descendingIterator()
*/
+// @Override
+// public Iterator<TOP> descendingIterator() {
+// return set.descendingIterator();
+// }
+ /**
+ * @see NavigableSet#descendingIterator()
+ */
@Override
public Iterator<TOP> descendingIterator() {
- return set.descendingIterator();
+ set.processBatch();
+ return new Iterator<TOP>() {
+ private int pos = set.a_nextFreeslot - 1; // 2 slots: next free = 2, first slot = 0
+ // 1 slot: next free = 1, first slot = 0
+ // 0 slots: next free = 0; first slot = 0 (not -1)
+ { if (pos >= 0) { // pos is -1 if set is empty
+ decrToNext();
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ return pos >= set.a_firstUsedslot;
+ }
+
+ @Override
+ public TOP next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+
+ TOP r = set.a[pos--];
+ decrToNext();
+ return r;
+ }
+
+ private void decrToNext() {
+ while (pos >= set.a_firstUsedslot) {
+ if (set.a[pos] != null) {
+ break;
+ }
+ pos --;
+ }
+ }
+ };
}
/**
@@ -348,7 +434,7 @@ public class CopyOnWriteOrderedFsSet_arr
@Override
public NavigableSet<TOP> subSet(TOP fromElement, boolean fromInclusive, TOP toElement,
boolean toInclusive) {
- return set.subSet(fromElement, fromInclusive, toElement, toInclusive);
+ return new SubSet(sosa, fromElement, fromInclusive, toElement, toInclusive, false, null);
}
/**
@@ -356,7 +442,10 @@ public class CopyOnWriteOrderedFsSet_arr
*/
@Override
public NavigableSet<TOP> headSet(TOP toElement, boolean inclusive) {
- return set.headSet(toElement, inclusive);
+ if (isEmpty()) {
+ return this;
+ }
+ return new SubSet(sosa, first(), true, toElement, inclusive, false, null);
}
/**
@@ -364,7 +453,10 @@ public class CopyOnWriteOrderedFsSet_arr
*/
@Override
public NavigableSet<TOP> tailSet(TOP fromElement, boolean inclusive) {
- return set.tailSet(fromElement, inclusive);
+ if (isEmpty()) {
+ return this;
+ }
+ return new SubSet(sosa, fromElement, inclusive, last(), true, false, null);
}
/**
@@ -372,7 +464,7 @@ public class CopyOnWriteOrderedFsSet_arr
*/
@Override
public SortedSet<TOP> subSet(TOP fromElement, TOP toElement) {
- return set.subSet(fromElement, toElement);
+ return new SubSet(sosa, fromElement, true, toElement, false, false, null);
}
/**
@@ -380,7 +472,7 @@ public class CopyOnWriteOrderedFsSet_arr
*/
@Override
public SortedSet<TOP> headSet(TOP toElement) {
- return set.headSet(toElement);
+ return headSet(toElement, false);
}
/**
@@ -388,7 +480,7 @@ public class CopyOnWriteOrderedFsSet_arr
*/
@Override
public SortedSet<TOP> tailSet(TOP fromElement) {
- return set.tailSet(fromElement);
+ return tailSet(fromElement, true);
}
/**
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/OrderedFsSet_array.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/OrderedFsSet_array.java?rev=1778675&r1=1778674&r2=1778675&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/OrderedFsSet_array.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/internal/util/OrderedFsSet_array.java Fri Jan 13 22:36:03 2017
@@ -30,6 +30,7 @@ import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;
+import java.util.function.Supplier;
import org.apache.uima.jcas.cas.TOP;
import org.apache.uima.util.impl.Constants;
@@ -89,12 +90,12 @@ public class OrderedFsSet_array implemen
// }
- private TOP[] a = new TOP[DEFAULT_MIN_SIZE];
+ TOP[] a = new TOP[DEFAULT_MIN_SIZE];
/**
* index of slot at the end which is free, all following slots are free too
*/
- private int a_nextFreeslot = 0;
- private int a_firstUsedslot = 0;
+ int a_nextFreeslot = 0;
+ int a_firstUsedslot = 0;
private final ArrayList<TOP> batch = new ArrayList<>();
@@ -428,7 +429,7 @@ public class OrderedFsSet_array implemen
return sz;
}
- private void processBatch() {
+ void processBatch() {
if (batch.size() != 0) {
// validateA();
doProcessBatch();
@@ -1363,7 +1364,7 @@ public class OrderedFsSet_array implemen
*/
@Override
public NavigableSet<TOP> subSet(TOP fromElement, boolean fromInclusive, TOP toElement, boolean toInclusive) {
- return new SubSet(fromElement, fromInclusive, toElement, toInclusive, false, null);
+ return new SubSet(() -> this, fromElement, fromInclusive, toElement, toInclusive, false, null);
}
/**
@@ -1418,7 +1419,8 @@ public class OrderedFsSet_array implemen
* only used to create iterators over that subset
* -- no insert/delete
*/
- public class SubSet implements NavigableSet<TOP> {
+ public static class SubSet implements NavigableSet<TOP> {
+ final Supplier<OrderedFsSet_array> theSet;
final private TOP fromElement;
final private TOP toElement;
final private boolean fromInclusive;
@@ -1432,12 +1434,16 @@ public class OrderedFsSet_array implemen
private int sizeSubSet = -1; // lazy - computed on first ref
- SubSet(TOP fromElement, boolean fromInclusive, TOP toElement, boolean toInclusive) {
- this(fromElement, fromInclusive, toElement, toInclusive, true, comparatorWithID);
+ private OrderedFsSet_array theSet() {
+ return theSet.get();
}
- SubSet(TOP fromElement, boolean fromInclusive, TOP toElement, boolean toInclusive, boolean doTest, Comparator<TOP> comparator) {
-
+ SubSet(Supplier<OrderedFsSet_array> theSet, TOP fromElement, boolean fromInclusive, TOP toElement, boolean toInclusive) {
+ this(theSet, fromElement, fromInclusive, toElement, toInclusive, true, theSet.get().comparatorWithID);
+ }
+
+ SubSet(Supplier<OrderedFsSet_array> theSet, TOP fromElement, boolean fromInclusive, TOP toElement, boolean toInclusive, boolean doTest, Comparator<TOP> comparator) {
+ this.theSet = theSet;
this.fromElement = fromElement;
this.toElement = toElement;
this.fromInclusive = fromInclusive;
@@ -1445,23 +1451,24 @@ public class OrderedFsSet_array implemen
if (doTest && comparator.compare(fromElement, toElement) > 0) {
throw new IllegalArgumentException();
}
- processBatch();
- firstPosInRange = fromInclusive ? ceilingPos(fromElement) : higherPos(fromElement);
- lastPosInRange = toInclusive ? floorPos(toElement) : lowerPos(toElement);
+ OrderedFsSet_array s = theSet();
+ theSet().processBatch();
+ firstPosInRange = fromInclusive ? s.ceilingPos(fromElement) : s.higherPos(fromElement);
+ lastPosInRange = toInclusive ? s.floorPos(toElement) : s.lowerPos(toElement);
// lastPosInRange can be LT firstPosition if fromInclusive is false
// In this case, the subset is empty
if (lastPosInRange < firstPosInRange) {
firstElementInRange = null;
lastElementInRange = null;
} else {
- firstElementInRange = a[firstPosInRange];
- lastElementInRange = a[lastPosInRange];
+ firstElementInRange = s.a[firstPosInRange];
+ lastElementInRange = s.a[lastPosInRange];
}
}
@Override
public Comparator<? super TOP> comparator() {
- return comparatorWithID;
+ return theSet().comparatorWithID;
}
@Override
@@ -1502,7 +1509,7 @@ public class OrderedFsSet_array implemen
if (!isInRange(fs)) {
return false;
}
- return OrderedFsSet_array.this.contains(o);
+ return theSet().contains(o);
}
@Override
@@ -1561,7 +1568,7 @@ public class OrderedFsSet_array implemen
return lastElementInRange;
}
// in range
- return OrderedFsSet_array.this.lower(fs);
+ return theSet().lower(fs);
}
@Override
@@ -1578,7 +1585,7 @@ public class OrderedFsSet_array implemen
return lastElementInRange;
}
- return OrderedFsSet_array.this.floor(fs);
+ return theSet().floor(fs);
}
@Override
@@ -1592,7 +1599,7 @@ public class OrderedFsSet_array implemen
return firstElementInRange;
}
- return OrderedFsSet_array.this.ceiling(fs);
+ return theSet().ceiling(fs);
}
@Override
@@ -1605,7 +1612,7 @@ public class OrderedFsSet_array implemen
return firstElementInRange;
}
- return OrderedFsSet_array.this.higher(fs);
+ return theSet().higher(fs);
}
@Override
@@ -1637,14 +1644,14 @@ public class OrderedFsSet_array implemen
throw new NoSuchElementException();
}
- TOP r = a[pos++];
+ TOP r = theSet().a[pos++];
incrToNext();
return r;
}
private void incrToNext() {
while (pos <= lastPosInRange) {
- if (a[pos] != null) {
+ if (theSet().a[pos] != null) {
break;
}
pos ++;
@@ -1677,14 +1684,14 @@ public class OrderedFsSet_array implemen
throw new NoSuchElementException();
}
- TOP r = a[pos--];
+ TOP r = theSet().a[pos--];
decrToNext();
return r;
}
private void decrToNext() {
while (pos >= firstPosInRange) {
- if (a[pos] != null) {
+ if (theSet().a[pos] != null) {
break;
}
pos --;
@@ -1699,7 +1706,7 @@ public class OrderedFsSet_array implemen
if (!isInRange(fromElement1) || !isInRange(toElement1)) {
throw new IllegalArgumentException();
}
- return OrderedFsSet_array.this.subSet(fromElement1, fromInclusive1, toElement1, toInclusive1);
+ return theSet().subSet(fromElement1, fromInclusive1, toElement1, toInclusive1);
}
@Override
@@ -1728,19 +1735,19 @@ public class OrderedFsSet_array implemen
}
private boolean isGtLast(TOP fs) {
- return comparatorWithID.compare(fs, lastElementInRange) > 0;
+ return theSet().comparatorWithID.compare(fs, lastElementInRange) > 0;
}
private boolean isGeLast(TOP fs) {
- return comparatorWithID.compare(fs, lastElementInRange) >= 0;
+ return theSet().comparatorWithID.compare(fs, lastElementInRange) >= 0;
}
private boolean isLtFirst(TOP fs) {
- return comparatorWithID.compare(fs, firstElementInRange) < 0;
+ return theSet().comparatorWithID.compare(fs, firstElementInRange) < 0;
}
private boolean isLeFirst(TOP fs) {
- return comparatorWithID.compare(fs, firstElementInRange) <= 0;
+ return theSet().comparatorWithID.compare(fs, firstElementInRange) <= 0;
}
private boolean isInRange(TOP fs) {
@@ -1751,7 +1758,7 @@ public class OrderedFsSet_array implemen
if (firstElementInRange == null) {
return false;
}
- int r = comparatorWithID.compare(fs, firstElementInRange);
+ int r = theSet().comparatorWithID.compare(fs, firstElementInRange);
return fromInclusive ? (r >= 0) : (r > 0);
}
@@ -1759,7 +1766,7 @@ public class OrderedFsSet_array implemen
if (lastElementInRange == null) {
return false;
}
- int r = comparatorWithID.compare(fs, lastElementInRange);
+ int r = theSet().comparatorWithID.compare(fs, lastElementInRange);
return toInclusive ? (r <= 0) : (r < 0);
}
}