Package convex.core.data
Class VectorLeaf<T extends ACell>
java.lang.Object
convex.core.data.AObject
convex.core.data.ACell
convex.core.data.ACountable<T>
convex.core.data.ADataStructure<T>
convex.core.data.ACollection<T>
convex.core.data.ASequence<T>
convex.core.data.AVector<T>
convex.core.data.VectorLeaf<T>
- Type Parameters:
T
- Type of vector elements
- All Implemented Interfaces:
IAssociative<CVMLong,
,T> IValidated
,IWriteable
,Iterable<T>
,Collection<T>
,List<T>
,SequencedCollection<T>
A Persistent Vector implementation representing 0-16 elements with a
packed Vector prefix.
Design goals:
- Allows fast access to most recently appended items
- O(1) append, equals - O(log n) access, update
- O(log n) comparisons
- Fast computation of common prefix
- 0x80 - VectorLeaf tag byte
- VLC Long - Length of list. Greater than 16 implies prefix must be present. Low 4 bits specify N (0 means 16 in presence of prefix)
- [Ref]*N - N Elements with length
- Ref? - Tail Ref (excluded if not present)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final VectorLeaf
<?> static final Ref
<VectorLeaf<?>> static final int
static final int
Maximum size of a single VectorLeaf before a tail is requiredFields inherited from class convex.core.data.ADataStructure
count
Fields inherited from class convex.core.data.ACell
cachedRef, memorySize
-
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
Appends a single element to this vectorappendChunk
(AVector<T> chunk) Appends a ListVector chunk to this vector.Updates a value at the given position in the sequence.long
Computes the length of the longest common prefix of this vector and another vector.Concatenates 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>
VectorLeaf<T> Creates a VectorLeaf with the given itemsstatic <T extends ACell>
VectorLeaf<T> Creates a VectorLeaf with the given items appended to the specified tailstatic <T extends ACell>
VectorLeaf<T> create
(T[] things) 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
boolean
equals
(VectorLeaf<T> v) 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 indexgetElementRefUnsafe
(long i) Gets an element Ref from this vector, assuming bounds already checkedint
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 VectorLeaf has a prefix AVector.boolean
Returns true if this vector is in canonical format, i.e.boolean
Returns true if this Vector is a single fully packed tree.static final boolean
isValidCount
(long count) Tests if a given count should result in a VectorLeaflistIterator
(long index) Gets the ListIterator for a long positionlong
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 remain after the first (i.e.long
static <T extends ACell>
VectorLeaf<T> Reads aVectorLeaf
from the provided Blob Assumes the header byte and count is already read.<R> R
reduce
(BiFunction<? super R, ? super T, ? extends R> func, R value) slice
(long start, long end) Produces a slice of this sequence, beginning with the specified start index and of the given length.Converts this Cell to a 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.withPrefix
(AVector<T> newPrefix) Methods inherited from class convex.core.data.AVector
appendAll, conj, conjAll, cons, empty, equals, flatMap, get, getTag, getType, indexOf, isPacked, iterator, lastIndexOf, listIterator, mergeWith, print, reverse, subVector, toArray
Methods inherited from class convex.core.data.ASequence
add, addAll, assoc, checkRange, contains, containsKey, get, get, isCVMValue, remove, set, spliterator, spliterator, 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
checkIndex, count, isDataValue, isEmpty, size, toCVMString
Methods inherited from class convex.core.data.ACountable
slice
Methods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getHash, getMemorySize, getMemorySize, getRef, hashCode, isEmbedded, toString
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
-
EMPTY
-
EMPTY_REF
-
MAX_SIZE
public static final int MAX_SIZEMaximum size of a single VectorLeaf before a tail is required- See Also:
-
MAX_ENCODING_LENGTH
public static final int MAX_ENCODING_LENGTH- See Also:
-
-
Method Details
-
create
Creates a VectorLeaf with the given items- Parameters:
elements
- Elements to addoffset
- Offset into element arraylength
- Number of elements to include from array- Returns:
- New VectorLeaf
-
create
public static <T extends ACell> VectorLeaf<T> create(ACell[] elements, int offset, int length, AVector<T> prefix) Creates a VectorLeaf with the given items appended to the specified tail- Parameters:
elements
- Elements to addoffset
- Offset into element arraylength
- Number of elements to include from arrayprefix
- Prefix vector to append to- Returns:
- The updated VectorLeaf
-
create
-
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
-
append
-
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 -
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:
chunk
- A chunk to append. Must be a ListVector of maximum size- Returns:
- The updated vector, of the same type as this vector @
-
get
-
getElementRef
-
getElementRefUnsafe
-
assoc
Description copied from class:ASequence
Updates a value at the given position in the sequence. -
read
public static <T extends ACell> VectorLeaf<T> read(long count, Blob b, int pos) throws BadFormatException Reads aVectorLeaf
from the provided Blob Assumes the header byte and count is already read.- Parameters:
count
- Number of elements, assumed to be validb
- Blob to read frompos
- Start position in Blob (location of tag byte)- Returns:
- New decoded instance
- Throws:
BadFormatException
- In the event of any encoding error
-
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 highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures- Returns:
- The estimated size for the binary representation of this object.
-
getEncodingLength
public int 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
-
hasPrefix
public boolean hasPrefix()Returns true if this VectorLeaf has a prefix AVector.- Returns:
- true if this VectorLeaf has a prefix, false otherwise
-
withPrefix
-
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
-
listIterator
-
listIterator
Description copied from class:ASequence
Gets the ListIterator for a long position- Specified by:
listIterator
in classAVector<T extends ACell>
- Parameters:
index
-- Returns:
- ListIterator instance.
-
prefixLength
public long prefixLength() -
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- Parameters:
arr
-offset
-
-
longIndexOf
Description copied from class:ASequence
Gets the first long index at which the specified value appears in the the sequence. Similar to Java's standard List.indexOf(...) but supports long indexes.- 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. Similar to Java's standard List.lastIndexOf(...) but supports long indexes.- 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.
-
forEach
-
anyMatch
-
allMatch
-
map
Description copied from class:ACollection
Maps a function over a collection, applying it to each element in turn. -
visitElementRefs
-
reduce
-
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
-
toCanonical
Description copied from class:ACell
Converts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise. Callers should usually use getCanonical(), which caches canonical instances once created- Specified by:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-
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.- Overrides:
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. WARNING: May need to convert to a canonical instance -
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. The implementation *should* re-attach any original encoding in order to prevent re-encoding or surplus hashing 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
-
equals
-
equals
-
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
-
slice
Description copied from class:ASequence
Produces a slice of this sequence, beginning with the specified start index and of the given length. The start and end must be contained within this sequence. Will return the same sequence if the start is zero and the length matches this sequence. -
next
-
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 Value 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
-
isValidCount
public static final boolean isValidCount(long count) Tests if a given count should result in a VectorLeaf- Parameters:
count
- Count of elements in vector- Returns:
- true if vector should be a VectorLeaf, false otherwise
-