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 void Add <T>(UnsafeSortedSet *set, T item)
            where T : unmanaged, IComparable <T>
        {
            UDebug.Assert(set != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == set->_typeHandle);

            UnsafeOrderedCollection.Insert <T>(&set->_collection, item);
        }