convex.core.data.AHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of convex-core Show documentation
Show all versions of convex-core Show documentation
Convex core libraries and common utilities
The newest version!
package convex.core.data;
import java.util.HashSet;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import convex.core.exceptions.InvalidDataException;
import convex.core.util.MergeFunction;
import convex.core.util.Utils;
public abstract class AHashMap extends AMap {
protected AHashMap(long count) {
super(count);
}
@Override
public AHashMap empty() {
return Maps.empty();
}
/**
* Dissoc given a Ref to the key value.
* @param key Ref of key to remove
* @return Map with specified key removed.
*/
public abstract AHashMap dissocRef(Ref key);
public abstract AHashMap assocRef(Ref keyRef, V value);
@Override
public abstract AHashMap assoc(ACell key, ACell value);
@Override
public abstract AHashMap dissoc(ACell key);
protected abstract AHashMap assocRef(Ref keyRef, V value, int shift);
public abstract AHashMap assocEntry(MapEntry e);
protected abstract AHashMap assocEntry(MapEntry e, int shift);
/**
* Merge another map into this map. Replaces existing entries if they are
* different
*
* O(n) in size of map to merge.
*
* @param m HashMap to merge into this HashMap
* @return Merged HashMap
*/
public AHashMap merge(AHashMap m) {
AHashMap result = this;
long n = m.count();
for (int i = 0; i < n; i++) {
result = result.assocEntry(m.entryAt(i));
}
return result;
}
@Override
public AHashMap merge(AMap m) {
if (m instanceof AHashMap) return merge((AHashMap)m);
return (AHashMap) super.merge(m);
}
/**
* 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.
*
* @param b Other map to merge with
* @param func Merge function, returning a new value for each key
* @return A merged map, or this map if no changes occurred
*/
public abstract AHashMap mergeDifferences(AHashMap b, MergeFunction func);
protected abstract AHashMap mergeDifferences(AHashMap b, MergeFunction func, int shift);
/**
* 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.
*
* @param b Other map to merge with
* @param func Merge function, returning a new value for each key
* @return A merged map, or this map if no changes occurred
*/
public abstract AHashMap mergeWith(AHashMap b, MergeFunction func);
protected abstract AHashMap mergeWith(AHashMap b, MergeFunction func, int shift);
@Override
public AHashMap filterValues(Predicate pred) {
return mergeWith(this, (a, b) -> pred.test(a) ? a : null);
}
/**
* 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.
*
* @param func A function that maps old map entries to updated map entries.
* @return The updated Map, or this Map if no changes
*/
public abstract AHashMap mapEntries(Function, MapEntry> func);
/**
* Validates 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.
*
* @param string
* @throws InvalidDataException
*/
protected abstract void validateWithPrefix(String string) throws InvalidDataException;
@Override
public abstract AHashMap updateRefs(IRefFunction func);
/**
* Returns true if this map contains all the same keys as another map
* @param map Map to compare with
* @return True if this map contains all the keys of the other
*/
public abstract boolean containsAllKeys(AHashMap map);
@SuppressWarnings("unchecked")
public final boolean containsKey(ACell key) {
if (count==0) return false;
return getEntry((K)key)!=null;
}
/**
* Writes this HashMap to a byte array. Will include values by default.
* @param bs Byte array to encode into
* @param pos Start position to encode at
* @return Updated position
*/
public abstract int encode(byte[] bs, int pos);
/**
* Gets the keys in this Map as a Vector
*
* @return Vector of keys in map defined order
*/
public AVector getKeys() {
int n=Utils.checkedInt(count);
ACell[] keys=new ACell[n];
for (int i=0; i> entrySet() {
int len = size();
HashSet> h = new HashSet>(len);
accumulateEntrySet(h);
return h;
}
@Override
public AHashMap slice(long start) {
return slice(start,count);
}
@Override
public abstract AHashMap slice(long start, long end);
// TODO: better conversion to Sets
// public abstract AHashSet> buildEntrySet();
// public abstract AHashSet buildKeySet();
}