Ejemplo n.º 1
0
        public void TestMixtureModelOnChr1()
        {
            string            file = Path.Combine(TestPaths.LocalTestDataDirectory, "Chr1.csv");
            SparseArray <int> AD   = new SparseArray <int>();
            SparseArray <int> DP   = new SparseArray <int>();

            using (StreamReader sr = new StreamReader(new FileStream(file, FileMode.Open)))
            {
                var counter = 0;
                while (counter < 20000)
                {
                    string   line = sr.ReadLine();
                    string[] arr  = line.Split(',');
                    int      dp   = int.Parse(arr[arr.Length - 1]);
                    DP.Add(dp);

                    if (arr.Length == 2)
                    {
                        AD.Add(dp - int.Parse(arr[0]));
                    }
                    else
                    {
                        AD.Add(int.Parse(arr[arr.Length - 2]));
                    }

                    counter++;
                }
            }

            MixtureModel model = MixtureModel.FitMixtureModel(AD, DP);

            Assert.Equal(0.000656, model.Means[0], 3);
            Assert.Equal(0.366, model.Means[1], 3);
            Assert.Equal(0.998, model.Means[2], 3);
        }
Ejemplo n.º 2
0
        public void IndexOfTest()
        {
            SparseArray <int> test = new SparseArray <int>();

            test.Add(100);
            test.Add(200);

            Assert.Equal(1, test.IndexOf(200));
            Assert.Equal(-1, test.IndexOf(300));
        }
Ejemplo n.º 3
0
        public void AddTest()
        {
            int count = _array.Count;

            _array.Add(0);
            Assert.Equal(count + 1, _array.Count);
            Assert.Equal(0, _array[_array.Count - 1]);

            _array.Add(5);
            Assert.Equal(count + 2, _array.Count);
            Assert.Equal(5, _array[_array.Count - 1]);
        }
Ejemplo n.º 4
0
        public void SparseArrayEnumeratorTest()
        {
            SparseArray <Int32> array = new SparseArray <Int32>(this.values);

            IEnumerator         enumerator                = this.values.GetEnumerator();
            IEnumerator <Int32> arrayEnumerator           = array.GetEnumerator();
            IEnumerator         arrayCollectionEnumerator = (array as IEnumerable).GetEnumerator();

            while (enumerator.MoveNext())
            {
                arrayEnumerator.MoveNext().ShouldBeTrue();
                arrayCollectionEnumerator.MoveNext().ShouldBeTrue();

                arrayEnumerator.Current.ShouldBe(enumerator.Current);
                arrayEnumerator.Current.ShouldBe(arrayCollectionEnumerator.Current);
            }

            arrayEnumerator.Reset();
            arrayCollectionEnumerator.Reset();

            while (arrayEnumerator.MoveNext())
            {
                arrayCollectionEnumerator.MoveNext().ShouldBeTrue();
                arrayEnumerator.Current.ShouldBe(arrayCollectionEnumerator.Current);
            }

            // exceptions
            array.Add(0);

            Should.Throw <InvalidOperationException>(() => arrayEnumerator.MoveNext());
            Should.Throw <InvalidOperationException>(() => arrayEnumerator.Reset());
        }
Ejemplo n.º 5
0
        public void SparseArrayAddTest()
        {
            // empty array
            SparseArray <Int32> array = new SparseArray <Int32>(0);

            for (Int32 index = 0; index < this.values.Length; index++)
            {
                array.Add(this.values[index]);
            }

            array.Length.ShouldBe(this.values.Length);

            for (Int32 index = 0; index < array.Count; index++)
            {
                array[index].ShouldBe(this.values[index]);
            }

            // filled array
            array = new SparseArray <Int32>(Enumerable.Repeat(0, 10));

            for (Int32 index = 0; index < this.values.Length; index++)
            {
                array.Add(this.values[index]);
            }

            array.Length.ShouldBe(this.values.Length + 10);

            for (Int32 index = 10; index < array.Count; index++)
            {
                array[index].ShouldBe(this.values[index - 10]);
            }
        }
Ejemplo n.º 6
0
        private static void Push(FloatingText txt, int key)
        {
            txt._key = key;

            var stack = Stacks[key];

            if (stack == null)
            {
                stack = new List <FloatingText>();
                Stacks.Add(key, stack);
            }

            if (stack.Count > 0)
            {
                var below      = txt;
                var aboveIndex = stack.Count - 1;
                while (aboveIndex >= 0)
                {
                    var above = stack[aboveIndex];
                    if (above.Y + above.Height > below.Y)
                    {
                        above.Y = below.Y - above.Height;

                        below = above;
                        aboveIndex--;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            stack.Add(txt);
        }
Ejemplo n.º 7
0
        public void SparseArrayAddTest()
        {
            // empty array

            SparseArray <Int32> array = new SparseArray <Int32>(0);

            for (Int32 i = 0; i < _values.Length; i++)
            {
                array.Add(_values[i]);
            }

            Assert.AreEqual(_values.Length, array.Length);

            for (Int32 i = 0; i < array.Count; i++)
            {
                Assert.AreEqual(_values[i], array[i]);
            }


            // filled array

            array = new SparseArray <Int32>(Enumerable.Repeat(0, 10));

            for (Int32 i = 0; i < _values.Length; i++)
            {
                array.Add(_values[i]);
            }

            Assert.AreEqual(_values.Length + 10, array.Length);

            for (Int32 i = 10; i < array.Count; i++)
            {
                Assert.AreEqual(_values[i - 10], array[i]);
            }
        }
Ejemplo n.º 8
0
        public SparseArrayTests()
        {
            int[] arr = new int[] { 0, 0, 55, 50, 0, 22, 80, 0, 56, 52, 40, 0, 63 };
            _array = new SparseArray <int>();

            foreach (int val in arr)
            {
                _array.Add(val);
            }
        }
Ejemplo n.º 9
0
        public void OutOfOrderStartingMeansTest()
        {
            string            file = Path.Combine(TestPaths.LocalTestDataDirectory, "Chr1.csv");
            SparseArray <int> AD   = new SparseArray <int>();
            SparseArray <int> DP   = new SparseArray <int>();

            using (StreamReader sr = new StreamReader(new FileStream(file, FileMode.Open)))
            {
                int counter = 0;
                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null || counter > 100000)
                    {
                        break;
                    }

                    string[] arr = line.Split(',');
                    int      dp  = int.Parse(arr[arr.Length - 1]);
                    DP.Add(dp);

                    if (arr.Length == 2)
                    {
                        AD.Add(dp - int.Parse(arr[0]));
                    }
                    else
                    {
                        AD.Add(int.Parse(arr[arr.Length - 2]));
                    }
                    counter++;
                }
            }

            var model1 = MixtureModel.FitMixtureModel(AD, DP, new double[] { 0.01, 0.45, 0.99 });
            var model2 = MixtureModel.FitMixtureModel(AD, DP, new double[] { 0.45, 0.01, 0.99 });

            for (int i = 0; i < model1.Means.Length; i++)
            {
                Assert.Equal(model1.Means[i], model2.Means[i], 4);
                Assert.Equal(model1.MixtureWeights[i], model2.MixtureWeights[i], 4);
            }
        }
Ejemplo n.º 10
0
        public void MalformedDataTest()
        {
            SparseArray <int> alleleDepth = new SparseArray <int>();
            SparseArray <int> totalDepth  = new SparseArray <int>();

            for (int i = 0; i < 10; i++)
            {
                alleleDepth.Add(0);
                totalDepth.Add(10);
            }

            Assert.Throws <MixtureModelException>(() => MixtureModel.FitMixtureModel(alleleDepth, totalDepth));
        }
Ejemplo n.º 11
0
        public virtual Plant Plant(Plant.Seed seed, int pos)
        {
            var plant = plants[pos];

            if (plant != null)
            {
                plant.Wither();
            }

            plant = seed.Couch(pos);
            plants.Add(pos, plant);

            GameScene.Add(plant);

            return(plant);
        }
Ejemplo n.º 12
0
        public void SparseArrayEnumeratorTest()
        {
            SparseArray <Int32> array = new SparseArray <Int32>(_values);

            IEnumerator         enumerator                = _values.GetEnumerator();
            IEnumerator <Int32> arrayEnumerator           = array.GetEnumerator();
            IEnumerator         arrayCollectionEnumerator = (array as IEnumerable).GetEnumerator();

            while (enumerator.MoveNext())
            {
                Assert.IsTrue(arrayEnumerator.MoveNext());
                Assert.IsTrue(arrayCollectionEnumerator.MoveNext());

                Assert.AreEqual(enumerator.Current, arrayEnumerator.Current);
                Assert.AreEqual(arrayCollectionEnumerator.Current, arrayEnumerator.Current);
            }


            arrayEnumerator.Reset();
            arrayCollectionEnumerator.Reset();

            while (arrayEnumerator.MoveNext())
            {
                Assert.IsTrue(arrayCollectionEnumerator.MoveNext());
                Assert.AreEqual(arrayCollectionEnumerator.Current, arrayEnumerator.Current);
            }


            // exceptions

            array.Add(0);

            Assert.Throws <InvalidOperationException>(() => arrayEnumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => arrayEnumerator.Reset());

            arrayEnumerator.Dispose();

            Assert.Throws <ObjectDisposedException>(() => arrayEnumerator.MoveNext());

            arrayEnumerator.Dispose();
        }
Ejemplo n.º 13
0
        public virtual Heap Drop(Item item, int cell)
        {
            if (Dungeon.IsChallenged(Challenges.NO_FOOD) && item is Food)
            {
                item = new Gold(item.Price());
            }
            else if (Dungeon.IsChallenged(Challenges.NO_ARMOR) && item is Armor)
            {
                item = new Gold(item.Price());
            }
            else if (Dungeon.IsChallenged(Challenges.NO_HEALING) && item is PotionOfHealing)
            {
                item = new Gold(item.Price());
            }

            if ((map[cell] == Terrain.ALCHEMY) && !(item is Plant.Seed))
            {
                int n;
                do
                {
                    n = cell + NEIGHBOURS8[pdsharp.utils.Random.Int(8)];
                } while (map[n] != Terrain.EMPTY_SP);
                cell = n;
            }

            var heap = heaps[cell];

            if (heap == null)
            {
                heap     = new Heap();
                heap.Pos = cell;
                if (map[cell] == Terrain.CHASM || (Dungeon.Level != null && pit[cell]))
                {
                    GameScene.Discard(heap);
                }
                else
                {
                    heaps.Add(cell, heap);
                    GameScene.Add(heap);
                }
            }
            else
            if (heap.HeapType == Heap.Type.LockedChest || heap.HeapType == Heap.Type.CrystalChest)
            {
                int n;
                do
                {
                    n = cell + NEIGHBOURS8[pdsharp.utils.Random.Int(8)];
                }while (!passable[n] && !avoid[n]);

                return(Drop(item, n));
            }

            heap.Drop(item);

            if (Dungeon.Level != null)
            {
                Press(cell, null);
            }

            return(heap);
        }
Ejemplo n.º 14
0
        public virtual void RestoreFromBundle(Bundle bundle)
        {
            mobs  = new HashSet <Mob>();
            heaps = new SparseArray <Heap>();

            Blobs  = new Dictionary <Type, Blob>();
            plants = new SparseArray <Plant>();

            map     = bundle.GetIntArray(MAP);
            visited = bundle.GetBooleanArray(VISITED);
            mapped  = bundle.GetBooleanArray(MAPPED);

            entrance = bundle.GetInt(ENTRANCE);
            exit     = bundle.GetInt(EXIT);

            weakFloorCreated = false;

            AdjustMapSize();

            var collection = bundle.GetCollection(HEAPS);

            foreach (var h in collection)
            {
                var heap = (Heap)h;
                if (resizingNeeded)
                {
                    heap.Pos = AdjustPos(heap.Pos);
                }

                heaps.Add(heap.Pos, heap);
            }

            collection = bundle.GetCollection(PLANTS);
            foreach (var p in collection)
            {
                var plant = (Plant)p;
                if (resizingNeeded)
                {
                    plant.Pos = AdjustPos(plant.Pos);
                }

                plants.Add(plant.Pos, plant);
            }

            collection = bundle.GetCollection(MOBS);
            foreach (var m in collection)
            {
                var mob = (Mob)m;
                if (mob == null)
                {
                    continue;
                }

                if (resizingNeeded)
                {
                    mob.pos = AdjustPos(mob.pos);
                }

                mobs.Add(mob);
            }

            collection = bundle.GetCollection(BLOBS);
            foreach (var blob in collection.OfType <Blob>())
            {
                Blobs.Add(blob.GetType(), blob);
            }

            BuildFlagMaps();
            CleanWalls();
        }
Ejemplo n.º 15
0
    private static void Tests1()
    {
        int  keysCount = 1 << 15;
        int  maxValue  = 1 << 16;
        uint umaxValue = 1 << 16;

        int[]  keys  = new int[keysCount];
        uint[] ukeys = new uint[keysCount];

        var r = new Random(42);

        for (int i = 0; i < keysCount; i++)
        {
            keys[i]  = r.Next(maxValue);
            ukeys[i] = (uint)keys[i];
        }

        foreach (var warmup in new[] { true, false })
        {
            for (int li = 0; li < 40; li++)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();


                var dict = new Dictionary <int, int>();

                Measure($"Add to Dictionary", warmup, () =>
                {
                    for (int i = 0; i < keysCount; i++)
                    {
                        dict[keys[i]] = maxValue - keys[i];
                    }
                    return(0);
                });

                Measure($"Sum in Dictionary", warmup, () =>
                {
                    int checksum = 0;
                    for (int i = 0; i < keysCount; i++)
                    {
                        checksum += dict[keys[i]];
                    }
                    return(checksum);
                });

                Measure($"Rem from Dictionary", warmup, () =>
                {
                    for (int i = 0; i < keysCount; i++)
                    {
                        dict.Remove(keys[i]);
                    }
                    return(0);
                });


                var sa = new SparseArray(16, 10);

                Measure($"Add to SparseArray", warmup, () =>
                {
                    for (int i = 0; i < keysCount; i++)
                    {
                        sa.Add(keys[i], maxValue - keys[i]);
                    }
                    return(0);
                });

                Measure($"Sum in SparseArray", warmup, () =>
                {
                    int checksum = 0;
                    for (int i = 0; i < keysCount; i++)
                    {
                        checksum += sa.Get(keys[i]);
                    }
                    return(checksum);
                });

                //Measure($"Rem from SparseArray", warmup, () =>
                //{
                //    for (int i = 0; i < keysCount; i++)
                //        dict.Remove(keys[i]);
                //    return 0;
                //});


                var um = new UInt32UInt32Map();

                Measure($"Add to UInt32UInt32Map", warmup, () =>
                {
                    for (int i = 0; i < keysCount; i++)
                    {
                        um.GetRef(ukeys[i]) = (umaxValue - ukeys[i]);
                    }
                    return(0);
                });

                Measure($"Sum in UInt32UInt32Map", warmup, () =>
                {
                    uint checksum = 0;
                    for (int i = 0; i < keysCount; i++)
                    {
                        checksum += um.GetRef(ukeys[i]);
                    }
                    return(checksum);
                });


                var im = new Int32Int32Map();

                Measure($"Add to Int32Int32Map", warmup, () =>
                {
                    for (int i = 0; i < keysCount; i++)
                    {
                        im.GetRef(keys[i]) = (maxValue - keys[i]);
                    }
                    return(0);
                });

                Measure($"Sum in Int32Int32Map", warmup, () =>
                {
                    int checksum = 0;
                    for (int i = 0; i < keysCount; i++)
                    {
                        checksum += im.GetRef(keys[i]);
                    }
                    return(checksum);
                });


                /*
                 * var pool = new BufferPool(256).SpecializeFor<int>();
                 * QuickDictionary<int, int, Buffer<int>, Buffer<int>, Buffer<int>, PrimitiveComparer<int>>
                 *  .Create(pool, pool, pool, 2, 3, out var bdict);
                 *
                 * Measure($"Add to BepuDictionary", warmup, () =>
                 * {
                 *  for (int i = 0; i < keysCount; i++)
                 *      bdict.Add(keys[i], maxValue - keys[i], pool, pool, pool);
                 *  return 0;
                 * });
                 *
                 * Measure($"Sum in BepuDictionary", warmup, () =>
                 * {
                 *  int checksum = 0;
                 *  for (int i = 0; i < keysCount; i++)
                 *  {
                 *      bdict.TryGetValue(keys[i], out int value);
                 *      checksum +=  value;
                 *  }
                 *
                 *  return checksum;
                 * });
                 *
                 * Measure($"Rem from BepuDictionary", warmup, () =>
                 * {
                 *  for (int i = 0; i < keysCount; i++)
                 *      bdict.FastRemove(keys[i]);
                 *  return 0;
                 * });
                 *
                 * //dictionary.EnsureCapacity(dictionary.Count * 3, pool, pool, pool);
                 * //dictionary.Compact(pool, pool, pool);
                 *
                 * bdict.Dispose(pool, pool, pool);
                 * pool.Raw.Clear();
                 */
            }
        }


        foreach (var result in results_)
        {
            Console.WriteLine(
                $"Benchmarked avg of {result.Value.Count} samples totalling {result.Value.Average():F3} µs to {result.Key}");
        }


        //TestSetResizing<Buffer<int>, BufferPool<int>>(bufferPool);


        //var arrayPool = new ArrayPool<int>();
        //TestSetResizing<Array<int>, ArrayPool<int>>(arrayPool);
        //arrayPool.Clear();
    }