Пример #1
0
        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++;
            }
        }
Пример #2
0
        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()));
        }
Пример #3
0
        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);
            }
        }
Пример #4
0
        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));
        }
Пример #5
0
        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));
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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));
        }
Пример #9
0
        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);
        }
Пример #10
0
        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);
            }
        }
Пример #11
0
 public Enumerator(UnsafeDictionary *map)
 {
     _valueOffset = map->_valueOffset;
     _keyOffset   = map->_collection.KeyOffset;
     _iterator    = new UnsafeHashCollection.Enumerator(&map->_collection);
 }
Пример #12
0
        public static int GetCount(UnsafeDictionary *map)
        {
            UDebug.Assert(map != null);

            return(map->_collection.UsedCount - map->_collection.FreeCount);
        }
Пример #13
0
        public static bool IsFixedSize(UnsafeDictionary *map)
        {
            UDebug.Assert(map != null);

            return(map->_collection.Entries.Dynamic == 0);
        }
Пример #14
0
        public static void Clear(UnsafeDictionary *map)
        {
            UDebug.Assert(map != null);

            UnsafeHashCollection.Clear(&map->_collection);
        }
Пример #15
0
        public static int GetCapacity(UnsafeDictionary *map)
        {
            UDebug.Assert(map != null);

            return(map->_collection.Entries.Length);
        }
Пример #16
0
 public KeyEnumerator(UnsafeDictionary *dictionary)
 {
     _keyOffset = dictionary->_collection.KeyOffset;
     _iterator  = new UnsafeHashCollection.Enumerator(&dictionary->_collection);
 }
Пример #17
0
 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));
 }
Пример #18
0
 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);
 }
Пример #19
0
 public ValueEnumerator(UnsafeDictionary *dictionary)
 {
     _valueOffset = dictionary->_valueOffset;
     _iterator    = new UnsafeHashCollection.Enumerator(&dictionary->_collection);
 }
Пример #20
0
 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);
 }