Пример #1
0
        public void Create([Values(1, 100, Int16.MaxValue)] Int16 capacity)
        {
            IndexPool pool = new IndexPool(capacity);

            Assert.AreEqual(0, pool.Count);
            //Assert.AreEqual(Count(pool), pool.Count);
        }
Пример #2
0
 public int Allocate()
 {
     if (IndexesRemaining == 0)
     {
         Expand();
     }
     return(IndexPool.AllocateInstance());
 }
Пример #3
0
 public ComponentPool(int expansionSize, int initialSize)
 {
     Count            = initialSize;
     ExpansionSize    = expansionSize;
     IndexPool        = new IndexPool(expansionSize, initialSize);
     Components       = new T[initialSize];
     _onPoolExtending = new Subject <bool>();
 }
Пример #4
0
        public void should_allocate_and_remove_next_available_index()
        {
            var indexPool = new IndexPool(10, 10);
            var index     = indexPool.AllocateInstance();

            Assert.InRange(index, 0, 10);
            Assert.DoesNotContain(index, indexPool.AvailableIndexes);
        }
Пример #5
0
 public ComponentPool(int expansionSize, int initialSize)
 {
     Count            = initialSize;
     ExpansionSize    = expansionSize;
     IndexPool        = new IndexPool(expansionSize, initialSize);
     Components       = new T[initialSize];
     _onPoolExtending = new Subject <bool>();
     IsStructType     = typeof(T).IsValueType;
 }
Пример #6
0
        public void should_allocate_up_front_ids()
        {
            var indexPool = new IndexPool(3, 3);

            Assert.Equal(3, indexPool.AvailableIndexes.Count);

            var expectedIdEntries = Enumerable.Range(0, 3).ToArray();

            Assert.All(indexPool.AvailableIndexes, x => expectedIdEntries.Contains(x));
        }
Пример #7
0
        public void Expand(int amountToAdd)
        {
            var newCount   = Components.Length + amountToAdd;
            var newEntries = new T[newCount];

            Components.CopyTo(newEntries, 0);
            IndexPool.Expand(newCount - 1);
            Components = newEntries;
            Count      = newCount;

            _onPoolExtending.OnNext(true);
        }
Пример #8
0
        public void should_expand_correctly_for_new_indexes()
        {
            var explicitNewIndex = 5;
            var indexPool        = new IndexPool(3, 3);

            indexPool.Expand(explicitNewIndex);

            Assert.Equal(explicitNewIndex + 1, indexPool.AvailableIndexes.Count);

            var expectedIdEntries = Enumerable.Range(0, explicitNewIndex + 1).ToArray();

            Assert.All(indexPool.AvailableIndexes, x => expectedIdEntries.Contains(x));
        }
Пример #9
0
        public void NotInvalidOperationRelease()
        {
            IndexPool pool = new IndexPool(10);
            int       e1   = pool.Reserve();
            int       e2   = pool.Reserve();

            foreach (Int16 item in pool)
            {
                pool.Release(item);
            }

            Assert.That(pool.Count == 0);
        }
Пример #10
0
        public void should_expand_correctly_with_auto_expansion()
        {
            var defaultExpansionAmount = 30;
            var originalSize           = 10;
            var expectedSize           = defaultExpansionAmount + originalSize;
            var indexPool = new IndexPool(defaultExpansionAmount, originalSize);

            indexPool.Expand();

            Assert.Equal(indexPool.AvailableIndexes.Count, expectedSize);

            var expectedIdEntries = Enumerable.Range(1, expectedSize).ToArray();

            Assert.All(indexPool.AvailableIndexes, x => expectedIdEntries.Contains(x));
        }
Пример #11
0
        public void should_expand_and_allocate_and_remove_next_available_index_when_empty()
        {
            var defaultExpansionAmount = 30;
            var originalSize           = 10;
            var expectedSize           = defaultExpansionAmount + originalSize;
            var indexPool = new IndexPool(defaultExpansionAmount, originalSize);

            indexPool.Clear();
            var index = indexPool.AllocateInstance();

            var expectedIdEntries = Enumerable.Range(0, expectedSize).ToList();

            Assert.InRange(index, 0, expectedSize);
            Assert.DoesNotContain(index, indexPool.AvailableIndexes);
            Assert.All(indexPool.AvailableIndexes, x => expectedIdEntries.Contains(x));
        }
Пример #12
0
        public void Release(int index)
        {
            var instance = Components[index];

            if (!IsStructType)
            {
                Components[index] = default;
            }

            if (instance is IDisposable disposable)
            {
                disposable.Dispose();
            }

            IndexPool.ReleaseInstance(index);
        }
Пример #13
0
        public void should_continually_expand_correctly_with_auto_expansion()
        {
            var expectedExpansions     = 5;
            var defaultExpansionAmount = 30;
            var originalSize           = 10;
            var expectedSize           = (defaultExpansionAmount * expectedExpansions) + originalSize;
            var expectedIndexEntries   = Enumerable.Range(0, expectedSize).ToArray();
            var indexPool = new IndexPool(defaultExpansionAmount, originalSize);

            for (var i = 0; i < expectedExpansions; i++)
            {
                indexPool.Expand();
            }

            Assert.Equal(expectedSize, indexPool.AvailableIndexes.Count);
            Assert.All(indexPool.AvailableIndexes, x => expectedIndexEntries.Contains(x));
        }
Пример #14
0
        public void should_continually_expand_correctly_when_allocating_over_count()
        {
            var expectedAllocations    = 200;
            var defaultExpansionAmount = 5;
            var originalSize           = 10;
            var expectedIndexEntries   = Enumerable.Range(0, expectedAllocations).ToArray();
            var actualIndexEntries     = new List <int>();
            var indexPool = new IndexPool(defaultExpansionAmount, originalSize);

            for (var i = 0; i < expectedAllocations; i++)
            {
                var index = indexPool.AllocateInstance();
                actualIndexEntries.Add(index);
            }

            Assert.Equal(0, indexPool.AvailableIndexes.Count);
            Assert.Equal(expectedAllocations, actualIndexEntries.Count);
            Assert.All(indexPool.AvailableIndexes, x => expectedIndexEntries.Contains(x));
        }
Пример #15
0
        public void Reserve()
        {
            IndexPool pool = new IndexPool(10);

            TestStruct[] data = new TestStruct[10];

            int e1 = pool.Reserve();

            Assert.That(e1 >= 0);
            Assert.AreEqual(1, pool.Count);
            data[e1].X   = 1;
            data[e1].Foo = "Two";
            data[e1].Bar = 3.45f;

            int e3 = pool.Reserve();
            int e4 = pool.Reserve();

            pool.Release((Int16)e3);
            int e5 = pool.Reserve();
            int e6 = pool.Reserve();

            pool.Release((Int16)e4);
            pool.Release((Int16)e5);
            pool.Release((Int16)e6);

            int e2 = pool.Reserve();

            Assert.That(e2 >= 0);
            //Assert.AreEqual(2, pool.Count);
            data[e2].X   = 2;
            data[e2].Foo = "Three";
            data[e2].Bar = 4.56f;

            foreach (var item in pool)
            {
                UnityEngine.Debug.Log(item);
                UnityEngine.Debug.Log(data[item].ToString());
            }
        }
Пример #16
0
 public void Release(int index) => IndexPool.ReleaseInstance(index);