Package convex.core.data
Class MapEntry<K extends ACell,V extends ACell>
java.lang.Object
- Type Parameters:
K- The type of keysV- The type of values
- All Implemented Interfaces:
IAssociative<CVMLong,,ACell> IValidated,IWriteable,Comparable<MapEntry<K,,V>> Iterable<ACell>,Collection<ACell>,List<ACell>,Map.Entry<K,V>
public class MapEntry<K extends ACell,V extends ACell>
extends AMapEntry<K,V>
implements Comparable<MapEntry<K,V>>
Map.Entry implementation for persistent maps. This is primarily intended as an efficient
implementation class for handling entries in Convex maps, and also to support the Java Map.Entry
interface for compatibility and developer convenience.
From a CVM perspective, a MapEntry is just a regular 2 element Vector. As such, MapEntry is *not* canonical
and getting the canonical form of a MapEntry requires converting to a Vector
Contains exactly 2 Refs, one for key and one for value
Implements Comparable using the Hash value of keys.
-
Field Summary
Fields inherited from class convex.core.data.ADataStructure
countFields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY -
Method Summary
Modifier and TypeMethodDescriptionassoc(long i, R a) Updates a value at the given position in the sequence.intConcatenates the elements from another sequence to the end of this sequence.booleanstatic MapEntryprotected <R> voidcopyToArray(R[] arr, int offset) Copies the elements of this collection in order to an array at the specified offsetcreate(K key, V value) Creates a new MapEntry with the provided key and valueintencode(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.static intencodeCompressed(MapEntry<?, ?> me, byte[] bs, int pos) Writes a MapEntry or null content in compressed format (no count).intencodeRaw(byte[] bs, int pos) Writes the raw MapEntry content.booleanChecks for equality with another Cell.booleanintEstimate the encoded data size for this Cell.get(long i) Gets the element at the specified index in this vectorgetElementRef(long i) Gets the element Ref at the specified indexgetElementRefUnsafe(long i) Gets an element Ref from this vector, assuming bounds already checkedgetKey()Gets the Hash of the key for this MapEntrygetRef(int i) Gets a numbered child Ref from within this Cell.final intGets the number of Refs contained within this Cell.bytegetTag()Gets the tag byte for this cell.getType()Gets the most specific known runtime Type for this Cell.getValue()booleanReturns true if this vector is in canonical format, i.e.final booleanReturns true if this Cell represents a first class CVM Value.booleanChecks if the keys of two map entries are equalMaps a function over a collection, applying it to each element in turn.Create a map entry, converting key and value to correct CVM types.<R> Rreduce(BiFunction<? super R, ? super ACell, ? extends R> func, R value) 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.voidvalidate()Validates the complete structure of this object.voidValidates the local structure and invariants of this cell.voidvisitElementRefs(Consumer<Ref<ACell>> f) Visits all elements in this sequence, calling the specified consumer for each.Create a new MapEntry with an updated key.Create a new MapEntry with an updated value.Methods inherited from class convex.core.data.AMapEntry
allMatch, anyMatch, append, appendChunk, commonPrefixLength, forEach, getChunk, isFullyPacked, iterator, listIterator, listIterator, longIndexOf, longLastIndexOf, next, set, setValue, toArrayMethods inherited from class convex.core.data.AVector
appendAll, conj, conjAll, cons, empty, flatMap, get, indexOf, isPacked, lastIndexOf, listIterator, mergeWith, print, reverse, slice, toArrayMethods inherited from class convex.core.data.ASequence
add, addAll, assoc, checkRange, containsKey, get, get, remove, spliterator, spliterator, subList, toCellArrayMethods inherited from class convex.core.data.ACollection
add, addAll, clear, containsAll, remove, removeAll, retainAllMethods inherited from class convex.core.data.ADataStructure
checkIndex, count, isEmpty, size, toCVMStringMethods inherited from class convex.core.data.ACountable
sliceMethods 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, toStringMethods 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, toArray
-
Method Details
-
getType
Description copied from class:ACellGets the most specific known runtime Type for this Cell. -
createRef
-
create
Creates a new MapEntry with the provided key and value- Type Parameters:
K- Type of KeyV- Type of value- Parameters:
key- Key to use for MapEntryvalue- Value to use for MapEntry- Returns:
- New MapEntry instance
-
of
Create a map entry, converting key and value to correct CVM types.- Type Parameters:
K- Type of KeysV- Type of Values- Parameters:
key- Key to use for map entryvalue- Value to use for map entry- Returns:
- New MapEntry
-
withValue
Description copied from class:AMapEntryCreate a new MapEntry with an updated value. Shares old key. Returns the same MapEntry if unchanged -
assoc
Description copied from class:ASequenceUpdates a value at the given position in the sequence. -
withKey
Description copied from class:AMapEntryCreate a new MapEntry with an updated key. Shares old value. Returns the same MapEntry if unchanged -
getKey
-
map
Description copied from class:ACollectionMaps a function over a collection, applying it to each element in turn. -
reduce
-
copyToArray
protected <R> void copyToArray(R[] arr, int offset) Description copied from class:ACollectionCopies the elements of this collection in order to an array at the specified offset- Specified by:
copyToArrayin classACollection<ACell>- Type Parameters:
R- Type of array elements required
-
getKeyHash
Gets the Hash of the key for this MapEntry- Returns:
- the Hash of the Key
-
getValue
-
getKeyRef
-
getValueRef
-
compareTo
- Specified by:
compareToin interfaceComparable<K extends ACell>
-
getRefCount
public final int getRefCount()Description copied from class:ACellGets 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. WARNING: may not be valid id Cell is not canonical Contained Refs may be either external or embedded. -
getRef
Description copied from class:ACellGets a numbered child Ref from within this Cell. WARNING: May be unreliable is cell is not canonical -
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.- Specified by:
updateRefsin classAVector<ACell>- Parameters:
func- Ref update function- Returns:
- Cell with updated Refs
-
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
-
keyEquals
Checks if the keys of two map entries are equal- Parameters:
b- MapEntry to compare with this MapEntry- Returns:
- true if this entry's key equals that of the other entry, false otherwise.
-
toVector
Description copied from class:ACollectionConverts this collection to a canonical vector of elements- Specified by:
toVectorin classACollection<ACell>- Returns:
- This collection coerced to a vector
-
contains
-
get
Description copied from class:AVectorGets the element at the specified index in this vector -
getElementRef
Description copied from class:ASequenceGets the element Ref at the specified index- Specified by:
getElementRefin classASequence<ACell>- Parameters:
i- Index of element to get- Returns:
- Ref to element at specified index
-
getElementRefUnsafe
Description copied from class:AVectorGets an element Ref from this vector, assuming bounds already checked- Specified by:
getElementRefUnsafein classAVector<ACell>- Parameters:
i- Index at which to get element Ref- Returns:
- Element Ref
-
encode
public int encode(byte[] bs, int pos) Description copied from class:ACellWrites 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. -
encodeRaw
public int encodeRaw(byte[] bs, int pos) Writes the raw MapEntry content. Puts the key and value Refs onto the given ByteBuffer -
encodeCompressed
Writes a MapEntry or null content in compressed format (no count). Useful for embedding an optional MapEntry inside a larger Encoding- Parameters:
me- MapEntry to encodebs- Byte array to write topos- Starting position for encoding in byte array- Returns:
- Updated position after writing
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACellReturns 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:
isCVMValuein classACell- Returns:
- true if the object is a CVM Value, false otherwise
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteableEstimate 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- Specified by:
estimatedEncodingSizein interfaceIWriteable- Returns:
- The estimated size for the binary representation of this object.
-
visitElementRefs
Description copied from class:ASequenceVisits all elements in this sequence, calling the specified consumer for each.- Specified by:
visitElementRefsin classASequence<ACell>- Parameters:
f- Function to call for each element
-
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 -
subVector
Description copied from class:ASequenceGets a vector containing the specified subset of this sequence. -
validate
Description copied from interface:IValidatedValidates 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:
validatein interfaceIValidated- Overrides:
validatein classACell- Throws:
InvalidDataException- If the data Value is invalid in any way
-
validateCell
Description copied from class:ACellValidates 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:
validateCellin classACell- Throws:
InvalidDataException- If the Cell is invalid
-
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. -
convertOrNull
-
isCanonical
public boolean isCanonical()Description copied from class:AVectorReturns true if this vector is in canonical format, i.e. suitable as top-level serialised representation of a vector. -
toCanonical
Description copied from class:ACellConverts this Cell to its canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.- Specified by:
toCanonicalin classACell- Returns:
- Canonical version of Cell
-