Example #1
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);
            }
        }
Example #2
0
        public static bool ContainsKey <K>(UnsafeDictionary *map, K key) where K : unmanaged, IEquatable <K>
        {
            UDebug.Assert(map != null);
            UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey);

            return(UnsafeHashCollection.Find <K>(&map->_collection, key, key.GetHashCode()) != null);
        }
Example #3
0
        public static bool Contains <T>(UnsafeHashSet *set, T key)
            where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(set != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == set->_typeHandle);

            return(UnsafeHashCollection.Find(&set->_collection, key, key.GetHashCode()) != null);
        }
Example #4
0
        public static bool Add <T>(UnsafeHashSet *set, T key)
            where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(set != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == set->_typeHandle);

            var hash  = key.GetHashCode();
            var entry = UnsafeHashCollection.Find <T>(&set->_collection, key, hash);

            if (entry == null)
            {
                UnsafeHashCollection.Insert <T>(&set->_collection, key, hash);
                return(true);
            }

            return(false);
        }
Example #5
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));
        }
Example #6
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);
        }
Example #7
0
        /// <summary>
        /// Modifies the current hashset to contain only elements that are present in that hashset and in the specified hashset.
        /// </summary>
        public static void IntersectsWith <T>(UnsafeHashSet *set, UnsafeHashSet *other)
            where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(set != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == set->_typeHandle);
            UDebug.Assert(other != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == other->_typeHandle);

            // When this set has no elements, there is nothing to intersect.
            // When this set equals other, it is already intersecting.
            if (GetCount(set) == 0 || set == other)
            {
                return;
            }

            // When the other set has no elements, clear this one completely
            if (GetCount(other) == 0)
            {
                Clear(set);
                return;
            }

            for (int i = set->_collection.UsedCount - 1; i >= 0; --i)
            {
                var entry = UnsafeHashCollection.GetEntry(&set->_collection, i);
                if (entry->State == UnsafeHashCollection.EntryState.Used)
                {
                    var key     = *(T *)((byte *)entry + set->_collection.KeyOffset);
                    var keyHash = key.GetHashCode();

                    // if we don't find this in other collection, remove it (And)
                    if (UnsafeHashCollection.Find <T>(&other->_collection, key, keyHash) == null)
                    {
                        UnsafeHashCollection.Remove <T>(&set->_collection, key, keyHash);
                    }
                }
            }
        }
Example #8
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);
            }
        }