edu.stanford.nlp.util
Class Index<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by edu.stanford.nlp.util.Index<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, RandomAccess

public class Index<E>
extends AbstractCollection<E>
implements Serializable, RandomAccess

An Index is a collection that maps between an Object vocabulary and a contiguous non-negative integer index beginning (inclusively) at 0. It supports constant-time lookup in both directions (via get(int) and indexOf(Object). The indexOf(Object) method compares objects by equals, as other Collections.

The typical usage would be:

Index index = new Index(collection);

followed by

int i = index.indexOf(object);

or

Object o = index.get(i);

The source contains a concrete example of use as the main method.

An Index can be locked or unlocked: a locked index cannot have new items added to it.

Since:
1.0
Author:
Dan Klein
See Also:
AbstractCollection, Serialized Form

Field Summary
protected  Map<Object,Integer> indexes
           
protected  boolean locked
           
protected  List<E> objects
           
 
Constructor Summary
Index()
          Creates a new Index.
Index(Collection<? extends E> c)
          Creates a new Index and adds every member of c to it.
 
Method Summary
 boolean add(E o)
          Adds an object to the Index.
 boolean addAll(Collection<? extends E> c)
          Adds every member of Collection to the Index.
 void clear()
          Clears this Index.
 boolean contains(Object o)
          Checks whether an Object already has an index in the Index
static Index deserializeReadable(String file)
           
 String firstNToString(int n)
           
 E get(int i)
          Gets the object whose index is the integer argument.
 int indexOf(Object o)
          Takes an Object and returns the integer index of the Object.
 int indexOf(Object o, boolean add)
          Takes an Object and returns the integer index of the Object.
 int[] indices(List<E> elems)
          Returns the index of each elem in an array.
 boolean isLocked()
          Queries the Index for whether it's locked or not.
 Iterator<E> iterator()
          Returns an iterator over the elements of the collection.
 void lock()
          Locks the Index.
static void main(String[] args)
           
 Collection<E> objects(int[] indices)
          Looks up the objects corresponding to an array of indices, and returns them in a Collection.
 List<E> objectsList()
          Returns a complete List of indexed objects, in the order of their indices.
 boolean remove(Object o)
          Removes an object from the index, if it exists (otherwise nothing happens).
 void serializeReadable(String file)
           
 int size()
          Checks the number of indexed objects.
 String toString()
           
 void unlock()
          Unlocks the Index.
 Index<E> unmodifiableView()
          Returns and unmodifiable view of the Index.
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

objects

protected List<E> objects

indexes

protected Map<Object,Integer> indexes

locked

protected boolean locked
Constructor Detail

Index

public Index()
Creates a new Index.


Index

public Index(Collection<? extends E> c)
Creates a new Index and adds every member of c to it.

Method Detail

clear

public void clear()
Clears this Index.

Specified by:
clear in interface Collection<E>
Overrides:
clear in class AbstractCollection<E>

indices

public int[] indices(List<E> elems)
Returns the index of each elem in an array.


objects

public Collection<E> objects(int[] indices)
Looks up the objects corresponding to an array of indices, and returns them in a Collection.

Parameters:
indices -
Returns:
a Collection of the objects corresponding to the indices argument.

size

public int size()
Checks the number of indexed objects.

Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the number of indexed objects.

get

public E get(int i)
Gets the object whose index is the integer argument.

Parameters:
i - the integer index to be queried for the corresponding argument
Returns:
the object whose index is the integer argument.

objectsList

public List<E> objectsList()
Returns a complete List of indexed objects, in the order of their indices. DANGER! The current implementation returns the actual index list, not a defensive copy. Messing with this List can seriously screw up the state of the Index. (perhaps this method needs to be eliminated? I don't think it's ever used in ways that we couldn't use the Index itself for directly. --Roger, 12/29/04)

Returns:
a complete List of indexed objects

isLocked

public boolean isLocked()
Queries the Index for whether it's locked or not.

Returns:
whether or not the Index is locked

lock

public void lock()
Locks the Index. A locked index cannot have new elements added to it (calls to add(E) will leave the Index unchanged and return false).


unlock

public void unlock()
Unlocks the Index. A locked index cannot have new elements added to it (calls to add(E) will leave the Index unchanged and return false).


indexOf

public int indexOf(Object o)
Takes an Object and returns the integer index of the Object. Returns -1 if the Object is not in the Index.

Parameters:
o - the Object whose index is desired.
Returns:
the index of the Object argument. Returns -1 if the object is not in the index.

indexOf

public int indexOf(Object o,
                   boolean add)
Takes an Object and returns the integer index of the Object. Returns -1 if the Object is not in the Index.

Parameters:
o - the Object whose index is desired.
Returns:
the index of the Object argument. Returns -1 if the object is not in the index.

addAll

public boolean addAll(Collection<? extends E> c)
Adds every member of Collection to the Index. Does nothing for members already in the Index.

Specified by:
addAll in interface Collection<E>
Overrides:
addAll in class AbstractCollection<E>
Returns:
true if some item was added to the index and false if no item was already in the index or if the index is locked

add

public boolean add(E o)
Adds an object to the Index. If it was already in the Index, then nothing is done. If it is not in the Index, then it is added iff the Index hasn't been locked.

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractCollection<E>
Returns:
true if the item was added to the index and false if the item was already in the index or if the index is locked

contains

public boolean contains(Object o)
Checks whether an Object already has an index in the Index

Specified by:
contains in interface Collection<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
o - the object to be queried.
Returns:
true iff there is an index for the queried object.

serializeReadable

public void serializeReadable(String file)

deserializeReadable

public static Index deserializeReadable(String file)

toString

public String toString()
Overrides:
toString in class AbstractCollection<E>

firstNToString

public String firstNToString(int n)

main

public static void main(String[] args)

iterator

public Iterator<E> iterator()
Returns an iterator over the elements of the collection.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class AbstractCollection<E>
Returns:

remove

public boolean remove(Object o)
Removes an object from the index, if it exists (otherwise nothing happens). Note, the indices of other elements will not be changed, so indices will no longer necessarily be contiguous

Specified by:
remove in interface Collection<E>
Overrides:
remove in class AbstractCollection<E>
Parameters:
o - the object to remove
Returns:
whether anything was removed

unmodifiableView

public Index<E> unmodifiableView()
Returns and unmodifiable view of the Index. It is just a locked index that cannot be unlocked, so if you try to add something, nothing will happen (it won't throw an excpetion). Trying to unlock it will throw an UnsupportedOperationException. If the underlying Index is modified, the change will "write-through" to the view.



Stanford NLP Group