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<E>
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:
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 int
static int
Maximum number of entries in a MapLeafFields inherited from class convex.core.data.ADataStructure
count
Fields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
accumulateEntrySet(HashSet<Map.Entry<K,V>> h)
Accumulate all entries from this map in the given HashSet.protected void
accumulateKeySet(HashSet<K> h)
Accumulate all keys from this map in the given HashSet.protected void
accumulateValues(ArrayList<V> al)
Accumulate all values from this map in the given ArrayList.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)
boolean
containsAllKeys(AHashMap<K,V> b)
Returns true if this map contains all the same keys as another mapprotected boolean
containsAllKeys(MapLeaf<K,V> b)
boolean
containsKey(ACell key)
Checks if the data structure contains the specified keyboolean
containsValue(Object value)
Creates a ListMap with the specified entries.Creates a ListMap 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()
int
encode(byte[] bs, int pos)
Writes this HashMap to a byte array.int
encodeRaw(byte[] bs, int pos)
Writes this Cell's encoding to a byte array, excluding the tag byteentryAt(long i)
Gets the entry in this map at a specified index, according to the map-specific order.entrySet()
boolean
Checks this map for equality with another map.boolean
boolean
equalsKeys(AMap<K,V> a)
Returns true if this map has exactly the same keys as the other mapboolean
equalsKeys(MapLeaf<K,V> a)
Returns true if this map has all keys equal to the other Mapint
Estimate 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 Maskvoid
forEach(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.int
Gets the number of Refs contained within this Cell.byte
getTag()
Gets the tag byte for this cell.boolean
Returns true if this Cell is in a canonical format for message writing.boolean
Returns true if this object represents a first class CVM Value.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.read(ByteBuffer bb, long count)
Reads a MapLeaf from the provided ByteBuffer Assumes the header byte is already read.<R> R
reduceEntries(BiFunction<? super R,MapEntry<K,V>,? extends R> func, R initial)
Reduce over all map entries in this map<R> R
reduceValues(BiFunction<? super R,? super V,? extends R> func, R initial)
Reduce over all values in this mapConverts this Cell to its canonical version.updateRefs(IRefFunction func)
Updates all Refs in this object using the given function.void
Validates the local structure and invariants of this cell.protected void
validateWithPrefix(String prefix)
Validates the map with a given hex prefix.Methods inherited from class convex.core.data.AHashMap
empty, filterValues, getKeys, merge
Methods inherited from class convex.core.data.AMap
clear, conj, containsKey, containsKeyRef, entryVector, equals, get, get, get, getElementRef, getType, print, put, putAll, remove, values
Methods inherited from class convex.core.data.ADataStructure
conjAll, count, isEmpty, size
Methods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, calcMemorySize, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, hashCode, isEmbedded, toString, validate, write
Methods inherited from class convex.core.data.AObject
attachEncoding, print
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods 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:
- Constant Field Values
-
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 ListMap 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
-
containsKey
Description copied from class:ADataStructure
Checks if the data structure contains the specified key -
getEntry
Description copied from class:AMap
Gets the MapEntry for the given key -
getKeyRefEntry
Description copied from class:AMap
Get 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:AMap
Gets the map entry with the specified hash -
containsValue
-
get
Description copied from class:ADataStructure
Get the value associated with a given key. -
dissoc
Description copied from class:AMap
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. -
dissocRef
Description copied from class:AHashMap
Dissoc given a Ref to the key value. -
assocEntry
Description copied from class:AMap
Associate 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:AMap
Associates the given key with the specified value. -
assoc
-
assocRef
-
assocRef
-
keySet
-
accumulateKeySet
Description copied from class:AMap
Accumulate all keys from this map in the given HashSet. -
accumulateValues
Description copied from class:AMap
Accumulate all values from this map in the given ArrayList. -
entrySet
-
accumulateEntrySet
Description copied from class:AMap
Accumulate all entries from this map in the given HashSet. -
encode
public int encode(byte[] bs, int pos)Description copied from class:AHashMap
Writes this HashMap to a byte array. Will include values by default. -
encodeRaw
public int encodeRaw(byte[] bs, int pos)Description copied from class:ACell
Writes this Cell's encoding to a byte array, excluding the tag byte -
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 likely to contain the entire object when represented in binary format, including the tag byte.- Returns:
- The estimated size for the binary representation of this object.
-
read
public static <K extends ACell, V extends ACell> MapLeaf<K,V> read(ByteBuffer bb, long count) throws BadFormatExceptionReads a MapLeaf from the provided ByteBuffer Assumes the header byte is already read.- Parameters:
bb
- ByteBuffer to read fromcount
- 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:ACell
Returns true if this Cell is in a canonical format for message writing. Reading or writing a non-canonical value should be considered illegal, but non-canonical objects may be used on a temporary internal basis.- Specified by:
isCanonical
in classACell
- Returns:
- true if the object is in canonical format, false otherwise
-
isCVMValue
public final boolean isCVMValue()Description copied from class:ACell
Returns true if this object represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false. 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
-
getRefCount
public 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. Contained Refs may be either external or embedded.- Specified by:
getRefCount
in classACell
- Returns:
- The number of Refs in this Cell
-
getRef
Description copied from class:ACell
Gets a numbered child Ref from within this Cell. -
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. 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
Filters this ListMap to contain only key hashes with the hex digits specified in the given Mask- Parameters:
digitPos
- Position of the hex digit to filtermask
- Mask of digits to include- Returns:
- Filtered ListMap
-
entryAt
Description copied from class:AMap
Gets the entry in this map at a specified index, according to the map-specific order. -
mergeWith
Description copied from class:AHashMap
Merge 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:AHashMap
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. If the function returns null, the entry is removed. Returns the same map if no changes occurred. -
mergeDifferences
-
mergeDifferences
-
reduceValues
Description copied from class:AMap
Reduce over all values in this map -
reduceEntries
Description copied from class:AMap
Reduce over all map entries in this map -
equalsKeys
Description copied from class:AMap
Returns true if this map has exactly the same keys as the other map -
equalsKeys
Returns true if this map has all keys equal to the other Map- Parameters:
a
- A map to compare keys with- Returns:
- Boolean true if the two maps have the same keys
-
equals
Description copied from class:AMap
Checks this map for equality with another map. In general, maps should be considered equal if they have the same canonical representation, i.e. the same hash value. Subclasses may override this this they have a more efficient equals implementation or a more specific definition of equality. -
equals
-
mapEntries
Description copied from class:AHashMap
Maps 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:AHashMap
Validates the map with a given hex prefix. 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:
validateWithPrefix
in classAHashMap<K extends ACell,V extends ACell>
- Throws:
InvalidDataException
-
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
-
containsAllKeys
Description copied from class:AHashMap
Returns true if this map contains all the same keys as another map -
containsAllKeys
-
getTag
public byte getTag()Description copied from class:ACell
Gets the tag byte for this cell. The tag byte is always written as the first byte of the Cell's Encoding -
toCanonical
Description copied from class:ACell
Converts this Cell to its canonical version. Returns this if already canonical- Specified by:
toCanonical
in classACell
- Returns:
- Canonical version of Cell
-