Package convex.core.data
Class AVector<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>
- Type Parameters:
T- Type of element in Vector
- All Implemented Interfaces:
IAssociative<CVMLong,,T> IValidated,IWriteable,Iterable<T>,Collection<T>,List<T>,SequencedCollection<T>
- Direct Known Subclasses:
ASpecialVector,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
countFields inherited from class convex.core.data.ACell
cachedRef, memorySize -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract booleanabstract booleanAppends 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.abstract longComputes 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.Adds an element to the sequence in the natural positionconjAll(ACollection<? extends T> 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 intencodeRaw(byte[] bs, int pos) Writes this Cell's encoding to a byte array, excluding the tag byte.final booleanChecks for equality with another Cell.abstract booleanget(int index) Gets the element at the specified index in this sequence.abstract Tget(long i) Gets the element at the specified index in this vectorabstract VectorLeaf<T> getChunk(long offset) Gets the VectorLeaf chunk at a given offsetgetElementRefUnsafe(long i) Gets an element Ref from this vector, assuming bounds already checkedbytegetTag()Gets the tag byte for this cell.getType()Gets the most specific known runtime Type for this Cell.final intabstract booleanReturns true if this vector is in canonical format, i.e.abstract booleanReturns true if this Vector is a single fully packed tree.booleanisPacked()Returns true if this Vector is a packed packed tree.iterator()final intfinal 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 remain after the first (i.e.booleanprint(BlobBuilder sb, long limit) Prints this Object to a readable String Representation.abstract <R> Rreduce(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.subVector(long start, long length) Gets a vector containing the specified subset of this sequence, or null if range is invalidObject[]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, isCVMValue, longIndexOf, longLastIndexOf, remove, set, spliterator, spliterator, subList, toCellArray, visitElementRefsMethods inherited from class convex.core.data.ACollection
add, addAll, clear, containsAll, copyToArray, encode, remove, removeAll, retainAll, toArray, toVectorMethods inherited from class convex.core.data.ADataStructure
checkIndex, count, isDataValue, isEmpty, size, toCVMStringMethods inherited from class convex.core.data.ACountable
sliceMethods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getMemorySize, getRef, getRef, getRefCount, hashCode, isEmbedded, toCanonical, toString, validate, validateCellMethods inherited from class convex.core.data.AObject
attachEncoding, print, printMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface convex.core.data.IWriteable
estimatedEncodingSizeMethods inherited from interface java.util.List
add, addAll, addFirst, addLast, clear, containsAll, equals, getFirst, getLast, hashCode, isEmpty, listIterator, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, size, sort, toArray
-
Constructor Details
-
AVector
public AVector(long count)
-
-
Method Details
-
getType
-
get
-
appendChunk
-
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
-
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:AObjectPrints 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:ASequenceGets 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:ACollectionMaps a function over a collection, applying it to each element in turn. -
flatMap
-
concat
Description copied from class:ASequenceConcatenates the elements from another sequence to the end of this sequence. Potentially O(n) in size of resulting sequence -
reduce
-
iterator
-
toArray
-
indexOf
-
lastIndexOf
-
listIterator
-
listIterator
Description copied from class:ASequenceGets the ListIterator for a long position- Specified by:
listIteratorin classASequence<T extends ACell>- Parameters:
index-- 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:
isCanonicalin classACell- Returns:
- true if the vector is in canonical format, false otherwise
-
updateRefs
Description copied from class:ACellUpdates 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:
updateRefsin classACell- Parameters:
func- Ref update function- Returns:
- Cell with updated Refs
-
commonPrefixLength
-
appendAll
-
conj
-
conjAll
Description copied from class:ADataStructureAdds 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:
conjAllin classADataStructure<T extends ACell>- Parameters:
xs- New elements to add- Returns:
- The updated data structure, or null if a failure occurred due to invalid element types
-
cons
-
next
-
slice
Description copied from class:ASequenceProduces 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. -
subVector
Description copied from class:ASequenceGets a vector containing the specified subset of this sequence, or null if range is invalid -
assoc
Description copied from class:ASequenceUpdates a value at the given position in the sequence. -
empty
-
reverse
-
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 @
-
equals
Description copied from class:ACellChecks for equality with another Cell. In general, Cells are considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if they have a more efficient equals implementation. MUST NOT require reads from Store. -
equals
-
getTag
public byte getTag()Description copied from class:ACellGets 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:ACellWrites this Cell's encoding to a byte array, excluding the tag byte. -
getElementRefUnsafe
-