Package convex.core.data
Class AVector<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>
- Type Parameters:
T
- Type of element in Vector
- All Implemented Interfaces:
IAssociative<CVMLong,
,T> IValidated
,IWriteable
,Iterable<T>
,Collection<T>
,List<T>
- Direct Known Subclasses:
AMapEntry
,VectorArray
,VectorLeaf
,VectorTree
Abstract base class for vectors.
Vectors are immutable sequences of values, with efficient appends to the tail
of the list.
This is a hierarchy with multiple implementations for different vector types,
but all should conform to the general AVector interface. We use an abstract
base class in preference to an interface because we control the hierarchy and
it offers some mild performance advantages.
General design goals: - Immutability - Cell structure breakdown for larger
vectors, while keeping a shallow tree - Optimised performance for end of
vector (conj, pop, last etc.) - Fast prefix comparisons to support consensus
algorithm
"If I had any recommendation to you at all, it's just if you're thinking
about designing a system and you're not sure, whether you can answer all that
questions in the forward direction, choose immutability. You can almost back
into a little more than 50% of this design just by haven taken immutability
as a constraint, saying 'oh my god now what am I gonna do? I cannot change
this. I better do this!' And keep forcing you into good answers. So if I had
any architectural guidance from this: Just do it. Choose immutability and see
where it takes you." - Rich Hickey
-
Field Summary
Fields inherited from class convex.core.data.ADataStructure
count
Fields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract boolean
abstract boolean
Appends a single element to this vectorappendChunk
(VectorLeaf<T> listVector) Appends a ListVector chunk to this vector.assoc
(long i, R value) Updates a value at the given position in the sequence.abstract 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.conj
(R value) Adds an element to the sequence in the natural positionconjAll
(ACollection<R> xs) Adds multiple elements to this data structure, in the natural manner defined by the general data structure type.Prepends an element to this sequence, returning a list.empty()
Returns an empty instance of the same Type as this data structure.abstract int
encodeRaw
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, excluding the tag byte.get
(int index) Gets the element at the specified index in this sequence.abstract T
get
(long i) Gets the element at the specified index in this vectorabstract VectorLeaf<T>
getChunk
(long offset) Gets the VectorLeaf chunk at a given offsetbyte
getTag()
Gets the tag byte for this cell.getType()
Gets the most specific known runtime Type for this Cell.final int
abstract boolean
Returns true if this vector is in canonical format, i.e.abstract boolean
Returns true if this Vector is a single fully packed tree.boolean
isPacked()
Returns true if this Vector is a packed packed tree.iterator()
final int
final ListIterator<T>
listIterator
(int index) abstract ListIterator<T>
listIterator
(long index) Gets the ListIterator for a long positionMaps a function over a collection, applying it to each element in turn.mergeWith
(AVector<T> b, MergeFunction<T> func) Merges this vector with another vector, using the provided merge function.next()
Gets the sequence of all elements after the first, or null if no elements remainboolean
print
(BlobBuilder sb, long limit) Prints this Object to a readable String Representation.abstract <R> R
reduce
(BiFunction<? super R, ? super T, ? extends R> func, R value) reverse()
Reverses a sequence, converting Lists to Vectors and vice versaslice
(long start, long end) Produces a slice of this sequence, beginning with the specified start index and of the given length.abstract Spliterator<T>
spliterator
(long position) Object[]
toArray()
updateRefs
(IRefFunction func) Updates all Refs in this object using the given function.Methods inherited from class convex.core.data.ASequence
add, addAll, assoc, checkRange, contains, containsKey, forEach, get, get, getElementRef, longIndexOf, longLastIndexOf, remove, set, subList, subVector, toCellArray, visitElementRefs
Methods inherited from class convex.core.data.ACollection
add, addAll, clear, containsAll, copyToArray, encode, remove, removeAll, retainAll, toArray, toVector
Methods inherited from class convex.core.data.ADataStructure
count, isEmpty, size, toCVMString
Methods inherited from class convex.core.data.ACountable
slice
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, getEncodingLength, getHash, getMemorySize, getRef, getRef, getRefCount, hashCode, isCompletelyEncoded, isCVMValue, isEmbedded, mark, mark, toCanonical, toString, validate, validateCell, 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
Methods inherited from interface convex.core.data.IWriteable
estimatedEncodingSize
-
Constructor Details
-
AVector
public AVector(long count)
-
-
Method Details
-
getType
Description copied from class:ACell
Gets the most specific known runtime Type for this Cell.- Specified by:
getType
in classACollection<T extends ACell>
- Returns:
- The Type of this Call
-
get
Gets the element at the specified index in this vector -
appendChunk
Appends a ListVector chunk to this vector. This vector must contain a whole number of chunks- Parameters:
listVector
- A chunk to append. Must be a ListVector of maximum size- Returns:
- The updated vector, of the same type as this vector @
-
getChunk
Gets the VectorLeaf chunk at a given offset- Parameters:
offset
- Offset into this vector. Must be a valid chunk start position- Returns:
- The chunk referenced
-
append
Appends a single element to this vector- Parameters:
value
- Value to append- Returns:
- Updated vector
-
isFullyPacked
public abstract boolean isFullyPacked()Returns true if this Vector is a single fully packed tree. i.e. a full ListVector or TreeVector.- Returns:
- true if fully packed, false otherwise
-
isPacked
public boolean isPacked()Returns true if this Vector is a packed packed tree. i.e. an exact whole number of chunks- Returns:
- true if packed, false otherwise
-
print
Description copied from class:AObject
Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible. -
get
Description copied from class:ASequence
Gets the element at the specified index in this sequence. Behaves as if the index was considered as a long -
anyMatch
-
allMatch
-
map
Description copied from class:ACollection
Maps a function over a collection, applying it to each element in turn. -
flatMap
-
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 -
reduce
-
spliterator
-
spliterator
-
iterator
-
toArray
-
indexOf
-
lastIndexOf
-
listIterator
-
listIterator
Description copied from class:ASequence
Gets the ListIterator for a long position- Specified by:
listIterator
in classASequence<T extends ACell>
- Returns:
- ListIterator instance.
-
isCanonical
public abstract boolean isCanonical()Returns true if this vector is in canonical format, i.e. suitable as top-level serialised representation of a vector.- Specified by:
isCanonical
in classACell
- Returns:
- true if the vector is in canonical format, false otherwise
-
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.- Overrides:
updateRefs
in classACell
- Parameters:
func
- Ref update function- Returns:
- Cell with updated Refs
-
commonPrefixLength
Computes the length of the longest common prefix of this vector and another vector.- Parameters:
b
- Any vector- Returns:
- Length of the longest common prefix
-
appendAll
-
conj
Description copied from class:ASequence
Adds an element to the sequence in the natural position -
conjAll
Description copied from class:ADataStructure
Adds multiple elements to this data structure, in the natural manner defined by the general data structure type. e.g. append at the end of a vector. This may be more efficient than using 'conj' for individual items.- Overrides:
conjAll
in classADataStructure<T extends ACell>
- Type Parameters:
R
- Type of Value added- Parameters:
xs
- New elements to add- Returns:
- The updated data structure, or null if a failure occurred due to invalid elementtypes
-
cons
Description copied from class:ASequence
Prepends an element to this sequence, returning a list. -
next
Description copied from class:ASequence
Gets the sequence of all elements after the first, or null if no elements remain -
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. -
assoc
Description copied from class:ASequence
Updates a value at the given position in the sequence. -
empty
Description copied from class:ADataStructure
Returns an empty instance of the same Type as this data structure.- Specified by:
empty
in classADataStructure<T extends ACell>
- Returns:
- An empty data structure
-
reverse
Description copied from class:ASequence
Reverses a sequence, converting Lists to Vectors and vice versa -
mergeWith
Merges this vector with another vector, using the provided merge function. Returns the same vector if the result is equal to this vector, or the other vector if the result is exactly equal to the other vector. The merge function is passed null for elements where one vector is shorter than the other.- Parameters:
b
- Another vectorfunc
- A merge function to apply to all elements of this and the other vector- Returns:
- A new vector, equal in length to the largest of the two vectors passed @
-
getTag
public byte getTag()Description copied from class:ACell
Gets the tag byte for this cell. The tag byte is always equal to the first byte of the Cell's canonical Encoding, and is sufficient to distinguish how to read the rest of the encoding. -
encodeRaw
public abstract int encodeRaw(byte[] bs, int pos) Description copied from class:ACell
Writes this Cell's encoding to a byte array, excluding the tag byte.
-