|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.util.AbstractCollection<E>
edu.stanford.nlp.util.HashIndex<E>
public class HashIndex<E>
An Index is a collection that maps between an Object vocabulary and a
contiguous non-negative integer index series beginning (inclusively) at 0.
It supports constant-time lookup in
both directions (via get(int) and indexOf(E).
The indexOf(E) method compares objects by
equals, as other Collections.
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.
AbstractCollection,
Serialized Form| Constructor Summary | |
|---|---|
HashIndex()
Creates a new Index. |
|
HashIndex(java.util.Collection<? extends E> c)
Creates a new Index and adds every member of c to it. |
|
HashIndex(Index<? extends E> index)
|
|
HashIndex(int capacity)
Creates a new Index. |
|
| Method Summary | |
|---|---|
boolean |
add(E o)
Adds an object to the Index. |
boolean |
addAll(java.util.Collection<? extends E> c)
Adds every member of Collection to the Index. |
void |
clear()
Clears this Index. |
boolean |
contains(java.lang.Object o)
Checks whether an Object already has an index in the Index |
E |
get(int i)
Gets the object whose index is the integer argument. |
int |
indexOf(E o)
Returns the integer index of the Object in the Index or -1 if the Object is not already in the Index. |
int |
indexOf(E o,
boolean add)
Takes an Object and returns the integer index of the Object, perhaps adding it to the index first. |
int[] |
indices(java.util.Collection<E> elems)
Returns the index of each elem in a List. |
boolean |
isLocked()
Queries the Index for whether it's locked or not. |
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements of the collection. |
static Index<java.lang.String> |
loadFromFilename(java.lang.String file)
This assumes each line is of the form (number=value) and it adds each value in order of the lines in the file |
static Index<java.lang.String> |
loadFromFileWithList(java.lang.String file)
This assumes each line is one value and creates index by adding values in the order of the lines in the file |
static Index<java.lang.String> |
loadFromReader(java.io.BufferedReader br)
This is the analogue of loadFromFilename, and is intended to be included in a routine
that unpacks a text-serialized form of an object that incorporates an Index. |
void |
lock()
Locks the Index. |
java.util.Collection<E> |
objects(int[] indices)
Looks up the objects corresponding to an array of indices, and returns them in a Collection. |
java.util.List<E> |
objectsList()
Returns a complete List of indexed objects, in the order of their indices. |
boolean |
remove(java.lang.Object o)
|
boolean |
removeAll(java.util.Collection<?> e)
|
void |
saveToFilename(java.lang.String file)
Save the contents of this index into a file. |
void |
saveToWriter(java.io.Writer bw)
This saves the contents of this index into string form, as part of a larger text-serialization. |
int |
size()
Returns the number of indexed objects. |
java.lang.String |
toString()
Returns a readable version of the Index contents |
java.lang.String |
toString(int n)
Returns a readable version of at least part of the Index contents. |
java.lang.String |
toStringOneEntryPerLine()
|
java.lang.String |
toStringOneEntryPerLine(int n)
|
void |
unlock()
Unlocks the Index. |
HashIndex<E> |
unmodifiableView()
Returns an unmodifiable view of the Index. |
| Methods inherited from class java.util.AbstractCollection |
|---|
containsAll, isEmpty, retainAll, toArray, toArray |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface edu.stanford.nlp.util.Index |
|---|
toArray |
| Methods inherited from interface java.util.Collection |
|---|
equals, hashCode |
| Constructor Detail |
|---|
public HashIndex()
public HashIndex(int capacity)
capacity - Initial capacity of Index.public HashIndex(java.util.Collection<? extends E> c)
c - A collection of objectspublic HashIndex(Index<? extends E> index)
| Method Detail |
|---|
public void clear()
clear in interface Index<E>clear in interface java.util.Collection<E>clear in class java.util.AbstractCollection<E>public int[] indices(java.util.Collection<E> elems)
elems - The list of items
public java.util.Collection<E> objects(int[] indices)
Collection.
This collection is not a copy, but accesses the data structures of the Index.
objects in interface Index<E>indices - An array of indices
Collection of the objects corresponding to the indices argument.public int size()
size in interface Index<E>size in interface java.util.Collection<E>size in class java.util.AbstractCollection<E>public E get(int i)
get in interface Index<E>i - the integer index to be queried for the corresponding argument
public java.util.List<E> objectsList()
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)
objectsList in interface Index<E>List of indexed objectspublic boolean isLocked()
isLocked in interface Index<E>public void lock()
add(E) will
leave the Index unchanged and return false).
lock in interface Index<E>public void unlock()
add(E) will
leave the Index unchanged and return false).
unlock in interface Index<E>public int indexOf(E o)
indexOf in interface Index<E>o - the Object whose index is desired.
public int indexOf(E o,
boolean add)
Notes: The method indexOf(x, true) is the direct replacement for the number(x) method in the old Numberer class. This method now uses a Semaphore object to make the index safe for concurrent multithreaded usage. (CDM: Is this better than using a synchronized block?)
indexOf in interface Index<E>o - the Object whose index is desired.add - Whether it is okay to add new items to the index
public boolean addAll(java.util.Collection<? extends E> c)
addAll in interface Index<E>addAll in interface java.util.Collection<E>addAll in class java.util.AbstractCollection<E>public boolean add(E o)
add in interface Index<E>add in interface java.util.Collection<E>add in class java.util.AbstractCollection<E>public boolean contains(java.lang.Object o)
contains in interface Index<E>contains in interface java.util.Collection<E>contains in class java.util.AbstractCollection<E>o - the object to be queried.
public void saveToFilename(java.lang.String file)
Index
saveToFilename in interface Index<E>file - File name.public static Index<java.lang.String> loadFromFilename(java.lang.String file)
file -
public void saveToWriter(java.io.Writer bw)
throws java.io.IOException
saveToFileName.
saveToWriter in interface Index<E>bw - Writer to save to.
java.io.IOException - Exception thrown if cannot save.
public static Index<java.lang.String> loadFromReader(java.io.BufferedReader br)
throws java.io.IOException
loadFromFilename, and is intended to be included in a routine
that unpacks a text-serialized form of an object that incorporates an Index.
NOTE: presumes that the next readLine() will read in the first line of the
portion of the text file representing the saved Index. Currently reads until it
encounters a blank line, consuming that line and returning the Index.
TODO: figure out how best to terminate: currently a blank line is considered to be a terminator.
br - The Reader to read the index from
java.io.IOExceptionpublic java.lang.String toString()
toString in class java.util.AbstractCollection<E>public java.lang.String toStringOneEntryPerLine()
public java.lang.String toString(int n)
n - Show the first n items in the Index
public java.lang.String toStringOneEntryPerLine(int n)
public java.util.Iterator<E> iterator()
iterator in interface java.lang.Iterable<E>iterator in interface java.util.Collection<E>iterator in class java.util.AbstractCollection<E>public HashIndex<E> unmodifiableView()
public boolean remove(java.lang.Object o)
remove in interface java.util.Collection<E>remove in class java.util.AbstractCollection<E>public boolean removeAll(java.util.Collection<?> e)
removeAll in interface java.util.Collection<E>removeAll in class java.util.AbstractCollection<E>public static Index<java.lang.String> loadFromFileWithList(java.lang.String file)
file -
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||