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
count
Fields inherited from class convex.core.data.ACell
cachedRef, memorySize
-
Method Summary
Modifier and TypeMethodDescriptionUpdates a value at the given position in the sequence.int
Concatenates the elements from another sequence to the end of this sequence.boolean
static MapEntry
protected <R> void
copyToArray
(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 valueint
encode
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.static int
encodeCompressed
(MapEntry<?, ?> me, byte[] bs, int pos) Writes a MapEntry or null content in compressed format (no count).int
encodeRaw
(byte[] bs, int pos) Writes the raw MapEntry content.boolean
boolean
int
Estimate 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 int
Gets the number of Refs contained within this Cell.byte
getTag()
Gets the tag byte for this cell.getType()
Gets the most specific known runtime Type for this Cell.getValue()
boolean
Returns true if this vector is in canonical format, i.e.boolean
Checks 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> R
reduce
(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.void
validate()
Validates the complete structure of this object.void
Validates the local structure and invariants of this cell.void
visitElementRefs
(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, slice
Methods inherited from class convex.core.data.AVector
appendAll, conj, conjAll, cons, empty, equals, flatMap, get, indexOf, isPacked, lastIndexOf, listIterator, mergeWith, print, reverse, subVector, toArray
Methods inherited from class convex.core.data.ASequence
add, addAll, assoc, checkRange, 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, getEncodingLength, 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
-
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:AMapEntry
Create a new MapEntry with an updated value. Shares old key. Returns the same MapEntry if unchanged -
assoc
Description copied from class:ASequence
Updates a value at the given position in the sequence. -
withKey
-
getKey
-
map
Description copied from class:ACollection
Maps 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:ACollection
Copies the elements of this collection in order to an array at the specified offset- Specified by:
copyToArray
in 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: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. -
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<ACell>
- Parameters:
func
- Ref update function- Returns:
- Cell with updated Refs
-
equals
-
equals
-
keyEquals
-
toVector
Description copied from class:ACollection
Converts this collection to a canonical vector of elements- Specified by:
toVector
in 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: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. -
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: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- Specified by:
estimatedEncodingSize
in interfaceIWriteable
- Returns:
- The estimated size for the binary representation of this object.
-
visitElementRefs
-
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 -
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
-
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. -
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. -
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
-