/// <summary> /// Removes all key/value pairs from set <paramref name="a"/> whose keys are not also contained in <paramref name="b"/>. /// </summary> /// <param name="a">A dictionary to change.</param> /// <param name="b">Another dictionary of compatible type to <paramref name="a"/>.</param> /// <param name="graph">The graph containing the edge containing the attribute which gets changed.</param> /// <param name="owner">The edge containing the attribute which gets changed.</param> /// <param name="attrType">The attribute type of the attribute which gets changed.</param> /// <returns>A truth value telling whether at least one element was changed in a</returns> public static bool IntersectChanged <K>(Dictionary <K, SetValueType> a, Dictionary <K, SetValueType> b, IGraph graph, IEdge owner, AttributeType attrType) { // First determine all elements from a not contained in b List <K> toBeRemoved = new List <K>(a.Count); foreach (KeyValuePair <K, SetValueType> entry in a) { if (!b.ContainsKey(entry.Key)) { toBeRemoved.Add(entry.Key); } } // Then remove them foreach (K key in toBeRemoved) { graph.ChangingEdgeAttribute(owner, attrType, AttributeChangeType.RemoveElement, key, null); a.Remove(key); graph.ChangedEdgeAttribute(owner, attrType); } return(toBeRemoved.Count > 0); }
// convenience helper function for firing the changed node/edge attribute event (contains graph element type dispatching) public static void ChangedAttribute(IGraph graph, IGraphElement elem, AttributeType attrType) { if (elem is INode) { graph.ChangedNodeAttribute((INode)elem, attrType); } else { graph.ChangedEdgeAttribute((IEdge)elem, attrType); } }
/// <summary> /// Appends all values from deque <paramref name="b"/> to <paramref name="a"/>. /// </summary> /// <param name="a">A Deque to change.</param> /// <param name="b">Another Deque of compatible type to <paramref name="a"/>.</param> /// <param name="graph">The graph containing the edge containing the attribute which gets changed.</param> /// <param name="owner">The edge containing the attribute which gets changed.</param> /// <param name="attrType">The attribute type of the attribute which gets changed.</param> /// <returns>A truth value telling whether at least one element was changed in a</returns> public static bool ConcatenateChanged <V>(Deque <V> a, Deque <V> b, IGraph graph, IEdge owner, AttributeType attrType) { // Append b to a foreach (V entry in b) { graph.ChangingEdgeAttribute(owner, attrType, AttributeChangeType.PutElement, entry, null); a.Enqueue(entry); graph.ChangedEdgeAttribute(owner, attrType); } return(b.Count > 0); }
/// <summary> /// Removes all key/value pairs from map <paramref name="a"/> whose keys are contained in <paramref name="b"/>. /// </summary> /// <param name="a">A dictionary to change.</param> /// <param name="b">Another dictionary of compatible key type to <paramref name="a"/>.</param> /// <param name="graph">The graph containing the edge containing the attribute which gets changed.</param> /// <param name="owner">The edge containing the attribute which gets changed.</param> /// <param name="attrType">The attribute type of the attribute which gets changed.</param> /// <returns>A truth value telling whether at least one element was changed in a</returns> public static bool ExceptChanged <K, V, W>(Dictionary <K, V> a, Dictionary <K, W> b, IGraph graph, IEdge owner, AttributeType attrType) { bool changed = false; // Remove all elements from a contained in b. foreach (KeyValuePair <K, W> entry in b) { graph.ChangingEdgeAttribute(owner, attrType, AttributeChangeType.RemoveElement, null, entry.Key); changed |= a.Remove(entry.Key); graph.ChangedEdgeAttribute(owner, attrType); } return(changed); }
/// <summary> /// Adds all key/value pairs from map <paramref name="b"/> to <paramref name="a"/>. /// If both dictionaries contain one key, the value from <paramref name="b"/> takes precedence /// (this way the common case "a = a | map<int, int> { 7 -> 13 };" would update an existing entry /// with key 7 to 13). /// </summary> /// <param name="a">A dictionary to change.</param> /// <param name="b">Another dictionary of compatible type to <paramref name="a"/>.</param> /// <param name="graph">The graph containing the edge containing the attribute which gets changed.</param> /// <param name="owner">The edge containing the attribute which gets changed.</param> /// <param name="attrType">The attribute type of the attribute which gets changed.</param> /// <returns>A truth value telling whether at least one element was changed in a</returns> public static bool UnionChanged <K, V>(Dictionary <K, V> a, Dictionary <K, V> b, IGraph graph, IEdge owner, AttributeType attrType) { bool changed = false; // Add all elements from b not contained in a (different values count as not contained, overwriting old value). foreach (KeyValuePair <K, V> entry in b) { if (!a.ContainsKey(entry.Key) || EqualityComparer <V> .Default.Equals(a[entry.Key], entry.Value)) { graph.ChangingEdgeAttribute(owner, attrType, AttributeChangeType.PutElement, entry.Value, entry.Key); a[entry.Key] = entry.Value; graph.ChangedEdgeAttribute(owner, attrType); changed = true; } } return(changed); }