Package convex.core.data
Class SetLeaf<T extends ACell>
java.lang.Object
convex.core.data.AObject
convex.core.data.ACell
convex.core.data.ACountable<E>
convex.core.data.ADataStructure<T>
convex.core.data.ACollection<T>
convex.core.data.ASet<T>
convex.core.data.AHashSet<T>
convex.core.data.SetLeaf<T>
- Type Parameters:
T
- Type of values
- All Implemented Interfaces:
IAssociative<T,CVMBool>
,IValidated
,IWriteable
,Iterable<T>
,Collection<T>
,Set<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 Summary
FieldsModifier and TypeFieldDescriptionstatic int
Maximum number of elements in a SetLeafstatic int
Fields inherited from class convex.core.data.AHashSet
MAX_SHIFT, OP_DIFF_LEFT, OP_DIFF_RIGHT, OP_INTERSECTION, OP_UNION
Fields inherited from class convex.core.data.ADataStructure
count
Fields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
accumulateValues(ArrayList<T> al)
boolean
containsAll(ASet<T> b)
Tests if this set contains all the elements of another setprotected boolean
containsAll(SetLeaf<T> b)
boolean
containsHash(Hash hash)
Tests if this Set contains a given hashprotected <R> void
copyToArray(R[] arr, int offset)
Copies the elements of this collection in order to an array at the specified offsetCreates a SetLeaf with the specified elements.Creates a SetLeaf with the specified elements.create(V item)
emptySet()
int
encode(byte[] bs, int pos)
Writes this Cell's encoding to a byte array, including a tag byte which will be written first.int
encodeRaw(byte[] bs, int pos)
Writes this Cell's encoding to a byte array, excluding the tag byte.boolean
Checks if another set is exactly equal to this setboolean
int
Estimate the encoded data size for this Cell.Updates the set to exclude the given elementexcludeRef(Ref<T> key)
filterHexDigits(int digitPos, int mask)
Filters this ListMap to contain only key hashes with the hex digits specified in the given Maskget(long index)
Gets the element at the specified index in this collectiongetElementRef(long i)
Gets a Ref to the element at the specified index in this collectiongetRef(int i)
Gets a numbered child Ref from within this Cell.getRefByHash(Hash hash)
Gets the Ref in the Set for a given hash, or null if not foundint
Gets the number of Refs contained within this Cell.getValueRef(ACell k)
Gets the Ref in the Set for a given value, or null if not foundincludeRef(Ref<T> ref)
Adds a value to this set using a Ref to the valueincludeRef(Ref<T> e, int shift)
boolean
Returns true if this Cell is in a canonical representation for message writing.boolean
Returns true if this Cell represents a first class CVM Value.boolean
Tests if this set is a (non-strict) subset of another Setread(ByteBuffer bb, long count)
Reads a MapLeaf from the provided ByteBuffer Assumes the header byte is already read.<R> R
reduceValues(BiFunction<? super R,? super T,? extends R> func, R initial)
Converts this Cell to its canonical version.unsafeCreate(Ref<V>... refs)
Create a SetLeaf with raw element Refs.unsafeCreate(V... elements)
Create a SetLeaf with raw elements.updateRefs(IRefFunction func)
Updates all Refs in this object using the given function.void
validate()
Validates the complete structure of this object.void
Validates the local structure and invariants of this cell.protected void
validateWithPrefix(Hash prefix, int digit, int position)
Validates the set with a given hex prefix.Methods inherited from class convex.core.data.AHashSet
applyOp, applySelf, conj, conjAll, contains, disjAll, excludeAll, get, getByHash, include, includeAll, intersectAll, reverseOp, toArray
Methods inherited from class convex.core.data.ASet
assoc, contains, containsKey, empty, equals, get, getTag, getType, map, print, toVector
Methods inherited from class convex.core.data.ACollection
add, addAll, clear, containsAll, iterator, remove, removeAll, retainAll, toArray, toCellArray
Methods inherited from class convex.core.data.ADataStructure
count, isEmpty, size, toCVMString
Methods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, hashCode, isCompletelyEncoded, isEmbedded, mark, mark, toString, write
Methods inherited from class convex.core.data.AObject
attachEncoding, print, print
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Field Details
-
MAX_ELEMENTS
public static final int MAX_ELEMENTSMaximum number of elements in a SetLeaf- See Also:
- Constant Field Values
-
MAX_ENCODING_LENGTH
public static int MAX_ENCODING_LENGTH
-
-
Method Details
-
create
Creates a SetLeaf with the specified elements. Null entries are ignored/removed.- Parameters:
elements
- Refs of Elements to include- Returns:
- New ListMap
-
unsafeCreate
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
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
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 arraylength
- Number of entries to take from entries array, starting at offset- Returns:
- A new ListMap
-
create
-
getValueRef
Description copied from class:ASet
Gets the Ref in the Set for a given value, or null if not found- Specified by:
getValueRef
in classASet<T extends ACell>
- Parameters:
k
- Value to check for set membership- Returns:
- Ref to value, or null
-
containsHash
Description copied from class:AHashSet
Tests if this Set contains a given hash- Specified by:
containsHash
in classAHashSet<T extends ACell>
- Parameters:
hash
- Hash to test for set membership- Returns:
- True if set contains value for given hash, false otherwise
-
getRefByHash
Description copied from class:ASet
Gets the Ref in the Set for a given hash, or null if not found- Specified by:
getRefByHash
in classASet<T extends ACell>
- Parameters:
hash
- Hash to check for set membership- Returns:
- Ref to value with given Hash, or null
-
exclude
Description copied from class:ASet
Updates the set to exclude the given element -
excludeRef
- Specified by:
excludeRef
in classAHashSet<T extends ACell>
-
accumulateValues
-
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 interfaceIWriteable
- Specified by:
encode
in classACollection<T extends ACell>
- Parameters:
bs
- A byte array to which to write the encodingpos
- 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. -
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 BadFormatExceptionReads a MapLeaf from the provided ByteBuffer Assumes the header byte is already read.- Parameters:
bb
- ByteBuffer to read fromcount
- Count of map elements- Returns:
- A Map as deserialised from the provided ByteBuffer
- Throws:
BadFormatException
- If encoding is invalid
-
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 classACell
- 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 classACell
- 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 classACell
- Returns:
- The number of Refs in this Cell
-
getRef
Description copied from class:ACell
Gets a numbered child Ref from within this Cell. -
getElementRef
Description copied from class:ACountable
Gets a Ref to the element at the specified index in this collection- Specified by:
getElementRef
in classACountable<T extends ACell>
- Parameters:
i
- Index of element to get- Returns:
- Element at the specified index
-
updateRefs
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 classACell
- Parameters:
func
- Ref update function- Returns:
- Cell with updated Refs
-
filterHexDigits
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 filtermask
- Mask of digits to include- Returns:
- Filtered ListMap
-
mergeWith
-
mergeWith
-
mergeWith
-
reduceValues
-
equals
Description copied from class:ASet
Checks if another set is exactly equal to this set -
equals
-
validate
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:- Call super.validate() - which will indirectly call validateCell()
- Call validate() on any contained cells in this class
- Specified by:
validate
in interfaceIValidated
- Overrides:
validate
in classACell
- Throws:
InvalidDataException
- If the data Valie is invalid in any way
-
validateWithPrefix
protected void validateWithPrefix(Hash prefix, int digit, int position) throws InvalidDataExceptionDescription 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 classAHashSet<T extends ACell>
- Parameters:
prefix
- Hash for earlier prefix valuesdigit
- Hex digit expected at position [shift]- Throws:
InvalidDataException
-
validateCell
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 classACell
- Throws:
InvalidDataException
- If the Cell is invalid
-
containsAll
Description copied from class:ASet
Tests if this set contains all the elements of another set- Specified by:
containsAll
in classASet<T extends ACell>
- Parameters:
b
- Set to compare with- Returns:
- True if other set is completely contained within this set, false otherwise
-
isSubset
Description copied from class:ASet
Tests if this set is a (non-strict) subset of another Set -
containsAll
-
includeRef
Description copied from class:ASet
Adds a value to this set using a Ref to the value- Specified by:
includeRef
in classAHashSet<T extends ACell>
- Parameters:
ref
- Ref to value to include- Returns:
- Updated set
-
includeRef
- Specified by:
includeRef
in classAHashSet<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 classACollection<T extends ACell>
- Type Parameters:
R
- Type of array elements required
-
get
Description copied from class:ACountable
Gets the element at the specified index in this collection -
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 classAHashSet<T extends ACell>
- Returns:
- Canonical version of Cell
-