Package convex.core.data
Class MapEntry<K extends ACell,V extends ACell>
- 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> SequencedCollection<ACell>
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 elements, 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, memorySize -
Method Summary
Modifier and TypeMethodDescriptionUpdates 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.booleanbooleanintEstimate 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.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) 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.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, setValue, sliceMethods inherited from class convex.core.data.AVector
appendAll, conj, conjAll, cons, empty, equals, flatMap, get, indexOf, isPacked, lastIndexOf, listIterator, mergeWith, print, reverse, subVector, toArrayMethods inherited from class convex.core.data.ASequence
add, addAll, assoc, checkRange, containsKey, get, get, isCVMValue, remove, set, spliterator, spliterator, subList, toCellArrayMethods inherited from class convex.core.data.ACollection
add, addAll, clear, containsAll, remove, removeAll, retainAll, toArrayMethods 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, hashCode, isEmbedded, 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
-
createRef
-
create
-
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
-
convertOrNull
-
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
-
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- Parameters:
arr-offset-
-
getKeyHash
-
getValue
-
getKeyRef
-
getValueRef
-
compareTo
-
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. Contained Refs may be either external or embedded. -
getRef
Description copied from class:ACellGets a numbered child Ref from within this Cell. WARNING: May need to convert to a canonical instance -
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
-
equals
-
keyEquals
-
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
-
getElementRef
-
getElementRefUnsafe
-
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
-
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
-
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
-
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 -
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. -
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 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:
toCanonicalin classACell- Returns:
- Canonical version of Cell
-