Class AHashMap<K extends ACell,​V extends ACell>

All Implemented Interfaces:
IValidated, IWriteable, Map<K,​V>
Direct Known Subclasses:
MapLeaf, MapTree

public abstract class AHashMap<K extends ACell,​V extends ACell> extends AMap<K,​V>
  • Constructor Details

    • AHashMap

      protected AHashMap(long count)
  • Method Details

    • empty

      public AHashMap<K,​V> empty()
      Description copied from class: ADataStructure
      Returns an empty instance of the same general type as this data structure.
      Specified by:
      empty in class ADataStructure<MapEntry<K extends ACell,​V extends ACell>>
      Returns:
      An empty data structure
    • dissocRef

      public abstract AHashMap<K,​V> dissocRef(Ref<K> key)
      Dissoc given a Ref to the key value.
      Parameters:
      key - Ref of key to remove
      Returns:
      Map with specified key removed.
    • assocRef

      public abstract AHashMap<K,​V> assocRef(Ref<K> keyRef, V value)
    • assoc

      public abstract AHashMap<K,​V> assoc(ACell key, ACell value)
      Description copied from class: AMap
      Associates the given key with the specified value.
      Specified by:
      assoc in class AMap<K extends ACell,​V extends ACell>
      Parameters:
      key - Map key to associate
      value - Map value
      Returns:
      An updated map with the new association, or null if the association fails
    • dissoc

      public abstract AHashMap<K,​V> dissoc(ACell key)
      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.
      Specified by:
      dissoc in class AMap<K extends ACell,​V extends ACell>
      Parameters:
      key - Key to remove.
      Returns:
      Updated map
    • assocRef

      protected abstract AHashMap<K,​V> assocRef(Ref<K> keyRef, V value, int shift)
    • assocEntry

      public abstract AHashMap<K,​V> assocEntry(MapEntry<K,​V> e)
      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.
      Specified by:
      assocEntry in class AMap<K extends ACell,​V extends ACell>
      Parameters:
      e - A map entry
      Returns:
      The updated map
    • assocEntry

      protected abstract AHashMap<K,​V> assocEntry(MapEntry<K,​V> e, int shift)
    • merge

      public AHashMap<K,​V> merge(AHashMap<K,​V> m)
      Merge another map into this map. Replaces existing entries if they are different O(n) in size of map to merge.
      Parameters:
      m - HashMap to merge into this HashMap
      Returns:
      Merged HashMap
    • mergeDifferences

      public abstract AHashMap<K,​V> 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. If the function returns null, the entry is removed. Returns the same map if no changes occurred.
      Parameters:
      b - Other map to merge with
      func - Merge function, returning a new value for each key
      Returns:
      A merged map, or this map if no changes occurred
    • mergeDifferences

      protected abstract AHashMap<K,​V> mergeDifferences(AHashMap<K,​V> b, MergeFunction<V> func, int shift)
    • mergeWith

      public abstract AHashMap<K,​V> mergeWith(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. 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.
      Parameters:
      b - Other map to merge with
      func - Merge function, returning a new value for each key
      Returns:
      A merged map, or this map if no changes occurred
    • mergeWith

      protected abstract AHashMap<K,​V> mergeWith(AHashMap<K,​V> b, MergeFunction<V> func, int shift)
    • filterValues

      public AHashMap<K,​V> filterValues(Predicate<V> pred)
      Description copied from class: AMap
      Filters all values in this map with the given predicate.
      Overrides:
      filterValues in class AMap<K extends ACell,​V extends ACell>
      Parameters:
      pred - A predicate specifying which elements to retain.
      Returns:
      The updated map containing those entries where the predicate returned true.
    • equals

      public boolean equals(AMap<K,​V> a)
      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.
      Specified by:
      equals in class AMap<K extends ACell,​V extends ACell>
      Parameters:
      a - Map to compare with
      Returns:
      true if maps are equal, false otherwise.
    • mapEntries

      public abstract AHashMap<K,​V> mapEntries(Function<MapEntry<K,​V>,​MapEntry<K,​V>> func)
      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.
      Parameters:
      func - A function that maps old map entries to updated map entries.
      Returns:
      The updated Map, or this Map if no changes
    • validateWithPrefix

      protected abstract void validateWithPrefix(String string) throws InvalidDataException
      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.
      Parameters:
      string -
      Throws:
      InvalidDataException
    • updateRefs

      public abstract AHashMap<K,​V> updateRefs(IRefFunction func)
      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.
      Overrides:
      updateRefs in class ACell
      Parameters:
      func - Ref update function
      Returns:
      Cell with updated Refs
    • containsAllKeys

      public abstract boolean containsAllKeys(AHashMap<K,​V> map)
      Returns true if this map contains all the same keys as another map
      Parameters:
      map - Map to compare with
      Returns:
      True if this map contains all the keys of the other
    • encode

      public abstract int encode(byte[] bs, int pos)
      Writes this HashMap to a byte array. Will include values by default.
      Specified by:
      encode in interface IWriteable
      Specified by:
      encode in class ACell
      Parameters:
      bs - Byte array to encode into
      pos - Start position to encode at
      Returns:
      Updated position
    • getKeys

      public AVector<K> getKeys()