Beispiel #1
0
 /// <summary>Associates a key with a value in the dictionary, and gets the old value
 /// if the key was already present.</summary>
 /// <returns>The old value associated with the same key. If a new pair was added,
 /// the result has no value.</returns>
 public static Maybe <V> GetAndSet <K, V>(this IDictionaryEx <K, V> dict, K key, V value)
 {
     if (dict.GetAndEdit(ref key, ref value, DictEditMode.AddOrReplace))
     {
         return(value);
     }
     return(new Maybe <V>());
 }
Beispiel #2
0
 /// <summary>Adds a new key/value pair if the key was not present, or gets the existing
 /// value if the key was present.</summary>
 /// <returns>The existing value. If a new pair was added, the result has no value.</returns>
 /// <seealso cref="GetOrAdd{K, V}(IDictionaryEx{K, V}, K, V)"/>
 public static Maybe <V> AddOrGetExisting <K, V>(this IDictionaryEx <K, V> dict, K key, V value)
 {
     if (dict.GetAndEdit(ref key, ref value, DictEditMode.AddIfNotPresent))
     {
         return(value);
     }
     return(default(Maybe <V>));
 }
Beispiel #3
0
 /// <summary>Replaces an item in the map if the key was already present.
 /// If the key was not already present, this method has no effect.</summary>
 /// <returns>The old value if a value was replaced, or an empty value of <see cref="Maybe{V}"/> otherwise.</returns>
 public static Maybe <V> SwapIfPresent <K, V>(this IDictionaryEx <K, V> dict, K key, V value)
 {
     return(dict.GetAndEdit(ref key, ref value, DictEditMode.ReplaceIfPresent) ?
            (Maybe <V>)value : default(Maybe <V>));
 }
Beispiel #4
0
 /// <summary>Replaces an item in the map if the key was already present.
 /// If the key was not already present, this method has no effect.</summary>
 /// <returns>True if a value existed and was replaced, false if not.</returns>
 public static bool ReplaceIfPresent <K, V>(this IDictionaryEx <K, V> dict, K key, V value)
 {
     return(dict.GetAndEdit(ref key, ref value, DictEditMode.ReplaceIfPresent));
 }
Beispiel #5
0
 /// <summary>Adds a key/value pair to the dictionary if the key is not already present,
 /// and returns the existing or new value.</summary>
 /// <returns>The existing value (if the key already existed) or the new value.</returns>
 public static V GetOrAdd <K, V>(this IDictionaryEx <K, V> dict, K key, V value)
 {
     dict.GetAndEdit(ref key, ref value, DictEditMode.AddIfNotPresent);
     return(value);
 }
Beispiel #6
0
 public static bool TryAdd <K, V>(this IDictionaryEx <K, V> dict, K key, V value)
 {
     return(!dict.GetAndEdit(ref key, ref value, DictEditMode.AddIfNotPresent));
 }