Esempio n. 1
0
File: Map.cs Progetto: mangzee/Towel
 /// <summary>
 /// This constructor is for cloning purposes.
 /// <para>Runtime: O(n)</para>
 /// </summary>
 /// <param name="map">The map to clone.</param>
 internal MapHashLinked(MapHashLinked <T, K, Equate, Hash> map)
 {
     _equate = map._equate;
     _hash   = map._hash;
     _table  = (Node[])map._table.Clone();
     _count  = map._count;
 }
Esempio n. 2
0
 private MapHashLinked(MapHashLinked <T, K> setHashList)
 {
     _equate = setHashList._equate;
     _hash   = setHashList._hash;
     _table  = setHashList._table.Clone() as Node[];
     _count  = setHashList._count;
 }
Esempio n. 3
0
File: Trie.cs Progetto: Thaina/Towel
        /// <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 = 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);
            }
Esempio n. 4
0
File: Trie.cs Progetto: Thaina/Towel
 /// <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);
 }
Esempio n. 5
0
 public TreeMap(T head, Func <T, T, bool> equate, Func <T, int> hash)
 {
     _equate = equate;
     _hash   = hash;
     _head   = head;
     _tree   = new MapHashLinked <Node, T>(_equate, _hash)
     {
         { _head, new Node(default, new SetHashLinked <T>(_equate, _hash)) }
Esempio n. 6
0
 /// <summary>Constructor for cloning purposes.</summary>
 /// <param name="graphToClone">The graph to clone.</param>
 internal GraphMap(GraphMap <T> graphToClone)
 {
     _edges = graphToClone._edges;
     _map   = new MapHashLinked <SetHashLinked <T>, T>(
         graphToClone._map.Equate,
         graphToClone._map.Hash);
     Stepper((T node) => this.Add(node));
     Stepper((T a, T b) => Add(a, b));
 }
Esempio n. 7
0
 public TreeMap(T head, Equate <T> equate, Hash <T> hash)
 {
     _equate = equate;
     _hash   = hash;
     _head   = head;
     _tree   = new MapHashLinked <Node, T>(_equate, _hash)
     {
         { _head, new Node(default(T), new SetHashLinked <T>(_equate, _hash)) }
     };
 }
Esempio n. 8
0
        /// <summary>Adds an edge to the graph.</summary>
        /// <param name="start">The starting point of the edge.</param>
        /// <param name="end">The ending point of the edge.</param>
        public void Add(T start, T end)
        {
            if (!_map.Contains(start))
            {
                throw new InvalidOperationException("Adding an edge to a non-existing starting node.");
            }
            if (!_map[start].Contains(end))
            {
                throw new InvalidOperationException("Adding an edge to a non-existing ending node.");
            }
            if (_map[start] is null)
            {
                _map[start] = new MapHashLinked <bool, T>(_map.Equate, _map.Hash);
            }

            _map[start].Add(end, true);
        }
Esempio n. 9
0
File: Trie.cs Progetto: Thaina/Towel
        /// <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);
            }
        }
Esempio n. 10
0
 public void Clear()
 {
     _edges = 0;
     _map   = new MapHashLinked <MapHashLinked <bool, T>, T>(_map.Equate, _map.Hash);
 }
Esempio n. 11
0
 /// <summary>Constructs a new GraphMap.</summary>
 /// <param name="equate">The equate delegate for the data structure to use.</param>
 /// <param name="hash">The hash function for the data structure to use.</param>
 public GraphMap(Equate <T> equate, Hash <T> hash)
 {
     _edges = 0;
     _map   = new MapHashLinked <MapHashLinked <bool, T>, T>(equate, hash);
 }
Esempio n. 12
0
 /// <summary>Constructs a new GraphMap.</summary>
 /// <param name="equate">The equate delegate for the data structure to use.</param>
 /// <param name="hash">The hash function for the data structure to use.</param>
 public GraphMap(Func <T, T, bool> equate, Func <T, int> hash)
 {
     _edges = 0;
     _map   = new MapHashLinked <SetHashLinked <T>, T>(equate, hash);
 }
Esempio n. 13
0
 public void Clear()
 {
     this._edges = 0;
     this._map   = new MapHashLinked <MapHashLinked <bool, T>, T>(this._map.Equate, this._map.Hash);
 }
Esempio n. 14
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);
 }
Esempio n. 15
0
File: Trie.cs Progetto: Thaina/Towel
 internal Node(Equate <T> equate, Hash <T> hash)
 {
     Count = 0;
     Map   = new MapHashLinked <Node, T>(equate, hash);
 }