Esempio n. 1
0
 public void Run()
 {
     if (this.enumerator.MoveNext())
     {
         CountedSet <TValue> .Node current = this.enumerator.Current;
         if (current.Release())
         {
             this.s.totalRetains--;
             this.count++;
             this.Run();
             this.dict.Remove(current.v);
             this.s.nodeCount--;
             this.array[this.count--] = current.v;
         }
         else
         {
             this.s.totalRetains--;
         }
     }
     else
     {
         this.Dispose();
         if (this.count > 0)
         {
             this.array = new TValue[this.count];
         }
         this.count--;
     }
 }
Esempio n. 2
0
        private bool CompareNonNull(IEnumerable <T> actual, IEnumerable <T> expected)
        {
            Debug.Assert(actual != null);
            Debug.Assert(expected != null);

            var actualCount   = new CountedSet(actual, comparer);
            var expectedCount = new CountedSet(expected, comparer);

            if (actualCount.Count != expectedCount.Count)
            {
                return(false);
            }
            if (actualCount.NullCount != expectedCount.NullCount)
            {
                return(false);
            }

            foreach (T item in actualCount)
            {
                if (actualCount.HowMany(item) != expectedCount.HowMany(item))
                {
                    return(false);
                }
            }

            return(true);
        }
        public void CountedSetInitialisesWithSpecifiedCollection()
        {
            var set = new CountedSet <string>(new[] { "foo", "foo", "bar" });

            Assert.IsNotNull(set);
            Assert.AreEqual(2, set.Count);
        }
        public void RemoveReturnsFalseIfSetDoesNotContainItem()
        {
            var  set    = new CountedSet <string>();
            bool result = set.Remove("foo");

            Assert.IsFalse(result);
        }
Esempio n. 5
0
        private void _btnWeakCacheDetails_Click(object sender, System.EventArgs e)
        {
            HashSet hashSet         = new HashSet();
            int     totalMemorySize = 0;

            CountedSet cacheEntries = new CountedSet();

            foreach (IntHashTable.Entry entry in MyPalStorage.Storage.ResourceWeakCache)
            {
                WeakReference weakRef = (WeakReference)entry.Value;
                Resource      res     = (Resource)weakRef.Target;
                hashSet.Add(res.Id);
                cacheEntries.Add(res.Type);
                totalMemorySize += res.EstimateMemorySize();
            }
            foreach (Resource res in MyPalStorage.Storage.ResourceCache)
            {
                if (res != null && !hashSet.Contains(res.Id))
                {
                    cacheEntries.Add(res.Type);
                    totalMemorySize += res.EstimateMemorySize();
                }
            }

            StringBuilder builder = new StringBuilder();

            cacheEntries.SortByCount();
            foreach (CountedSet.Entry de in cacheEntries)
            {
                builder.Append(de.Count + " - " + de.Value + "\n");
            }
            builder.Append("Estimated memory size " + totalMemorySize / 1024 + "K");
            _lblResourceWeakCacheDetails.Text = builder.ToString();
        }
        public void CountedSetTryGetCountReturnsFalseIfItemIsNotPresent()
        {
            var set = new CountedSet <string>();

            bool result = set.TryGetCount("foo", out _);

            Assert.IsFalse(result);
        }
        public void CountedSetClearEmptiesSet()
        {
            var set = new CountedSet <string>(new[] { "foo", "foo", "bar" });

            set.Clear();

            Assert.IsEmpty(set);
        }
        public void CountedSetAddsItem()
        {
            var set = new CountedSet <string>();

            set.Add("foo");

            Assert.AreEqual(1, set.Count);
        }
        public void CountedSetTryGetCountReturnsZeroIfItemIsNotPresent()
        {
            var set = new CountedSet <string>();

            int count;

            Assert.IsFalse(set.TryGetCount("foo", out count));
            Assert.AreEqual(0, count);
        }
        public void IfItemIsAddedToSetOnceRemoveCompletelyRemovesItem()
        {
            var set = new CountedSet <string>();

            set.Add("foo");
            set.Remove("foo");

            Assert.IsFalse(set.Contains("foo"));
        }
Esempio n. 11
0
 public ReleaseRecursor(CountedSet <TValue> v)
 {
     this.s          = v;
     this.dict       = this.s.index;
     this.enumerator = this.dict.Values.GetEnumerator();
     this.array      = CountedSet <TValue> .empty;
     this.count      = 0;
     this.disposed   = false;
 }
Esempio n. 12
0
 public void RetainAll()
 {
     foreach (CountedSet <TValue> .Node value in this.index.Values)
     {
         value.Retain();
         CountedSet <TValue> countedSet = this;
         countedSet.totalRetains = countedSet.totalRetains + 1;
     }
 }
        public void IfItemIsAddedToSetOnceAssociatedCountIsOne()
        {
            int count = 0;
            var set   = new CountedSet <string>();

            set.Add("foo");
            set.TryGetCount("foo", out count);

            Assert.AreEqual(1, count);
        }
        public void IfItemIsAddedToSetMultipleTimesRemoveDoesNotCompletelyRemoveItem()
        {
            var set = new CountedSet <string>();

            set.Add("foo");
            set.Add("foo");
            set.Remove("foo");

            Assert.IsTrue(set.Contains("foo"));
        }
        public void CountedSetSupportsLookupByKey()
        {
            var set = new CountedSet <string>();

            set.Add("foo");
            set.Add("foo");

            int count = set["foo"];

            Assert.AreEqual(2, count);
        }
Esempio n. 16
0
        public bool Retain()
        {
            if (this.done)
            {
                return(false);
            }
            CountedSet <TValue> .Node node = this;
            uint num  = node.count;
            uint num1 = num;

            node.count = num + 1;
            return(num1 == 0);
        }
        public void CountedSetSupportsGenericIteration()
        {
            var set = new CountedSet <string>(new[] { "foo", "foo", "foo", "bar", "bar" });

            Assert.DoesNotThrow(() =>
            {
                IEnumerator <string> enumerator = set.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    //ignore empty block
                }
            });
        }
        public void CountedSetSupportsNonGenericIteration()
        {
            var set = new CountedSet <string>(new[] { "foo", "foo", "foo", "bar", "bar" });

            Assert.DoesNotThrow(() =>
            {
                System.Collections.IEnumerator enumerator = (set as System.Collections.IEnumerable).GetEnumerator();
                while (enumerator.MoveNext())
                {
                    //ignore empty block
                }
            });
        }
        public void CountSetSupportsNonGenericIterationForExpectedNumberOfElements()
        {
            const int expected = 2;
            int       count    = 0;

            var set = new CountedSet <string>(new[] { "foo", "foo", "foo", "bar", "bar" });

            System.Collections.IEnumerator enumerator = (set as System.Collections.IEnumerable).GetEnumerator();
            while (enumerator.MoveNext())
            {
                count++;
            }
            Assert.AreEqual(expected, count);
        }
Esempio n. 20
0
    public TValue[] ReleaseAll()
    {
        TValue[] tValueArray;
        CountedSet <TValue> .ReleaseRecursor releaseRecursor = new CountedSet <TValue> .ReleaseRecursor(this);

        try
        {
            releaseRecursor.Run();
            tValueArray = releaseRecursor.array;
        }
        finally
        {
            ((IDisposable)(object)releaseRecursor).Dispose();
        }
        return(tValueArray);
    }
Esempio n. 21
0
        public bool Release()
        {
            bool flag;

            if (this.done)
            {
                flag = false;
            }
            else
            {
                CountedSet <TValue> .Node node = this;
                UInt32 num  = node.count - 1;
                uint   num1 = num;
                node.count = num;
                flag       = num1 == 0;
            }
            return(flag);
        }
        public void IfItemIsAddedToSetMultipleTimesRemoveReducesCountByOne()
        {
            int firstCount  = 0;
            int secondCount = 0;
            var set         = new CountedSet <string>();

            set.Add("foo");
            set.Add("foo");

            set.TryGetCount("foo", out firstCount);

            set.Remove("foo");

            set.TryGetCount("foo", out secondCount);

            Assert.AreEqual(2, firstCount);
            Assert.AreEqual(1, secondCount);
        }
        public void CountedSetMaintainsDistinctCountForDifferentItems()
        {
            int fooCount = 0;
            int barCount = 0;

            var set = new CountedSet <string>();

            set.Add("foo");
            set.Add("foo");
            set.Add("foo");
            set.Add("bar");
            set.Add("bar");

            set.TryGetCount("foo", out fooCount);
            set.TryGetCount("bar", out barCount);

            Assert.AreEqual(3, fooCount);
            Assert.AreEqual(2, barCount);
        }
Esempio n. 24
0
    public int Release(TValue value)
    {
        CountedSet <TValue> .Node node;
        if (!this.index.TryGetValue(value, out node))
        {
            return(-1);
        }
        bool flag = node.Release();
        CountedSet <TValue> countedSet = this;

        countedSet.totalRetains = countedSet.totalRetains - 1;
        if (flag)
        {
            this.index.Remove(value);
            CountedSet <TValue> countedSet1 = this;
            countedSet1.nodeCount = countedSet1.nodeCount - 1;
        }
        return((int)node.count);
    }
Esempio n. 25
0
 public void Run()
 {
     if (!this.enumerator.MoveNext())
     {
         this.Dispose();
         if (this.count > 0)
         {
             this.array = new TValue[this.count];
         }
         CountedSet <TValue> .ReleaseRecursor releaseRecursor = this;
         releaseRecursor.count = releaseRecursor.count - 1;
     }
     else
     {
         CountedSet <TValue> .Node current = this.enumerator.Current;
         if (!current.Release())
         {
             CountedSet <TValue> tValues = this.s;
             tValues.totalRetains = tValues.totalRetains - 1;
         }
         else
         {
             CountedSet <TValue> tValues1 = this.s;
             tValues1.totalRetains = tValues1.totalRetains - 1;
             CountedSet <TValue> .ReleaseRecursor releaseRecursor1 = this;
             releaseRecursor1.count = releaseRecursor1.count + 1;
             this.Run();
             this.dict.Remove(current.v);
             CountedSet <TValue> tValues2 = this.s;
             tValues2.nodeCount = tValues2.nodeCount - 1;
             TValue[] tValueArray = this.array;
             CountedSet <TValue> .ReleaseRecursor releaseRecursor2 = this;
             int num  = releaseRecursor2.count;
             int num1 = num;
             releaseRecursor2.count = num - 1;
             tValueArray[num1]      = current.v;
         }
     }
 }
        public void IfItemIsAddedToSetMutlipleTimesItMustBeRemovedAnEqualNumberOfTimes()
        {
            bool firstCheck  = false;
            bool secondCheck = false;
            bool thirdCheck  = false;

            var set = new CountedSet <string>();

            set.Add("foo");
            set.Add("foo");
            set.Add("foo");

            set.Remove("foo");
            firstCheck = set.Contains("foo");
            set.Remove("foo");
            secondCheck = set.Contains("foo");
            set.Remove("foo");
            thirdCheck = set.Contains("foo");

            Assert.IsTrue(firstCheck);
            Assert.IsTrue(secondCheck);
            Assert.IsFalse(thirdCheck);
        }
Esempio n. 27
0
        public bool Apply(IEnumerable <T> actual)
        {
            var expectedSet = new CountedSet <T>(expected, comparer);
            var actualSet   = new CountedSet <T>(actual, comparer);

            if (actualSet.TotalCount > expectedSet.TotalCount)
            {
                return(false);
            }

            foreach (var actualKvp in actualSet)
            {
                int actualCount   = actualKvp.Value;
                int expectedCount = expectedSet[actualKvp.Key];

                if (actualCount > expectedCount)
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 28
0
    public int Retain(TValue value)
    {
        CountedSet <TValue> .Node node;
        if (!this.index.TryGetValue(value, out node))
        {
            Dictionary <TValue, CountedSet <TValue> .Node> tValues = this.index;
            CountedSet <TValue> .Node node1 = new CountedSet <TValue> .Node()
            {
                v = value
            };
            node           = node1;
            tValues[value] = node1;
            CountedSet <TValue> countedSet = this;
            countedSet.nodeCount = countedSet.nodeCount + 1;
        }
        uint num = node.count;

        node.Retain();
        CountedSet <TValue> countedSet1 = this;

        countedSet1.totalRetains = countedSet1.totalRetains + 1;
        return((int)num);
    }
        public void IfItemIsAddedToSetMultipleTimesAssociatedCountIsIncreasedByOne()
        {
            int firstCount  = 0;
            int secondCount = 0;
            int thirdCount  = 0;

            var set = new CountedSet <string>();

            //add first time
            set.Add("foo");
            set.TryGetCount("foo", out firstCount);

            //add second time
            set.Add("foo");
            set.TryGetCount("foo", out secondCount);

            //add third time
            set.Add("foo");
            set.TryGetCount("foo", out thirdCount);

            Assert.AreEqual(1, firstCount);
            Assert.AreEqual(2, secondCount);
            Assert.AreEqual(3, thirdCount);
        }
Esempio n. 30
0
 public override int GetHashCode(CountedSet <TValue> .Node obj)
 {
     return(this.Comparer.GetHashCode(obj.v));
 }