/// <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); }
/// <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); }
/// <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); } }