Ejemplo n.º 1
0
 public static PrimitiveIntSet asSetAllowDuplicates(PrimitiveIntIterator iterator) {
   PrimitiveIntSet set = Primitive.intSet();
   while (iterator.hasNext()) {
     set.add(iterator.next());
   }
   return set;
 }
Ejemplo n.º 2
0
 public static int last(PrimitiveIntIterator iterator, int defaultItem) {
   int result = defaultItem;
   while (iterator.hasNext()) {
     result = iterator.next();
   }
   return result;
 }
Ejemplo n.º 3
0
 /**
  * Returns the index of the given item in the iterator(zero-based). If no items in {@code
  * iterator} equals {@code item} {@code -1} is returned.
  *
  * @param item the item to look for.
  * @param iterator of items.
  * @return index of found item or -1 if not found.
  */
 public static int indexOf(PrimitiveIntIterator iterator, int item) {
   for (int i = 0; iterator.hasNext(); i++) {
     if (item == iterator.next()) {
       return i;
     }
   }
   return -1;
 }
Ejemplo n.º 4
0
 public static PrimitiveIntSet asSet(PrimitiveIntIterator iterator) {
   PrimitiveIntSet set = Primitive.intSet();
   while (iterator.hasNext()) {
     int next = iterator.next();
     if (!set.add(next)) {
       throw new IllegalStateException("Duplicate " + next + " from " + iterator);
     }
   }
   return set;
 }
Ejemplo n.º 5
0
 /**
  * Validates whether two {@link Iterator}s are equal or not, i.e. if they have contain same number
  * of items and each orderly item equals one another.
  *
  * @param first the {@link Iterator} containing the first items.
  * @param other the {@link Iterator} containing the other items.
  * @return whether the two iterators are equal or not.
  */
 public static boolean equals(PrimitiveIntIterator first, PrimitiveIntIterator other) {
   boolean firstHasNext, otherHasNext;
   // single | so that both iterator's hasNext() gets evaluated.
   while ((firstHasNext = first.hasNext()) | (otherHasNext = other.hasNext())) {
     if (firstHasNext != otherHasNext || first.next() != other.next()) {
       return false;
     }
   }
   return true;
 }
Ejemplo n.º 6
0
 @Override
 protected boolean fetchNext() {
   while (source.hasNext()) {
     int testItem = source.next();
     if (accept(testItem)) {
       return next(testItem);
     }
   }
   return false;
 }
Ejemplo n.º 7
0
 @Override
 protected boolean fetchNext() {
   if (currentRound == null || !currentRound.hasNext()) {
     currentRound = iterators.iterator();
   }
   while (currentRound.hasNext()) {
     PrimitiveIntIterator iterator = currentRound.next();
     if (iterator.hasNext()) {
       return next(iterator.next());
     }
   }
   currentRound = null;
   return false;
 }
Ejemplo n.º 8
0
 @Override
 protected boolean fetchNext() {
   if (currentIterator == null || !currentIterator.hasNext()) {
     while (iterators.hasNext()) {
       currentIterator = iterators.next();
       if (currentIterator.hasNext()) {
         break;
       }
     }
   }
   return currentIterator != null && currentIterator.hasNext()
       ? next(currentIterator.next())
       : false;
 }
Ejemplo n.º 9
0
  public static int[] asArray(PrimitiveIntIterator iterator) {
    int[] array = new int[8];
    int i = 0;
    for (; iterator.hasNext(); i++) {
      if (i >= array.length) {
        array = copyOf(array, i << 1);
      }
      array[i] = iterator.next();
    }

    if (i < array.length) {
      array = copyOf(array, i);
    }
    return array;
  }
Ejemplo n.º 10
0
 public static int single(PrimitiveIntIterator iterator) {
   try {
     assertMoreItems(iterator);
     int item = iterator.next();
     if (iterator.hasNext()) {
       throw new NoSuchElementException(
           "More than one item in "
               + iterator
               + ", first:"
               + item
               + ", second:"
               + iterator.next());
     }
     closeSafely(iterator);
     return item;
   } catch (NoSuchElementException exception) {
     closeSafely(iterator, exception);
     throw exception;
   }
 }
Ejemplo n.º 11
0
  public static int itemAt(PrimitiveIntIterator iterator, int index) {
    if (index >= 0) { // Look forwards
      for (int i = 0; iterator.hasNext() && i < index; i++) {
        iterator.next();
      }
      assertMoreItems(iterator);
      return iterator.next();
    }

    // Look backwards
    int fromEnd = index * -1;
    int[] trail = new int[fromEnd];
    int cursor = 0;
    for (; iterator.hasNext(); cursor++) {
      trail[cursor % trail.length] = iterator.next();
    }
    if (cursor < fromEnd) {
      throw new NoSuchElementException("Item " + index + " not found in " + iterator);
    }
    return trail[cursor % fromEnd];
  }
Ejemplo n.º 12
0
  public static int itemAt(PrimitiveIntIterator iterator, int index, int defaultItem) {
    if (index >= 0) { // Look forwards
      for (int i = 0; iterator.hasNext() && i < index; i++) {
        iterator.next();
      }
      return iterator.hasNext() ? iterator.next() : defaultItem;
    }

    // Look backwards
    int fromEnd = index * -1;
    int[] trail = new int[fromEnd];
    int cursor = 0;
    for (; iterator.hasNext(); cursor++) {
      trail[cursor % trail.length] = iterator.next();
    }
    return cursor < fromEnd ? defaultItem : trail[cursor % fromEnd];
  }
Ejemplo n.º 13
0
 public static int count(PrimitiveIntIterator iterator) {
   int count = 0;
   for (; iterator.hasNext(); iterator.next(), count++) { // Just loop through this
   }
   return count;
 }
Ejemplo n.º 14
0
 public static int first(PrimitiveIntIterator iterator, int defaultItem) {
   return iterator.hasNext() ? iterator.next() : defaultItem;
 }
Ejemplo n.º 15
0
 private static void assertMoreItems(PrimitiveIntIterator iterator) {
   if (!iterator.hasNext()) {
     throw new NoSuchElementException("No element in " + iterator);
   }
 }
Ejemplo n.º 16
0
 public static int first(PrimitiveIntIterator iterator) {
   assertMoreItems(iterator);
   return iterator.next();
 }