Exemplo n.º 1
0
        public void RepeatedInsert()
        {
            KukuTable ct = new KukuTable(
                new KukuTableParameters
            {
                TableSize        = 1 << 10,
                    StashSize    = 0,
                    LocFuncCount = 4,
                    LocFuncSeed  = Item.MakeZeroItem(),
                    MaxProbe     = 10,
                    EmptyItem    = Item.MakeZeroItem()
            }
                );

            Assert.IsTrue(ct.Insert(Item.MakeItem(1, 0)));
            Assert.IsTrue(ct.Insert(Item.MakeItem(0, 1)));
            Assert.IsTrue(ct.Insert(Item.MakeItem(1, 1)));
            Assert.IsTrue(ct.Insert(Item.MakeItem(2, 2)));

            Assert.IsFalse(ct.Insert(Item.MakeItem(1, 0)));
            Assert.IsFalse(ct.Insert(Item.MakeItem(0, 1)));
            Assert.IsFalse(ct.Insert(Item.MakeItem(1, 1)));
            Assert.IsFalse(ct.Insert(Item.MakeItem(2, 2)));
        }
Exemplo n.º 2
0
        public void Populate2()
        {
            KukuTable ct = new KukuTable(
                new KukuTableParameters
            {
                TableSize        = 1 << 10,
                    StashSize    = 0,
                    LocFuncCount = 2,
                    LocFuncSeed  = Item.MakeZeroItem(),
                    MaxProbe     = 10,
                    EmptyItem    = Item.MakeZeroItem()
            }
                );

            for (uint i = 0; i < ct.TableSize; i++)
            {
                Assert.IsTrue(ct.IsEmpty(i));
            }

            Assert.IsTrue(ct.Insert(Item.MakeItem(1, 0)));
            Assert.IsTrue(ct.Insert(Item.MakeItem(0, 1)));
            Assert.IsTrue(ct.Insert(Item.MakeItem(1, 1)));
            Assert.IsTrue(ct.Insert(Item.MakeItem(2, 2)));
            Utilities.AssertThrows <ArgumentException>(() => ct.Insert(ct.EmptyItem));
            Utilities.AssertThrows <ArgumentException>(() => ct.Insert(Item.MakeZeroItem()));

            int nonEmpties = 0;

            for (uint i = 0; i < ct.TableSize; i++)
            {
                nonEmpties += ct.IsEmpty(i) ? 0 : 1;
            }
            Assert.AreEqual(4, nonEmpties);

            Assert.IsTrue(ct.Query(Item.MakeItem(1, 0)).Found);
            Assert.IsTrue(ct.Query(Item.MakeItem(0, 1)).Found);
            Assert.IsTrue(ct.Query(Item.MakeItem(1, 1)).Found);
            Assert.IsTrue(ct.Query(Item.MakeItem(2, 2)).Found);
            Assert.IsFalse(ct.Query(Item.MakeItem(3, 3)).Found);
        }
Exemplo n.º 3
0
        static void RunExample(KukuTableParameters parameters)
        {
            var kukuTable = new KukuTable(parameters);

            ulong roundCounter = 0;

            while (true)
            {
                Console.WriteLine($"Inserted {roundCounter * 20} items.");
                Console.WriteLine($"Fill rate: {kukuTable.FillRate}.");

                Console.ReadKey();

                for (ulong i = 0; i < 20; i++)
                {
                    var item = Item.MakeItem(i + 1, roundCounter + 1);
                    if (!kukuTable.Insert(item))
                    {
                        Console.WriteLine($"Insertion failed: round_counter = {roundCounter}, i = {i}");
                        Console.WriteLine($"Inserted successfully {roundCounter * 20 + i} items.");
                        Console.WriteLine($"Fill rate: {kukuTable.FillRate}.");

                        var lastItem = kukuTable.LeftoverItem;
                        Console.WriteLine($"Leftover item: [{string.Join(',', lastItem)}].");
                        break;
                    }
                }

                PrintTable(kukuTable);

                if (!kukuTable.IsEmptyItem(kukuTable.LeftoverItem))
                {
                    break;
                }

                roundCounter++;
            }

            while (true)
            {
                Console.WriteLine("Query item: ");
                var  input = Console.ReadLine();
                Item item;
                try
                {
                    if (input == null)
                    {
                        continue;
                    }

                    var data = input.Split(",").Select(ulong.Parse).ToArray();
                    item = Item.MakeItem(data[0], data[1]);
                }
                catch
                {
                    Console.WriteLine($"Incorrect format. Expected: <ulong>,<ulong>");
                    continue;
                }

                var result = kukuTable.Query(item);
                Console.WriteLine($"Found: {result.Found}");
                if (result.Found)
                {
                    Console.WriteLine($"Location: {result.Location}");
                    Console.WriteLine($"In stash: {result.InStash}");
                    Console.WriteLine($"Hash function index: {result.LocFuncIndex}");
                }
            }
        }
Exemplo n.º 4
0
        public void Populate1()
        {
            KukuTable ct = new KukuTable(
                new KukuTableParameters
            {
                TableSize    = 1,
                StashSize    = 0,
                LocFuncCount = 1,
                LocFuncSeed  = Item.MakeZeroItem(),
                MaxProbe     = 10,
                EmptyItem    = Item.MakeZeroItem()
            }
                );

            Assert.IsTrue(ct.IsEmptyItem(ct[0]));
            Assert.IsTrue(ct.Insert(Item.MakeItem(1, 0)));
            Assert.IsFalse(ct.Insert(Item.MakeItem(0, 1)));
            Utilities.AssertThrows <ArgumentException>(() => ct.Insert(ct.EmptyItem));
            Utilities.AssertThrows <ArgumentException>(() => ct.Insert(Item.MakeZeroItem()));
            Assert.IsFalse(ct.IsEmpty(0));

            ct = new KukuTable(
                new KukuTableParameters
            {
                TableSize    = 1,
                StashSize    = 0,
                LocFuncCount = 2,
                LocFuncSeed  = Item.MakeZeroItem(),
                MaxProbe     = 10,
                EmptyItem    = Item.MakeZeroItem()
            }
                );

            Assert.IsTrue(ct.IsEmptyItem(ct[0]));
            Assert.IsTrue(ct.Insert(Item.MakeItem(1, 0)));
            Assert.IsFalse(ct.Insert(Item.MakeItem(0, 1)));
            Utilities.AssertThrows <ArgumentException>(() => ct.Insert(ct.EmptyItem));
            Utilities.AssertThrows <ArgumentException>(() => ct.Insert(Item.MakeZeroItem()));
            Assert.IsFalse(ct.IsEmpty(0));

            ct = new KukuTable(
                new KukuTableParameters
            {
                TableSize    = 2,
                StashSize    = 0,
                LocFuncCount = 1,
                LocFuncSeed  = Item.MakeZeroItem(),
                MaxProbe     = 10,
                EmptyItem    = Item.MakeZeroItem()
            }
                );

            Assert.IsTrue(ct.IsEmptyItem(ct[0]));
            Assert.IsTrue(ct.Insert(Item.MakeItem(1, 0)));

            // Collision
            Assert.IsFalse(ct.Insert(Item.MakeItem(0, 1)));

            // No collision
            Assert.IsTrue(ct.Insert(Item.MakeItem(0, 2)));

            Assert.IsFalse(ct.IsEmpty(0));
            Assert.IsFalse(ct.IsEmpty(1));
        }