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 2015/05/05 05:31:59 UTC

svn commit: r1677731 [1/2] - in /uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/cas/impl/ main/java/org/apache/uima/internal/util/ main/java/org/apache/uima/internal/util/rb_trees/ test/java/org/apache/uima/cas/impl/ test/java/org/apache/ui...

Author: schor
Date: Tue May  5 03:31:59 2015
New Revision: 1677731

URL: http://svn.apache.org/r1677731
Log:
[UIMA-4379][UIMA-4352] also remove two unused classes

Added:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntIteratorImplBase.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4bag.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4set.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4sorted.java
Removed:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSVectorIndex.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/LowLevelIteratorWrapper.java
Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIteratorImplBase.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSLeafIndexImpl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSRBTSetIndex.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/ComparableIntPointerIterator.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/CompIntArrayRBT.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/Int2IntRBT.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/IntArrayRBT.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/IntArrayRBTcommon.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/UnambiguousIteratorTest.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/FilteredIteratorTest.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IndexComparitorTest.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/IteratorTest.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/SubiteratorTest.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntArrayIndex.java Tue May  5 03:31:59 2015
@@ -19,8 +19,6 @@
 
 package org.apache.uima.cas.impl;
 
-import java.util.NoSuchElementException;
-
 import org.apache.uima.cas.FeatureStructure;
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.admin.FSIndexComparator;
@@ -36,162 +34,20 @@ import org.apache.uima.internal.util.Int
  */
 public class FSIntArrayIndex<T extends FeatureStructure> extends FSLeafIndexImpl<T> {
 
-  private class IntIterator4sorted implements ComparableIntPointerIterator, LowLevelIterator {
-
-    private int itPos;
-
-    private IntComparator comp;
-
-    private int modificationSnapshot; // to catch illegal modifications
-
-    private int[] detectIllegalIndexUpdates; // shared copy with Index Repository
-
-    private int typeCode;
-
-    public boolean isConcurrentModification() {
-      return modificationSnapshot != detectIllegalIndexUpdates[typeCode];
-    }
-
-    public void resetConcurrentModification() {
-      modificationSnapshot = detectIllegalIndexUpdates[typeCode];
-    }
-
-    private IntIterator4sorted() {
-      super();
-      this.itPos = 0;
-    }
-
-    private IntIterator4sorted(IntComparator comp) {
-      this();
-      this.comp = comp;
-    }
-
-    public boolean isValid() {
-      return ((this.itPos >= 0) && (this.itPos < FSIntArrayIndex.this.index.size()));
-    }
-
-    public void moveToFirst() {
-      this.itPos = 0;
-    }
-
-    public void moveToLast() {
-      this.itPos = FSIntArrayIndex.this.index.size() - 1;
-    }
-
-    public void moveToNext() {
-      ++this.itPos;
-    }
-
-    public void moveToPrevious() {
-      --this.itPos;
-    }
-
-    public int ll_get() {
-      if (!isValid()) {
-        throw new NoSuchElementException();
-      }
-      return FSIntArrayIndex.this.index.get(this.itPos);
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#copy()
-     */
-    public Object copy() {
-      IntIterator4sorted copy = new IntIterator4sorted(this.comp);
-      copy.itPos = this.itPos;
-      return copy;
-    }
-
-    /**
-     * @see java.lang.Comparable#compareTo(Object)
-     */
-    public int compareTo(Object o) throws NoSuchElementException {
-      return this.comp.compare(get(), ((IntIterator4sorted) o).get());
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#moveTo(int)
-     */
-    public void moveTo(int i) {
-      final int position = find(i);
-      boolean found = false;
-      if (position >= 0) {
-        this.itPos = position;
-        found = true;
-      } else {  // not found
-        this.itPos = -(position + 1);
-      }
-      
-      // https://issues.apache.org/jira/browse/UIMA-4094
-      // make sure you go to earliest one
-      if (!found || !isValid()) {
-        // this means the moveTo found the insert point at the end of the index
-        // so just return invalid, since there's no way to return an insert point for a position
-        // that satisfies the FS at that position is greater than fs  
-        return;
-      }    
-      // Go back until we find a FS that is really smaller
-      while (true) {
-        moveToPrevious();
-        if (isValid()) {
-          int prev = get();
-          if (compare(prev, i) != 0) {
-            moveToNext(); // go back
-            break;
-          }
-        } else {
-          moveToFirst();  // went to before first, so go back to 1st
-          break;
-        }
-      }
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.uima.cas.impl.LowLevelIterator#ll_get()
-     */
-    public int get() throws NoSuchElementException {
-      return ll_get();
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.uima.cas.impl.LowLevelIterator#moveToNext()
-     */
-    public void inc() {
-      moveToNext();
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.uima.cas.impl.LowLevelIterator#moveToPrevious()
-     */
-    public void dec() {
-      moveToPrevious();
-    }
-
-    public int ll_indexSize() {
-      return FSIntArrayIndex.this.size();
-    }
-
-    public LowLevelIndex ll_getIndex() {
-      return FSIntArrayIndex.this;
-    }
-
-  }
-
   // The index, a vector of FS references.
-  final private IntVector index;
+  final private IntVector indexIntVector;
   
   final private int initialSize;
+  
+  final private boolean isAnnotationIndex;
+    
+  private IntComparator annotationIntComparator = null; // lazy init because index repo not set up initially
 
-  FSIntArrayIndex(CASImpl cas, Type type, int initialSize, int indexType) {
+  FSIntArrayIndex(CASImpl cas, Type type, int initialSize, int indexType, boolean isAnnotationIndex) {
     super(cas, type, indexType);
     this.initialSize = initialSize;
-    this.index = new IntVector(initialSize);
+    this.indexIntVector = new IntVector(initialSize);
+    this.isAnnotationIndex = isAnnotationIndex;
   }
 
   /**
@@ -203,15 +59,15 @@ public class FSIntArrayIndex<T extends F
   }
 
   IntVector getVector() {
-    return this.index;
+    return this.indexIntVector;
   }
 
   public void flush() {
     // do this way to reset size if it grew
-    if (this.index.size() > this.initialSize) {
-      this.index.resetSize(initialSize);
+    if (this.indexIntVector.size() > this.initialSize) {
+      this.indexIntVector.resetSize(initialSize);
     } else {
-      this.index.removeAllElements();
+      this.indexIntVector.removeAllElements();
     }
   }
 
@@ -222,18 +78,18 @@ public class FSIntArrayIndex<T extends F
 
   public final boolean insert(int fs) {
     // First, check if we can insert at the end.
-    final int[] indexArray = this.index.getArray();
-    final int length = this.index.size();
+    final int[] indexArray = this.indexIntVector.getArray();
+    final int length = this.indexIntVector.size();
     
     if (length == 0) {
-      this.index.add(fs);
+      this.indexIntVector.add(fs);
       return true;
     }
     final int last = indexArray[length - 1];
     // can't use compare <= because the = implies (depending on IS_ALLOW_DUP_A...)
     //   more work to find the EQ one, or not
     if (compare(last, fs) < 0) {
-      this.index.add(fs);
+      this.indexIntVector.add(fs);
       return true;
     }
     
@@ -246,31 +102,31 @@ public class FSIntArrayIndex<T extends F
       int pos2 = refineToExactFsSearch(fs, pos);
       if (pos2 < 0) { 
         // the exact match wasn't found, OK to add
-        this.index.add(pos + 1, fs);
+        this.indexIntVector.add(pos + 1, fs);
       }
     } else if (pos >= 0) {
-      this.index.add(pos + 1, fs);
+      this.indexIntVector.add(pos + 1, fs);
     }
     else {
-      this.index.add(-(pos + 1), fs);
+      this.indexIntVector.add(-(pos + 1), fs);
     }
     return true;
   }
 
   final boolean insert(int fs, int count) {
     // First, check if we can insert at the end.
-    final int[] indexArray = this.index.getArray();
-    final int length = this.index.size();
+    final int[] indexArray = this.indexIntVector.getArray();
+    final int length = this.indexIntVector.size();
     
     if (length == 0) {
-      this.index.multiAdd(fs, count);
+      this.indexIntVector.multiAdd(fs, count);
       return true;
     }
     final int last = indexArray[length - 1];
     // can't use compare <= because the = implies (depending on IS_ALLOW_DUP_A...)
     //   more work to find the EQ one, or not
     if (compare(last, fs) < 0) {
-      this.index.multiAdd(fs, count);
+      this.indexIntVector.multiAdd(fs, count);
       return true;
     }
     
@@ -283,13 +139,13 @@ public class FSIntArrayIndex<T extends F
       int pos2 = refineToExactFsSearch(fs, pos);
       if (pos2 < 0) { 
         // the exact match wasn't found, OK to add
-        this.index.multiAdd(pos + 1, fs, count);
+        this.indexIntVector.multiAdd(pos + 1, fs, count);
       }
     } else if (pos >= 0) {
-      this.index.multiAdd(pos + 1, fs, count);
+      this.indexIntVector.multiAdd(pos + 1, fs, count);
     }
     else {
-      this.index.multiAdd(-(pos + 1), fs, count);
+      this.indexIntVector.multiAdd(-(pos + 1), fs, count);
     }
     return true;
   }
@@ -305,7 +161,7 @@ public class FSIntArrayIndex<T extends F
    * @return index of an arbitrary FS that matches on the compare function or a negative number of insertion point if not found
    */
   private final int find(int fsRef) {
-    return binarySearch(this.index.getArray(), fsRef, 0, this.index.size());
+    return binarySearch(this.indexIntVector.getArray(), fsRef, 0, this.indexIntVector.size());
   }
   
   /**
@@ -329,7 +185,7 @@ public class FSIntArrayIndex<T extends F
     while (true) {
       pos --;
       if (pos >= 0) {
-        int prev = index.get(pos);
+        int prev = indexIntVector.get(pos);
         if (compare(prev, fsRef) != 0) {
           pos++; // go back
           break;
@@ -423,7 +279,7 @@ public class FSIntArrayIndex<T extends F
   //   the same address as fsRef, or
   //   -1 if not found
   private final int refineToExactFsSearch(int fsRef, int startingPos) {
-    final int[] array = this.index.getArray();
+    final int[] array = this.indexIntVector.getArray();
     // search down and up for == fsRef, while key values ==
     for (int movingPos = startingPos; movingPos >= 0; movingPos --) {
       final int v = array[movingPos];
@@ -435,7 +291,7 @@ public class FSIntArrayIndex<T extends F
       }
     }
     // search up
-    final int lenArray = this.index.size();
+    final int lenArray = this.indexIntVector.size();
     for (int movingPos = startingPos + 1; movingPos < lenArray; movingPos ++) {
       final int v = array[movingPos];
       if (v == fsRef) {
@@ -448,20 +304,19 @@ public class FSIntArrayIndex<T extends F
     return -1;
   }
 
-  public ComparableIntPointerIterator pointerIterator(IntComparator comp,
-          int[] detectIllegalIndexUpdates, int typeCode) {
-    IntIterator4sorted ivi = new IntIterator4sorted(comp);
-    ivi.modificationSnapshot = detectIllegalIndexUpdates[typeCode];
-    ivi.detectIllegalIndexUpdates = detectIllegalIndexUpdates;
-    ivi.typeCode = typeCode;
-    return ivi;
+  /**
+   * The comp value is only used when ordering iterators in type/subtype collections
+   */
+  public ComparableIntPointerIterator<T> pointerIterator(
+      IntComparator comp, int[] detectIllegalIndexUpdates, int typeCode) {
+    return new IntIterator4sorted<T>(this, detectIllegalIndexUpdates, comp);
   }
 
   /**
    * @see org.apache.uima.cas.impl.FSLeafIndexImpl#refIterator()
    */
   protected IntPointerIterator refIterator() {
-    return new IntIterator4sorted();
+    return new IntIterator4sorted<T>(this, null);  // null means no detectIllegalIndexUpdates checking
   }
 
   /*
@@ -470,20 +325,15 @@ public class FSIntArrayIndex<T extends F
    * @see org.apache.uima.cas.impl.LowLevelIndex#ll_iterator()
    */
   public LowLevelIterator ll_iterator() {
-    return new IntIterator4sorted();
+    return new IntIterator4sorted<T>(this, null);  // null means no detectIllegalIndexUpdates checking
   }
 
   /**
    * @see org.apache.uima.cas.impl.FSLeafIndexImpl#refIterator(int)
    */
   protected IntPointerIterator refIterator(int fsAddr) {
-    IntIterator4sorted it = new IntIterator4sorted();
-    final int pos = findLeftmost(fsAddr);
-    if (pos >= 0) {
-      it.itPos = pos;
-    } else {
-      it.itPos = -(pos + 1);
-    }
+    IntIterator4sorted<T> it = new IntIterator4sorted<T>(this, null);
+    it.moveTo(fsAddr);
     return it;
   }
 
@@ -506,7 +356,7 @@ public class FSIntArrayIndex<T extends F
     final int resultAddr = find(fsRef);
     // If found, create new FS to return.
     if (resultAddr >= 0) {
-      int foundFsRef = this.index.get(resultAddr);
+      int foundFsRef = this.indexIntVector.get(resultAddr);
       return (fsRef == foundFsRef) ? 
           fs : 
           fsi.getCASImpl().createFS(foundFsRef);
@@ -519,7 +369,7 @@ public class FSIntArrayIndex<T extends F
    * @see org.apache.uima.cas.FSIndex#size()
    */
   public int size() {
-    return this.index.size();
+    return this.indexIntVector.size();
   }
 
   /**
@@ -552,13 +402,24 @@ public class FSIntArrayIndex<T extends F
     if (pos < 0) {
       return false;  // not in index
     }
-    this.index.remove(pos);
+    this.indexIntVector.remove(pos);
     return true;
   }
 
   @Override
   protected void bulkAddTo(IntVector v) {
-    v.addBulk(index);
+    v.addBulk(indexIntVector);
+  }
+  
+  @Override
+  public int compare(int fs1, int fs2) {
+    if (isAnnotationIndex) {
+      if (annotationIntComparator == null) {
+        annotationIntComparator = lowLevelCAS.indexRepository.getAnnotationIntComparator();
+      }
+      return annotationIntComparator.compare(fs1, fs2);
+    }
+    return super.compare(fs1, fs2);
   }
 
 }

Added: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntIteratorImplBase.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntIteratorImplBase.java?rev=1677731&view=auto
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntIteratorImplBase.java (added)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIntIteratorImplBase.java Tue May  5 03:31:59 2015
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.cas.impl;
+
+import java.util.ConcurrentModificationException;
+import java.util.NoSuchElementException;
+
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.internal.util.ComparableIntPointerIterator;
+
+/**
+ * Base class for int Iterators over indexes.
+ * 
+ * There are 3 styles of indexes, one for Sorted, one for Sets and one for Bag.
+ *   There is a separate int iterator for each of these styles:
+ *     IntIterator4bag
+ *     IntIterator4sorted
+ *     IntArrayRBTIterator
+ *     
+ *   There are also specialized int iterators:
+ *     SnapshotPointerIterator - iterates over a one-time flat snapshot
+ *     FlatIterator - iterates over a flattened array of Java Objects (not ints) - so this is
+ *       excluded from this discussion because this class is only for int iterators.
+ *    
+ * This class is the superclass of the 3 standard int iterators, and the SnapshotPointerIterator.
+ *   
+ */
+public abstract class FSIntIteratorImplBase<T extends FeatureStructure> 
+           implements ComparableIntPointerIterator<T>, LowLevelIterator {
+
+  final private FSLeafIndexImpl<T> fsLeafIndexImpl;
+  
+  private int modificationSnapshot; // to catch illegal modifications
+
+  /**
+   * This is a ref to the shared value in the FSIndexRepositoryImpl
+   * OR it may be null which means skip the checking (done for some internal routines
+   * which know they are not updating the index, and assume no other thread is)
+   */
+  final protected int[] detectIllegalIndexUpdates; // shared copy with Index Repository
+
+  final private int typeCode;
+    
+  public void checkConcurrentModification() {
+    if ((null != this.detectIllegalIndexUpdates) && 
+        (this.modificationSnapshot != this.detectIllegalIndexUpdates[this.typeCode])) {
+      throw new ConcurrentModificationException();
+    }
+  }
+
+  public void resetConcurrentModification() {
+    this.modificationSnapshot = (null == this.detectIllegalIndexUpdates) ? 0 : this.detectIllegalIndexUpdates[this.typeCode];
+  }
+
+  /**
+   * 
+   * @param index
+   * @param detectIllegalIndexUpdates may be null
+   */
+  public FSIntIteratorImplBase(FSLeafIndexImpl<T> fsLeafIndexImpl, int[] detectIllegalIndexUpdates) {
+    this.fsLeafIndexImpl = fsLeafIndexImpl;
+    this.typeCode = (detectIllegalIndexUpdates == null) ? 0 : fsLeafIndexImpl.getTypeCode();
+    this.detectIllegalIndexUpdates = detectIllegalIndexUpdates;
+    resetConcurrentModification();
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.uima.internal.util.IntPointerIterator#inc()
+   */
+  @Override
+  public void inc() {
+    moveToNext(); 
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.uima.internal.util.IntPointerIterator#dec()
+   */
+  @Override
+  public void dec() {
+    moveToPrevious();
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.uima.cas.impl.LowLevelIterator#ll_getIndex()
+   */
+  @Override
+  public LowLevelIndex ll_getIndex() {
+    return fsLeafIndexImpl;
+  }
+
+  /* (non-Javadoc)
+   * @see java.lang.Comparable#compareTo(java.lang.Object)
+   * 
+   * Only the sorted iterator(s) override this
+   */
+  @Override
+  public int compareTo(FSIntIteratorImplBase<T> o) {
+    throw new UnsupportedOperationException();
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.uima.cas.impl.LowLevelIterator#ll_get()
+   */
+  @Override
+  public int ll_get() throws NoSuchElementException {
+    return get();
+  }
+}

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIteratorImplBase.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIteratorImplBase.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIteratorImplBase.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSIteratorImplBase.java Tue May  5 03:31:59 2015
@@ -19,12 +19,8 @@
 
 package org.apache.uima.cas.impl;
 
-import java.util.Comparator;
-
 import org.apache.uima.cas.FSIterator;
 import org.apache.uima.cas.FeatureStructure;
-import org.apache.uima.cas.text.AnnotationFS;
-import org.apache.uima.cas.text.AnnotationIndex;
 
 /**
  * Base class for FSIterator implementations. Defines the hasNext, next, and remove methods required
@@ -34,20 +30,20 @@ public abstract class FSIteratorImplBase
 
   // Jira UIMA-464: add annotation comparator to be able to use Collections.binarySearch() on
   // annotation list.
-  protected static class AnnotationComparator<T extends FeatureStructure> implements Comparator<T> {
-
-    private AnnotationIndex<? extends AnnotationFS> index;  // used only to support compare
-
-    protected AnnotationComparator(AnnotationIndex<? extends FeatureStructure> index) {
-      super();
-      this.index = index;
-    }
-
-    public int compare(T fs1, T fs2) {
-      return this.index.compare(fs1, fs2);
-    }
-
-  }
+//  protected static class AnnotationComparator<T extends FeatureStructure> implements Comparator<T> {
+//
+//    private AnnotationIndex<? extends AnnotationFS> index;  // used only to support compare
+//
+//    protected AnnotationComparator(AnnotationIndex<? extends FeatureStructure> index) {
+//      super();
+//      this.index = index;
+//    }
+//
+//    public int compare(T fs1, T fs2) {
+//      return this.index.compare(fs1, fs2);
+//    }
+//
+//  }
 
   /*
    * (non-Javadoc)

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSLeafIndexImpl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSLeafIndexImpl.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSLeafIndexImpl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSLeafIndexImpl.java Tue May  5 03:31:59 2015
@@ -19,8 +19,9 @@
 
 package org.apache.uima.cas.impl;
 
+import java.util.Comparator;
+
 import org.apache.uima.cas.CAS;
-import org.apache.uima.cas.FSIndex;
 import org.apache.uima.cas.FSIterator;
 import org.apache.uima.cas.Feature;
 import org.apache.uima.cas.FeatureStructure;
@@ -38,9 +39,9 @@ import org.apache.uima.internal.util.Int
  * 
  * @param <T> the Java cover class type for this index, passed along to (wrapped) iterators producing Java cover classes
  */
-public abstract class FSLeafIndexImpl<T extends FeatureStructure> implements IntComparator, FSIndex<T>, FSIndexImpl {
+public abstract class FSLeafIndexImpl<T extends FeatureStructure> implements Comparator<T>, IntComparator, FSIndexImpl {
 
-  private final int indexType;
+  private final int indexType;  // Sorted, Set, Bag, Default-bag, etc.
 
   // A reference to the low-level CAS.
   final protected CASImpl lowLevelCAS;
@@ -84,6 +85,8 @@ public abstract class FSLeafIndexImpl<T
   private int numKeys;
 
   final private Type type; // The type of this
+  
+  final private int typeCode;
 
   
   
@@ -120,6 +123,7 @@ public abstract class FSLeafIndexImpl<T
     this.indexType = 0; // must do because it's final
     this.lowLevelCAS = null;
     this.type = null;
+    this.typeCode = 0;
   }
 
   /**
@@ -133,6 +137,7 @@ public abstract class FSLeafIndexImpl<T
     this.indexType = indexType;
     this.lowLevelCAS = cas;
     this.type = type;
+    this.typeCode = ((TypeImpl)type).getCode();
   }
 
   abstract boolean insert(int fs);
@@ -140,7 +145,10 @@ public abstract class FSLeafIndexImpl<T
   abstract boolean insert(int fs, int count);  // for bulk addback
 
   /**
-   * @param fs -
+   * @param fs - the Feature Structure to be removed.
+   * Only this exact Feature Structure is removed (this is a stronger test than, for example,
+   * what moveTo(fs) does, where the fs in that case is used as a template).  
+   * It is not an error if this exact Feature Structure is not in an index.
    * @return true if something was removed, false if not found
    */
   abstract boolean remove(int fs);
@@ -149,7 +157,7 @@ public abstract class FSLeafIndexImpl<T
 
   // public abstract ComparableIntIterator iterator(IntComparator comp);
 
-  public abstract ComparableIntPointerIterator pointerIterator(IntComparator comp,
+  public abstract ComparableIntPointerIterator<T> pointerIterator(IntComparator comp,
           int[] detectIllegalIndexUpdates, int typeCode);
 
   public FSIndexComparator getComparator() {
@@ -240,10 +248,22 @@ public abstract class FSLeafIndexImpl<T
     return this.comparator.getLowLevelCAS().getFeatureOffset(((FeatureImpl) feat).getCode());
   }
 
+  /**
+   * Note: may return other than -1 , 0, and 1  (e.g., might return -6)
+   * @param fs1 -
+   * @param fs2 -
+   * @return 0 if equal, < 0 if fs1 < fs2, > 0 if fs1 > fs2
+   */
   public int ll_compare(int ref1, int ref2) {
     return this.compare(ref1, ref2);
   }
 
+  /**
+   * Note: may return other than -1 , 0, and 1  (e.g., might return -6)
+   * @param fs1 -
+   * @param fs2 -
+   * @return 0 if equal, < 0 if fs1 < fs2, > 0 if fs1 > fs2
+   */
   public int compare(int fs1, int fs2) {
     final int[] heap = this.lowLevelCAS.getHeap().heap;
     final int[] localKeyType = this.keyType;
@@ -443,6 +463,10 @@ public abstract class FSLeafIndexImpl<T
   public Type getType() {
     return this.type;
   }
+  
+  int getTypeCode() {
+    return this.typeCode;
+  }
 
   protected abstract IntPointerIterator refIterator();
 
@@ -465,15 +489,19 @@ public abstract class FSLeafIndexImpl<T
   // It would be good to refactor this so that this confusion is eliminated,
   // perhaps by not having this class implement FSIndex
 
-  /**
-   * @see org.apache.uima.cas.FSIndex#iterator()
-   */
-  public FSIterator<T> iterator() {
-    return new FSIteratorWrapper<T>(refIterator(), this.lowLevelCAS);
-  }
+//  /**
+//   * @see org.apache.uima.cas.FSIndex#iterator()
+//   */
+//  public FSIterator<T> iterator() {
+//    return new FSIteratorWrapper<T>(refIterator(), this.lowLevelCAS);
+//  }
 
   /**
    * @see org.apache.uima.cas.FSIndex#iterator(FeatureStructure)
+   * 
+   * This has no callers, and is probably not used.
+   * The iterator it produces is only over one leaf index and
+   * doesn't include Concurrent Modification Exception testing
    */
   public FSIterator<T> iterator(FeatureStructure fs) {
     return new FSIteratorWrapper<T>(refIterator(((FeatureStructureImpl) fs).getAddress()),
@@ -499,44 +527,12 @@ public abstract class FSLeafIndexImpl<T
       return this.ll_iterator();
   }
 
-  @Override
-  public FSIndex<T> withSnapshotIterators() {
-    // should never be called
-    // this is an artifact of the fact that FSLeafIndexImpl implements FSIndex interface
-    //   which seems incorrect?
-    throw new UnsupportedOperationException();
-  }
-  
-  @Override
-  public void fillFlatArray(FeatureStructure[] flatArray) {
-    // should never be called
-    // this is an artifact of the fact that FSLeafIndexImpl implements FSIndex interface
-    //   which seems incorrect?
-    throw new UnsupportedOperationException();  
-  }
-
-  @Override
-  public int getNumberOfTypes() {
-    // should never be called
-    // this is an artifact of the fact that FSLeafIndexImpl implements FSIndex interface
-    //   which seems incorrect?
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public boolean isValidCache(int[] indexUpdateCounts) {
-    // should never be called
-    // this is an artifact of the fact that FSLeafIndexImpl implements FSIndex interface
-    //   which seems incorrect?
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public void captureIndexUpdatecounters(int[] indexUpdateCounts) {
-    // should never be called
-    // this is an artifact of the fact that FSLeafIndexImpl implements FSIndex interface
-    //   which seems incorrect?
-    throw new UnsupportedOperationException();
-  }
+//  @Override
+//  public FSIndex<T> withSnapshotIterators() {
+//    // should never be called
+//    // this is an artifact of the fact that FSLeafIndexImpl implements FSIndex interface
+//    //   which seems incorrect?
+//    throw new UnsupportedOperationException();
+//  }
 
 }

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSRBTSetIndex.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSRBTSetIndex.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSRBTSetIndex.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/FSRBTSetIndex.java Tue May  5 03:31:59 2015
@@ -39,9 +39,10 @@ import org.apache.uima.internal.util.rb_
  * @param <T> the Java cover class type for this index, passed along to (wrapped) iterators producing Java cover classes
  *
  */
-class FSRBTSetIndex<T extends FeatureStructure> extends FSLeafIndexImpl<T> {
+// internal use only
+public class FSRBTSetIndex<T extends FeatureStructure> extends FSLeafIndexImpl<T> {
 
-  private CompIntArrayRBT tree;
+  CompIntArrayRBT tree;
 
   /**
    * Constructor for FSRBTIndex.
@@ -88,23 +89,25 @@ class FSRBTSetIndex<T extends FeatureStr
   }
 
   public IntPointerIterator refIterator() {
-    return this.tree.pointerIterator();
+    return this.tree.pointerIterator(this, null, this);
   }
 
   ComparableIntIterator refIterator(IntComparator comp) {
-    return this.tree.iterator(comp);
+    return (ComparableIntIterator) this.tree.pointerIterator(this, null, comp);
   }
 
-  public ComparableIntPointerIterator pointerIterator(IntComparator comp,
-          int[] detectIllegalIndexUpdates, int typeCode) {
-    return this.tree.pointerIterator(comp, detectIllegalIndexUpdates, typeCode);
+  public ComparableIntPointerIterator<T> pointerIterator(
+      IntComparator comp, int[] detectIllegalIndexUpdates, int typeCode) {
+    return this.tree.pointerIterator(this, detectIllegalIndexUpdates, comp);
   }
 
   /**
    * @see org.apache.uima.cas.impl.FSLeafIndexImpl#refIterator(int)
    */
   protected IntPointerIterator refIterator(int fsCode) {
-    return this.tree.pointerIterator(fsCode);
+    ComparableIntPointerIterator<T> it = this.tree.pointerIterator(this, null, null);
+    it.moveTo(fsCode);
+    return it;
   }
 
   /**
@@ -139,11 +142,11 @@ class FSRBTSetIndex<T extends FeatureStr
    * @see org.apache.uima.cas.impl.LowLevelIndex#ll_iterator()
    */
   public LowLevelIterator ll_iterator() {
-    return new LowLevelIteratorWrapper(this.tree.pointerIterator(), this);
+    return (LowLevelIterator) this.tree.pointerIterator(this, null, this);
   }
 
   /*
-   * (non-Javadoc)
+   * This code is written to remove the exact fs, not just one which matches equal to the argument
    * 
    * @see org.apache.uima.cas.impl.FSLeafIndexImpl#remove(int)
    */
@@ -156,5 +159,10 @@ class FSRBTSetIndex<T extends FeatureStr
   protected void bulkAddTo(IntVector v) {
     throw new UnsupportedOperationException();
   }
+  
+  // For testing only
+  public void setTree(CompIntArrayRBT compIntArrayRBT) {
+    this.tree = compIntArrayRBT;
+  }
 
 }

Added: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4bag.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4bag.java?rev=1677731&view=auto
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4bag.java (added)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4bag.java Tue May  5 03:31:59 2015
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.cas.impl;
+
+import java.util.NoSuchElementException;
+
+import org.apache.uima.cas.FeatureStructure;
+
+class IntIterator4bag<T extends FeatureStructure> extends FSIntIteratorImplBase<T> {
+
+  private int itPos;
+
+  final private FSBagIndex<T> fsBagIndex; // just an optimization, is == to fsLeafIndexImpl from super class, allows dispatch w/o casting
+    
+
+  IntIterator4bag(FSBagIndex<T> fsBagIndex, int[] detectIllegalIndexUpdates) {
+    super(fsBagIndex, detectIllegalIndexUpdates);
+    this.fsBagIndex = fsBagIndex;
+    moveToFirst();
+  }
+
+  public boolean isValid() {
+    return fsBagIndex.isValid(this.itPos);
+  }
+
+  /**
+   * If empty, make position -1 (invalid)
+   */
+  public void moveToFirst() {
+    resetConcurrentModification();
+    this.itPos = fsBagIndex.moveToFirst();
+  }
+
+  /**
+   * If empty, make position -1 (invalid)
+   */
+  public void moveToLast() {
+    resetConcurrentModification();
+    this.itPos = fsBagIndex.moveToLast();
+  }
+
+  public void moveToNext() {
+    checkConcurrentModification(); 
+    this.itPos = fsBagIndex.moveToNext(itPos);
+  }
+
+  public void moveToPrevious() {
+    checkConcurrentModification(); 
+    this.itPos = fsBagIndex.moveToPrevious(itPos);
+  }
+
+  public int get() {
+    if (!isValid()) {
+      throw new NoSuchElementException();
+    }
+    checkConcurrentModification(); 
+    return fsBagIndex.get(itPos);
+  }
+
+  /**
+   * @see org.apache.uima.internal.util.IntPointerIterator#copy()
+   */
+  public Object copy() {
+    IntIterator4bag<T> copy = new IntIterator4bag<T>(this.fsBagIndex, this.detectIllegalIndexUpdates);
+    copy.itPos = this.itPos;
+    return copy;
+  }
+
+  /**
+   * @see org.apache.uima.internal.util.IntPointerIterator#moveTo(int)
+   */
+  public void moveTo(int i) {
+    resetConcurrentModification();
+    this.itPos = fsBagIndex.findLeftmost(i);
+  }
+
+  public int ll_indexSize() {
+    return fsBagIndex.size();
+  }
+
+}
+

Added: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4set.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4set.java?rev=1677731&view=auto
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4set.java (added)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4set.java Tue May  5 03:31:59 2015
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.cas.impl;
+
+import java.util.NoSuchElementException;
+
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.internal.util.IntComparator;
+import org.apache.uima.internal.util.rb_trees.IntArrayRBT;
+
+// Internal use only
+public class IntIterator4set<T extends FeatureStructure> extends FSIntIteratorImplBase<T> {
+  private static final int NIL = 0;
+  
+  private int currentNode;
+  
+  final private IntComparator comp; 
+
+  final private FSRBTSetIndex<T> fsSetIndex; // just an optimization, is == to fsLeafIndexImpl from super class  
+  
+  final private IntArrayRBT intArrayRBTindex;
+  
+  // Internal Use Only
+  public IntIterator4set(FSRBTSetIndex<T> fsSetIndex, int[] detectIllegalIndexUpdates, IntComparator comp) {
+    super(fsSetIndex, detectIllegalIndexUpdates);
+    this.fsSetIndex = fsSetIndex;
+    intArrayRBTindex = fsSetIndex.tree;
+    this.comp = comp;
+    moveToFirst();
+  }
+
+  public boolean isValid() {
+    return this.currentNode != NIL;
+  }
+
+  /**
+   * If empty, make position -1 (invalid)
+   */
+  public void moveToFirst() {
+    resetConcurrentModification();
+    this.currentNode = intArrayRBTindex.getFirstNode();
+  }
+
+  /**
+   * If empty, make position -1 (invalid)
+   */
+  public void moveToLast() {
+    resetConcurrentModification();
+    this.currentNode = intArrayRBTindex.greatestNode;
+  }
+
+  public void moveToNext() {
+    checkConcurrentModification();
+    this.currentNode = intArrayRBTindex.nextNode(this.currentNode);
+  }
+
+  public void moveToPrevious() {
+    checkConcurrentModification(); 
+    this.currentNode = intArrayRBTindex.previousNode(this.currentNode);
+  }
+
+  public int get() {
+    if (!isValid()) {
+      throw new NoSuchElementException();
+    }
+    checkConcurrentModification(); 
+    return intArrayRBTindex.getKeyForNode(this.currentNode);
+  }
+
+  /**
+   * @see org.apache.uima.internal.util.IntPointerIterator#copy()
+   */
+  public Object copy() {
+    IntIterator4set<T> copy = new IntIterator4set<T>(this.fsSetIndex, this.detectIllegalIndexUpdates, this.comp);
+    copy.currentNode = this.currentNode;
+    return copy;
+  }
+
+  /**
+   * @see org.apache.uima.internal.util.IntPointerIterator#moveTo(int)
+   */
+  public void moveTo(int i) {
+    resetConcurrentModification();
+    this.currentNode = intArrayRBTindex.findInsertionPoint(i);
+  }
+
+  public int ll_indexSize() {
+    return fsSetIndex.size();
+  }
+  
+  /* (non-Javadoc)
+   * @see org.apache.uima.cas.impl.FSIntIteratorImplBase#compareTo(org.apache.uima.cas.impl.FSIntIteratorImplBase)
+   */
+  @Override
+  public int compareTo(FSIntIteratorImplBase<T> o) {
+    return this.comp.compare(get(), ((IntIterator4set<T>) o).get());
+  }
+
+}
+

Added: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4sorted.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4sorted.java?rev=1677731&view=auto
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4sorted.java (added)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/IntIterator4sorted.java Tue May  5 03:31:59 2015
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.cas.impl;
+
+import java.util.NoSuchElementException;
+
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.internal.util.IntComparator;
+import org.apache.uima.internal.util.IntVector;
+
+class IntIterator4sorted<T extends FeatureStructure> extends FSIntIteratorImplBase<T> {
+
+  private int itPos;
+
+  final private IntComparator comp;
+
+  final private IntVector indexIntVector;
+  final private FSIntArrayIndex<T> fsIntArrayIndex;  // just an optimization, is == to fsLeafIndexImpl from super class
+  
+  // used when iterating without ordering among iterators
+  IntIterator4sorted(FSIntArrayIndex<T> index, int[] detectIllegalIndexUpdates) {
+    super(index, detectIllegalIndexUpdates);
+    this.indexIntVector = index.getVector();
+    this.fsIntArrayIndex = index;
+    this.itPos = 0;
+    comp = null;
+  }
+
+  IntIterator4sorted(FSIntArrayIndex<T> index, int[] detectIllegalIndexUpdates, IntComparator comp) {
+    super(index, detectIllegalIndexUpdates);
+    this.fsIntArrayIndex = index;
+    this.indexIntVector = index.getVector();
+    this.comp = comp;
+    this.itPos = 0;
+  }
+
+  public boolean isValid() {
+    return ((this.itPos >= 0) && (this.itPos < this.indexIntVector.size()));
+  }
+
+  public void moveToFirst() {
+    resetConcurrentModification();
+    this.itPos = 0;
+  }
+
+  public void moveToLast() {
+    resetConcurrentModification();
+    this.itPos = this.indexIntVector.size() - 1;
+  }
+
+  public void moveToNext() {
+    if (itPos < 0) {
+      return;
+    }
+    checkConcurrentModification();
+    ++this.itPos;
+  }
+
+  public void moveToPrevious() {
+    if (itPos >= this.indexIntVector.size()) {
+      return;
+    }
+    checkConcurrentModification(); 
+    --this.itPos;
+  }
+
+  public int get() {
+    if (!isValid()) {
+      throw new NoSuchElementException();
+    }
+    checkConcurrentModification(); 
+    return this.indexIntVector.get(this.itPos);
+  }
+
+  /**
+   * @see org.apache.uima.internal.util.IntPointerIterator#copy()
+   */
+  public Object copy() {
+    IntIterator4sorted<T> copy = new IntIterator4sorted<T>(this.fsIntArrayIndex, this.detectIllegalIndexUpdates, this.comp);
+    copy.itPos = this.itPos;
+    return copy;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.uima.cas.impl.FSIntIteratorImplBase#compareTo(org.apache.uima.cas.impl.FSIntIteratorImplBase)
+   */
+  @Override
+  public int compareTo(FSIntIteratorImplBase<T> o) {
+    return this.comp.compare(get(), ((IntIterator4sorted<T>) o).get());
+  }
+
+  /**
+   * @see org.apache.uima.internal.util.IntPointerIterator#moveTo(int)
+   */
+  public void moveTo(int i) {
+    resetConcurrentModification();
+    final int pos = fsIntArrayIndex.findLeftmost(i);
+    if (pos >= 0) {
+      itPos = pos;
+    } else {
+      itPos = -(pos + 1);
+    }
+//
+//      
+//      
+//      final int position = find(i);
+//      boolean found = false;
+//      if (position >= 0) {
+//        this.itPos = position;
+//        found = true;
+//      } else {  // not found
+//        this.itPos = -(position + 1);
+//      }
+//      
+//      // https://issues.apache.org/jira/browse/UIMA-4094
+//      // make sure you go to earliest one
+//      if (!found || !isValid()) {
+//        // this means the moveTo found the insert point at the end of the index
+//        // so just return invalid, since there's no way to return an insert point for a position
+//        // that satisfies the FS at that position is greater than fs  
+//        return;
+//      }    
+//      // Go back until we find a FS that is really smaller
+//      while (true) {
+//        moveToPrevious();
+//        if (isValid()) {
+//          int prev = get();
+//          if (compare(prev, i) != 0) {
+//            moveToNext(); // go back
+//            break;
+//          }
+//        } else {
+//          moveToFirst();  // went to before first, so go back to 1st
+//          break;
+//        }
+//      }
+  }
+
+
+
+  public int ll_indexSize() {
+    return indexIntVector.size();
+  }
+
+}
+

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/ComparableIntPointerIterator.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/ComparableIntPointerIterator.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/ComparableIntPointerIterator.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/ComparableIntPointerIterator.java Tue May  5 03:31:59 2015
@@ -19,12 +19,29 @@
 
 package org.apache.uima.internal.util;
 
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.impl.FSIntIteratorImplBase;
+
 /**
- * Interface for a comparable IntListIterator. Two IntIterators are compared according to the
+ * Interface for a comparable IntPointerIterator. 
+ * 
+ * This allows two iterators to be compared with each other.  Two IntIterators are compared according to the
  * element that would be returned by the next call to next().
+ * 
+ * The purpose of this is for keeping multiple iterators, one for each subtype of a type, in a sorted order,
+ * when desiring to iterate over a type and its subtypes, in a combined merged order.
+ * 
+ * This is only needed for iterators over Sorted indexes. Iterators over Bags and Sets have no ordering requirement.
+ * 
+ * (Note, however, that backwards compatibility may require this)
+ * 
+ * All ComparableIntPointerIterators must implement the ConcurrentModificationException mechanism, so that interface is included
+ * 
  */
-public interface ComparableIntPointerIterator extends IntPointerIterator, Comparable {
-  public boolean isConcurrentModification();
+public interface ComparableIntPointerIterator<F extends FeatureStructure> extends IntPointerIterator, Comparable<FSIntIteratorImplBase<F>> {
 
-  public void resetConcurrentModification();
+//  public void checkConcurrentModification();
+//
+//  public void resetConcurrentModification();
+  
 }

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/CompIntArrayRBT.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/CompIntArrayRBT.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/CompIntArrayRBT.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/CompIntArrayRBT.java Tue May  5 03:31:59 2015
@@ -44,146 +44,9 @@ public class CompIntArrayRBT extends Int
     super(initialSize);
     this.comp = comp;
   }
-
-  // Insert a node for key. Returns index of new node if node was inserted, or
-  // index of old node for the key.
-  protected int treeInsert(final int k) {
-    int x = this.root;
-    int y = NIL;
-    int z;
-//    int cv; // Return value of compare().
-    if ((this.greatestNode != NIL) && (this.comp.compare(getKey(this.greatestNode), k) < 0)) {
-      y = this.greatestNode;
-      z = newNode(k);
-      this.greatestNode = z;
-    } else {
-      while (x != NIL) {
-        y = x;
-        final int cv = this.comp.compare(k, getKey(x));
-        if (cv < 0) {
-          x = getLeft(x);
-        } else if (cv > 0) {
-          x = getRight(x);
-        } else { // cv == 0
-          return x;
-        }
-      }
-      // The key was not found, so we create a new node, inserting the
-      // key.
-      z = newNode(k);
-    }
-    if (y == NIL) {
-      this.root = z;
-      this.greatestNode = z;
-      setParent(z, NIL);
-    } else {
-      setParent(z, y);
-      final int cv = this.comp.compare(k, getKey(y));
-      if (cv < 0) {
-        setLeft(y, z);
-      } else {
-        setRight(y, z);
-      }
-    }
-    return z;
+  
+  protected int compare(int v1, int v2) {
+    return this.comp.compare(v1, v2);
   }
-
-  protected int treeInsertWithDups(final int k) {
-    int x = this.root;
-    int y, z;
-    boolean wentLeft = false;
-    if ((this.greatestNode != NIL) && (this.comp.compare(getKey(this.greatestNode), k) <= 0)) {
-      y = this.greatestNode;
-      z = newNode(k);
-      this.greatestNode = z;
-      setParent(z, y);
-      setRight(y, z);
-      return z;
-    }
-    y = NIL;
-    int xKey;
-    while (x != NIL) {
-      y = x;
-      xKey = getKey(x);
-      final int cv = this.comp.compare(k, xKey);
-      if (cv < 0) {
-        x = getLeft(x);
-      } else if (cv > 0) {
-        x = getRight(x);
-      } else { // k == key[x]
-        // Randomly search to the left or right.
-        // if (false) {
-        if (this.rand.nextBoolean()) {
-          wentLeft = true;
-          x = getLeft(x);
-        } else {
-          wentLeft = false;
-          x = getRight(x);
-        }
-      }
-    }
-    z = newNode(k);
-    if (y == NIL) {
-      this.root = z;
-      this.greatestNode = z;
-      setParent(z, NIL);
-    } else {
-      setParent(z, y);
-      final int cv = this.comp.compare(k, getKey(y));
-      if (cv < 0) {
-        setLeft(y, z);
-      } else if (cv > 0) {
-        setRight(y, z);
-      } else { // k == key[y]
-        // Randomly insert node to the left or right.
-        if (wentLeft) {
-          setLeft(y, z);
-        } else {
-          setRight(y, z);
-        }
-      }
-    }
-    // color[root] = black;
-    return z;
-  }
-
-  public int findKey(final int k) {
-    int node = this.root;
-    int cv;
-    while (node != NIL) {
-      cv = this.comp.compare(k, getKey(node));
-      if (cv < 0) {
-        node = getLeft(node);
-      } else if (cv > 0) {
-        node = getRight(node);
-      } else {
-        return node;
-      }
-    }
-    // node == NIL
-    return NIL;
-  }
-
-  public int findInsertionPoint(final int k) {
-    int node = this.root;
-    int found = this.root;
-    int cv = 0;
-    while (node != NIL) {
-      found = node;
-      cv = this.comp.compare(k, getKey(node));
-      if (cv < 0) {
-        node = getLeft(node);
-      } else if (cv > 0) {
-        node = getRight(node);
-      } else {
-        return node;
-      }
-    }
-    // node == NIL
-    if (cv > 0) {
-      return nextNode(found);
-    }
-    return found;
-  }
-
+  
 }

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/Int2IntRBT.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/Int2IntRBT.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/Int2IntRBT.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/Int2IntRBT.java Tue May  5 03:31:59 2015
@@ -63,7 +63,7 @@ public class Int2IntRBT extends IntArray
       if (!isValid()) {
         throw new NoSuchElementException();
       }
-      return Int2IntRBT.this.getKey(this.currentNode);
+      return Int2IntRBT.this.getKeyForNode(this.currentNode);
     }
     
     public int getValue() {
@@ -137,7 +137,7 @@ public class Int2IntRBT extends IntArray
         throw new NoSuchElementException();
       }
       this.currentNode = (this.currentNode == NIL) ? getFirstNode() : nextNode(this.currentNode);
-      return Int2IntRBT.this.getKey(this.currentNode);
+      return Int2IntRBT.this.getKeyForNode(this.currentNode);
     }
 
     /**
@@ -154,7 +154,7 @@ public class Int2IntRBT extends IntArray
       if (!hasPrevious()) {
         throw new NoSuchElementException();
       }
-      final int currentKey = Int2IntRBT.this.getKey(this.currentNode);
+      final int currentKey = Int2IntRBT.this.getKeyForNode(this.currentNode);
       if (this.currentNode == getFirstNode()) {
         this.currentNode = NIL;
       } else {
@@ -207,17 +207,10 @@ public class Int2IntRBT extends IntArray
   
   public Int2IntRBT copy() {
     Int2IntRBT c = new Int2IntRBT();
-    if (useklrp) {
-      c.klrp = klrp.clone();
-      c.klrp1 = (klrp1 != null) ? klrp1.clone() : null;
-      c.klrp2 = (klrp2 != null) ? klrp2.clone() : null;
-      c.klrp3 = (klrp3 != null) ? klrp3.clone() : null;
-    } else {
-      c.key = key.clone();
-      c.left = left.clone();
-      c.right = right.clone();
-      c.parent = parent.clone();
-    }
+    c.klrp = klrp.clone();
+    c.klrp1 = (klrp1 != null) ? klrp1.clone() : null;
+    c.klrp2 = (klrp2 != null) ? klrp2.clone() : null;
+    c.klrp3 = (klrp3 != null) ? klrp3.clone() : null;
     c.color = color.clone();
     c.values = values.clone();
     c.root = root;
@@ -249,7 +242,7 @@ public class Int2IntRBT extends IntArray
    * @return negative index if key is found
    */
   private int treeInsert(final int k, final int v) {
-    if ((this.greatestNode != NIL) && (getKey(this.greatestNode) < k)) {
+    if ((this.greatestNode != NIL) && (getKeyForNode(this.greatestNode) < k)) {
       final int y = this.greatestNode;
       final int z = newNode(k);
       values[z] = v;   // addition
@@ -262,7 +255,7 @@ public class Int2IntRBT extends IntArray
     int y = NIL;
     while (x != NIL) {
       y = x;
-      final int xKey = getKey(x);
+      final int xKey = getKeyForNode(x);
       if (k == xKey) {
         // key found
         prevValue = values[x];  // addition
@@ -281,7 +274,7 @@ public class Int2IntRBT extends IntArray
       setParent(z, NIL);
     } else {
       setParent(z, y);
-      if (k < getKey(y)) {
+      if (k < getKeyForNode(y)) {
         setLeft(y, z);
       } else {
         setRight(y, z);
@@ -399,8 +392,8 @@ public class Int2IntRBT extends IntArray
     if (lastNodeGotten == NIL) {
       node = findKey(k);
     } else {
-      final int distanceToTop = Math.abs(k - getKey(this.root));
-      final int distanceToLast = Math.abs(k - getKey(this.lastNodeGotten));
+      final int distanceToTop = Math.abs(k - getKeyForNode(this.root));
+      final int distanceToLast = Math.abs(k - getKeyForNode(this.lastNodeGotten));
       node = (distanceToTop < distanceToLast) ? findKey(k) : findKeyFromLast(k);
     }
     if (node != NIL) {
@@ -412,7 +405,7 @@ public class Int2IntRBT extends IntArray
   
   private int findKeyFromLast(final int k) {
     int node = lastNodeGotten;
-    int keyNode = getKey(node);
+    int keyNode = getKeyForNode(node);
     int prevNode;
     if (k < keyNode) {
       do {
@@ -421,7 +414,7 @@ public class Int2IntRBT extends IntArray
         if (node == NIL) {
           break;
         }
-        keyNode = getKey(node);
+        keyNode = getKeyForNode(node);
       } while  (k < keyNode);
       if (keyNode == k) {
         return node;
@@ -435,7 +428,7 @@ public class Int2IntRBT extends IntArray
         if (node == NIL) {
           break;
         }
-        keyNode = getKey(node);
+        keyNode = getKeyForNode(node);
       } while (k > keyNode);
       if (keyNode == k) {
         return node;
@@ -453,7 +446,7 @@ public class Int2IntRBT extends IntArray
 //    int found = node;
 //    while (node != NIL) {
 //      found = node;
-//      final int keyNode = getKey(node);
+//      final int keyNode = getKeyForNode(node);
 //      if (k < keyNode) {
 //        node = getLeft(node);
 //      } else if (k == keyNode) {
@@ -463,12 +456,12 @@ public class Int2IntRBT extends IntArray
 //        while (true) {
 //          final int left_node = getLeft(node);
 //          if ((left_node == NIL) ||
-//              (getKey(left_node) != keyNode)) {
+//              (getKeyForNode(left_node) != keyNode)) {
 //            break;
 //          }
 //          node = left_node;
 //        }
-////        while ((getLeft(node) != NIL) && (getKey(getLeft(node)) == keyNode)) {
+////        while ((getLeft(node) != NIL) && (getKeyForNode(getLeft(node)) == keyNode)) {
 ////          node = getLeft(node);
 ////        }
 //        return node;

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/IntArrayRBT.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/IntArrayRBT.java?rev=1677731&r1=1677730&r2=1677731&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/IntArrayRBT.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/rb_trees/IntArrayRBT.java Tue May  5 03:31:59 2015
@@ -20,9 +20,11 @@
 package org.apache.uima.internal.util.rb_trees;
 
 import java.util.NoSuchElementException;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
-import org.apache.uima.internal.util.ComparableIntIterator;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.impl.FSRBTSetIndex;
+import org.apache.uima.cas.impl.IntIterator4set;
 import org.apache.uima.internal.util.ComparableIntPointerIterator;
 import org.apache.uima.internal.util.IntComparator;
 import org.apache.uima.internal.util.IntListIterator;
@@ -46,142 +48,143 @@ import org.apache.uima.internal.util.Int
  */
 public class IntArrayRBT extends IntArrayRBTcommon {
 
-  /**
-   * Implement a comparable iterator over the keys.
-   * Adds support to PointerIterator for
-   *   - concurrent modification detection
-   *   - comparing two iterators (for type/subtype ordering)
-   * 
-   */
-  private class IntArrayRBTIterator extends PointerIterator implements
-          ComparableIntPointerIterator {
-
-    private final IntComparator comp;
-
-    private int modificationSnapshot; // to catch illegal modifications
-
-    private int[] detectIllegalIndexUpdates; // shared copy with Index Repository
-
-    private int typeCode;
-
-    public boolean isConcurrentModification() {
-      return modificationSnapshot != detectIllegalIndexUpdates[typeCode];
-    }
-
-    public void resetConcurrentModification() {
-      modificationSnapshot = detectIllegalIndexUpdates[typeCode];
-    }
-
-    private IntArrayRBTIterator(IntComparator comp) {
-      super();
-      this.comp = comp;
-    }
-
-    /**
-     * @see java.lang.Comparable#compareTo(Object)
-     */
-    public int compareTo(Object o) throws NoSuchElementException {
-      ComparableIntPointerIterator it = (ComparableIntPointerIterator) o;
-      // assert(this.comp != null);
-      return this.comp.compare(get(), it.get());
-    }
-
-    public Object copy() {
-      IntArrayRBTIterator copy = new IntArrayRBTIterator(this.comp);
-      copy.currentNode = this.currentNode;
-      return copy;
-    }
-
-    @Override
-    public String toString() {
-      return "ComparablePointerIterator [comp=" + comp + ", typeCode=" + typeCode + ", currentNode=" + currentNode
-          + "]";
-    }
-    
-  }
-
-  /**
-   * IntPointerIterator support for IntArrayRBT style indexes
-   * 
-   * No Concurrent Modification testing
-   * 
-   * No support for type/subtype iterator comparison
-   * 
-   * For these, see above class
-   * 
-   * 
-   */
-  private class PointerIterator implements IntPointerIterator {
-
-    protected int currentNode;
-
-    private PointerIterator() {
-      super();
-      moveToFirst();
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#dec()
-     */
-    public void dec() {
-      this.currentNode = previousNode(this.currentNode);
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#get()
-     */
-    public int get() {
-      if (!isValid()) {
-        throw new NoSuchElementException();
-      }
-      return IntArrayRBT.this.getKey(this.currentNode);
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#inc()
-     */
-    public void inc() {
-      this.currentNode = nextNode(this.currentNode);
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#isValid()
-     */
-    public boolean isValid() {
-      return (this.currentNode != NIL);
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#moveToFirst()
-     */
-    public void moveToFirst() {
-      this.currentNode = getFirstNode();
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#moveToLast()
-     */
-    public void moveToLast() {
-      this.currentNode = IntArrayRBT.this.greatestNode;
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#copy()
-     */
-    public Object copy() {
-      PointerIterator it = new PointerIterator();
-      it.currentNode = this.currentNode;
-      return it;
-    }
-
-    /**
-     * @see org.apache.uima.internal.util.IntPointerIterator#moveTo(int)
-     */
-    public void moveTo(int i) {
-      this.currentNode = findInsertionPoint(i);
-    }
+//  /**
+//   * This is the int iterator used for Set indexes
+//   * Implement a comparable iterator over the keys.
+//   * Adds support to PointerIterator for
+//   *   - concurrent modification detection
+//   *   - comparing two iterators (for type/subtype ordering)
+//   * 
+//   */
+//  private class IntIteratorForSets extends PointerIterator implements
+//          ComparableIntPointerIterator {
+//
+//    private final IntComparator comp;
+//
+//    private int modificationSnapshot; // to catch illegal modifications
+//
+//    private int[] detectIllegalIndexUpdates; // shared copy with Index Repository
+//
+//    private int typeCode;
+//
+//    public boolean isConcurrentModification() {
+//      return modificationSnapshot != detectIllegalIndexUpdates[typeCode];
+//    }
+//
+//    public void resetConcurrentModification() {
+//      modificationSnapshot = detectIllegalIndexUpdates[typeCode];
+//    }
+//
+//    private IntIteratorForSets(IntComparator comp) {
+//      super();
+//      this.comp = comp;
+//    }
+//
+//    /**
+//     * @see java.lang.Comparable#compareTo(Object)
+//     */
+//    public int compareTo(Object o) throws NoSuchElementException {
+//      ComparableIntPointerIterator it = (ComparableIntPointerIterator) o;
+//      // assert(this.comp != null);
+//      return this.comp.compare(get(), it.get());
+//    }
+//
+//    public Object copy() {
+//      IntIteratorForSets copy = new IntIteratorForSets(this.comp);
+//      copy.currentNode = this.currentNode;
+//      return copy;
+//    }
+//
+//    @Override
+//    public String toString() {
+//      return "ComparablePointerIterator [comp=" + comp + ", typeCode=" + typeCode + ", currentNode=" + currentNode
+//          + "]";
+//    }
+//    
+//  }
 
-  }
+//  /**
+//   * IntPointerIterator support for IntArrayRBT style indexes
+//   * 
+//   * No Concurrent Modification testing
+//   * 
+//   * No support for type/subtype iterator comparison
+//   * 
+//   * For these, see above class
+//   * 
+//   * 
+//   */
+//  private class PointerIterator implements IntPointerIterator {
+//
+//    protected int currentNode;
+//
+//    private PointerIterator() {
+//      super();
+//      moveToFirst();
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#dec()
+//     */
+//    public void dec() {
+//      this.currentNode = previousNode(this.currentNode);
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#get()
+//     */
+//    public int get() {
+//      if (!isValid()) {
+//        throw new NoSuchElementException();
+//      }
+//      return IntArrayRBT.this.getKey(this.currentNode);
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#inc()
+//     */
+//    public void inc() {
+//      this.currentNode = nextNode(this.currentNode);
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#isValid()
+//     */
+//    public boolean isValid() {
+//      return (this.currentNode != NIL);
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#moveToFirst()
+//     */
+//    public void moveToFirst() {
+//      this.currentNode = getFirstNode();
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#moveToLast()
+//     */
+//    public void moveToLast() {
+//      this.currentNode = IntArrayRBT.this.greatestNode;
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#copy()
+//     */
+//    public Object copy() {
+//      PointerIterator it = new PointerIterator();
+//      it.currentNode = this.currentNode;
+//      return it;
+//    }
+//
+//    /**
+//     * @see org.apache.uima.internal.util.IntPointerIterator#moveTo(int)
+//     */
+//    public void moveTo(int i) {
+//      this.currentNode = findInsertionPoint(i);
+//    }
+//
+//  }
 
   private class IntArrayRBTKeyIterator implements IntListIterator {
 
@@ -189,47 +192,48 @@ public class IntArrayRBT extends IntArra
 
     protected IntArrayRBTKeyIterator() {
       super();
-      this.currentNode = NIL;
+      moveToStart();
     }
 
+    @Override
     public final boolean hasNext() {
-      return (this.currentNode != IntArrayRBT.this.greatestNode);
+      return (this.currentNode != NIL);
     }
 
+    @Override
     public final int next() {
       if (!hasNext()) {
         throw new NoSuchElementException();
       }
-      this.currentNode = (this.currentNode == NIL) ? getFirstNode() : nextNode(this.currentNode);
-      return IntArrayRBT.this.getKey(this.currentNode);
+      int r = IntArrayRBT.this.getKeyForNode(this.currentNode);
+      this.currentNode = nextNode(this.currentNode);
+      return r;
     }
 
     /**
      * @see org.apache.uima.internal.util.IntListIterator#hasPrevious()
      */
+    @Override
     public boolean hasPrevious() {
-      return (this.currentNode != NIL);
+      return (previousNode(this.currentNode) != NIL);
     }
 
     /**
      * @see org.apache.uima.internal.util.IntListIterator#previous()
      */
+    @Override
     public int previous() {
-      if (!hasPrevious()) {
-        throw new NoSuchElementException();
-      }
-      final int currentKey = IntArrayRBT.this.getKey(this.currentNode);
-      if (this.currentNode == getFirstNode()) {
-        this.currentNode = NIL;
-      } else {
-        this.currentNode = previousNode(this.currentNode);
+      this.currentNode = previousNode(this.currentNode);
+      if (this.currentNode != NIL) {
+        return getKey(this.currentNode);
       }
-      return currentKey;
+      throw new NoSuchElementException();
     }
 
     /**
      * @see org.apache.uima.internal.util.IntListIterator#moveToEnd()
      */
+    @Override
     public void moveToEnd() {
       this.currentNode = IntArrayRBT.this.greatestNode;
     }
@@ -237,42 +241,37 @@ public class IntArrayRBT extends IntArra
     /**
      * @see org.apache.uima.internal.util.IntListIterator#moveToStart()
      */
+    @Override
     public void moveToStart() {
-      this.currentNode = NIL;
-    }
-
-    protected final int getKey(int node) {
-      return IntArrayRBT.this.getKey(node);
-    }
-
-  }
-
-  private class ComparableIterator extends IntArrayRBTKeyIterator implements ComparableIntIterator {
-
-    private final IntComparator comparator;
-
-    private ComparableIterator(IntComparator comp) {
-      super();
-      this.comparator = comp;
+      this.currentNode = getFirstNode();
     }
 
-    /**
-     * @see java.lang.Comparable#compareTo(Object)
-     */
-    public int compareTo(Object o) {
-      ComparableIterator it = (ComparableIterator) o;
-      return this.comparator.compare(
-          IntArrayRBT.this.getKey(this.currentNode), 
-          it.getKey(it.currentNode));
+    protected final int getKey(int node) { 
+      return IntArrayRBT.this.getKeyForNode(node);
     }
 
   }
 
-
- 
-
-  // Random number generator to randomize inserts of identical keys.
-  protected final Random rand;
+//  private class ComparableIterator extends IntArrayRBTKeyIterator implements ComparableIntIterator {
+//
+//    private final IntComparator comparator;
+//
+//    private ComparableIterator(IntComparator comp) {
+//      super();
+//      this.comparator = comp;
+//    }
+//
+//    /**
+//     * @see java.lang.Comparable#compareTo(Object)
+//     */
+//    public int compareTo(Object o) {
+//      ComparableIterator it = (ComparableIterator) o;
+//      return this.comparator.compare(
+//          IntArrayRBT.this.getKey(this.currentNode), 
+//          it.getKey(it.currentNode));
+//    }
+//
+//  }
 
   /**
    * Constructor for IntArrayRBT.
@@ -283,83 +282,78 @@ public class IntArrayRBT extends IntArra
 
   public IntArrayRBT(int initialSize) {
     super();
-    this.rand = new Random();
   }
     
-  public int getKeyForNode(final int node) {
-    return getKey(node);
-  }
-
+  /**
+   * 
+   * @param k the value to insert
+   * @return negative of the node number of the found key, if the key was found, else, the node number of the inserted new node
+   */
   protected int treeInsert(final int k) {
-    if ((this.greatestNode != NIL) && (getKey(this.greatestNode) < k)) {
-      final int y = this.greatestNode;
-      final int z = newNode(k);
-      this.greatestNode = z;
-      setRight(y, z);
-      setParent(z, y);
-      return z;
-    } 
-    int x = this.root;
-    int y = NIL;
-    while (x != NIL) {
-      y = x;
-      final int xKey = getKey(x);
-      if (k == xKey) {
-        return -x;
-      }
-      x = (k < xKey) ? getLeft(x) : getRight(x);
-    }
-    // The key was not found, so we create a new node, inserting the
-    // key.
-    final int z = newNode(k);
-    if (y == NIL) {
-      setAsRoot(z);
-      this.greatestNode = z;
-      setParent(z, NIL);
-    } else {
-      setParent(z, y);
-      if (k < getKey(y)) {
-        setLeft(y, z);
-      } else {
-        setRight(y, z);
-      }
-    }
-    return z;
+    return treeInsert(k, false);
   }
 
   protected int treeInsertWithDups(final int k) {
-    if ((this.greatestNode != NIL) && (getKey(this.greatestNode) <= k)) {
-      final int y = this.greatestNode;
-      final int z = newNode(k);
-      this.greatestNode = z;
-      setRight(y, z);
-      setParent(z, y);
-      return z;
+    return treeInsert(k, true);
+  }
+    
+  protected int treeInsert(final int k, boolean withDups) {  
+    if (this.greatestNode != NIL) {
+      final int lt = withDups? 1 : 0;
+      if (compare(getKeyForNode(this.greatestNode), k) < lt) {
+        final int y = this.greatestNode;
+        final int z = newNode(k);
+        this.greatestNode = z;
+        setRight(y, z);
+        setParent(z, y);
+        return z;
+      }
     }
+    
+    int x = this.root;  // could be NIL
     int y = NIL;
-    int x = this.root;
+    
+    // find existing value (key)
+    int cr = 0;
+    boolean wentLeft = false;
+    ThreadLocalRandom rand = withDups? ThreadLocalRandom.current() : null;
     while (x != NIL) {
       y = x;
-      final int xKey = getKey(x);
-      x = (k < xKey) ? getLeft(x) :
-          (k > xKey) ? getRight(x) :
-        //(k == xKey)
-          (this.rand.nextBoolean()) ?  getLeft(x) : getRight(x);
+      cr = compare(k, getKeyForNode(x));
+      if (cr == 0) {
+        if (withDups) {
+          if (rand.nextBoolean()) {
+            x = getLeft(x);
+            wentLeft = true;
+          } else {
+            x = getRight(x);
+            wentLeft = false;
+          }  
+        } else {
+          // not with dups, found, return negative of found index
+          return -x;
+        }
+      } else {
+        // cr not 0
+        x = (cr < 0) ? getLeft(x) : getRight(x);
+      }
     }
+    
+    // The key was not found or was found but inserting dups
+    
     final int z = newNode(k);
-    if (y == NIL) {
-      setAsRoot(z);
+    if (y == NIL) {  // only happens if this.root is NIL, e.g. table is empty.
+      setAsRoot(z); // also set parent to NIL
       this.greatestNode = z;
-      setParent(z, NIL);
     } else {
       setParent(z, y);
-      if (k < getKey(y)) {
+      if (cr < 0) {
         setLeft(y, z);
-      } else if (k > getKey(y)) {
+      } else if (cr > 0) {
         setRight(y, z);
       } else { // k == key[y]
         // Randomly insert node to the left or right.
-        if (this.rand.nextBoolean()) {
+        if (wentLeft) {
           setLeft(y, z);
         } else {
           setRight(y, z);
@@ -398,9 +392,9 @@ public class IntArrayRBT extends IntArra
     return true;
   }
 
-  public int insertKeyWithDups(int k) {
-    return insertKey(k, true);
-  }
+//  public int insertKeyWithDups(int k) {
+//    return insertKey(k, true);
+//  }
 
   private int insertKey(final int k, final boolean withDups) {
     if (this.root == NIL) {
@@ -410,13 +404,34 @@ public class IntArrayRBT extends IntArra
       this.greatestNode = x;
       return x;
     }
-    final int x = withDups ? treeInsertWithDups(k) : treeInsert(k);
-    if (x < NIL) {
+    final int x = treeInsert(k, withDups);
+    if (x < NIL) {  // means was found, not inserted
       return -x;
     }
     return commonInsertKey(x);
   }
   
+  //for testing only
+  public int insertKeyShowNegative(int k) {
+    if (this.root == NIL) {
+      final int x = newNode(k);
+      setAsRoot(x);
+      this.color[this.root] = black;
+      this.greatestNode = x;
+      return x;
+    }
+    final int x = treeInsert(k, false);
+    if (x < NIL) {  // means was found, not inserted
+      return x;
+    }
+    return commonInsertKey(x);
+    
+  }
+  /**
+   * Code run after the insert operation, done to rebalance the red black tree
+   * @param x -
+   * @return -
+   */
   private int commonInsertKey(int x) {
     this.color[x] = red;
     final int node = x;
@@ -469,64 +484,6 @@ public class IntArrayRBT extends IntArra
   // return (node == (next - 1));
   // }
 
-  /**
-   * Find the first node such that k &lt;= key[node].
-   */
-  public int findKey(final int k) {
-    int node = this.root;
-    while (node != NIL) {
-      final int keyNode = getKey(node);
-      if (k < keyNode) {
-        node = getLeft(node);
-      } else if (k == keyNode) {
-        return node;
-      } else {
-        node = getRight(node);
-      }
-    }
-    // node == NIL
-    return NIL;
-  }
-
-  /**
-   * Find the node such that key[node] &ge; k and key[previous(node)] &lt; k.
-   * @param k the key
-   * @return the index of the node
-   */
-  public int findInsertionPoint(final int k) {
-    boolean onlyWentRight = true;
-    int node = this.root;
-    int found = node;
-    while (node != NIL) {
-      found = node;
-      final int keyNode = getKey(node);
-      if (k < keyNode) {
-        onlyWentRight = false;
-        node = getLeft(node);
-      } else if (k == keyNode) {
-        // In the presence of duplicates, we have to check if there are
-        // identical
-        // keys to the left of us.
-        while (true) {
-          final int left_node = getLeft(node);
-          if ((left_node == NIL) ||
-              (getKey(left_node) != keyNode)) {
-            break;
-          }
-          node = left_node;
-        }
-//        while ((getLeft(node) != NIL) && (getKey(getLeft(node)) == keyNode)) {
-//          node = getLeft(node);
-//        }
-        return node;
-      } else {
-        node = getRight(node);
-      }
-    }
-    // node == NIL
-    return onlyWentRight ? NIL : found;
-  }
-
 //  private final boolean isLeftDtr(int node) {
 //    return ((node != this.root) && (node == getLeft(getParent(node))));
 //  }
@@ -537,7 +494,11 @@ public class IntArrayRBT extends IntArra
 
   public boolean deleteKey(int aKey) {
     final int node = findKey(aKey);
-    if (node == NIL) {
+    // for use with FS set index, key is the fs addr, and the compare fn is the UIMA Set compare definition
+    //   Multiple FSs may compare equal, even though they are different FSs
+    //   Only delete if the fs addr found is the one being looked for
+    //   For this use, there are never multiple FSs comparing equal in the set because it's a set.
+    if (node == NIL || getKeyForNode(node) != aKey) {
       return false;
     }
     deleteNode(node);
@@ -547,44 +508,52 @@ public class IntArrayRBT extends IntArra
     }
     return true;
   }
-
+  
   public void clear() {
     flush();
   }
   
-  /**
-   * Method iterator.
-   * @param comp comparator  
-   * @return IntListIterator
-   */
-  public ComparableIntIterator iterator(IntComparator comp) {
-    return new ComparableIterator(comp);
-  }
+//  /**
+//   * Method iterator.
+//   * @param comp comparator  
+//   * @return IntListIterator
+//   */
+//  public ComparableIntIterator iterator(IntComparator comp) {
+//    return new ComparableIterator(comp);
+//  }
 
   public IntListIterator iterator() {
     return new IntArrayRBTKeyIterator();
   }
 
-  public IntPointerIterator pointerIterator() {
-    return new PointerIterator();
+  // this version doesn't do ConcurrentModificationException testing
+  public <T extends FeatureStructure> IntPointerIterator pointerIterator(IntComparator comp, FSRBTSetIndex<T> fsSetIndex) {
+    return new IntIterator4set<T>(fsSetIndex, null, comp);
   }
 
-  public IntPointerIterator pointerIterator(int aKey) {
-    PointerIterator it = new PointerIterator();
-    it.currentNode = this.findKey(aKey);
-    return it;
-  }
-
-  public ComparableIntPointerIterator pointerIterator(IntComparator comp,
-          int[] detectIllegalIndexUpdates, int typeCode) {
-    // assert(comp != null);
-    IntArrayRBTIterator cpi = new IntArrayRBTIterator(comp);
-    cpi.modificationSnapshot = detectIllegalIndexUpdates[typeCode];
-    cpi.detectIllegalIndexUpdates = detectIllegalIndexUpdates;
-    cpi.typeCode = typeCode;
+//  public IntPointerIterator pointerIterator(int aKey) {
+//    PointerIterator it = new PointerIterator();
+//    it.currentNode = this.findKey(aKey);
+//    return it;
+//  }
+
+  public <T extends FeatureStructure> ComparableIntPointerIterator<T> pointerIterator(
+      FSRBTSetIndex<T> fsSetIndex, int[] detectIllegalIndexUpdates, IntComparator comp) {
+    IntIterator4set<T> cpi = new IntIterator4set<T>(fsSetIndex, detectIllegalIndexUpdates, comp);
     return cpi;
   }
+  
+  //debug
+  public boolean debugScanFor(int key) {
+    for (int i = 1; i < next; i++) {
+      if (getKeyForNode(i) == key) {
+        return true;
+      }
+    }
+    return false;
+  }
 
-
-
+  public int getKeyForNode(int node) { // is public
+    return super.getKeyForNode(node);  // is protected
+  }
 }