
it.unimi.dsi.fastutil.objects.ObjectBigListIterators Maven / Gradle / Ivy
Show all versions of fastutil Show documentation
/* Copyright (C) 1991-2016 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
. */
/* This header is separate from features.h so that the compiler can
include it implicitly at the start of every compilation. It must
not itself include or any other header that includes
because the implicit include comes before any feature
test macros that may be defined in a source file before it first
explicitly includes a system header. GCC knows the name of this
header in order to preinclude it. */
/* glibc's intent is to support the IEC 559 math functionality, real
and complex. If the GCC (4.9 and later) predefined macros
specifying compiler intent are available, use them to determine
whether the overall intent is to support these features; otherwise,
presume an older compiler has intent to support these features and
define these macros by default. */
/* wchar_t uses Unicode 9.0.0. Version 9.0 of the Unicode Standard is
synchronized with ISO/IEC 10646:2014, fourth edition, plus
Amd. 1 and Amd. 2 and 273 characters from forthcoming 10646, fifth edition.
(Amd. 2 was published 2016-05-01,
see https://www.iso.org/obp/ui/#iso:std:iso-iec:10646:ed-4:v1:amd:2:v1:en) */
/* We do not support C11 . */
/* Generic definitions */
/* Assertions (useful to generate conditional code) */
/* Current type and class (and size, if applicable) */
/* Value methods */
/* Interfaces (keys) */
/* Interfaces (values) */
/* Abstract implementations (keys) */
/* Abstract implementations (values) */
/* Static containers (keys) */
/* Static containers (values) */
/* Implementations */
/* Synchronized wrappers */
/* Unmodifiable wrappers */
/* Other wrappers */
/* Methods (keys) */
/* Methods (values) */
/* Methods (keys/values) */
/* Methods that have special names depending on keys (but the special names depend on values) */
/* Equality */
/* Object/Reference-only definitions (keys) */
/* Object/Reference-only definitions (values) */
/*
* Copyright (C) 2002-2016 Sebastiano Vigna
*
* Licensed 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 it.unimi.dsi.fastutil.objects;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A class providing static methods and objects that do useful things with
* type-specific iterators.
*
* @see Iterator
*/
public class ObjectBigListIterators {
private ObjectBigListIterators() {
}
/**
* A class returning no elements and a type-specific big list iterator
* interface.
*
*
* This class may be useful to implement your own in case you subclass a
* type-specific iterator.
*/
public static class EmptyBigListIterator extends AbstractObjectBigListIterator implements java.io.Serializable, Cloneable {
private static final long serialVersionUID = -7046029254386353129L;
protected EmptyBigListIterator() {
}
public boolean hasNext() {
return false;
}
public boolean hasPrevious() {
return false;
}
public K next() {
throw new NoSuchElementException();
}
public K previous() {
throw new NoSuchElementException();
}
public long nextIndex() {
return 0;
}
public long previousIndex() {
return -1;
}
public long skip(long n) {
return 0;
};
public long back(long n) {
return 0;
};
public Object clone() {
return EMPTY_BIG_LIST_ITERATOR;
}
private Object readResolve() {
return EMPTY_BIG_LIST_ITERATOR;
}
}
/**
* An empty iterator (immutable). It is serializable and cloneable.
*
*
* The class of this objects represent an abstract empty iterator that can
* iterate as a type-specific (list) iterator.
*/
@SuppressWarnings("rawtypes")
public final static EmptyBigListIterator EMPTY_BIG_LIST_ITERATOR = new EmptyBigListIterator();
/** An iterator returning a single element. */
private static class SingletonBigListIterator extends AbstractObjectBigListIterator {
private final K element;
private int curr;
public SingletonBigListIterator(final K element) {
this.element = element;
}
public boolean hasNext() {
return curr == 0;
}
public boolean hasPrevious() {
return curr == 1;
}
public K next() {
if (!hasNext()) throw new NoSuchElementException();
curr = 1;
return element;
}
public K previous() {
if (!hasPrevious()) throw new NoSuchElementException();
curr = 0;
return element;
}
public long nextIndex() {
return curr;
}
public long previousIndex() {
return curr - 1;
}
}
/**
* Returns an iterator that iterates just over the given element.
*
* @param element
* the only element to be returned by a type-specific list
* iterator.
* @return an iterator that iterates just over element
.
*/
public static ObjectBigListIterator singleton(final K element) {
return new SingletonBigListIterator(element);
}
/** An unmodifiable wrapper class for big list iterators. */
public static class UnmodifiableBigListIterator extends AbstractObjectBigListIterator {
final protected ObjectBigListIterator i;
public UnmodifiableBigListIterator(final ObjectBigListIterator i) {
this.i = i;
}
public boolean hasNext() {
return i.hasNext();
}
public boolean hasPrevious() {
return i.hasPrevious();
}
public K next() {
return i.next();
}
public K previous() {
return i.previous();
}
public long nextIndex() {
return i.nextIndex();
}
public long previousIndex() {
return i.previousIndex();
}
}
/**
* Returns an unmodifiable list iterator backed by the specified list
* iterator.
*
* @param i
* the list iterator to be wrapped in an unmodifiable list
* iterator.
* @return an unmodifiable view of the specified list iterator.
*/
public static ObjectBigListIterator unmodifiable(final ObjectBigListIterator i) {
return new UnmodifiableBigListIterator(i);
}
/** A class exposing a list iterator as a big-list iterator.. */
public static class BigListIteratorListIterator extends AbstractObjectBigListIterator {
protected final ObjectListIterator i;
protected BigListIteratorListIterator(final ObjectListIterator i) {
this.i = i;
}
private int intDisplacement(long n) {
if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE) throw new IndexOutOfBoundsException("This big iterator is restricted to 32-bit displacements");
return (int) n;
}
public void set(K ok) {
i.set(ok);
}
public void add(K ok) {
i.add(ok);
}
public int back(int n) {
return i.back(n);
}
public long back(long n) {
return i.back(intDisplacement(n));
}
public void remove() {
i.remove();
}
public int skip(int n) {
return i.skip(n);
}
public long skip(long n) {
return i.skip(intDisplacement(n));
}
public boolean hasNext() {
return i.hasNext();
}
public boolean hasPrevious() {
return i.hasPrevious();
}
public K next() {
return i.next();
}
public K previous() {
return i.previous();
}
public long nextIndex() {
return i.nextIndex();
}
public long previousIndex() {
return i.previousIndex();
}
}
/**
* Returns a big-list iterator backed by the specified list iterator.
*
* @param i
* the list iterator to adapted to the big-list-iterator
* interface.
* @return a big-list iterator backed by the specified list iterator.
*/
public static ObjectBigListIterator asBigListIterator(final ObjectListIterator i) {
return new BigListIteratorListIterator(i);
}
}