Ejemplo n.º 1
0
        public void CopyFrom(RefList <T> other)
        {
            ArrayUtils.Copy(in other.arr, ref this.arr);

            if (this.freePrepared != null)
            {
                PoolHashSetCopyable <int> .Recycle(ref this.freePrepared);
            }
            this.freePrepared = PoolHashSetCopyable <int> .Spawn(other.freePrepared.Count);

            this.freePrepared.CopyFrom(other.freePrepared);

            if (this.freePeek != null)
            {
                PoolQueueCopyable <int> .Recycle(ref this.freePeek);
            }
            this.freePeek = PoolQueueCopyable <int> .Spawn(other.freePeek.Count);

            this.freePeek.CopyFrom(other.freePeek);

            if (this.free != null)
            {
                PoolHashSetCopyable <int> .Recycle(ref this.free);
            }
            this.free = PoolHashSetCopyable <int> .Spawn(other.free.Count);

            this.free.CopyFrom(other.free);

            this.size         = other.size;
            this.capacity     = other.capacity;
            this.count        = other.count;
            this.fromIndex    = other.fromIndex;
            this.initCapacity = other.initCapacity;
        }
Ejemplo n.º 2
0
        private void Init(int capacity)
        {
            if (capacity < 0)
            {
                capacity = 0;
            }

            this.count     = 0;
            this.size      = 0;
            this.capacity  = -1;
            this.fromIndex = 0;
            if (this.freePeek == null)
            {
                this.freePeek = PoolQueueCopyable <int> .Spawn(capacity);
            }
            this.freePeek.Clear();
            if (this.free == null)
            {
                this.free = PoolHashSetCopyable <int> .Spawn(capacity);
            }
            this.free.Clear();
            if (this.freePrepared == null)
            {
                this.freePrepared = PoolHashSetCopyable <int> .Spawn(capacity);
            }
            this.freePrepared.Clear();
            this.Resize_INTERNAL(capacity);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks if this is a superset of other
        ///
        /// Implementation Notes:
        /// The following properties are used up-front to avoid element-wise checks:
        /// 1. If other has no elements (it's the empty set), then this is a superset, even if this
        /// is also the empty set.
        /// 2. If other has unique elements according to this equality comparer, and this has less
        /// than the number of elements in other, then this can't be a superset
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns>true if this is a superset of other; false if not</returns>
        public bool IsSupersetOf(IEnumerable <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            // try to fall out early based on counts
            ICollection <T> otherAsCollection = other as ICollection <T>;

            if (otherAsCollection != null)
            {
                // if other is the empty set then this is a superset
                if (otherAsCollection.Count == 0)
                {
                    return(true);
                }
                HashSetCopyable <T> otherAsSet = other as HashSetCopyable <T>;
                // try to compare based on counts alone if other is a hashset with
                // same equality comparer
                if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
                {
                    if (otherAsSet.Count > m_count)
                    {
                        return(false);
                    }
                }
            }

            return(ContainsAllElements(other));
        }
Ejemplo n.º 4
0
 internal Enumerator(HashSetCopyable <T> set)
 {
     this.set = set;
     index    = 0;
     version  = set.m_version;
     current  = default(T);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Internal method used for HashSetEqualityComparer. Compares set1 and set2 according
        /// to specified comparer.
        ///
        /// Because items are hashed according to a specific equality comparer, we have to resort
        /// to n^2 search if they're using different equality comparers.
        /// </summary>
        /// <param name="set1"></param>
        /// <param name="set2"></param>
        /// <param name="comparer"></param>
        /// <returns></returns>
        internal static bool HashSetEquals(HashSetCopyable <T> set1, HashSetCopyable <T> set2, IEqualityComparer <T> comparer)
        {
            // handle null cases first
            if (set1 == null)
            {
                return(set2 == null);
            }
            else if (set2 == null)
            {
                // set1 != null
                return(false);
            }

            // all comparers are the same; this is faster
            if (AreEqualityComparersEqual(set1, set2))
            {
                if (set1.Count != set2.Count)
                {
                    return(false);
                }
                // suffices to check subset
                foreach (T item in set2)
                {
                    if (!set1.Contains(item))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else    // n^2 search because items are hashed according to their respective ECs
            {
                foreach (T set2Item in set2)
                {
                    bool found = false;
                    foreach (T set1Item in set1)
                    {
                        if (comparer.Equals(set2Item, set1Item))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        return(false);
                    }
                }
                return(true);
            }
        }
Ejemplo n.º 6
0
        public void CopyFrom(HashSetCopyable <T> other)
        {
            if (this.m_buckets != null)
            {
                PoolArray <int> .Recycle(ref this.m_buckets);
            }
            if (other.m_buckets != null)
            {
                this.m_buckets = PoolArray <int> .Spawn(other.m_buckets.Length);

                for (int i = 0; i < this.m_buckets.Length; ++i)
                {
                    this.m_buckets[i] = other.m_buckets[i];
                }
            }

            if (this.m_slots != null)
            {
                PoolArray <Slot> .Recycle(ref this.m_slots);
            }
            if (other.m_slots != null)
            {
                this.m_slots = PoolArray <Slot> .Spawn(other.m_slots.Length);

                for (int i = 0; i < this.m_slots.Length; ++i)
                {
                    this.m_slots[i] = other.m_slots[i];
                }
            }

            this.m_count     = other.m_count;
            this.m_lastIndex = other.m_lastIndex;
            this.m_freeList  = other.m_freeList;
            this.m_comparer  = other.m_comparer;
            this.m_version   = other.m_version;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Checks if equality comparers are equal. This is used for algorithms that can
 /// speed up if it knows the other item has unique elements. I.e. if they're using
 /// different equality comparers, then uniqueness assumption between sets break.
 /// </summary>
 /// <param name="set1"></param>
 /// <param name="set2"></param>
 /// <returns></returns>
 private static bool AreEqualityComparersEqual(HashSetCopyable <T> set1, HashSetCopyable <T> set2)
 {
     return(set1.Comparer.Equals(set2.Comparer));
 }