Package convex.core.data
Class VectorTree<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.ASequence<T>
convex.core.data.AVector<T>
convex.core.data.VectorTree<T>
- Type Parameters:
T
- Type of Vector elements
- All Implemented Interfaces:
IAssociative<CVMLong,T>
,IValidated
,IWriteable
,Iterable<T>
,Collection<T>
,List<T>
Persistent Vector implemented as a merkle tree of chunks
shift indicates the level of the tree: 4 = 1st level, 8 = second etc.
Invariants:
- All children except the last must be fully packed
- Each non-terminal leaf chunk must be a tailless VectorLeaf of size 16
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic int
static int
static int
Fields inherited from class convex.core.data.ADataStructure
count
Fields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY
-
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
Appends a single element to this vectorappendChunk(VectorLeaf<T> b)
Appends a ListVector chunk to this vector.assoc(long i, R value)
Updates a value at the given position in the sequence.long
commonPrefixLength(AVector<T> b)
Computes the length of the longest common prefix of this vector and another vector.static int
computeShift(long count)
Computes the shift value for a BlockVector of the given count Note: if returns zero, count cannot be supported by a valid BlockVectorConcatenates the elements from another sequence to the end of this sequence.protected <K> void
copyToArray(K[] arr, int offset)
Copies the elements of this collection in order to an array at the specified offsetstatic <T extends ACell>
VectorTree<T>Create a TreeVector with the specified elements - things must have at least 32 elements (the minimum TreeVector size) - must be a whole multiple of 16 elements (complete chunks only)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.int
Estimate the encoded data size for this Cell.void
get(long i)
Gets the element at the specified index in this vectorgetChunk(long offset)
Gets the VectorLeaf chunk at a given offsetgetElementRef(long i)
Gets the element Ref at the specified indexlong
Method to calculate the encoding length of a Cell.getRef(int i)
Gets a numbered child Ref from within this Cell.int
Gets the number of Refs contained within this Cell.boolean
Returns true if this vector is in canonical format, i.e.boolean
Returns true if this Cell represents a first class CVM Value.boolean
Returns true if this Vector is a single fully packed tree.listIterator(long index)
Gets the ListIterator for a long positionlong
longIndexOf(Object o)
Gets the first long index at which the specified value appears in the the sequence.long
Gets the last long index at which the specified value appears in the the sequence.Maps a function over a collection, applying it to each element in turn.next()
Gets the sequence of all elements after the first, or null if no elements remainstatic <T extends ACell>
VectorTree<T>read(ByteBuffer bb, long count)
Reads a VectorTree from the provided ByteBuffer Assumes the header byte and count is already read.<R> R
reduce(BiFunction<? super R,? super T,? extends R> func, R value)
spliterator(long position)
subVector(long start, long length)
Gets a vector containing the specified subset of this sequence.Converts this Cell to its canonical version.toVector()
Converts this collection to a canonical vector of elementsupdateRefs(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.void
visitElementRefs(Consumer<Ref<T>> f)
Visits all elements in this sequence, calling the specified consumer for each.Methods inherited from class convex.core.data.AVector
appendAll, conj, conjAll, cons, empty, flatMap, get, getTag, getType, indexOf, isPacked, iterator, lastIndexOf, listIterator, mergeWith, print, reverse, slice, spliterator, toArray
Methods inherited from class convex.core.data.ASequence
add, addAll, assoc, checkRange, contains, containsKey, get, get, remove, set, subList, toCellArray
Methods inherited from class convex.core.data.ACollection
add, addAll, clear, containsAll, remove, removeAll, retainAll, toArray
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, equals, getCanonical, getChildRefs, getEncoding, 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
-
MINIMUM_SIZE
public static final int MINIMUM_SIZE- See Also:
- Constant Field Values
-
MAX_EMBEDDED_LENGTH
public static final int MAX_EMBEDDED_LENGTH- See Also:
- Constant Field Values
-
MAX_ENCODING_LENGTH
public static final int MAX_ENCODING_LENGTH
-
-
Method Details
-
computeShift
public static int computeShift(long count)Computes the shift value for a BlockVector of the given count Note: if returns zero, count cannot be supported by a valid BlockVector- Parameters:
count
- Number of elements- Returns:
- Shift value
-
create
Create a TreeVector with the specified elements - things must have at least 32 elements (the minimum TreeVector size) - must be a whole multiple of 16 elements (complete chunks only)- Parameters:
things
- Elements to includeoffset
- Offset into element arraylength
- Number of elements to include- Returns:
- New TreeVector instance
-
get
Description copied from class:AVector
Gets the element at the specified index in this vector -
getElementRef
Description copied from class:ASequence
Gets the element Ref at the specified index- Specified by:
getElementRef
in classASequence<T extends ACell>
- Parameters:
i
- Index of element to get- Returns:
- Ref to element at specified index
-
assoc
Description copied from class:ASequence
Updates a value at the given position in the sequence. -
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. -
getEncodingLength
public long getEncodingLength()Description copied from class:ACell
Method to calculate the encoding length of a Cell. May be overridden to avoid creating encodings during memory size calculations. This reduces hashing!- Overrides:
getEncodingLength
in classACell
- Returns:
- Exact encoding length of this Cell
-
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 <T extends ACell> VectorTree<T> read(ByteBuffer bb, long count) throws BadFormatExceptionReads a VectorTree from the provided ByteBuffer Assumes the header byte and count is already read.- Parameters:
bb
- ByteBuffer to read fromcount
- Number of elements- Returns:
- TreeVector instance as read from ByteBuffer
- Throws:
BadFormatException
- If encoding is invalid
-
appendChunk
Description copied from class:AVector
Appends a ListVector chunk to this vector. This vector must contain a whole number of chunks- Specified by:
appendChunk
in classAVector<T extends ACell>
- Parameters:
b
- A chunk to append. Must be a ListVector of maximum size- Returns:
- The updated vector, of the same type as this vector @
-
isFullyPacked
public boolean isFullyPacked()Description copied from class:AVector
Returns true if this Vector is a single fully packed tree. i.e. a full ListVector or TreeVector.- Specified by:
isFullyPacked
in classAVector<T extends ACell>
- Returns:
- true if fully packed, false otherwise
-
append
Description copied from class:AVector
Appends a single element to this vector -
concat
Description copied from class:ASequence
Concatenates the elements from another sequence to the end of this sequence. Potentially O(n) in size of resulting sequence -
copyToArray
protected <K> void copyToArray(K[] 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:
K
- Type of array elements required
-
longIndexOf
Description copied from class:ASequence
Gets the first long index at which the specified value appears in the the sequence.- Specified by:
longIndexOf
in classASequence<T extends ACell>
- Parameters:
o
- Any value which could appear as an element of the sequence.- Returns:
- Index of the value, or -1 if not found.
-
longLastIndexOf
Description copied from class:ASequence
Gets the last long index at which the specified value appears in the the sequence.- Specified by:
longLastIndexOf
in classASequence<T extends ACell>
- Parameters:
o
- Any value which could appear as an element of the sequence.- Returns:
- Index of the value, or -1 if not found.
-
listIterator
-
listIterator
Description copied from class:ASequence
Gets the ListIterator for a long position- Specified by:
listIterator
in classAVector<T extends ACell>
- Returns:
- ListIterator instance.
-
forEach
-
anyMatch
-
allMatch
-
map
Description copied from class:ACollection
Maps a function over a collection, applying it to each element in turn. -
visitElementRefs
Description copied from class:ASequence
Visits all elements in this sequence, calling the specified consumer for each.- Specified by:
visitElementRefs
in classASequence<T extends ACell>
- Parameters:
f
- Function to call for each element
-
reduce
-
spliterator
- Specified by:
spliterator
in classAVector<T extends ACell>
-
isCanonical
public boolean isCanonical()Description copied from class:AVector
Returns true if this vector is in canonical format, i.e. suitable as top-level serialised representation of a vector.- Specified by:
isCanonical
in classAVector<T extends ACell>
- Returns:
- true if the vector 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
-
toVector
Description copied from class:ACollection
Converts this collection to a canonical vector of elements- Specified by:
toVector
in classACollection<T extends ACell>
- Returns:
- This collection coerced to a vector
-
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. -
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.- Specified by:
updateRefs
in classAVector<T extends ACell>
- Parameters:
func
- Ref update function- Returns:
- Cell with updated Refs
-
commonPrefixLength
Description copied from class:AVector
Computes the length of the longest common prefix of this vector and another vector.- Specified by:
commonPrefixLength
in classAVector<T extends ACell>
- Parameters:
b
- Any vector- Returns:
- Length of the longest common prefix
-
getChunk
Description copied from class:AVector
Gets the VectorLeaf chunk at a given offset -
subVector
Description copied from class:ASequence
Gets a vector containing the specified subset of this sequence. -
next
Description copied from class:ASequence
Gets the sequence of all elements after the first, or null if no elements remain -
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
-
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
-
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 classACell
- Returns:
- Canonical version of Cell
-