Package convex.core.data
Class MapLeaf<K extends ACell,V extends ACell>
java.lang.Object
convex.core.data.AObject
convex.core.data.ACell
convex.core.data.ACountable<MapEntry<K,V>>
convex.core.data.ADataStructure<MapEntry<K,V>>
convex.core.data.AMap<K,V>
convex.core.data.AHashMap<K,V>
convex.core.data.MapLeaf<K,V>
- Type Parameters:
K- Type of keysV- Type of values
- All Implemented Interfaces:
IAssociative<K,,V> IValidated,IWriteable,Map<K,V>
Limited size Persistent Merkle Map implemented as a small sorted list of
Key/Value pairs
Must be sorted by Key hash value to ensure uniqueness of representation
-
Nested Class Summary
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intstatic final intMaximum number of entries in a MapLeafFields inherited from class convex.core.data.ADataStructure
countFields inherited from class convex.core.data.ACell
cachedRef, memorySize -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaccumulateEntrySet(Set<Map.Entry<K, V>> h) Accumulate all entries from this map in the given mutable Set.protected voidaccumulateKeySet(Set<K> h) Accumulate all keys from this map in the given mutable Set.protected voidaccumulateValues(List<V> al) Accumulate all values from this map in the given mutable list.Associates the given key with the specified value.assocEntry(MapEntry<K, V> e) Associate the given map entry into the map.assocEntry(MapEntry<K, V> e, int shift) booleancontainsAllKeys(AHashMap<K, V> b) Returns true if this map contains all the same keys as another mapprotected booleancontainsAllKeys(MapLeaf<K, V> b) booleancontainsValue(ACell value) CHecks if this map contains the given value.Creates a ListMap with the specified entries.Creates a MapLeaf with the specified entries.Dissociates a key from this map, returning an updated map if the key was removed, or the same unchanged map if the key is not present.Dissoc given a Ref to the key value.emptyMap()intencode(byte[] bs, int pos) Writes this HashMap to a byte array.intencodeRaw(byte[] bs, int pos) Writes this Cell's encoding to a byte array, excluding the tag byte.entryAt(long i) Gets the entry in this map at a specified index, according to the map-specific order.booleanChecks for equality with another Cell.booleanintEstimate the encoded data size for this Cell.filterHexDigits(int digitPos, int mask) Filters this ListMap to contain only key hashes with the hex digits specified in the given MaskvoidforEach(BiConsumer<? super K, ? super V> action) Get the value associated with a given key.Gets the MapEntry for the given keygetEntryByHash(Hash hash) Gets the map entry with the specified hashgetKeyRefEntry(Ref<ACell> ref) Get an entry given a Ref to the key value.getRef(int i) Gets a numbered child Ref from within this Cell.intGets the number of Refs contained within this Cell.bytegetTag()Gets the tag byte for this cell.booleanReturns true if this Cell is in a canonical representation.final booleanReturns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM should return false.keySet()Maps a function over all entries in this Map to produce updated entries.mergeDifferences(AHashMap<K, V> b, MergeFunction<V> func) Merge this map with another map, using the given function for each key that is present in either map and has a different value The function is passed null for missing values in either map, and must return type V.mergeDifferences(AHashMap<K, V> b, MergeFunction<V> func, int shift) mergeDifferences(MapLeaf<K, V> b, MergeFunction<V> func, int shift) Merge this map with another map, using the given function for each key that is present in either map.Reads a MapLeaf from the provided Blob encoding.<R> RreduceEntries(BiFunction<? super R, MapEntry<K, V>, ? extends R> func, R initial) Reduce over all map entries in this map<R> RreduceValues(BiFunction<? super R, ? super V, ? extends R> func, R initial) Reduce over all values in this mapslice(long start, long end) Gets a slice of this data structureConverts this Cell to a canonical version.unsafeCreate(MapEntry<K, V>... items) Creates aMapLeafupdateRefs(IRefFunction func) Updates all Refs in this object using the given function.voidValidates the local structure and invariants of this cell.protected voidvalidateWithPrefix(String prefix) Validates the map with a given hex prefix for the Hash.Methods inherited from class convex.core.data.AHashMap
containsKey, empty, entrySet, filterValues, getKeys, merge, merge, sliceMethods inherited from class convex.core.data.AMap
clear, conj, containsKey, containsKeyRef, containsValue, entryVector, get, get, get, getElementRef, getType, print, put, putAll, remove, valuesMethods inherited from class convex.core.data.ADataStructure
checkIndex, conjAll, count, isDataValue, isEmpty, size, toCVMStringMethods 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, validateMethods 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.Map
compute, computeIfAbsent, computeIfPresent, equals, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove, replace, replace, replaceAll, size
-
Field Details
-
MAX_ENTRIES
public static final int MAX_ENTRIESMaximum number of entries in a MapLeaf- See Also:
-
MAX_ENCODING_LENGTH
public static int MAX_ENCODING_LENGTH
-
-
Method Details
-
create
Creates a ListMap with the specified entries. Entries must have distinct keys but may otherwise be specified in any order. Null entries are ignored/removed.- Parameters:
entries- Entries for map- Returns:
- New ListMap
-
create
protected static <K extends ACell,V extends ACell> MapLeaf<K,V> create(MapEntry<K, V>[] entries, int offset, int length) Creates a MapLeaf with the specified entries. Null entries are ignored/removed.- Parameters:
entries-offset- Offset into entries arraylength- Number of entries to take from entries array, starting at offset- Returns:
- A new ListMap
-
create
-
unsafeCreate
-
getEntry
-
getKeyRefEntry
Description copied from class:AMapGet an entry given a Ref to the key value. This is more efficient than directly looking up using the key for some map types, and should be preferred if the caller already has a Ref available. -
getEntryByHash
Description copied from class:AMapGets the map entry with the specified hash -
containsValue
Description copied from class:AMapCHecks if this map contains the given value. WARNING: probably O(n) -
get
Description copied from class:ADataStructureGet the value associated with a given key. -
dissoc
Description copied from class:AMapDissociates a key from this map, returning an updated map if the key was removed, or the same unchanged map if the key is not present. -
dissocRef
-
assocEntry
Description copied from class:AMapAssociate the given map entry into the map. May return null if the map entry is not valid for this map type. -
assocEntry
-
assoc
Description copied from class:AMapAssociates the given key with the specified value. -
assoc
-
assocRef
-
assocRef
-
keySet
-
accumulateKeySet
-
accumulateValues
-
accumulateEntrySet
-
encode
public int encode(byte[] bs, int pos) Description copied from class:AHashMapWrites this HashMap to a byte array. Will include values by default. -
encodeRaw
public int encodeRaw(byte[] bs, int pos) Description copied from class:ACellWrites this Cell's encoding to a byte array, excluding the tag byte. -
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- Returns:
- The estimated size for the binary representation of this object.
-
read
public static <K extends ACell,V extends ACell> MapLeaf<K,V> read(Blob b, int pos, long count) throws BadFormatException Reads a MapLeaf from the provided Blob encoding.- Parameters:
b- Blob to read frompos- Start position in Blob (index of tag byte)count- Count of map elements- Returns:
- A Map as deserialised from the provided ByteBuffer
- Throws:
BadFormatException- If encoding is invalid
-
emptyMap
-
forEach
-
isCanonical
public boolean isCanonical()Description copied from class:ACellReturns true if this Cell is in a canonical representation. Non-canonical objects may be used on a temporary internal basis, they should be converted to canonical representations for general purpose use.- Specified by:
isCanonicalin classACell- Returns:
- true if the object is in canonical format, false otherwise
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACellReturns true if this cell is a first class CVM Value allowable in the CVM state Sub-structural cells that are not themselves first class values should return false Records and types that are not permissible on the CVM 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
-
getRefCount
public 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.- Overrides:
getRefCountin classACell- Returns:
- The number of Refs in this Cell
-
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. -
filterHexDigits
-
entryAt
Description copied from class:AMapGets the entry in this map at a specified index, according to the map-specific order. Caller responsible for bounds check! -
mergeWith
Description copied from class:AHashMapMerge this map with another map, using the given function for each key that is present in either map. The function is applied to the corresponding values with the same key. The function is passed null for missing values in either map, and must return type V. If the function returns null, the entry is removed. Returns the same map if no changes occurred. PERF WARNING: This method's contract requires calling the function on all values in both sets, which will cause a full data structure traversal. If the function will only return one or other of the compared values consider using mergeDifferences instead. -
mergeWith
-
mergeDifferences
Description copied from class:AHashMapMerge this map with another map, using the given function for each key that is present in either map and has a different value The function is passed null for missing values in either map, and must return type V. If the function returns null, the entry is removed. Returns the same map if no changes occurred. -
mergeDifferences
-
mergeDifferences
-
reduceValues
Description copied from class:AMapReduce over all values in this map -
reduceEntries
Description copied from class:AMapReduce over all map entries in this map -
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
-
mapEntries
Description copied from class:AHashMapMaps a function over all entries in this Map to produce updated entries. May not change keys, but may return null to remove an entry. -
validateWithPrefix
Description copied from class:AHashMapValidates the map with a given hex prefix for the Hash. This is necessary to ensure that child maps are valid, in particular have the correct shift level and that all key hashes start with the correct prefix of hex characters. TODO: consider faster way of passing prefix than hex string, probably a byte[] stack.- Specified by:
validateWithPrefixin classAHashMap<K extends ACell,V extends ACell> - Parameters:
prefix-- Throws:
InvalidDataException
-
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
-
containsAllKeys
Description copied from class:AHashMapReturns true if this map contains all the same keys as another map -
containsAllKeys
-
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. -
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
-
slice
Description copied from class:ACountableGets a slice of this data structure
-