Exemplo n.º 1
0
 /// <summary>This constructor is for cloning purposes.</summary>
 /// <param name="map">The map to clone.</param>
 /// <runtime>O(n)</runtime>
 internal MapHashLinked(MapHashLinked <T, K> map)
 {
     _equate = map._equate;
     _hash   = map._hash;
     _table  = (Node[])map._table.Clone();
     _count  = map._count;
 }
Exemplo n.º 2
0
        public void GetEnumeratorPairsTest()
        {
            IMap <string, int> map = MapHashLinked.New <string, int>();

            map.Add(1, "Hello");
            map.Add(2, "World");
            (int, string)[] array = new (int, string)[2];
Exemplo n.º 3
0
 /// <summary>Constructs a new trie that uses linked hash tables of linked lists.</summary>
 /// <param name="equate">The equality delegate for the keys.</param>
 /// <param name="hash">The hashing function for the keys.</param>
 public TrieLinkedHashLinked(Func <T, T, bool> equate = null, Func <T, int> hash = null)
 {
     _count = 0;
     _map   = new MapHashLinked <Node, T>(
         equate ?? Statics.Equate,
         hash ?? DefaultHash);
 }
Exemplo n.º 4
0
        /// <summary>Tries to remove a value.</summary>
        /// <param name="stepper">The relative keys of the value.</param>
        /// <param name="exception">The exception that occurred if the remove failed.</param>
        /// <returns>True if the remove was successful or false if not.</returns>
        public bool TryRemove(Stepper <T> stepper, out Exception exception)
        {
            if (stepper is null)
            {
                exception = new ArgumentNullException(nameof(stepper));
                return(false);
            }
            IStack <(T, MapHashLinked <Node, T>, Node)> pathStack = new StackLinked <(T, MapHashLinked <Node, T>, Node)>();
            T finalKey = default;
            MapHashLinked <Node, T> finalMap = null;
            Node      node = null;
            Exception capturedException = null;

            stepper(key =>
            {
                finalKey = key;
                finalMap = node is null
                                        ? _map
                                        : node.Map;
                if (finalMap.Contains(key))
                {
                    node = finalMap[key];
                }
                else
                {
                    capturedException ??= new ArgumentException(nameof(stepper), "Attempted to remove a non-existing item.");
                }
                pathStack.Push((finalKey, finalMap, node));
            });
            if (!(capturedException is null))
            {
                exception = capturedException;
                return(false);
            }
Exemplo n.º 5
0

        
Exemplo n.º 6
0
 /// <summary>Constructs a new trie that uses linked hash tables of linked lists.</summary>
 /// <param name="equate">The equality delegate for the keys.</param>
 /// <param name="hash">The hashing function for the keys.</param>
 public TrieLinkedHashLinked(Equate <T> equate = null, Hash <T> hash = null)
 {
     _count = 0;
     _map   = new MapHashLinked <Node, T>(
         equate ?? Towel.Equate.Default,
         hash ?? Towel.Hash.Default);
 }
Exemplo n.º 7
0
        public void PropertyTest()
        {
            IMap <string, int> map = MapHashLinked.New <string, int>();

            map.Add(1, "Hello");
            map.Add(2, "World");
            Assert.IsTrue(map.Count is 2);
        }
Exemplo n.º 8
0
 public TreeMap(T head, Equate <T> equate, Hash <T> hash)
 {
     this._equate = equate;
     this._hash   = hash;
     this._head   = head;
     this._tree   = new MapHashLinked <NodeData, T>(this._equate, this._hash);
     this._tree.Add(this._head, new NodeData(default(T), new SetHashList <T>(this._equate, this._hash)));
 }
Exemplo n.º 9
0
 /// <summary>This constructor is for cloning purposes.</summary>
 /// <param name="map">The map to clone.</param>
 /// <runtime>O(n)</runtime>
 internal MapHashLinked(MapHashLinked <T, K, Equate, Hash> map)
 {
     _equate = map._equate;
     _hash   = map._hash;
     _table  = new Node[map._table.Length];
     map._table.CopyTo(_table);
     _count = map._count;
 }
Exemplo n.º 10
0
        /// <inheritdoc/>
        public (bool Success, Exception?Exception) TryRemove(Action <Action <T> > stepper)
        {
            if (stepper is null)
            {
                return(false, new ArgumentNullException(nameof(stepper)));
            }
            StackLinked <(T, MapHashLinked <Node, T, TEquate, THash>, Node)> stack = new();
            T finalKey;
            MapHashLinked <Node, T, TEquate, THash>?finalMap = null;
            Node?     node      = null;
            Exception?exception = null;

            stepper(key =>
            {
                finalKey = key;
                finalMap = node is null ? _map : node.Map;
                if (finalMap.Contains(key))
                {
                    node = finalMap[key];
                }
                else
                {
                    exception ??= new ArgumentException("Attempted to remove a non-existing item.", nameof(stepper));
                }
                stack.Push((finalKey, finalMap, node !));
            });
            if (exception is not null)
            {
                return(false, exception);
            }
            else if (node is null)
            {
                return(false, new ArgumentException("Stepper was empty.", nameof(stepper)));
            }
            else if (!node.IsLeaf)
            {
                return(false, new ArgumentException("Attempted to remove a non-existing item.", nameof(stepper)));
            }
            else
            {
                bool remove = true;
                while (stack.Count > 0)
                {
                    var(k, m, n) = stack.Pop();
                    n.Count--;
                    if (remove && n.Count is 0)
                    {
                        m.Remove(k);
                    }
                    else
                    {
                        remove = false;
                    }
                }
                _count--;
                return(true, null);
            }
        }
Exemplo n.º 11
0
        public void RemoveTest()
        {
            IMap <string, int> map = MapHashLinked.New <string, int>();

            map.Add(1, "Hello");
            map.Add(2, "World");
            map.Remove(1);
            Assert.ThrowsException <ArgumentException>(() => map[1]);
        }
Exemplo n.º 12
0
        [Benchmark] public void MapDelegates()
        {
            MapHashLinked <int, int> map = new MapHashLinked <int, int>();

            for (int i = 0; i < N; i++)
            {
                map.TryAdd(i, i, out _);
            }
        }
Exemplo n.º 13
0
        [Benchmark] public void MapStructs()
        {
            MapHashLinked <int, int, IntEquate, IntHash> map = new MapHashLinked <int, int, IntEquate, IntHash>();

            for (int i = 0; i < N; i++)
            {
                map.TryAdd(i, i, out _);
            }
        }
        public void MapDelegates()
        {
            IMap <int, int> map = MapHashLinked.New <int, int>();

            for (int i = 0; i < N; i++)
            {
                map.Add(i, i);
            }
        }
Exemplo n.º 15
0
        public void IndexerGetSetTest()
        {
            IMap <string, int> map = MapHashLinked.New <string, int>();

            map.Add(1, "Hello");
            map.Add(2, "World");
            const string s = "Added word";

            map[3] = s;
            Assert.IsTrue(map[3] is s);
        }
Exemplo n.º 16
0
 public void IterationSetup()
 {
     mapHashLinked        = new MapHashLinked <int, int>();
     mapHashLinkedStructs = new MapHashLinked <int, int, IntEquate, IntHash>();
     dictionary           = new System.Collections.Generic.Dictionary <int, int>();
     for (int i = 0; i < N; i++)
     {
         mapHashLinked.Add(i, i);
         mapHashLinkedStructs.Add(i, i);
         dictionary.Add(i, i);
     }
 }
Exemplo n.º 17
0
        public void Add_Testing()
        {
            {             // string, int
                const int          count = 100000;
                IMap <string, int> map   = MapHashLinked.New <string, int>();
                Iterate(count, i => map.Add(i, i.ToString()));
                map.Add(int.MinValue, int.MinValue.ToString());
                map.Add(int.MaxValue, int.MaxValue.ToString());

                // contains
                Iterate(count, i => Assert.IsTrue(map.Contains(i)));
                Assert.IsTrue(map.Contains(int.MinValue));
                Assert.IsTrue(map.Contains(int.MaxValue));
                Assert.IsFalse(map.Contains(-1));
                Assert.IsFalse(map.Contains(count));

                // get
                Iterate(count, i => Assert.IsTrue(map[i] == i.ToString()));
                Assert.IsTrue(map[int.MinValue] == int.MinValue.ToString());
                Assert.IsTrue(map[int.MaxValue] == int.MaxValue.ToString());

                Assert.ThrowsException <ArgumentException>(() => map.Add(0, 0.ToString()));
                Assert.ThrowsException <ArgumentException>(() => map.Add(int.MinValue, int.MinValue.ToString()));
                Assert.ThrowsException <ArgumentException>(() => map.Add(int.MaxValue, int.MaxValue.ToString()));
            }

            {             // int, string
                const int          count = 100000;
                IMap <int, string> map   = MapHashLinked.New <int, string>();
                Iterate(count, i => map.Add(i.ToString(), i));
                map.Add(int.MinValue.ToString(), int.MinValue);
                map.Add(int.MaxValue.ToString(), int.MaxValue);

                // contains
                Iterate(count, i => Assert.IsTrue(map.Contains(i.ToString())));
                Assert.IsTrue(map.Contains(int.MinValue.ToString()));
                Assert.IsTrue(map.Contains(int.MaxValue.ToString()));
                Assert.IsFalse(map.Contains((-1).ToString()));
                Assert.IsFalse(map.Contains(count.ToString()));

                // get
                Iterate(count, i => Assert.IsTrue(map[i.ToString()] == i));
                Assert.IsTrue(map[int.MinValue.ToString()] == int.MinValue);
                Assert.IsTrue(map[int.MaxValue.ToString()] == int.MaxValue);

                Assert.ThrowsException <ArgumentException>(() => map.Add(0.ToString(), 0));
                Assert.ThrowsException <ArgumentException>(() => map.Add(int.MinValue.ToString(), int.MinValue));
                Assert.ThrowsException <ArgumentException>(() => map.Add(int.MaxValue.ToString(), int.MaxValue));
            }
        }
Exemplo n.º 18
0
        public void Remove_Testing()
        {
            {             // string, int
                const int          count = 100000;
                IMap <string, int> map   = MapHashLinked.New <string, int>();
                Iterate(count, i => map.Add(i, i.ToString()));
                for (int i = 0; i < count; i += 3)
                {
                    map.Remove(i);
                }
                for (int i = 0; i < count; i++)
                {
                    if (i % 3 == 0)
                    {
                        Assert.IsFalse(map.Contains(i));
                    }
                    else
                    {
                        Assert.IsTrue(map.Contains(i));
                    }
                }
                Assert.IsFalse(map.Contains(-1));
                Assert.IsFalse(map.Contains(count));
            }

            {             // int, string
                const int          count = 100000;
                IMap <int, string> map   = MapHashLinked.New <int, string>();
                Iterate(count, i => map.Add(i.ToString(), i));
                for (int i = 0; i < count; i += 3)
                {
                    map.Remove(i.ToString());
                }
                for (int i = 0; i < count; i++)
                {
                    if (i % 3 == 0)
                    {
                        Assert.IsFalse(map.Contains(i.ToString()));
                    }
                    else
                    {
                        Assert.IsTrue(map.Contains(i.ToString()));
                    }
                }
                Assert.IsFalse(map.Contains((-1).ToString()));
                Assert.IsFalse(map.Contains(count.ToString()));
            }
        }
Exemplo n.º 19
0
        /// <summary>Tries to add a value to the trie.</summary>
        /// <param name="stepper">The relative keys of the value.</param>
        /// <param name="exception">The exception that occurred if the add failed.</param>
        /// <returns>True if the value was added or false if not.</returns>
        public bool TryAdd(Stepper <T> stepper, out Exception exception)
        {
            if (stepper is null)
            {
                exception = new ArgumentNullException(nameof(stepper));
                return(false);
            }
            IStack <Node> stack = new StackLinked <Node>();
            Node          node  = null;

            stepper(key =>
            {
                MapHashLinked <Node, T> map = node is null
                                        ? _map
                                        : node.Map;
                if (map.Contains(key))
                {
                    node = map[key];
                }
                else
                {
                    Node temp = new Node(Equate, Hash);
                    map[key]  = temp;
                    node      = temp;
                }
                stack.Push(node);
            });
            if (node is null)
            {
                exception = new ArgumentException(nameof(stepper), "Stepper was empty.");
                return(false);
            }
            else if (node.IsLeaf)
            {
                exception = new ArgumentException(nameof(stepper), "Attempted to add an already existing item.");
                return(false);
            }
            else
            {
                node.IsLeaf = true;
                stack.Stepper(n => n.Count++);
                _count++;
                exception = null;
                return(true);
            }
        }
Exemplo n.º 20
0
        internal static void BuildParsingLibrary()
        {
            // make a regex pattern with all the currently supported unit types and
            // build the unit string to unit type string map
            IList <string>        strings = new ListArray <string>();
            IMap <string, string> unitStringToUnitTypeString = new MapHashLinked <string, string>();

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypesWithAttribute <ParseableUnitAttribute>())
            {
                if (!type.IsEnum)
                {
                    throw new Exception("There is a bug in Towel. " + nameof(ParseableUnitAttribute) + " is on a non enum type.");
                }
                if (!type.Name.Equals("Units") || type.DeclaringType is null)
                {
                    throw new Exception("There is a bug in Towel. A unit type definition does not follow the required structure.");
                }
                string unitTypeString = type.DeclaringType.Name;
                foreach (Enum @enum in Enum.GetValues(type).Cast <Enum>())
                {
                    strings.Add(@enum.ToString());
                    unitStringToUnitTypeString.Add(@enum.ToString(), unitTypeString);
                }
            }
            strings.Add(@"\*");
            strings.Add(@"\/");
            AllUnitsRegexPattern       = string.Join("|", strings);
            UnitStringToUnitTypeString = unitStringToUnitTypeString;

            // make the Enum arrays to units map
            IMap <Enum, string> unitStringToEnumMap = new MapHashLinked <Enum, string>();

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypesWithAttribute <ParseableUnitAttribute>())
            {
                foreach (Enum @enum in Enum.GetValues(type))
                {
                    unitStringToEnumMap.Add(@enum.ToString(), @enum);
                }
            }
            UnitStringToEnumMap = unitStringToEnumMap;

            ParsingLibraryBuilt = true;
        }
Exemplo n.º 21
0
            internal static void BuildTypeSpecificParsingLibrary()
            {
                // make the delegates for constructing the measurements
                IMap <Func <T, object[], object>, string> unitsStringsToFactoryFunctions = new MapHashLinked <Func <T, object[], object>, string>();

                foreach (MethodInfo methodInfo in typeof(ParsingFunctions).GetMethods())
                {
                    if (methodInfo.DeclaringType == typeof(ParsingFunctions))
                    {
                        MethodInfo                 genericMethodInfo = methodInfo.MakeGenericMethod(typeof(T));
                        ParseableAttribute         parsableAttribute = genericMethodInfo.GetCustomAttribute <ParseableAttribute>();
                        Func <T, object[], object> factory           = genericMethodInfo.CreateDelegate <Func <T, object[], object> >();
                        unitsStringsToFactoryFunctions.Add(parsableAttribute.Key, factory);
                    }
                }
                UnitsStringsToFactoryFunctions = unitsStringsToFactoryFunctions;

                TypeSpecificParsingLibraryBuilt = true;
            }
Exemplo n.º 22
0
        public void Set_Testing()
        {
            {             // string, int
                const int          count = 100000;
                IMap <string, int> map   = MapHashLinked.New <string, int>();
                Iterate(count, i => map[i] = i.ToString());
                map[int.MinValue]          = int.MinValue.ToString();
                map[int.MaxValue]          = int.MaxValue.ToString();

                // contains
                Iterate(count, i => Assert.IsTrue(map.Contains(i)));
                Assert.IsTrue(map.Contains(int.MinValue));
                Assert.IsTrue(map.Contains(int.MaxValue));
                Assert.IsFalse(map.Contains(-1));
                Assert.IsFalse(map.Contains(count));

                // get
                Iterate(count, i => Assert.IsTrue(map[i] == i.ToString()));
                Assert.IsTrue(map[int.MinValue] == int.MinValue.ToString());
                Assert.IsTrue(map[int.MaxValue] == int.MaxValue.ToString());
            }

            {             // int, string
                const int          count = 100000;
                IMap <int, string> map   = MapHashLinked.New <int, string>();
                Iterate(count, i => map[i.ToString()] = i);
                map[int.MinValue.ToString()]          = int.MinValue;
                map[int.MaxValue.ToString()]          = int.MaxValue;

                // contains
                Iterate(count, i => Assert.IsTrue(map.Contains(i.ToString())));
                Assert.IsTrue(map.Contains(int.MinValue.ToString()));
                Assert.IsTrue(map.Contains(int.MaxValue.ToString()));
                Assert.IsFalse(map.Contains((-1).ToString()));
                Assert.IsFalse(map.Contains(count.ToString()));

                // get
                Iterate(count, i => Assert.IsTrue(map[i.ToString()] == i));
                Assert.IsTrue(map[int.MinValue.ToString()] == int.MinValue);
                Assert.IsTrue(map[int.MaxValue.ToString()] == int.MaxValue);
            }
        }
Exemplo n.º 23
0
            internal static void BuildTypeSpecificParsingLibrary()
            {
                // make the delegates for constructing the measurements
                var unitsStringsToFactoryFunctions = new MapHashLinked <Func <T, object[], object>, string, StringEquate, StringHash>();

                foreach (MethodInfo methodInfo in typeof(ParsingFunctions).GetMethods())
                {
                    if (methodInfo.DeclaringType == typeof(ParsingFunctions))
                    {
                        MethodInfo         genericMethodInfo = methodInfo.MakeGenericMethod(typeof(T));
                        ParseableAttribute?parsableAttribute = genericMethodInfo.GetCustomAttribute <ParseableAttribute>();
                        if (parsableAttribute is null)
                        {
                            throw new TowelBugException($"Encountered null {nameof(ConstructorInfo)} in {nameof(BuildTypeSpecificParsingLibrary)}.");
                        }
                        Func <T, object[], object> factory = genericMethodInfo.CreateDelegate <Func <T, object[], object> >();
                        unitsStringsToFactoryFunctions.Add(parsableAttribute.Key, factory);
                    }
                }
                UnitsStringsToFactoryFunctions = unitsStringsToFactoryFunctions;

                TypeSpecificParsingLibraryBuilt = true;
            }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            Random random = new Random();
            int    test   = 10;

            Console.WriteLine("You are runnning the Data Structures example.");
            Console.WriteLine("======================================================");
            Console.WriteLine();

            #region Link (aka Tuple)

            Console.WriteLine("  Link------------------------------------");
            Console.WriteLine();
            Console.WriteLine("    A \"Link\" is like a System.Tuple that implements");
            Console.WriteLine("    Towel.DataStructures.DataStructure. A Link/Tuple is");
            Console.WriteLine("    used when you have a small, known-sized set of objects");
            Console.WriteLine("    that you want to bundle together without making a custom");
            Console.WriteLine("    custom class.");
            Console.WriteLine();

            Link link = new Link <int, int, int, int, int, int>(0, 1, 2, 3, 4, 5);
            Console.Write("    Traversal: ");
            link.Stepper(i => Console.Write(i));
            Console.WriteLine();

            Console.WriteLine("    Size: " + link.Size);
            Console.WriteLine();

            #endregion

            #region Indexed (aka Array)

            Console.WriteLine("  Indexed---------------------------------");
            Console.WriteLine();
            Console.WriteLine("    An \"Indexed\" is just a wrapper for arrays that implements");
            Console.WriteLine("    Towel.DataStructures.DataStructure. An array is used when");
            Console.WriteLine("    dealing with static-sized, known-sized sets of data. Arrays");
            Console.WriteLine("    can be sorted along 1 dimensions for binary searching algorithms.");
            Console.WriteLine();

            IIndexed <int> indexed = new IndexedArray <int>(test);

            Console.Write("    Filling in (0-" + (test - 1) + ")...");
            for (int i = 0; i < test; i++)
            {
                indexed[i] = i;
            }
            Console.WriteLine();

            Console.Write("    Traversal: ");
            indexed.Stepper(i => Console.Write(i));
            Console.WriteLine();

            Console.WriteLine("    Length: " + indexed.Length);

            Console.WriteLine();

            #endregion

            #region Addable (aka List)

            Console.WriteLine("  Addable---------------------------------");
            Console.WriteLine();
            Console.WriteLine("    An \"Addable\" is like an IList that implements");
            Console.WriteLine("    Towel.DataStructures.DataStructure. \"AddableArray\" is");
            Console.WriteLine("    the array implementation while \"AddableLinked\" is the");
            Console.WriteLine("    the linked-list implementation. An Addable/List is used");
            Console.WriteLine("    when dealing with an unknown quantity of data that you");
            Console.WriteLine("    will likely have to enumerate/step through everything. The");
            Console.WriteLine("    AddableArray shares the properties of an Indexed/Array in");
            Console.WriteLine("    that it can be relateively quickly sorted along 1 dimensions");
            Console.WriteLine("    for binary search algorithms.");
            Console.WriteLine();

            // AddableArray ---------------------------------------
            IAddable <int> addableArray = new AddableArray <int>(test);

            Console.Write("    [AddableArray] Adding (0-" + (test - 1) + ")...");
            for (int i = 0; i < test; i++)
            {
                addableArray.Add(i);
            }
            Console.WriteLine();

            Console.Write("    [AddableArray] Traversal: ");
            addableArray.Stepper(i => Console.Write(i));
            Console.WriteLine();

            Console.WriteLine("    [AddableArray] Count: " + addableArray.Count);

            addableArray.Clear(); // Clears the addable

            Console.WriteLine();

            // AddableLinked ---------------------------------------
            IAddable <int> addableLinked = new AddableLinked <int>();

            Console.Write("    [AddableLinked] Adding (0-" + (test - 1) + ")...");
            for (int i = 0; i < test; i++)
            {
                addableLinked.Add(i);
            }
            Console.WriteLine();

            Console.Write("    [AddableLinked] Traversal: ");
            addableLinked.Stepper(i => Console.Write(i));
            Console.WriteLine();

            Console.WriteLine("    [AddableLinked] Count: " + addableLinked.Count);

            addableLinked.Clear(); // Clears the addable

            Console.WriteLine();

            #endregion

            #region FirstInLastOut (aka stack)
            {
                Console.WriteLine("  FirstInLastOut---------------------------------");
                Console.WriteLine();
                Console.WriteLine("    An \"FirstInLastOut\" is a Stack that implements");
                Console.WriteLine("    Towel.DataStructures.DataStructure. \"FirstInLastOutArray\" is");
                Console.WriteLine("    the array implementation while \"FirstInLastOutLinked\" is the");
                Console.WriteLine("    the linked-list implementation. A FirstInLastOut/Stack is used");
                Console.WriteLine("    specifically when you need the algorithm provided by the Push");
                Console.WriteLine("    and Pop functions.");
                Console.WriteLine();

                IFirstInLastOut <int> firstInLastOutArray = new FirstInLastOutArray <int>();

                Console.Write("    [FirstInLastOutArray] Pushing (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    firstInLastOutArray.Push(i);
                }
                Console.WriteLine();

                Console.Write("    [FirstInLastOutArray] Traversal: ");
                firstInLastOutArray.Stepper(i => Console.Write(i));
                Console.WriteLine();

                Console.WriteLine("    [FirstInLastOutArray] Pop: " + firstInLastOutArray.Pop());
                Console.WriteLine("    [FirstInLastOutArray] Pop: " + firstInLastOutArray.Pop());
                Console.WriteLine("    [FirstInLastOutArray] Peek: " + firstInLastOutArray.Peek());
                Console.WriteLine("    [FirstInLastOutArray] Pop: " + firstInLastOutArray.Pop());
                Console.WriteLine("    [FirstInLastOutArray] Count: " + firstInLastOutArray.Count);

                firstInLastOutArray.Clear(); // Clears the firstInLastOut

                Console.WriteLine();

                IFirstInLastOut <int> firstInLastOutLinked = new FirstInLastOutLinked <int>();

                Console.Write("    [FirstInLastOutLinked] Pushing (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    firstInLastOutLinked.Push(i);
                }
                Console.WriteLine();

                Console.Write("    [FirstInLastOutLinked] Traversal: ");
                firstInLastOutLinked.Stepper(i => Console.Write(i));
                Console.WriteLine();

                Console.WriteLine("    [FirstInLastOutLinked] Pop: " + firstInLastOutLinked.Pop());
                Console.WriteLine("    [FirstInLastOutLinked] Pop: " + firstInLastOutLinked.Pop());
                Console.WriteLine("    [FirstInLastOutLinked] Peek: " + firstInLastOutLinked.Peek());
                Console.WriteLine("    [FirstInLastOutLinked] Pop: " + firstInLastOutLinked.Pop());
                Console.WriteLine("    [FirstInLastOutLinked] Count: " + firstInLastOutLinked.Count);

                firstInLastOutLinked.Clear(); // Clears the firstInLastOut

                Console.WriteLine();
            }
            #endregion

            #region FirstInFirstOut (aka Queue)
            {
                Console.WriteLine("  FirstInFirstOut---------------------------------");
                Console.WriteLine();
                Console.WriteLine("    An \"FirstInFirstOut\" is a Queue that implements");
                Console.WriteLine("    Towel.DataStructures.DataStructure. \"FirstInFirstOutArray\" is");
                Console.WriteLine("    the array implementation while \"FirstInFirstOutLinked\" is the");
                Console.WriteLine("    the linked-list implementation. A FirstInFirstOut/Stack is used");
                Console.WriteLine("    specifically when you need the algorithm provided by the Queue");
                Console.WriteLine("    and Dequeue functions.");
                Console.WriteLine();

                IFirstInFirstOut <int> firstInFirstOutArray = new FirstInFirstOutArray <int>();

                Console.Write("    [FirstInFirstOutArray] Enqueuing (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    firstInFirstOutArray.Enqueue(i);
                }
                Console.WriteLine();

                Console.Write("    [FirstInFirstOutArray] Traversal: ");
                firstInFirstOutArray.Stepper(i => Console.Write(i));
                Console.WriteLine();

                Console.WriteLine("    [FirstInFirstOutArray] Dequeue: " + firstInFirstOutArray.Dequeue());
                Console.WriteLine("    [FirstInFirstOutArray] Dequeue: " + firstInFirstOutArray.Dequeue());
                Console.WriteLine("    [FirstInFirstOutArray] Peek: " + firstInFirstOutArray.Peek());
                Console.WriteLine("    [FirstInFirstOutArray] Dequeue: " + firstInFirstOutArray.Dequeue());
                Console.WriteLine("    [FirstInFirstOutArray] Count: " + firstInFirstOutArray.Count);

                firstInFirstOutArray.Clear(); // Clears the firstInLastOut

                Console.WriteLine();

                IFirstInFirstOut <int> firstInFirstOutLinked = new FirstInFirstOutLinked <int>();

                Console.Write("    [FirstInFirstOutLinked] Enqueuing (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    firstInFirstOutLinked.Enqueue(i);
                }
                Console.WriteLine();

                Console.Write("    [FirstInFirstOutLinked] Traversal: ");
                firstInFirstOutLinked.Stepper(i => Console.Write(i));
                Console.WriteLine();

                Console.WriteLine("    [FirstInFirstOutLinked] Pop: " + firstInFirstOutLinked.Dequeue());
                Console.WriteLine("    [FirstInFirstOutLinked] Pop: " + firstInFirstOutLinked.Dequeue());
                Console.WriteLine("    [FirstInFirstOutLinked] Peek: " + firstInFirstOutLinked.Peek());
                Console.WriteLine("    [FirstInFirstOutLinked] Pop: " + firstInFirstOutLinked.Dequeue());
                Console.WriteLine("    [FirstInFirstOutLinked] Count: " + firstInFirstOutLinked.Count);

                firstInFirstOutLinked.Clear(); // Clears the firstInLastOut

                Console.WriteLine();
            }
            #endregion

            #region Heap
            {
                Console.WriteLine("  Heap---------------------------------");
                Console.WriteLine();
                Console.WriteLine("    An \"Heap\" is a binary tree that stores items based on priorities.");
                Console.WriteLine("    It implements Towel.DataStructures.DataStructure like the others.");
                Console.WriteLine("    It uses sifting algorithms to move nodes vertically through itself.");
                Console.WriteLine("    It is often the best data structure for standard priority queues.");
                Console.WriteLine("    \"HeapArray\" is an implementation where the tree has been flattened");
                Console.WriteLine("    into an array.");
                Console.WriteLine();

                Console.WriteLine("    Let's say the priority is how close a number is to \"5\".");
                Console.WriteLine("    So \"Dequeue\" will give us the next closest value to \"5\".");
                Comparison Priority(int a, int b)
                {
                    int        _a         = Compute.AbsoluteValue(a - 5);
                    int        _b         = Compute.AbsoluteValue(b - 5);
                    Comparison comparison = Compare.Wrap(_b.CompareTo(_a));

                    return(comparison);
                }

                Console.WriteLine();

                IHeap <int> heapArray = new HeapArray <int>(Priority);

                Console.Write("    [HeapArray] Enqueuing (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    heapArray.Enqueue(i);
                }
                Console.WriteLine();

                Console.WriteLine("    [HeapArray] Dequeue: " + heapArray.Dequeue());
                Console.WriteLine("    [HeapArray] Dequeue: " + heapArray.Dequeue());
                Console.WriteLine("    [HeapArray] Peek: " + heapArray.Peek());
                Console.WriteLine("    [HeapArray] Dequeue: " + heapArray.Dequeue());
                Console.WriteLine("    [HeapArray] Count: " + heapArray.Count);

                heapArray.Clear(); // Clears the heapArray

                Console.WriteLine();
            }
            #endregion

            #region Tree

            //Console.WriteLine("  Tree-----------------------------");

            //Tree<int> tree_Map = new TreeMap<int>(0, Compute.Equal, Hash.Default);

            //for (int i = 1; i < test; i++)
            //{
            //    tree_Map.Add(i, i / Compute.SquareRoot(i));
            //}
            //Console.Write("    Children of 0 (root): ");
            //tree_Map.Children(0, (int i) => { Console.Write(i + " "); });
            //Console.WriteLine();
            //Console.Write("    Children of " + ((int)System.Math.Sqrt(test) - 1) + " (root): ");
            //tree_Map.Children(((int)System.Math.Sqrt(test) - 1), (int i) => { Console.Write(i + " "); });
            //Console.WriteLine();
            //Console.Write("    Traversal: ");
            //tree_Map.Stepper((int i) => { Console.Write(i + " "); });
            //Console.WriteLine();

            //Console.WriteLine();

            #endregion

            #region AVL Tree
            {
                Console.WriteLine("  AvlTree------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    An AVL Tree is a sorted binary tree.");
                Console.WriteLine("    It implements Towel.DataStructures.DataStructure like the others.");
                Console.WriteLine("    It allows for very fast 1D ranged queries/traversals.");
                Console.WriteLine("    It is very similar to an Red Black tree, but uses a different sorting algorithm.");
                Console.WriteLine();

                IAvlTree <int> avlTree = new AvlTreeLinked <int>();

                Console.Write("    Adding (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    avlTree.Add(i);
                }
                Console.WriteLine();

                Console.Write("    Traversal: ");
                avlTree.Stepper(i => Console.Write(i));
                Console.WriteLine();

                //// Note: Because the nodes in AVL Tree linked do not have
                //// a parent pointer, the IEnumerable "foreach" iteration
                //// is extremely slow and should be avoided. It requires
                //// a stack for it's iteration.
                //
                //Console.Write("    Traversal Foreach: ");
                //foreach (int i in avlTree)
                //{
                //    Console.Write(i);
                //}
                //Console.WriteLine();

                int minimum = random.Next(1, test / 2);
                int maximum = random.Next(1, test / 2) + test / 2;
                Console.Write("    Ranged Traversal [" + minimum + "-" + maximum + "]: ");
                avlTree.Stepper(i => Console.Write(i), minimum, maximum);
                Console.WriteLine();

                int removal = random.Next(0, test);
                Console.Write("    Remove(" + removal + "): ");
                avlTree.Remove(removal);
                avlTree.Stepper(i => Console.Write(i));
                Console.WriteLine();

                int contains = random.Next(0, test);
                Console.WriteLine("    Contains(" + contains + "): " + avlTree.Contains(contains));
                Console.WriteLine("    Current Least: " + avlTree.CurrentLeast);
                Console.WriteLine("    Current Greatest: " + avlTree.CurrentGreatest);
                Console.WriteLine("    Count: " + avlTree.Count);

                avlTree.Clear(); // Clears the AVL tree

                Console.WriteLine();
            }
            #endregion

            #region Red-Black Tree
            {
                Console.WriteLine("  Red-Black Tree------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    An Red-Black Tree is a sorted binary tree.");
                Console.WriteLine("    It implements Towel.DataStructures.DataStructure like the others.");
                Console.WriteLine("    It allows for very fast 1D ranged queries/traversals.");
                Console.WriteLine("    It is very similar to an AVL tree, but uses a different sorting algorithm.");
                Console.WriteLine();

                IRedBlackTree <int> redBlackTree = new RedBlackTreeLinked <int>();

                Console.Write("    Adding (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    redBlackTree.Add(i);
                }
                Console.WriteLine();

                Console.Write("    Traversal: ");
                redBlackTree.Stepper(i => Console.Write(i));
                Console.WriteLine();

                int minimum = random.Next(1, test / 2);
                int maximum = random.Next(1, test / 2) + test / 2;
                Console.Write("    Ranged Traversal [" + minimum + "-" + maximum + "]: ");
                redBlackTree.Stepper(i => Console.Write(i), minimum, maximum);
                Console.WriteLine();

                int removal = random.Next(0, test);
                Console.Write("    Remove(" + removal + "): ");
                redBlackTree.Remove(removal);
                redBlackTree.Stepper(i => Console.Write(i));
                Console.WriteLine();

                int contains = random.Next(0, test);
                Console.WriteLine("    Contains(" + contains + "): " + redBlackTree.Contains(contains));
                Console.WriteLine("    Current Least: " + redBlackTree.CurrentLeast);
                Console.WriteLine("    Current Greatest: " + redBlackTree.CurrentGreatest);
                Console.WriteLine("    Count: " + redBlackTree.Count);

                redBlackTree.Clear(); // Clears the Red Black tree

                Console.WriteLine();
            }
            #endregion

            #region BTree
            {
                Console.WriteLine("  B Tree------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    A B Tree is a sorted binary tree that allows multiple values to");
                Console.WriteLine("    be stored per node. This makes it sort of a hybrid between a");
                Console.WriteLine("    binary tree and an array. Because multiple values are stored ");
                Console.WriteLine("    per node, it means less nodes must be traversed to completely");
                Console.WriteLine("    traverse the values in the B tree.");
                Console.WriteLine();

                Console.WriteLine("    The generic B Tree in Towel is still in development.");

                Console.WriteLine();
            }
            #endregion

            #region Set
            {
                Console.WriteLine("  Set------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    A Set is like an Addable/List, but it does not allow duplicates. Sets are");
                Console.WriteLine("    usually implemented using hash codes. Implementations with hash codes");
                Console.WriteLine("    usually have very fast \"Contains\" checks to see if a value has already");
                Console.WriteLine("    been added to the set.");
                Console.WriteLine();

                ISet <int> setHashLinked = new SetHashLinked <int>();

                Console.Write("    Adding (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    setHashLinked.Add(i);
                }
                Console.WriteLine();

                Console.Write("    Traversal: ");
                setHashLinked.Stepper(i => Console.Write(i));
                Console.WriteLine();

                int a = random.Next(0, test);
                setHashLinked.Remove(a);
                Console.Write("    Remove(" + a + "): ");
                setHashLinked.Stepper(i => Console.Write(i));
                Console.WriteLine();

                int b = random.Next(0, test);
                Console.WriteLine("    Contains(" + b + "): " + setHashLinked.Contains(b));
                Console.WriteLine("    Count: " + setHashLinked.Count);

                Console.WriteLine();
            }
            #endregion

            #region Map (aka Dictionary)
            {
                Console.WriteLine("  Map------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    A Map (aka Dictionary) is similar to a Set, but it stores two values (a ");
                Console.WriteLine("    key and a value). Maps do not allow duplicate keys much like Sets don't");
                Console.WriteLine("    allow duplicate values. When provided with the key, the Map uses that key");
                Console.WriteLine("    to look up the value that it is associated with. Thus, it allows you to ");
                Console.WriteLine("    \"map\" one object to another. As with Sets, Maps are usually implemented");
                Console.WriteLine("    using hash codes.");
                Console.WriteLine();

                // Note: the first generic is the value, the second is the key
                IMap <string, int> mapHashLinked = new MapHashLinked <string, int>();

                Console.WriteLine("    Let's map each int to its word representation (ex 1 -> One).");

                Console.Write("    Adding (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    mapHashLinked.Add(i, ((decimal)i).ToEnglishWords());
                }
                Console.WriteLine();

                Console.WriteLine("    Traversal: ");
                mapHashLinked.Keys(i => Console.WriteLine("      " + i + "->" + mapHashLinked[i]));
                Console.WriteLine();

                int a = random.Next(0, test);
                mapHashLinked.Remove(a);
                Console.Write("    Remove(" + a + "): ");
                mapHashLinked.Keys(i => Console.Write(i));
                Console.WriteLine();

                int b = random.Next(0, test);
                Console.WriteLine("    Contains(" + b + "): " + mapHashLinked.Contains(b));
                Console.WriteLine("    Count: " + mapHashLinked.Count);

                Console.WriteLine();
            }
            #endregion

            #region OmnitreePoints
            {
                Console.WriteLine("  OmnitreePoints--------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    An Omnitree is an ND SPT that allows for");
                Console.WriteLine("    multidimensional sorting. Any time you need to look");
                Console.WriteLine("    items up based on multiple fields/properties, then");
                Console.WriteLine("    you might want to use an Omnitree. If you need to");
                Console.WriteLine("    perform ranged queries on multiple dimensions, then");
                Console.WriteLine("    the Omnitree is the data structure for you.");
                Console.WriteLine();
                Console.WriteLine("    The \"OmnitreePoints\" stores individual points (vectors),");
                Console.WriteLine("    and the \"OmnitreeBounds\" stores bounded objects (spaces).");
                Console.WriteLine();

                IOmnitreePoints <int, double, string, decimal> omnitree =
                    new OmnitreePointsLinked <int, double, string, decimal>(
                        // This is a location delegate. (how to locate the item along each dimension)
                        (int index, out double a, out string b, out decimal c) =>
                {
                    a = index;
                    b = index.ToString();
                    c = index;
                });

                Console.Write("    Adding (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    omnitree.Add(i);
                }
                Console.WriteLine();

                Console.Write("    Traversal: ");
                omnitree.Stepper(i => Console.Write(i));
                Console.WriteLine();

                int    minimumXZ = random.Next(1, test / 2);
                int    maximumXZ = random.Next(1, test / 2) + test / 2;
                string minimumY  = minimumXZ.ToString();
                string maximumY  = maximumXZ.ToString();
                Console.Write("    Spacial Traversal [" +
                              "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" +
                              "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: ");
                omnitree.Stepper(i => Console.Write(i),
                                 minimumXZ, maximumXZ,
                                 minimumY, maximumY,
                                 minimumXZ, maximumXZ);
                Console.WriteLine();

                // Note: this "look up" is just a very narrow spacial query that (since we know the data)
                // wil only give us one result.
                int    lookUp         = random.Next(0, test);
                string lookUpToString = lookUp.ToString();
                Console.Write("    Look Up (" + lookUp + ", \"" + lookUpToString + "\", " + lookUp + "): ");
                omnitree.Stepper(i => Console.Write(i),
                                 lookUp, lookUp,
                                 lookUp.ToString(), lookUp.ToString(),
                                 lookUp, lookUp);
                Console.WriteLine();

                // Ignoring dimensions on traversals example.
                // If you want to ignore a column on a traversal, you can do so like this:
                omnitree.Stepper(i => { /*Do Nothing*/ },
                                 lookUp, lookUp,
                                 Omnitree.Bound <string> .None, Omnitree.Bound <string> .None,
                                 Omnitree.Bound <decimal> .None, Omnitree.Bound <decimal> .None);

                Console.Write("    Counting Items In a Space [" +
                              "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" +
                              "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: ");
                omnitree.CountSubSpace(
                    minimumXZ, maximumXZ,
                    minimumY, maximumY,
                    minimumXZ, maximumXZ);
                Console.WriteLine();

                int    removalMinimum  = random.Next(1, test / 2);
                int    removalMaximum  = random.Next(1, test / 2) + test / 2;
                string removalMinimumY = removalMinimum.ToString();
                string removalMaximumY = removalMaximum.ToString();
                Console.Write("    Remove (" + removalMinimum + "-" + removalMaximum + "): ");
                omnitree.Remove(
                    removalMinimum, removalMaximum,
                    removalMinimumY, removalMaximumY,
                    removalMinimum, removalMaximum);
                omnitree.Stepper(i => Console.Write(i));
                Console.WriteLine();

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

                omnitree.Clear(); // Clears the Omnitree

                Console.WriteLine();
            }
            #endregion

            #region OmnitreeBounds
            {
                Console.WriteLine("  OmnitreeBounds--------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    An Omnitree is an ND SPT that allows for");
                Console.WriteLine("    multidimensional sorting. Any time you need to look");
                Console.WriteLine("    items up based on multiple fields/properties, then");
                Console.WriteLine("    you might want to use an Omnitree. If you need to");
                Console.WriteLine("    perform ranged queries on multiple dimensions, then");
                Console.WriteLine("    the Omnitree is the data structure for you.");
                Console.WriteLine();
                Console.WriteLine("    The \"OmnitreePoints\" stores individual points (vectors),");
                Console.WriteLine("    and the \"OmnitreeBounds\" stores bounded objects (spaces).");
                Console.WriteLine();

                IOmnitreeBounds <int, double, string, decimal> omnitree =
                    new OmnitreeBoundsLinked <int, double, string, decimal>(
                        // This is a location delegate. (how to locate the item along each dimension)
                        (int index,
                         out double min1, out double max1,
                         out string min2, out string max2,
                         out decimal min3, out decimal max3) =>
                {
                    string indexToString = index.ToString();

                    min1 = index; max1 = index;
                    min2 = indexToString; max2 = indexToString;
                    min3 = index; max3 = index;
                });

                Console.Write("    Adding (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    omnitree.Add(i);
                }
                Console.WriteLine();

                Console.Write("    Traversal: ");
                omnitree.Stepper(i => Console.Write(i));
                Console.WriteLine();

                int    minimumXZ = random.Next(1, test / 2);
                int    maximumXZ = random.Next(1, test / 2) + test / 2;
                string minimumY  = minimumXZ.ToString();
                string maximumY  = maximumXZ.ToString();
                Console.Write("    Spacial Traversal [" +
                              "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" +
                              "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: ");
                omnitree.StepperOverlapped(i => Console.Write(i),
                                           minimumXZ, maximumXZ,
                                           minimumY, maximumY,
                                           minimumXZ, maximumXZ);
                Console.WriteLine();

                // Note: this "look up" is just a very narrow spacial query that (since we know the data)
                // wil only give us one result.
                int    lookUpXZ = random.Next(0, test);
                string lookUpY  = lookUpXZ.ToString();
                Console.Write("    Look Up (" + lookUpXZ + ", \"" + lookUpY + "\", " + lookUpXZ + "): ");
                omnitree.StepperOverlapped(i => Console.Write(i),
                                           lookUpXZ, lookUpXZ,
                                           lookUpY, lookUpY,
                                           lookUpXZ, lookUpXZ);
                Console.WriteLine();

                // Ignoring dimensions on traversals example.
                // If you want to ignore a dimension on a traversal, you can do so like this:
                omnitree.StepperOverlapped(i => { /*Do Nothing*/ },
                                           lookUpXZ, lookUpXZ,
                                           // The "None" means there is no bound, so all values are valid
                                           Omnitree.Bound <string> .None, Omnitree.Bound <string> .None,
                                           Omnitree.Bound <decimal> .None, Omnitree.Bound <decimal> .None);

                Console.Write("    Counting Items In a Space [" +
                              "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" +
                              "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: " +
                              omnitree.CountSubSpaceOverlapped(
                                  minimumXZ, maximumXZ,
                                  minimumY, maximumY,
                                  minimumXZ, maximumXZ));
                Console.WriteLine();

                int    removalMinimumXZ = random.Next(1, test / 2);
                int    removalMaximumXZ = random.Next(1, test / 2) + test / 2;
                string removalMinimumY  = removalMinimumXZ.ToString();
                string removalMaximumY  = removalMaximumXZ.ToString();
                Console.Write("    Remove (" + removalMinimumXZ + "-" + removalMaximumXZ + "): ");
                omnitree.RemoveOverlapped(
                    removalMinimumXZ, removalMaximumXZ,
                    removalMinimumY, removalMaximumY,
                    removalMinimumXZ, removalMaximumXZ);
                omnitree.Stepper(i => Console.Write(i));
                Console.WriteLine();

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

                omnitree.Clear(); // Clears the Omnitree

                Console.WriteLine();
            }
            #endregion

            #region KD Tree
            {
                Console.WriteLine("  KD Tree------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    A KD Tree binary tree that stores points sorted along along an");
                Console.WriteLine("    arbitrary number of dimensions. So it performs multidimensional");
                Console.WriteLine("    sorting similar to the Omnitree (Quadtree/Octree) in Towel, but");
                Console.WriteLine("    it uses a completely different algorithm and format.");
                Console.WriteLine();

                Console.WriteLine("    The generic KD Tree in Towel is still in development.");

                Console.WriteLine();
            }
            #endregion

            #region Graph
            {
                Console.WriteLine("  Graph------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    A Graph is a data structure of nodes and edges. Nodes are values");
                Console.WriteLine("    and edges are connections between those values. Graphs are often");
                Console.WriteLine("    used to model real world data such as maps, and are often used in");
                Console.WriteLine("    path finding algoritms. See the \"Algorithms\" example for path");
                Console.WriteLine("    finding examples. This is just an example of how to make a graph.");
                Console.WriteLine("    A \"GraphSetOmnitree\" is an implementation where nodes are stored.");
                Console.WriteLine("    in a Set and edges are stored in an Omnitree (aka Quadtree).");
                Console.WriteLine();

                IGraph <int> graphSetOmnitree = new GraphSetOmnitree <int>();

                Console.WriteLine("    Adding Nodes (0-" + (test - 1) + ")...");
                for (int i = 0; i < test; i++)
                {
                    graphSetOmnitree.Add(i);
                }

                int edgesPerNode = 3;
                Console.WriteLine("    Adding Random Edges (0-3 per node)...");
                for (int i = 0; i < test; i++)
                {
                    // lets use a heap to randomize the edges using random priorities
                    IHeap <(int, int)> heap = new HeapArray <(int, int)>((x, y) => Compare.Wrap(x.Item2.CompareTo(y.Item2)));
                    for (int j = 0; j < test; j++)
                    {
                        if (j != i)
                        {
                            heap.Enqueue((j, random.Next()));
                        }
                    }

                    // dequeue some random edges from the heap and add them to the graph
                    int randomEdgeCount = random.Next(edgesPerNode + 1);
                    for (int j = 0; j < randomEdgeCount; j++)
                    {
                        graphSetOmnitree.Add(i, heap.Dequeue().Item1);
                    }
                }

                Console.Write("    Nodes (Traversal): ");
                graphSetOmnitree.Stepper(i => Console.Write(i));
                Console.WriteLine();

                Console.WriteLine("    Edges (Traversal): ");
                graphSetOmnitree.Stepper((from, to) => Console.WriteLine("      " + from + "->" + to));
                Console.WriteLine();

                int a = random.Next(0, test);
                Console.Write("    Neighbors (" + a + "):");
                graphSetOmnitree.Neighbors(a, i => Console.Write(" " + i));
                Console.WriteLine();

                int b = random.Next(0, test / 2);
                int c = random.Next(test / 2, test);
                Console.WriteLine("    Are Adjacent (" + b + ", " + c + "): " + graphSetOmnitree.Adjacent(b, c));
                Console.WriteLine("    Node Count: " + graphSetOmnitree.NodeCount);
                Console.WriteLine("    Edge Count: " + graphSetOmnitree.EdgeCount);

                graphSetOmnitree.Clear(); // Clears the graph

                Console.WriteLine();
            }
            #endregion

            #region Trie
            {
                Console.WriteLine("  Trie------------------------------------------------");
                Console.WriteLine();
                Console.WriteLine("    A Trie is a tree where portions of the data are stored in each node");
                Console.WriteLine("    such that when you traverse the tree to a leaf, you have read the contents");
                Console.WriteLine("    of that leaf along the way. Because of this, a Trie allows for its values");
                Console.WriteLine("    to share data, which is a form of compression. So a Trie may be used to save");
                Console.WriteLine("    memory. A trie may also be a very useful tool in pattern matching, because it");
                Console.WriteLine("    it allows for culling based are portions of the data.");
                Console.WriteLine();

                Console.WriteLine("    The generic Trie in Towel is still in development.");

                Console.WriteLine();
            }
            #endregion

            Console.WriteLine("============================================");
            Console.WriteLine("Examples Complete...");
            Console.ReadLine();
        }
Exemplo n.º 25
0
        static void Main(string[] args)
        {
            Random random = new Random();
            int    test   = 10;

            Console.WriteLine("You are runnning the Data Structures example.");
            Console.WriteLine("======================================================");
            Console.WriteLine();

            #region Link

            Console.WriteLine("  Testing Link-------------------------------");
            Console.WriteLine("   Size: 6");
            Link link = new Link <int, int, int, int, int, int>(0, 1, 2, 3, 4, 5);
            Console.Write("    Traversal: ");
            link.Stepper((dynamic current) => { Console.Write(current); });
            Console.WriteLine();
            // Saving to a file
            //string linklink_file = "link." + ToExtension(link.GetType());
            //Console.WriteLine("    File: \"" + linklink_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(linklink_file, link));
            //Link<int, int, int, int, int, int> deserialized_linklink;
            //Console.WriteLine("    Deserialized: " + Deserialize(linklink_file, out deserialized_linklink));
            Console.WriteLine();

            #endregion

            #region Array

            Console.WriteLine("  Testing Array_Array<int>-------------------");
            Indexed <int> array = new IndexedArray <int>(test);
            for (int i = 0; i < test; i++)
            {
                array[i] = i;
            }
            Console.Write("    Traversal: ");
            array.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            // Saving to a file
            //string arrayarray_file = "array." + ToExtension(array.GetType());
            //Console.WriteLine("    File: \"" + arrayarray_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(arrayarray_file, array));
            //ArrayArray<int> deserialized_arrayarray;
            //Console.WriteLine("    Deserialized: " + Deserialize(arrayarray_file, out deserialized_arrayarray));
            Console.WriteLine();

            #endregion

            #region List

            Console.WriteLine("  Testing List_Array<int>--------------------");
            Addable <int> list_array = new AddableArray <int>(test);
            for (int i = 0; i < test; i++)
            {
                list_array.Add(i);
            }
            Console.Write("    Traversal: ");
            list_array.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            //string list_array_serialization = (list_array as ListArray<int>).Serialize(x => x.ToString());
            //using (StreamWriter writer = new StreamWriter("ListArray.ListArray"))
            //{
            //    writer.WriteLine(list_array_serialization);
            //}
            //using (StreamReader reader = new StreamReader("ListArray.ListArray"))
            //{
            //    list_array = ListArray<int>.Deserialize(reader.ReadToEnd(), x => Int16.Parse(x.Trim()));
            //}
            //Console.Write("    Serialization/Deserialization is possible.");
            list_array.Add(11);
            list_array.Remove(7);
            Console.WriteLine();
            Console.WriteLine();


            //ListArray<ListArray<int>> list_array2 = new ListArray<ListArray<int>>(test);
            //for (int i = 0; i < test; i++)
            //{
            //    ListArray<int> nested_list = new ListArray<int>();
            //    for (int j = 0; j < test; j++)
            //    {
            //        nested_list.Add(j);
            //    }
            //    list_array2.Add(nested_list);
            //}
            //string list_array2_serialization = list_array2.Serialize(x => x.Serialize(y => y.ToString()));
            //using (StreamWriter writer = new StreamWriter("ListArray2.ListArray"))
            //{
            //    writer.WriteLine(list_array2_serialization);
            //}
            //using (StreamReader reader = new StreamReader("ListArray2.ListArray"))
            //{
            //    list_array2 = ListArray<ListArray<int>>.Deserialize(reader.ReadToEnd(), x => ListArray<int>.Deserialize(x, y => Int16.Parse(y.Trim())));
            //}

            Console.WriteLine("  Testing List_Linked<int>-------------------");
            Addable <int> list_linked = new AddableLinked <int>();
            for (int i = 0; i < test; i++)
            {
                list_linked.Add(i);
            }
            Console.Write("    Traversal: ");
            list_linked.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();



            // Saving to a file
            //string listlinked_file = "list_linked." + ToExtension(list_linked.GetType());
            //Console.WriteLine("    File: \"" + listlinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(listlinked_file, list_linked));
            //ListLinked<int> deserialized_listlinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(listlinked_file, out deserialized_listlinked));
            Console.WriteLine();

            #endregion

            #region Stack

            Console.WriteLine("  Testing Stack_Linked<int>------------------");
            FirstInLastOut <int> stack_linked = new FirstInLastOutLinked <int>();
            for (int i = 0; i < test; i++)
            {
                stack_linked.Push(i);
            }
            Console.Write("    Traversal: ");
            stack_linked.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            // Saving to a file
            //string stacklinked_file = "stack_linked." + ToExtension(stack_linked.GetType());
            //Console.WriteLine("    File: \"" + stacklinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(stacklinked_file, stack_linked));
            //StackLinked<int> deserialized_stacklinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(stacklinked_file, out deserialized_stacklinked));
            Console.WriteLine();

            #endregion

            #region Queue

            Console.WriteLine("  Testing Queue_Linked<int>------------------");
            FirstInFirstOut <int> queue_linked = new FirstInFirstOutLinked <int>();
            for (int i = 0; i < test; i++)
            {
                queue_linked.Enqueue(i);
            }
            Console.Write("    Traversal: ");
            queue_linked.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            // Saving to a file
            //string queuelinked_file = "queue_linked." + ToExtension(queue_linked.GetType());
            //Console.WriteLine("    File: \"" + queuelinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(queuelinked_file, queue_linked));
            //QueueLinked<int> deserialized_queuelinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(queuelinked_file, out deserialized_queuelinked));
            Console.WriteLine();

            #endregion

            #region Heap

            Console.WriteLine("  Testing Heap_Array<int>--------------------");
            Heap <int> heap_array = new HeapArray <int>(Compute.Compare);
            for (int i = 0; i < test; i++)
            {
                heap_array.Enqueue(i);
            }
            Console.Write("    Delegate: ");
            heap_array.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            // Saving to a file
            //string heaplinked_file = "heap_array." + ToExtension(heap_array.GetType());
            //Console.WriteLine("    File: \"" + heaplinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(heaplinked_file, heap_array));
            //HeapArray<int> deserialized_heaplinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(heaplinked_file, out deserialized_heaplinked));
            Console.WriteLine();

            #endregion

            #region Tree

            Console.WriteLine("  Testing Tree_Map<int>----------------------");
            Tree <int> tree_Map = new TreeMap <int>(0, Compute.Equal, Hash.Default);
            for (int i = 1; i < test; i++)
            {
                tree_Map.Add(i, i / (int)System.Math.Sqrt(test));
            }
            Console.Write("    Children of 0 (root): ");
            tree_Map.Children(0, (int i) => { Console.Write(i + " "); });
            Console.WriteLine();
            Console.Write("    Children of " + ((int)System.Math.Sqrt(test) - 1) + " (root): ");
            tree_Map.Children(((int)System.Math.Sqrt(test) - 1), (int i) => { Console.Write(i + " "); });
            Console.WriteLine();
            Console.Write("    Traversal: ");
            tree_Map.Stepper((int i) => { Console.Write(i + " "); });
            Console.WriteLine();
            // Saving to a file
            //string treelinked_file = "tree_Map." + ToExtension(tree_Map.GetType());
            //Console.WriteLine("    File: \"" + treelinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(treelinked_file, tree_Map));
            //TreeMap<int> deserialized_treelinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(treelinked_file, out deserialized_treelinked));
            Console.WriteLine();

            #endregion

            #region AVL Tree

            //Console.WriteLine("  Testing AvlTree_Linked<int>----------------");
            //// Construction
            //AvlTree<int> avlTree_linked = new AvlTree_Linked<int>(Logic.compare);
            //// Adding Items
            //Console.Write("    Adding (0-" + test + ")...");
            //for (int i = 0; i < test; i++)
            //	avlTree_linked.Add(i);
            //Console.WriteLine();
            //// Iteration
            //Console.Write("    Traversal: ");
            //avlTree_linked.Stepper((int current) => { Console.Write(current); });
            //Console.WriteLine();
            //// Removal
            //int avl_tree_linked_removal = random.Next(0, test);
            //avlTree_linked.Remove(avl_tree_linked_removal);
            //Console.Write("    Remove(" + avl_tree_linked_removal + "): ");
            //avlTree_linked.Stepper((int current) => { Console.Write(current); });
            //Console.WriteLine();
            //// Look Up Items
            //int avl_tree_linked_lookup = random.Next(0, test);
            //while (avl_tree_linked_lookup == avl_tree_linked_removal)
            //	avl_tree_linked_lookup = random.Next(0, test);
            //Console.WriteLine("    Look Up (" + avl_tree_linked_lookup + "): " + avlTree_linked.TryGet(avl_tree_linked_lookup, Logic.compare, out temp));
            //Console.WriteLine("    Look Up (" + avl_tree_linked_removal + "): " + avlTree_linked.TryGet(avl_tree_linked_removal, Logic.compare, out temp));
            //avlTree_linked.Get(avl_tree_linked_lookup, Logic.compare);
            //// Current Min-Max Values
            //Console.WriteLine("    Least: " + avlTree_linked.CurrentLeast + " Greatest: " + avlTree_linked.CurrentGreatest);
            //// Saving to a file
            //string avltreelinked_file = "avlTree_linked." + ToExtension(avlTree_linked.GetType());
            //Console.WriteLine("    File: \"" + avltreelinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(avltreelinked_file, avlTree_linked));
            //AvlTree_Linked<int> deserialized_avltreelinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(avltreelinked_file, out deserialized_avltreelinked));
            //Console.WriteLine();

            #endregion

            #region Red-Black Tree

            Console.WriteLine("  Testing RedBlack_Linked<int>---------------");
            RedBlackTree <int> redBlackTree_linked = new RedBlackTreeLinked <int>(Compute.Compare);
            for (int i = 0; i < test; i++)
            {
                redBlackTree_linked.Add(i);
            }
            Console.Write("    Traversal: ");
            redBlackTree_linked.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            // Saving to a file
            //string redblacktreelinked_file = "redBlackTree_linked." + ToExtension(redBlackTree_linked.GetType());
            //Console.WriteLine("    File: \"" + redblacktreelinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(redblacktreelinked_file, redBlackTree_linked));
            //RedBlackTreeLinked<int> deserialized_redblacktreelinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(redblacktreelinked_file, out deserialized_redblacktreelinked));
            Console.WriteLine();

            #endregion

            #region BTree

            //Console.WriteLine("  Testing BTree_LinkedArray<int>-------------");
            //BTree<int> btree_linked = new BTree_LinkedArray<int>(Logic.compare, 3);
            //for (int i = 0; i < test; i++)
            //	btree_linked.Add(i);
            //Console.Write("    Delegate: ");
            //btree_linked.Stepper((int current) => { Console.Write(current); });
            //Console.WriteLine();
            //Console.Write("    IEnumerator: ");
            //foreach (int current in btree_linked)
            //	Console.Write(current);
            //Console.WriteLine();
            //Console.WriteLine("  Press Enter to continue...");
            //string maplinked_file = "maplinked.quad";
            //Console.WriteLine("    File: \"" + maplinked_file + "\"");
            //Console.WriteLine("    Serialized: " + Serialize(maplinked_file, hashTable_linked));
            //Omnitree_LinkedLinkedLists<int, double> deserialized_maplinked;
            //Console.WriteLine("    Deserialized: " + Deserialize(maplinked_file, out deserialized_maplinked));
            //Console.ReadLine();
            //Console.WriteLine();

            #endregion

            #region Set

            Console.WriteLine("  Testing Set_Hash<int>----------------------");
            Set <int> set_linked = new SetHashList <int>(Compute.Equal, Hash.Default);
            for (int i = 0; i < test; i++)
            {
                set_linked.Add(i);
            }
            // Traversal
            Console.Write("    Traversal: ");
            set_linked.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            Console.Write("    Table Size: " + (set_linked as SetHashList <int>).TableSize);
            Console.WriteLine();
            Console.WriteLine();

            #endregion

            #region Map

            Console.WriteLine("  Testing MapHashList<int, int>--------------");
            Map <int, int> map_sethash = new MapHashLinked <int, int>(Compute.Equal, Hash.Default);
            for (int i = 0; i < test; i++)
            {
                map_sethash.Add(i, i);
            }
            Console.Write("    Look Ups: ");
            for (int i = 0; i < test; i++)
            {
                Console.Write(map_sethash[i]);
            }
            Console.WriteLine();
            // Traversal
            Console.Write("    Traversal: ");
            map_sethash.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            Console.Write("    Table Size: " + (map_sethash as MapHashLinked <int, int>).TableSize);
            Console.WriteLine();
            Console.WriteLine();

            #endregion

            #region OmnitreePoints
            {
                Console.WriteLine("  Testing OmnitreeLinkedLinked<int, double>-------");
                // Construction
                OmnitreePoints <int, double, double, double> omnitree_linked = new OmnitreePointsLinked <int, double, double, double>(
                    (int index, out double a, out double b, out double c) => { a = index; b = index; c = index; }); // axis average function
                                                                                                                    // Properties
                Console.WriteLine("      Dimensions: " + omnitree_linked.Dimensions);
                Console.WriteLine("      Count: " + omnitree_linked.Count);
                // Addition
                Console.Write("    Adding 0-" + test + ": ");
                for (int i = 0; i < test; i++)
                {
                    omnitree_linked.Add(i);
                }
                omnitree_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                Console.WriteLine("      Count: " + omnitree_linked.Count);
                // Traversal
                Console.Write("    Traversal [ALL]: ");
                omnitree_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                // Look Up 1
                Console.Write("    Traversal [(" + (test / 2) + ", " + (test / 2) + ", " + (test / 2) + ")->(" + test + ", " + test + ", " + test + ")]: ");
                omnitree_linked.Stepper((int current) => { Console.Write(current); },
                                        test / 2, test,
                                        test / 2, test,
                                        test / 2, test);
                Console.WriteLine();
                // Look Up 2
                Console.Write("    Look Up [" + (test / 3) + ", " + (test / 3) + ", " + (test / 3) + "]: ");
                omnitree_linked[(test / 3), (test / 3), (test / 3)]((int current) => { Console.Write(current); });
                Console.WriteLine();
                // Removal
                Console.Write("    Remove 0-" + test / 3 + ": ");
                omnitree_linked.Remove(
                    0, test / 3,
                    0, test / 3,
                    0, test / 3);
                omnitree_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                Console.WriteLine("      Count: " + omnitree_linked.Count);
                // Clear
                Console.Write("    Clear: ");
                omnitree_linked.Clear();
                omnitree_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                Console.WriteLine("      Count: " + omnitree_linked.Count);
                // Saving to a file
                //string omnitreelinked_file = "omnitree_linkedlinkedlists." + ToExtension(omnitree_linked.GetType());
                //Console.WriteLine("    File: \"" + omnitreelinked_file + "\"");
                //Console.WriteLine("    Serialized: " + Serialize(omnitreelinked_file, omnitree_linked));
                //OmnitreeLinkedLinkedLists<int, double> deserialized_omnitreeLinked;
                //Console.WriteLine("    Deserialized: " + Deserialize(omnitreelinked_file, out deserialized_omnitreeLinked));
                Console.WriteLine();

                //Console.WriteLine("  Testing Omnitree_LinkedArrayLists<int, double>--------");
                //// Construction
                //Omnitree<int, double> omnitree_array = new OmnitreeLinkedArray<int, double>(
                //	new double[] { -test - 1, -test - 1, -test - 1 }, // minimum dimensions of the omnitree
                //	new double[] { test + 1, test + 1, test + 1 }, // maximum dimensions of the omnitree
                //	(int index) => { return Accessor.Get(new double[] { index, index, index }); }, // "N-D" location function
                //	Compute<double>.Compare, // comparison function
                //	(double a, double b) => { return (a + b) / 2; }); // average function
                //// Properties
                //Console.WriteLine("      Origin: [" + omnitree_array.Origin(0) + ", " + omnitree_array.Origin(1) + ", " + omnitree_array.Origin(2) + "]");
                //Console.WriteLine("      Minimum: [" + omnitree_array.Min(0) + ", " + omnitree_array.Min(1) + ", " + omnitree_array.Min(2) + "]");
                //Console.WriteLine("      Maximum: [" + omnitree_array.Max(0) + ", " + omnitree_array.Max(1) + ", " + omnitree_array.Max(2) + "]");
                //Console.WriteLine("      Dimensions: " + omnitree_array.Dimensions);
                //Console.WriteLine("      Count: " + omnitree_array.Count);
                //// Addition
                //Console.Write("    Adding 0-" + test + ": ");
                //for (int i = 0; i < test; i++)
                //	omnitree_array.Add(i);
                //omnitree_array.Stepper((int current) => { Console.Write(current); });
                //Console.WriteLine();
                //Console.WriteLine("      Count: " + omnitree_array.Count);
                //// Traversal
                //Console.Write("    Traversal [ALL]: ");
                //			omnitree_array.Stepper((int current) => { Console.Write(current); });
                //Console.WriteLine();
                //// Look Up
                //Console.Write("    Traversal [" + (test / 2) + "-" + test + "]: ");
                //			omnitree_array.Stepper((int current) => { Console.Write(current); },
                //	new double[] { test / 2, test / 2, test / 2 },
                //	new double[] { test, test, test });
                //Console.WriteLine();
                //// Removal
                //Console.Write("    Remove 0-" + test / 3 + ": ");
                //omnitree_array.Remove(
                //	new double[] { 0, 0, 0 },
                //	new double[] { test / 3, test / 3, test / 3 });
                //omnitree_array.Stepper((int current) => { Console.Write(current); });
                //Console.WriteLine();
                //Console.WriteLine("      Count: " + omnitree_array.Count);
                //// Clear
                //Console.Write("    Clear: ");
                //omnitree_array.Clear();
                //			omnitree_array.Stepper((int current) => { Console.Write(current); });
                //Console.WriteLine();
                //Console.WriteLine("      Count: " + omnitree_array.Count);
                //// Saving to a file
                ////string omnitreearray_file = "omnitree_linkedarraylists." + ToExtension(omnitree_array.GetType());
                ////Console.WriteLine("    File: \"" + omnitreearray_file + "\"");
                ////Console.WriteLine("    Serialized: " + Serialize(omnitreearray_file, omnitree_array));
                ////OmnitreeLinkedLinkedLists<int, double> deserialized_omnitreearray;
                ////Console.WriteLine("    Deserialized: " + Deserialize(omnitreearray_file, out deserialized_omnitreearray));
                //Console.WriteLine();
            }
            #endregion

            #region OmnitreeBounds
            {
                Console.WriteLine("  Testing OmnitreeBoundsLinked<int, double>-------");
                // Construction
                OmnitreeBounds <int, double, double, double> omnitreeBounds_linked = new OmnitreeBoundsLinked <int, double, double, double>(
                    (int index,
                     out double min1, out double max1,
                     out double min2, out double max2,
                     out double min3, out double max3) =>
                {
                    min1 = index; max1 = index;
                    min2 = index; max2 = index;
                    min3 = index; max3 = index;
                });

                // Properties
                Console.WriteLine("      Dimensions: " + omnitreeBounds_linked.Dimensions);
                Console.WriteLine("      Count: " + omnitreeBounds_linked.Count);

                // Addition
                Console.Write("    Adding 0-" + test + ": ");
                for (int i = 0; i < test; i++)
                {
                    omnitreeBounds_linked.Add(i);
                }
                omnitreeBounds_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                Console.WriteLine("      Count: " + omnitreeBounds_linked.Count);
                // Traversal
                Console.Write("    Traversal [ALL]: ");
                omnitreeBounds_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                // Look Up 1
                //Console.Write("    Traversal [(" + (test / 2) + ", " + (test / 2) + ", " + (test / 2) + ")->(" + test + ", " + test + ", " + test + ")]: ");
                //omnitreeBounds_linked.Stepper((int current) => { Console.Write(current); },
                //    test / 2, test,
                //    test / 2, test,
                //    test / 2, test);
                //Console.WriteLine();
                // Removal
                Console.Write("    Remove 0-" + test / 3 + ": ");
                omnitreeBounds_linked.RemoveOverlapped(
                    0, test / 3,
                    0, test / 3,
                    0, test / 3);
                omnitreeBounds_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                Console.WriteLine("      Count: " + omnitreeBounds_linked.Count);
                // Clear
                Console.Write("    Clear: ");
                omnitreeBounds_linked.Clear();
                omnitreeBounds_linked.Stepper((int current) => { Console.Write(current); });
                Console.WriteLine();
                Console.WriteLine("      Count: " + omnitreeBounds_linked.Count);
                Console.WriteLine();
            }
            #endregion

            #region KD Tree

            ////List<KdTreeNode<float, string>> testNodes = new List_Linked<KdTreeNode<float, string>>();
            //KdTree_Linked<string, float> tree = new KdTree_Linked<string, float>(
            //	2,
            //	Logic.compare,
            //	float.MinValue,
            //	float.MaxValue,
            //	0,
            //	Arithmetic.Add,
            //	Arithmetic.Subtract,
            //	Arithmetic.Multiply);

            //List<KdTree_Linked<string, float>.Node> testNodes =
            //	new List_Linked<KdTree_Linked<string, float>.Node>
            //{
            //	new KdTree_Linked<string, float>.Node(new float[] { 5, 5 }, "Root"),
            //	new KdTree_Linked<string, float>.Node(new float[] { 2.5f, 2.5f }, "Root-Left"),
            //	new KdTree_Linked<string, float>.Node(new float[] { 7.5f, 7.5f }, "Root-Right"),
            //	new KdTree_Linked<string, float>.Node(new float[] { 1, 10 }, "Root-Left-Left"),
            //	new KdTree_Linked<string, float>.Node(new float[] { 10, 10 }, "Root-Right-Right")
            //};

            //foreach (var node in testNodes)
            //	if (!tree.Add(node.Point, node.Value))
            //		throw new Exception("Failed to add node to tree");

            //var nodesToRemove = new KdTreeNode<float, string>[] {
            //	testNodes[1], // Root-Left
            //	testNodes[0] // Root
            //};

            //foreach (var nodeToRemove in nodesToRemove)
            //{
            //	tree.RemoveAt(nodeToRemove.Point);
            //	testNodes.Remove(nodeToRemove);

            //	Assert.IsNull(tree.FindValue(nodeToRemove.Value));
            //	Assert.IsNull(tree.FindValueAt(nodeToRemove.Point));

            //	foreach (var testNode in testNodes)
            //	{
            //		Assert.AreEqual(testNode.Value, tree.FindValueAt(testNode.Point));
            //		Assert.AreEqual(testNode.Point, tree.FindValue(testNode.Value));
            //	}

            //	Assert.AreEqual(testNodes.Count, tree.Count);
            //}

            #endregion

            #region Graph

            Console.WriteLine("  Testing Graph_SetOmnitree<int>-------------");
            Graph <int> graph = new GraphSetOmnitree <int>(Compute.Equal, Compute.Compare, Hash.Default);
            // add nodes
            for (int i = 0; i < test; i++)
            {
                graph.Add(i);
            }
            // add edges
            for (int i = 0; i < test - 1; i++)
            {
                graph.Add(i, i + 1);
            }
            Console.Write("    Traversal: ");
            graph.Stepper((int current) => { Console.Write(current); });
            Console.WriteLine();
            Console.WriteLine("    Edges: ");
            //((Graph_SetQuadtree<int>)graph)._edges.Foreach((Graph_SetQuadtree<int>.Edge e) => { Console.WriteLine("     " + e.Start + " " + e.End); });
            graph.Stepper(
                (int current) =>
            {
                Console.Write("     " + current + ": ");
                graph.Neighbors(current,
                                (int a) =>
                {
                    Console.Write(a);
                });
                Console.WriteLine();
            });
            Console.WriteLine();

            #endregion

            Console.WriteLine("============================================");
            Console.WriteLine("Examples Complete...");
            Console.ReadLine();
        }
Exemplo n.º 26
0
 internal Node(Func <T, T, bool> equate, Func <T, int> hash)
 {
     Count = 0;
     Map   = new MapHashLinked <Node, T>(equate, hash);
 }
Exemplo n.º 27
0
 private GraphMap(Equate <T> equate, Hash <T> hash, GraphSetOmnitree <T> graph)
 {
     this._edges = 0;
     this._map   = new MapHashLinked <MapHashLinked <bool, T>, T>(equate, hash);
 }
Exemplo n.º 28
0
 internal Node(Equate <T> equate, Hash <T> hash)
 {
     Count = 0;
     Map   = new MapHashLinked <Node, T>(equate, hash);
 }
Exemplo n.º 29
0
 public void Clear()
 {
     this._edges = 0;
     this._map   = new MapHashLinked <MapHashLinked <bool, T>, T>(this._map.Equate, this._map.Hash);
 }
Exemplo n.º 30
0
        public Matrix <float>[] CalculateAnimatedJointMatrices()
        {
            // initialize the matrix array and set the base matrix of each joint
            Matrix <float>[] jointMatrices = new Matrix <float> [this._joints.Count];

            // if no animation is active, the base matrices wer all we needed
            if (this._activeAnimation == null)
            {
                this._joints.Stepper(x => jointMatrices[x.Id] = x.BindLocalTransform);
                return(jointMatrices);
            }

            // get the elapsed animation time
            TimeSpan elapsedAnimationTimeSpan = DateTime.Now - this._animationStartTime;
            float    elapsedAnimationFloat    = (float)elapsedAnimationTimeSpan.TotalSeconds;

            elapsedAnimationFloat %= this._activeAnimation.Duration;

            // get the previous/next key frames
            Animation.KeyFrame previousFrame = this._activeAnimation.KeyFrames[0];
            Animation.KeyFrame nextFrame     = null;
            // OPTIMIZATION NOTE: binary search is possible here
            for (int i = 1; i < this._activeAnimation.KeyFrames.Length; i++)
            {
                nextFrame = this._activeAnimation.KeyFrames[i];
                if (nextFrame.TimeSpan > elapsedAnimationFloat)
                {
                    break;
                }
                previousFrame = this._activeAnimation.KeyFrames[i];
            }

            // calculate the ratio between the two key frames
            float keyFrameTimeSpan    = nextFrame.TimeSpan - previousFrame.TimeSpan;
            float currentKeyFrameTime = elapsedAnimationFloat - previousFrame.TimeSpan;
            float keyFrameRatio       = currentKeyFrameTime / keyFrameTimeSpan;

            // interpolate the joint matrices
            Map <Matrix <float>, string> currentPose = new MapHashLinked <Matrix <float>, string>();

            previousFrame.JointTransformations.Keys(joint =>
            {
                Link <Vector <float>, Quaternion <float> > previousTransform = previousFrame.JointTransformations[joint];
                Link <Vector <float>, Quaternion <float> > nextTransform     = nextFrame.JointTransformations[joint];

                Vector <float> currentTranslation  = Vector <float> .LinearInterpolation(previousTransform._1, nextTransform._1, keyFrameRatio);
                Quaternion <float> currentRotation = Quaternion <float> .Lerp(previousTransform._2, nextTransform._2, keyFrameRatio);

                Matrix <float> rotation3x3 = Quaternion <float> .ToMatrix3x3(currentRotation);

                float[,] floatData = new float[, ]
                {
                    { rotation3x3[0, 0], rotation3x3[0, 1], rotation3x3[0, 2], currentTranslation[0] },
                    { rotation3x3[1, 0], rotation3x3[1, 1], rotation3x3[1, 2], currentTranslation[1] },
                    { rotation3x3[2, 0], rotation3x3[2, 1], rotation3x3[2, 2], currentTranslation[2] },
                    { 0, 0, 0, 1 },
                };

                Matrix <float> currentJointMatrix = new Matrix <float>(4, 4, (row, column) => floatData[row, column]);

                currentPose.Add(joint.Name, currentJointMatrix);
            });

            // multiply the current pose and the bind local transform
            this._joints.Stepper(x => jointMatrices[x.Id] = currentPose[x.Name] * x.BindLocalTransform.Inverse());

            return(jointMatrices);

            //for (int i = 0; i < this._joints.Count; i++)
            //{
            //    jointMatrices[i] = Matrix<float>.FactoryIdentity(4, 4);
            //}
            //return jointMatrices;
        }