public static void CopyTo <K, V>(UnsafeDictionary *map, KeyValuePair <K, V>[] destination, int destinationIndex) where K : unmanaged, IEquatable <K> where V : unmanaged { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } if (GetCount(map) + (uint)destinationIndex > destination.Length) { throw new ArgumentOutOfRangeException(ThrowHelper.Arg_ArrayPlusOffTooSmall); } UDebug.Assert(map != null); UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey); UDebug.Assert(typeof(V).TypeHandle.Value == map->_typeHandleValue); UDebug.Assert(destination != null); UDebug.Assert(destination.Length >= GetCount(map) + destinationIndex); var enumerator = GetEnumerator <K, V>(map); int i = 0; while (enumerator.MoveNext()) { destination[destinationIndex + i] = enumerator.Current; i++; } }
public static bool Remove <K>(UnsafeDictionary *map, K key) where K : unmanaged, IEquatable <K> { UDebug.Assert(map != null); UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey); return(UnsafeHashCollection.Remove <K>(&map->_collection, key, key.GetHashCode())); }
private static bool TryInsert <K, V>(UnsafeDictionary *map, K key, V value, MapInsertionBehaviour behaviour) where K : unmanaged, IEquatable <K> where V : unmanaged { UDebug.Assert(map != null); UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey); UDebug.Assert(typeof(V).TypeHandle.Value == map->_typeHandleValue); var hash = key.GetHashCode(); var entry = UnsafeHashCollection.Find <K>(&map->_collection, key, hash); // Entry is already present if (entry != null) { if (behaviour == MapInsertionBehaviour.Overwrite) { *GetValue <V>(map->_valueOffset, entry) = value; return(true); } if (behaviour == MapInsertionBehaviour.ThrowIfExists) { throw new ArgumentException(string.Format(ThrowHelper.Arg_AddingDuplicateWithKey, key)); } return(false); } // Create new entry else { entry = UnsafeHashCollection.Insert <K>(&map->_collection, key, hash); *GetValue <V>(map->_valueOffset, entry) = value; return(true); } }
public static KeyEnumerator <K> GetKeyEnumerator <K>(UnsafeDictionary *map) where K : unmanaged, IEquatable <K> { UDebug.Assert(map != null); UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey); return(new KeyEnumerator <K>(map)); }
public static ValueEnumerator <V> GetValueEnumerator <V>(UnsafeDictionary *map) where V : unmanaged { UDebug.Assert(map != null); UDebug.Assert(typeof(V).TypeHandle.Value == map->_typeHandleValue); return(new ValueEnumerator <V>(map)); }
public static bool ContainsValue <V>(UnsafeDictionary *map, V value) where V : unmanaged, IEquatable <V> { var iterator = new ValueEnumerator <V>(map); while (iterator.MoveNext()) { if (value.Equals(iterator.Current)) { return(true); } } return(false); }
public static void Free(UnsafeDictionary *set) { if (set == null) { return; } if (set->_collection.Entries.Dynamic == 1) { UnsafeHashCollection.Free(&set->_collection); } *set = default; Memory.Free(set); }
public static V Get <K, V>(UnsafeDictionary *map, K key) where K : unmanaged, IEquatable <K> where V : unmanaged { UDebug.Assert(map != null); UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey); UDebug.Assert(typeof(V).TypeHandle.Value == map->_typeHandleValue); var entry = UnsafeHashCollection.Find(&map->_collection, key, key.GetHashCode()); if (entry == null) { throw new ArgumentException(string.Format(ThrowHelper.Arg_KeyNotFoundWithKey, key)); } return(*GetValue <V>(map->_valueOffset, entry)); }
public static bool TryGetValue <K, V>(UnsafeDictionary *map, K key, out V val) where K : unmanaged, IEquatable <K> where V : unmanaged { UDebug.Assert(map != null); UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey); UDebug.Assert(typeof(V).TypeHandle.Value == map->_typeHandleValue); var entry = UnsafeHashCollection.Find <K>(&map->_collection, key, key.GetHashCode()); if (entry != null) { val = *GetValue <V>(map->_valueOffset, entry); return(true); } val = default; return(false); }
public static void AddOrGet <K, V>(UnsafeDictionary *map, K key, ref V value) where K : unmanaged, IEquatable <K> where V : unmanaged { UDebug.Assert(map != null); UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey); UDebug.Assert(typeof(V).TypeHandle.Value == map->_typeHandleValue); var hash = key.GetHashCode(); var entry = UnsafeHashCollection.Find <K>(&map->_collection, key, hash); if (entry == null) { // insert new entry for key entry = UnsafeHashCollection.Insert <K>(&map->_collection, key, hash); // assign value to entry *GetValue <V>(map->_valueOffset, entry) = value; } else { value = *GetValue <V>(map->_valueOffset, entry); } }
public Enumerator(UnsafeDictionary *map) { _valueOffset = map->_valueOffset; _keyOffset = map->_collection.KeyOffset; _iterator = new UnsafeHashCollection.Enumerator(&map->_collection); }
public static int GetCount(UnsafeDictionary *map) { UDebug.Assert(map != null); return(map->_collection.UsedCount - map->_collection.FreeCount); }
public static bool IsFixedSize(UnsafeDictionary *map) { UDebug.Assert(map != null); return(map->_collection.Entries.Dynamic == 0); }
public static void Clear(UnsafeDictionary *map) { UDebug.Assert(map != null); UnsafeHashCollection.Clear(&map->_collection); }
public static int GetCapacity(UnsafeDictionary *map) { UDebug.Assert(map != null); return(map->_collection.Entries.Length); }
public KeyEnumerator(UnsafeDictionary *dictionary) { _keyOffset = dictionary->_collection.KeyOffset; _iterator = new UnsafeHashCollection.Enumerator(&dictionary->_collection); }
public static bool TryAdd <K, V>(UnsafeDictionary *map, K key, V value) where K : unmanaged, IEquatable <K> where V : unmanaged { return(TryInsert(map, key, value, MapInsertionBehaviour.None)); }
public static void Add <K, V>(UnsafeDictionary *map, K key, V value) where K : unmanaged, IEquatable <K> where V : unmanaged { TryInsert(map, key, value, MapInsertionBehaviour.ThrowIfExists); }
public ValueEnumerator(UnsafeDictionary *dictionary) { _valueOffset = dictionary->_valueOffset; _iterator = new UnsafeHashCollection.Enumerator(&dictionary->_collection); }
public static void Set <K, V>(UnsafeDictionary *map, K key, V value) where K : unmanaged, IEquatable <K> where V : unmanaged { TryInsert(map, key, value, MapInsertionBehaviour.Overwrite); }