Example #1
0
        /// <summary>
        /// Checks if a string contains any of a collections on characters
        /// </summary>
        /// <param name="str"></param>
        /// <param name="chars"></param>
        /// <returns></returns>
        public static bool ContainsAny(this string str, params char[] chars)
        {
            if (chars == null)
            {
                throw new ArgumentNullException("chars");
            }
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            if (chars.Length < 1)
            {
                throw new InvalidOperationException("Attempting a contains check with an empty set.");
            }

            Set <char> set = new SetHashArray <char>();

            foreach (char c in chars)
            {
                set.Add(c);
            }
            foreach (char c in str)
            {
                if (set.Contains(c))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// Checks if a string contains any of a collections on characters
        /// </summary>
        /// <param name="@string"></param>
        /// <param name="chars"></param>
        /// <returns></returns>
        public static bool ContainsAny(this string @string, params char[] chars)
        {
            if (chars == null)
            {
                throw new ArgumentNullException(nameof(chars));
            }
            if (@string == null)
            {
                throw new ArgumentNullException(nameof(@string));
            }
            if (chars.Length < 1)
            {
                throw new InvalidOperationException("Attempting a contains check with an empty set.");
            }

            Towel.DataStructures.ISet <char> set = new SetHashArray <char>();
            foreach (char c in chars)
            {
                set.Add(c);
            }
            foreach (char c in @string)
            {
                if (set.Contains(c))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #3
0
 private SetHashArray(SetHashArray <T> setHashArray)
 {
     this._equate    = setHashArray._equate;
     this._hash      = setHashArray._hash;
     this._table     = setHashArray._table.Clone() as int[];
     this._nodes     = setHashArray._nodes.Clone() as Node[];
     this._count     = setHashArray._count;
     this._lastIndex = setHashArray._lastIndex;
     this._freeList  = setHashArray._freeList;
 }
Example #4
0
        public void Add()
        {
            ISet <int> addable  = new SetHashArray <int>();
            int        addCount = AddCount;

            for (int i = 0; i < addCount; i++)
            {
                addable.Add(i);
            }
        }
Example #5
0
        public void Add()
        {
            ISet <Person> set = new SetHashArray <Person>(
                (a, b) => a.Id == b.Id,
                x => x.Id.GetHashCode());

            foreach (Person person in RandomTestData)
            {
                set.Add(person);
            }
        }
Example #6
0
        /// <summary>Determines if the data contains any duplicates.</summary>
        /// <typeparam name="T">The generic type of the data.</typeparam>
        /// <param name="stepper">The stepper function for the data.</param>
        /// <param name="equate">An equality function for the data</param>
        /// <param name="hash">A hashing function for the data.</param>
        /// <returns>True if the data contains duplicates. False if not.</returns>
        /// <remarks>Use the StepperBreak overload if possible. It is more effiecient.</remarks>
        public static bool ContainsDuplicates <T>(this Stepper <T> stepper, Equate <T> equate, Hash <T> hash)
        {
            bool             duplicateFound = false;
            SetHashArray <T> set            = new SetHashArray <T>(equate, hash);

            stepper(x =>
            {
                if (set.Contains(x))
                {
                    duplicateFound = true;
                }
                else
                {
                    set.Add(x);
                }
            });
            return(duplicateFound);
        }
Example #7
0
        /// <summary>
        /// Determines if the data contains any duplicates.
        /// </summary>
        /// <typeparam name="T">The generic type of the data.</typeparam>
        /// <param name="stepper">The stepper function for the data.</param>
        /// <param name="equate">An equality function for the data</param>
        /// <param name="hash">A hashing function for the data.</param>
        /// <returns>True if the data contains duplicates. False if not.</returns>
        public static bool ContainsDuplicates <T>(this StepperBreak <T> stepper, Equate <T> equate, Hash <T> hash)
        {
            bool             duplicateFound = false;
            SetHashArray <T> set            = new SetHashArray <T>(equate, hash);

            stepper((T item) =>
            {
                if (set.Contains(item))
                {
                    duplicateFound = true;
                    return(StepStatus.Break);
                }
                else
                {
                    set.Add(item);
                    return(StepStatus.Continue);
                }
            });
            return(duplicateFound);
        }
Example #8
0
        public static void TestOmnitree1()
        {
            #region construction

            Omnitree.Location <Object3D, double, double, double> locate = (Object3D record, out double a, out double b, out double c) =>
            {
                a = record.X;
                b = record.Y;
                c = record.Z;
            };

            Compute <double> .Compare(0, 0);

            OmnitreePoints <Object3D, double, double, double> omnitree = new OmnitreePointsLinked <Object3D, double, double, double>(locate);

            #endregion

            #region random generation

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

            Random     random  = new Random(0);
            int        count   = 100;
            Object3D[] records = new Object3D[count];
            for (int i = 0; i < count; i++)
            {
                records[i] = new Object3D(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.Count: " + omnitree.Count);
            //Console.WriteLine("OmniTree._top.Count: " + (omnitree as OmnitreeLinked<TestObject, double>)._top.Count);

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

            #endregion

            #region validation

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

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

            #endregion

            #region querying

            Console.WriteLine("Value Querying: ");

            bool query_test = false;
            for (int i = 0; i < count; i++)
            {
                query_test = false;
                double a, b, c;
                locate(records[i], out a, out b, out c);
                omnitree[a, b, c]((Object3D record) => { query_test = true; });
                if (query_test == false)
                {
                    Console.WriteLine("Querying INVALID on value: " + i);
                    break;
                }
                if (i % (count / 10) == 0)
                {
                    Console.WriteLine(((double)i / (double)count * 100D) + "%");
                }
            }
            if (query_test == true)
            {
                Console.WriteLine("Querying Validated.");
            }
            else
            {
                Console.WriteLine("Querying INVALID.");
            }

            #endregion

            #region dynamic values (re-randomizing)

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

            foreach (Object3D 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++)
            {
                int tempCount = omnitree.Count;

                // 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, double>)._top.Count);

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

            #endregion

            Console.WriteLine();
            Console.WriteLine("TEST COMPLETE");
        }
Example #9
0
File: Set.cs Project: inktan/Towel
 internal Enumerator(SetHashArray <T, Equate, Hash> set)
 {
     _set     = set;
     _index   = 0;
     _current = default !;
Example #10
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");
        }