예제 #1
0
 public GraphSetOmnitree(Compare <T> compare, Hash <T> hash, T min, T max, Omnitree.Average <T> average)
 {
     this._nodes = new SetHashList <T>((T a, T b) => { return(compare(a, b) == Comparison.Equal); }, hash);
     Omnitree.Locate <Edge, T> locationFunction = (Edge a) => { return(Accessor.Get(new T[] { a.Start, a.End })); };
     this._edges = new OmnitreeLinked <Edge, T>(
         new T[] { min, min },
         new T[] { max, max },
         locationFunction, compare, average);
 }
예제 #2
0
        public static void TestOmnitree2()
        {
            #region construction

            Omnitree.Locate <TestObject, object> Locate = (TestObject record) =>
            {
                return((int i) =>
                {
                    switch (i)
                    {
                    case 0:
                        return record.X;

                    case 1:
                        return record.Y;

                    case 2:
                        return record.Z;

                    case 3:
                        return record.Id % 2 == 0;

                    default:
                        throw new System.Exception();
                    }
                });
            };

            Omnitree.Average <object> Average = (object a, object b) =>
            {
                if (a is double && b is double)
                {
                    return(((double)a + (double)b) / 2d);
                }
                else if (a is bool && b is bool)
                {
                    return(false);
                }
                else
                {
                    throw new System.Exception();
                }
            };

            Compare <object> Compare = (object a, object b) =>
            {
                if (a is double && b is double)
                {
                    if ((double)a < (double)b)
                    {
                        return(Comparison.Less);
                    }
                    else if ((double)a > (double)b)
                    {
                        return(Comparison.Greater);
                    }
                    else
                    {
                        return(Comparison.Equal);
                    }
                }
                else if (a is bool && b is bool)
                {
                    if ((bool)a == (bool)b)
                    {
                        return(Comparison.Equal);
                    }
                    else if ((bool)a == false)
                    {
                        return(Comparison.Less);
                    }
                    else
                    {
                        return(Comparison.Greater);
                    }
                }
                else
                {
                    throw new System.Exception();
                }
            };

            Omnitree <TestObject, object> omnitree = new OmnitreeLinked <TestObject, object>(
                new object[] { 0d, 0d, 0d, false },
                new object[] { 1d, 1d, 1d, true },
                Locate,
                Compare,
                Average);

            #endregion

            #region random generation

            Console.WriteLine("Generating random data...");

            Random       random  = new Random(0);
            int          count   = 100;
            TestObject[] records = new TestObject[count];
            for (int i = 0; i < count; i++)
            {
                records[i] = new TestObject(i, random.NextDouble(), random.NextDouble(), random.NextDouble());
            }

            Console.WriteLine("Generated random data.");

            #endregion

            #region adding

            Console.WriteLine("Building Omnitree...");

            for (int i = 0; i < count; i++)
            {
                omnitree.Add(records[i]);
                if (i % (count / 10) == 0)
                {
                    Console.WriteLine(((double)i / (double)count * 100D) + "%");
                }
            }

            Console.WriteLine("Omnitree Built.");

            Console.WriteLine("OmniTree.Count: " + omnitree.Count);
            //Console.WriteLine("OmniTree._top.Count: " + (omnitree as OmnitreeLinked<TestObject, object>)._top.Count);

            int test_count = 0;
            omnitree.Stepper((TestObject record) => { test_count++; });
            Console.WriteLine("OmniTree Stepper Count: " + test_count);

            #endregion

            #region validation

            SetHashArray <TestObject> setHash = new SetHashArray <TestObject>(
                (TestObject a, TestObject b) => { return(a.Id == b.Id); },
                (TestObject a) => { return(a.Id.GetHashCode()); });
            for (int i = 0; i < count; i++)
            {
                setHash.Add(records[i]);
            }

            bool validated2 = true;
            omnitree.Stepper((TestObject record) => { if (!setHash.Contains(record))
                                                      {
                                                          validated2 = false;
                                                      }
                             });
            if (validated2)
            {
                Console.WriteLine("Values Validated.");
            }
            else
            {
                Console.WriteLine("Values INVALID.");
            }

            #endregion

            #region querying

            Console.WriteLine("Value Querying: ");

            bool query_test2 = false;
            for (int i = 0; i < count; i++)
            {
                query_test2 = false;
                omnitree[Locate(records[i])]((TestObject record) => { query_test2 = true; });
                if (query_test2 == false)
                {
                    Console.WriteLine("Querying INVALID on value: " + i);
                    break;
                }
                if (i % (count / 10) == 0)
                {
                    Console.WriteLine(((double)i / (double)count * 100D) + "%");
                }
            }
            if (query_test2 == true)
            {
                Console.WriteLine("Querying Validated.");
            }
            else
            {
                Console.WriteLine("Querying INVALID.");
            }

            #endregion

            #region dynamic values (re-randomizing)

            Console.WriteLine("Moving randomized data...");

            foreach (TestObject record in records)
            {
                record.X += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D));
                record.Y += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D));
                record.Z += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D));
            }

            Console.WriteLine("Randomized data moved.");

            #endregion

            #region updating

            Console.WriteLine("Updating Tree Positions...");
            //// Update Method #1
            omnitree.Update();

            //// Update Method #2
            //omnitree.Update(omnitree.Min, omnitree.Max);

            Console.WriteLine("Tree Positions Updated.");

            #endregion

            #region removal

            Console.WriteLine("Removing Values: ");
            for (int i = 0; i < count; i++)
            {
                //// Removal Method #1
                omnitree.Remove(records[i]);

                //// Removal Method #2
                //omnitree.Remove(locate(records[i]), locate(records[i]));

                //// Removal Method #3
                //omnitree.Remove(locate(records[i]), locate(records[i]), (omnitree_record step) => { return records[i].Id == step.Id; });

                //// Removal Method #4
                //double[] location = new double[] { locate(records[i])(0), locate(records[i])(1), locate(records[i])(2) };
                //omnitree.Remove(location, location);

                //// Removal Method #5
                //double[] location = new double[] { locate(records[i])(0), locate(records[i])(1), locate(records[i])(2) };
                //omnitree.Remove(location, location, (omnitree_record step) => { return records[i].Id == step.Id; });

                if (omnitree.Count != count - (i + 1))
                {
                    throw new System.Exception();
                }
                if (i % (count / 10) == 0)
                {
                    Console.WriteLine(((double)i / (double)count * 100D) + "%");
                }
            }
            Console.WriteLine("Values Removed: ");

            Console.WriteLine("OmniTree.Count: " + omnitree.Count);

            //Console.WriteLine("OmniTree._top.Count: " + (omnitree as OmnitreeLinked<TestObject, object>)._top.Count);

            test_count = 0;
            omnitree.Stepper((TestObject record) => { test_count++; });
            Console.WriteLine("OmniTree Stepper Count: " + test_count);

            #endregion

            Console.WriteLine();
            Console.WriteLine("TEST COMPLETE");
        }