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
count
Fields 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.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
Checks for equality with another Cell.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 indexgetKey()
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.final boolean
Returns true if this Cell represents a first class CVM Value.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) 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.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, set, setValue, spliterator, toArray
Methods inherited from class convex.core.data.AVector
appendAll, conj, conjAll, cons, empty, flatMap, get, indexOf, isPacked, lastIndexOf, listIterator, mergeWith, print, reverse, slice, spliterator, toArray
Methods inherited from class convex.core.data.ASequence
add, addAll, assoc, checkRange, containsKey, get, get, remove, subList, toCellArray
Methods inherited from class convex.core.data.ACollection
add, addAll, clear, containsAll, remove, removeAll, retainAll
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, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, hashCode, isCompletelyEncoded, isEmbedded, mark, mark, toString, 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
-
Method Details
-
getType
Description copied from class:ACell
Gets 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: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
Description copied from class:AMapEntry
Create a new MapEntry with an updated key. Shares old value. Returns the same MapEntry if unchanged -
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
-
getKeyHash
Gets the Hash of the key for this MapEntry- Returns:
- the Hash of the Key
-
getValue
-
getKeyRef
-
getValueRef
-
compareTo
- Specified by:
compareTo
in interfaceComparable<K extends ACell>
-
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. WARNING: may not be valid id Cell is not canonical 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 be unreliable is cell is not canonical -
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
Description copied from class:ACell
Checks 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: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
Description copied from class:AVector
Gets the element at the specified index in this vector -
getElementRef
Description copied from class:ASequence
Gets the element Ref at the specified index- Specified by:
getElementRef
in classASequence<ACell>
- Parameters:
i
- Index of element to get- Returns:
- Ref to element at specified index
-
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
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:ACell
Returns 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:
isCVMValue
in classACell
- Returns:
- true if the object is a CVM Value, false otherwise
-
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
Description copied from class:ASequence
Visits all elements in this sequence, calling the specified consumer for each.- Specified by:
visitElementRefs
in classASequence<ACell>
- Parameters:
f
- Function to call for each element
-
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 -
subVector
Description copied from class:ASequence
Gets a vector containing the specified subset of this 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. -
convertOrNull
-
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 its canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.- Specified by:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-