Class SetTree<T extends ACell>

Type Parameters:
T - Type of set elemets
All Implemented Interfaces:
IAssociative<T,​CVMBool>, IValidated, IWriteable, Iterable<T>, Collection<T>, Set<T>

public class SetTree<T extends ACell> extends AHashSet<T>
Persistent Set for large hash sets requiring tree structure. Internally implemented as a radix tree, indexed by key hash. Uses an array of child Maps, with a bitmap mask indicating which hex digits are present, i.e. have non-empty children.
  • Field Details

    • MAX_ENCODING_LENGTH

      public static int MAX_ENCODING_LENGTH
  • Method Details

    • create

      public static <V extends ACell> SetTree<V> create(Ref<V>[] newEntries, int shift)
    • getElementRef

      public Ref<T> getElementRef(long i)
      Description copied from class: ACountable
      Gets a Ref to the element at the specified index in this collection
      Specified by:
      getElementRef in class ACountable<T extends ACell>
      Parameters:
      i - Index of element to get
      Returns:
      Element at the specified index
    • getRefByHash

      protected Ref<T> getRefByHash(Hash hash)
      Description copied from class: ASet
      Gets the Ref in the Set for a given hash, or null if not found
      Specified by:
      getRefByHash in class ASet<T extends ACell>
      Parameters:
      hash - Hash to check for set membership
      Returns:
      Ref to value with given Hash, or null
    • exclude

      public AHashSet<T> exclude(ACell key)
      Description copied from class: ASet
      Updates the set to exclude the given element
      Overrides:
      exclude in class AHashSet<T extends ACell>
      Parameters:
      key - Value to exclude
      Returns:
      Updated set
    • excludeRef

      public AHashSet<T> excludeRef(Ref<T> keyRef)
      Specified by:
      excludeRef in class AHashSet<T extends ACell>
    • toCanonical

      public AHashSet<T> toCanonical()
      Description copied from class: ACell
      Converts this Cell to its canonical version. Returns this if already canonical
      Specified by:
      toCanonical in class AHashSet<T extends ACell>
      Returns:
      Canonical version of Cell
    • digitForIndex

      public static int digitForIndex(int index, short mask)
    • include

      public SetTree<T> include(ACell value)
      Description copied from class: ASet
      Updates the set to include the given element
      Overrides:
      include in class AHashSet<T extends ACell>
      Parameters:
      value - Value to include
      Returns:
      Updated set
    • includeRef

      protected SetTree<T> includeRef(Ref<T> e, int shift)
      Specified by:
      includeRef in class AHashSet<T extends ACell>
    • includeRef

      public AHashSet<T> includeRef(Ref<T> ref)
      Description copied from class: ASet
      Adds a value to this set using a Ref to the value
      Specified by:
      includeRef in class AHashSet<T extends ACell>
      Parameters:
      ref - Ref to value to include
      Returns:
      Updated set
    • encode

      public int encode(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, including a tag byte which will be written first
      Specified by:
      encode in interface IWriteable
      Specified by:
      encode in class ACollection<T extends ACell>
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • encodeRaw

      public int encodeRaw(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, excluding the tag byte
      Specified by:
      encodeRaw in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • estimatedEncodingSize

      public int estimatedEncodingSize()
      Description copied from interface: IWriteable
      Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is likely to contain the entire object when represented in binary format, including the tag byte.
      Returns:
      The estimated size for the binary representation of this object.
    • read

      public static <V extends ACell> SetTree<V> read(ByteBuffer bb, long count) throws BadFormatException
      Reads a SetTree from the provided ByteBuffer Assumes the header byte and count is already read.
      Parameters:
      bb - ByteBuffer to read from
      count - Number of elements
      Returns:
      TreeMap instance as read from ByteBuffer
      Throws:
      BadFormatException - If encoding is invalid
    • isCanonical

      public boolean isCanonical()
      Description copied from class: ACell
      Returns true if this Cell is in a canonical format for message writing. Reading or writing a non-canonical value should be considered illegal, but non-canonical objects may be used on a temporary internal basis.
      Specified by:
      isCanonical in class ACell
      Returns:
      true if the object is in canonical format, false otherwise
    • isCVMValue

      public final boolean isCVMValue()
      Description copied from class: ACell
      Returns true if this object represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false. CVM values might not be in a canonical format, e.g. temporary data structures
      Specified by:
      isCVMValue in class ACell
      Returns:
      true if the object is a CVM Value, false otherwise
    • getRefCount

      public int getRefCount()
      Description copied from class: ACell
      Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance. Contained Refs may be either external or embedded.
      Specified by:
      getRefCount in class ACell
      Returns:
      The number of Refs in this Cell
    • getRef

      public <R extends ACell> Ref<R> getRef(int i)
      Description copied from class: ACell
      Gets a numbered child Ref from within this Cell.
      Overrides:
      getRef in class ACell
      Type Parameters:
      R - Type of referenced Cell
      Parameters:
      i - Index of ref to get
      Returns:
      The Ref at the specified index
    • updateRefs

      public SetTree<T> updateRefs(IRefFunction func)
      Description copied from class: ACell
      Updates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.
      Overrides:
      updateRefs in class ACell
      Parameters:
      func - Ref update function
      Returns:
      Cell with updated Refs
    • mergeWith

      public AHashSet<T> mergeWith(AHashSet<T> b, int setOp)
      Specified by:
      mergeWith in class AHashSet<T extends ACell>
    • mergeWith

      protected AHashSet<T> mergeWith(AHashSet<T> b, int setOp, int shift)
      Specified by:
      mergeWith in class AHashSet<T extends ACell>
    • equals

      public boolean equals(ASet<T> a)
      Description copied from class: ASet
      Checks if another set is exactly equal to this set
      Specified by:
      equals in class ASet<T extends ACell>
      Parameters:
      a - Set to compare with this set
      Returns:
      true if sets are equal, false otherwise
    • validate

      public void validate() throws InvalidDataException
      Description copied from interface: IValidated
      Validates the complete structure of this object. It is necessary to ensure all child Refs are validated, so the general contract for validate is:
      1. Call super.validate() - which will indirectly call validateCell()
      2. Call validate() on any contained cells in this class
      Specified by:
      validate in interface IValidated
      Overrides:
      validate in class ACell
      Throws:
      InvalidDataException - If the data Valie is invalid in any way
    • validateWithPrefix

      protected void validateWithPrefix(Hash base, int digit, int shift)
      Description copied from class: AHashSet
      Validates the set with a given hex prefix. This is necessary to ensure that child maps are valid, in particular have the correct shift level and that all hashes start with the correct prefix of hex characters.
      Specified by:
      validateWithPrefix in class AHashSet<T extends ACell>
      Parameters:
      base - Hash for earlier prefix values
      digit - Hex digit expected at position [shift]
    • validateCell

      public void validateCell() throws InvalidDataException
      Description copied from class: ACell
      Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.
      Specified by:
      validateCell in class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • containsAll

      public boolean containsAll(ASet<T> b)
      Description copied from class: ASet
      Tests if this set contains all the elements of another set
      Specified by:
      containsAll in class ASet<T extends ACell>
      Parameters:
      b - Set to compare with
      Returns:
      True if other set is completely contained within this set, false otherwise
    • containsAll

      protected boolean containsAll(SetTree<T> map)
    • getValueRef

      public Ref<T> getValueRef(ACell k)
      Description copied from class: ASet
      Gets the Ref in the Set for a given value, or null if not found
      Specified by:
      getValueRef in class ASet<T extends ACell>
      Parameters:
      k - Value to check for set membership
      Returns:
      Ref to value, or null
    • copyToArray

      protected <R> void copyToArray(R[] arr, int offset)
      Description copied from class: ACollection
      Copies the elements of this collection in order to an array at the specified offset
      Specified by:
      copyToArray in class ACollection<T extends ACell>
      Type Parameters:
      R - Type of array elements required
    • containsHash

      public boolean containsHash(Hash hash)
      Description copied from class: AHashSet
      Tests if this Set contains a given hash
      Specified by:
      containsHash in class AHashSet<T extends ACell>
      Parameters:
      hash - Hash to test for set membership
      Returns:
      True if set contains value for given hash, false otherwise