org.basex.util.hash
Class TokenSet

java.lang.Object
  extended by org.basex.util.hash.ASet
      extended by org.basex.util.hash.TokenSet
All Implemented Interfaces:
java.lang.Iterable<byte[]>
Direct Known Subclasses:
Functions, MemValues, Names, QNmCache, StopWords, TokenBoolMap, TokenIntMap, TokenMap, TokenObjMap

public class TokenSet
extends ASet
implements java.lang.Iterable<byte[]>

This is an efficient and memory-saving hash set for storing tokens. The first entry of the token set (offset 0) is always empty.

Author:
BaseX Team 2005-13, BSD License, Christian Gruen

Field Summary
protected  byte[][] keys
          Hashed keys.
 
Fields inherited from class org.basex.util.hash.ASet
bucket, next, size
 
Constructor Summary
TokenSet()
          Default constructor.
TokenSet(byte[]... key)
          Constructor, specifying initial keys.
TokenSet(DataInput in)
          Constructor, specifying some initial input.
 
Method Summary
 boolean add(byte[] key)
          Stores the specified key if it has not been stored before.
 boolean add(java.lang.String key)
          Stores the specified string as key if it has not been stored before.
 void clear()
          Resets the data structure.
 boolean contains(byte[] key)
          Checks if the set contains the specified key.
 int delete(byte[] key)
          Deletes the specified key.
protected  int hash(int id)
          Returns the hash value of the element with the specified id.
 int id(byte[] key)
          Returns the id of the specified key, or 0 if the key does not exist.
 java.util.Iterator<byte[]> iterator()
           
 byte[] key(int id)
          Returns the key with the specified id.
 int put(byte[] key)
          Stores the specified key and returns its id.
protected  void read(DataInput in)
          Reads the data structure from the specified input.
protected  void rehash(int newSize)
          Rehashes all entries.
 java.lang.String toString()
           
 void write(DataOutput out)
          Writes the data structure to the specified output.
 
Methods inherited from class org.basex.util.hash.ASet
checkSize, isEmpty, size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

keys

protected byte[][] keys
Hashed keys.

Constructor Detail

TokenSet

public TokenSet()
Default constructor.


TokenSet

public TokenSet(byte[]... key)
Constructor, specifying initial keys.

Parameters:
key - initial keys

TokenSet

public TokenSet(DataInput in)
         throws java.io.IOException
Constructor, specifying some initial input.

Parameters:
in - input stream
Throws:
java.io.IOException - I/O exception
Method Detail

read

protected void read(DataInput in)
             throws java.io.IOException
Reads the data structure from the specified input.

Parameters:
in - input stream
Throws:
java.io.IOException - I/O exception

write

public void write(DataOutput out)
           throws java.io.IOException
Writes the data structure to the specified output.

Parameters:
out - output stream
Throws:
java.io.IOException - I/O exception

add

public final boolean add(byte[] key)
Stores the specified key if it has not been stored before.

Parameters:
key - key to be added
Returns:
if the key did not exist yet and was stored

add

public boolean add(java.lang.String key)
Stores the specified string as key if it has not been stored before.

Parameters:
key - string to be added
Returns:
if the key did not exist yet and was stored

put

public final int put(byte[] key)
Stores the specified key and returns its id.

Parameters:
key - key to be added
Returns:
unique id of stored key (larger than zero)

contains

public final boolean contains(byte[] key)
Checks if the set contains the specified key.

Parameters:
key - key to be looked up
Returns:
result of check

id

public final int id(byte[] key)
Returns the id of the specified key, or 0 if the key does not exist.

Parameters:
key - key to be looked up
Returns:
id, or 0 if key does not exist

key

public final byte[] key(int id)
Returns the key with the specified id. All ids starts with 1 instead of 0.

Parameters:
id - id of the key to return
Returns:
key

delete

public int delete(byte[] key)
Deletes the specified key. The deletion of keys will lead to empty entries. If ASet.size is called after deletions, the original number of entries will be returned.

Parameters:
key - key
Returns:
deleted key or 0

hash

protected int hash(int id)
Description copied from class: ASet
Returns the hash value of the element with the specified id.

Specified by:
hash in class ASet
Parameters:
id - id of the element
Returns:
hash value

rehash

protected void rehash(int newSize)
Description copied from class: ASet
Rehashes all entries.

Specified by:
rehash in class ASet
Parameters:
newSize - new hash size

clear

public final void clear()
Description copied from class: ASet
Resets the data structure. Must be called when data structure is initialized.

Overrides:
clear in class ASet

iterator

public final java.util.Iterator<byte[]> iterator()
Specified by:
iterator in interface java.lang.Iterable<byte[]>

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object