Exemplo n.º 1
0
        public static void CopyTo <T>(UnsafeHashSet *set, void *destination, int destinationIndex)
            where T : unmanaged
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ThrowHelper.ArgumentOutOfRange_Index);
            }
            UDebug.Assert(set != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == set->_typeHandle);

            var enumerator = GetEnumerator <T>(set);
            var dest       = (T *)destination;

            int i = 0;

            while (enumerator.MoveNext())
            {
                dest[destinationIndex + i] = enumerator.Current;
                i++;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Removes all elements in the specified hashset from the current hashset.
        /// </summary>
        public static void ExceptWith <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, return
            if (GetCount(set) == 0)
            {
                return;
            }

            // A set except itself is an empty set.
            if (other == set)
            {
                Clear(set);
                return;
            }

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

                    UnsafeHashCollection.Remove(&set->_collection, key, keyHash);
                }
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
        public static Enumerator <T> GetEnumerator <T>(UnsafeHashSet *set)
            where T : unmanaged
        {
            UDebug.Assert(set != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == set->_typeHandle);

            return(new Enumerator <T>(set));
        }
Exemplo n.º 5
0
        public static void Free(UnsafeHashSet *set)
        {
            if (set->_collection.Entries.Dynamic == 1)
            {
                UnsafeHashCollection.Free(&set->_collection);
            }

            Native.Free(set);
        }
Exemplo n.º 6
0
        public static void Free(UnsafeHashSet *set)
        {
            if (set->_collection.Entries.Dynamic)
            {
                UnsafeHashCollection.Free(&set->_collection);
            }

            AllocHelper.Free(set);
        }
Exemplo n.º 7
0
 public static void Or <T>(UnsafeHashSet *set, UnsafeHashSet *other) where T : unmanaged, IEquatable <T>
 {
     for (int i = other->_collection.UsedCount - 1; i >= 0; --i)
     {
         var entry = UnsafeHashCollection.GetEntry(&other->_collection, i);
         if (entry->State == UnsafeHashCollection.EntryState.Used)
         {
             // always add to this collection
             Add <T>(set, *(T *)((byte *)entry + other->_collection.KeyOffset));
         }
     }
 }
Exemplo n.º 8
0
        public static bool Add <T>(UnsafeHashSet *set, T key)
            where T : unmanaged, IEquatable <T>
        {
            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);
        }
Exemplo n.º 9
0
        public static void Free(UnsafeHashSet *set)
        {
            if (set == null)
            {
                return;
            }

            if (set->_collection.Entries.Dynamic == 1)
            {
                UnsafeHashCollection.Free(&set->_collection);
            }

            *set = default;

            Memory.Free(set);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Modifies the current hashset to contain all elements that are present in itself, the specified hashset, or both.
        /// </summary>
        public static void UnionWith <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);

            for (int i = other->_collection.UsedCount - 1; i >= 0; --i)
            {
                var entry = UnsafeHashCollection.GetEntry(&other->_collection, i);
                if (entry->State == UnsafeHashCollection.EntryState.Used)
                {
                    // always add to this collection
                    Add <T>(set, *(T *)((byte *)entry + other->_collection.KeyOffset));
                }
            }
        }
Exemplo n.º 12
0
        public static void And <T>(UnsafeHashSet *set, UnsafeHashSet *other) where T : unmanaged, IEquatable <T>
        {
            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);
                    }
                }
            }
        }
Exemplo n.º 13
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);
                    }
                }
            }
        }
Exemplo n.º 14
0
 public static int Capacity(UnsafeHashSet *set)
 {
     return(set->_collection.Entries.Length);
 }
Exemplo n.º 15
0
 public static int Count(UnsafeHashSet *set)
 {
     return(set->_collection.UsedCount - set->_collection.FreeCount);
 }
Exemplo n.º 16
0
 public static void Clear(UnsafeHashSet *set)
 {
     UnsafeHashCollection.Clear(&set->_collection);
 }
Exemplo n.º 17
0
        public static bool IsFixedSize(UnsafeHashSet *set)
        {
            UDebug.Assert(set != null);

            return(set->_collection.Entries.Dynamic == 0);
        }
Exemplo n.º 18
0
        public static int GetCapacity(UnsafeHashSet *set)
        {
            UDebug.Assert(set != null);

            return(set->_collection.Entries.Length);
        }
Exemplo n.º 19
0
        public static int GetCount(UnsafeHashSet *set)
        {
            UDebug.Assert(set != null);

            return(set->_collection.UsedCount - set->_collection.FreeCount);
        }
Exemplo n.º 20
0
 public NativeHashSet(int capacity)
 {
     m_inner = UnsafeHashSet.Allocate <T>(capacity, false);
 }
Exemplo n.º 21
0
 public static bool Remove <T>(UnsafeHashSet *set, T key) where T : unmanaged, IEquatable <T>
 {
     return(UnsafeHashCollection.Remove <T>(&set->_collection, key, key.GetHashCode()));
 }
Exemplo n.º 22
0
 public static bool Contains <T>(UnsafeHashSet *set, T key) where T : unmanaged, IEquatable <T>
 {
     return(UnsafeHashCollection.Find <T>(&set->_collection, key, key.GetHashCode()) != null);
 }
Exemplo n.º 23
0
 public NativeHashSet(int capacity, bool fixedSize)
 {
     m_inner = UnsafeHashSet.Allocate <T>(capacity, fixedSize);
 }
Exemplo n.º 24
0
        public static void Clear(UnsafeHashSet *set)
        {
            UDebug.Assert(set != null);

            UnsafeHashCollection.Clear(&set->_collection);
        }
Exemplo n.º 25
0
 public Iterator(UnsafeHashSet *set)
 {
     _keyOffset = set->_collection.KeyOffset;
     _iterator  = new UnsafeHashCollection.Iterator(&set->_collection);
 }
Exemplo n.º 26
0
 public static Iterator <T> GetIterator <T>(UnsafeHashSet *set) where T : unmanaged
 {
     return(new Iterator <T>(set));
 }