private static bool TryInsert <K, V>(UnsafeSortedDictionary *map, K key, V value, MapInsertionBehaviour behaviour)
            where K : unmanaged, IComparable <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 = UnsafeOrderedCollection.Find <K>(&map->_collection, key);

            // 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 = UnsafeOrderedCollection.Insert <K>(&map->_collection, key);
                *GetValue <V>(map->_valueOffset, entry) = value;
                return(true);
            }
        }
Ejemplo n.º 2
0
        public static bool Contains <T>(UnsafeSortedSet *set, T item)
            where T : unmanaged, IComparable <T>
        {
            UDebug.Assert(set != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == set->_typeHandle);

            return(UnsafeOrderedCollection.Find <T>(&set->_collection, item) != null);
        }
        public static bool ContainsKey <K>(UnsafeSortedDictionary *map, K key)
            where K : unmanaged, IComparable <K>
        {
            UDebug.Assert(map != null);
            UDebug.Assert(typeof(K).TypeHandle.Value == map->_typeHandleKey);

            return(UnsafeOrderedCollection.Find <K>(&map->_collection, key) != null);
        }
        public static V Get <K, V>(UnsafeSortedDictionary *map, K key)
            where K : unmanaged, IComparable <K>
            where V : unmanaged
        {
            var entry = UnsafeOrderedCollection.Find(&map->_collection, key);

            if (entry == null)
            {
                throw new ArgumentException(string.Format(ThrowHelper.Arg_KeyNotFoundWithKey, key));
            }

            return(*GetValue <V>(map->_valueOffset, entry));
        }
        public static bool TryGetValue <K, V>(UnsafeSortedDictionary *map, K key, out V val)
            where K : unmanaged, IComparable <K>
            where V : unmanaged
        {
            var entry = UnsafeOrderedCollection.Find <K>(&map->_collection, key);

            if (entry != null)
            {
                val = *GetValue <V>(map->_valueOffset, entry);
                return(true);
            }

            val = default;
            return(false);
        }