Class SetLeaf<T extends ACell>

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

public class SetLeaf<T extends ACell> extends AHashSet<T>
Limited size Persistent Merkle Set implemented as a small sorted list of Values Must be sorted by Key hash value to ensure uniqueness of representation
  • Field Details

    • MAX_ELEMENTS

      public static final int MAX_ELEMENTS
      Maximum number of elements in a SetLeaf
      See Also:
      Constant Field Values
    • MAX_ENCODING_LENGTH

      public static int MAX_ENCODING_LENGTH
  • Method Details

    • create

      @SafeVarargs public static <V extends ACell> SetLeaf<V> create(Ref<V>... elements)
      Creates a SetLeaf with the specified elements. Null entries are ignored/removed.
      Parameters:
      elements - Refs of Elements to include
      Returns:
      New ListMap
    • unsafeCreate

      public static <V extends ACell> SetLeaf<V> unsafeCreate(Ref<V>... refs)
      Create a SetLeaf with raw element Refs. Can create an invalid Cell, useful mainly for testing
      Parameters:
      refs - Refs to set elements, in desired order
      Returns:
      SetLeaf instance, possibly invalid
    • unsafeCreate

      public static <V extends ACell> SetLeaf<V> unsafeCreate(V... elements)
      Create a SetLeaf with raw elements. Can create an invalid Cell, useful mainly for testing
      Parameters:
      elements - Elements to include in set, in desired order
      Returns:
      SetLeaf instance, possibly invalid
    • create

      protected static <V extends ACell> SetLeaf<V> create(Ref<V>[] entries, int offset, int length)
      Creates a SetLeaf with the specified elements. Null references are ignored/removed.
      Parameters:
      entries - Refs to elements to include (some may be null)
      offset - Offset into entries array
      length - Number of entries to take from entries array, starting at offset
      Returns:
      A new ListMap
    • create

      public static <V extends ACell> SetLeaf<V> create(V item)
    • 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
    • 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
    • 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 SetLeaf<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 SetLeaf<T> excludeRef(Ref<T> key)
      Specified by:
      excludeRef in class AHashSet<T extends ACell>
    • accumulateValues

      protected void accumulateValues(ArrayList<T> al)
    • 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. Cell must be canonical, or else an error may occur.
      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> SetLeaf<V> read(ByteBuffer bb, long count) throws BadFormatException
      Reads a MapLeaf from the provided ByteBuffer Assumes the header byte is already read.
      Parameters:
      bb - ByteBuffer to read from
      count - Count of map elements
      Returns:
      A Map as deserialised from the provided ByteBuffer
      Throws:
      BadFormatException - If encoding is invalid
    • emptySet

      public static <V extends ACell> SetLeaf<V> emptySet()
    • isCanonical

      public boolean isCanonical()
      Description copied from class: ACell
      Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).
      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 Cell represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false, pretty much everything else should return true. Note: 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 and is defined by the Cell encoding rules. Contained Refs may be either external or embedded.
      Specified by:
      getRefCount in class ACell
      Returns:
      The number of Refs in this Cell
    • getRef

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

      public SetLeaf 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
    • filterHexDigits

      public SetLeaf<T> filterHexDigits(int digitPos, int mask)
      Filters this ListMap to contain only key hashes with the hex digits specified in the given Mask
      Parameters:
      digitPos - Position of the hex digit to filter
      mask - Mask of digits to include
      Returns:
      Filtered ListMap
    • 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>
    • mergeWith

      public AHashSet<T> mergeWith(SetLeaf<T> b, int setOp, int shift)
    • reduceValues

      public <R> R reduceValues(BiFunction<? super R,​? super T,​? extends R> func, R initial)
    • 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
    • equals

      public boolean equals(SetLeaf<T> a)
    • 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 prefix, int digit, int position) throws InvalidDataException
      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:
      prefix - Hash for earlier prefix values
      digit - Hex digit expected at position [shift]
      Throws:
      InvalidDataException
    • 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
    • isSubset

      public boolean isSubset(ASet<T> b)
      Description copied from class: ASet
      Tests if this set is a (non-strict) subset of another Set
      Overrides:
      isSubset in class ASet<T extends ACell>
      Parameters:
      b - Set to test against
      Returns:
      True if this is a subset of the other set, false otherwise.
    • containsAll

      protected boolean containsAll(SetLeaf<T> b)
    • 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
    • includeRef

      protected AHashSet<T> includeRef(Ref<T> e, int shift)
      Specified by:
      includeRef in class AHashSet<T extends ACell>
    • 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
    • get

      public T get(long index)
      Description copied from class: ACountable
      Gets the element at the specified index in this collection
      Overrides:
      get in class ASet<T extends ACell>
      Parameters:
      index - Index of element to get
      Returns:
      Element at the specified index
    • toCanonical

      public AHashSet<T> toCanonical()
      Description copied from class: ACell
      Converts this Cell to its canonical version. Returns this Cell if already canonical, may be O(n) in size of value otherwise.
      Specified by:
      toCanonical in class AHashSet<T extends ACell>
      Returns:
      Canonical version of Cell