Class TokenSet

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

public class TokenSet extends ASet implements 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-21, BSD License, Christian Gruen
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[][]
    Hashed keys.

    Fields inherited from class org.basex.util.hash.ASet

    buckets, next, size
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    TokenSet(byte[]... key)
    Constructor, specifying initial keys.
    Constructor, specifying some initial input.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    add(byte[] key)
    Stores the specified key if it has not been stored before.
    final boolean
    add(String key)
    Stores the specified string as key if it has not been stored before.
    void
    Resets the data structure.
    final boolean
    contains(byte[] key)
    Checks if the set contains the specified key.
    protected int
    hash(int id)
    Returns the hash value of the element with the specified id.
    final int
    id(byte[] key)
    Returns the id of the specified key, or 0 if the key does not exist.
    final Iterator<byte[]>
     
    final byte[]
    key(int id)
    Returns the key with the specified id.
    final int
    put(byte[] key)
    Stores the specified key and returns its id.
    void
    Reads the data structure from the specified input.
    protected void
    rehash(int newSize)
    Rehashes all entries.
    int
    remove(byte[] key)
    Removes the entry with the specified key.
     
    void
    Writes the data structure to the specified output.

    Methods inherited from class org.basex.util.hash.ASet

    capacity, checkSize, isEmpty, size, toString, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • keys

      protected byte[][] keys
      Hashed keys.
  • Constructor Details

    • TokenSet

      public TokenSet()
      Default constructor.
    • TokenSet

      public TokenSet(byte[]... key)
      Constructor, specifying initial keys.
      Parameters:
      key - initial keys
    • TokenSet

      public TokenSet(DataInput in) throws IOException
      Constructor, specifying some initial input.
      Parameters:
      in - input stream
      Throws:
      IOException - I/O exception
  • Method Details

    • read

      public void read(DataInput in) throws IOException
      Reads the data structure from the specified input.
      Parameters:
      in - input stream
      Throws:
      IOException - I/O exception
    • write

      public void write(DataOutput out) throws IOException
      Writes the data structure to the specified output.
      Parameters:
      out - output stream
      Throws:
      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:
      true if the key did not exist yet and was stored
    • add

      public final boolean add(String key)
      Stores the specified string as key if it has not been stored before.
      Parameters:
      key - string to be added
      Returns:
      true 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
    • remove

      public int remove(byte[] key)
      Removes the entry with 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:
      id of the deleted key, or 0 if the key did not exist
    • 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 void clear()
      Description copied from class: ASet
      Resets the data structure.
      Overrides:
      clear in class ASet
    • iterator

      public final Iterator<byte[]> iterator()
      Specified by:
      iterator in interface Iterable<byte[]>
    • toString

      public String toString()
      Overrides:
      toString in class Object