public bool Exists(CfgNodeValue <TK, TV> keyVal) { lock (_lock) { return(SingleItems.ContainsKey(keyVal.Key) || RepeatedItems.ContainsKey(keyVal.Key) && RepeatedItems[keyVal.Key].Contains(keyVal)); } }
public void Add(CfgNodeValue <TK, TV> value) { lock (_lock) { AllItems.Add(value); if (!RepeatedItems.ContainsKey(value.Key)) { if (SingleItems.ContainsKey(value.Key)) { RepeatedItems.Add(value.Key, new List <CfgNodeValue <TK, TV> > { SingleItems[value.Key] }); SingleItems.Remove(value.Key); RepeatedItems[value.Key].Add(value); } else { SingleItems.Add(value.Key, value); } } else { RepeatedItems[value.Key].Add(value); } } }
public bool Exists(TK key) { lock (_lock) { return(SingleItems.ContainsKey(key) || RepeatedItems.ContainsKey(key)); } }
public List <CfgNodeValue <TK, TV> > GetSeveral(TK key) { lock (_lock) { return(SingleItems.ContainsKey(key) ? new List <CfgNodeValue <TK, TV> > { SingleItems[key] } : RepeatedItems.ContainsKey(key) ? RepeatedItems[key] : new List <CfgNodeValue <TK, TV> >()); } }
public CfgNodeValue <TK, TV> GetSingle(TK key) { lock (_lock) { if (!RepeatedItems.ContainsKey(key)) { return(SingleItems.ContainsKey(key) ? SingleItems[key] : null); } throw new Exception($"Key value: \"{key}\" is not unique"); } }
/// <summary> /// Remove all the elements that have the given key /// </summary> public void Remove(TK key) { lock (_lock) { AllItems.RemoveAll(v => v.Key.Equals(key)); if (SingleItems.ContainsKey(key)) { SingleItems.Remove(key); } else { RepeatedItems.Remove(key); } } }
/// <summary> /// Sets a new value for a single element that matches in KEY /// </summary> public void Update(TK key, TV value) { lock (_lock) { if (!RepeatedItems.ContainsKey(key)) { if (SingleItems.ContainsKey(key)) { SingleItems[key].Value = value; } } else if (RepeatedItems.ContainsKey(key)) { throw new Exception($"Key value: \"{key}\" is not unique. Use Replace()"); } } }
/// <summary> /// Replaces ALL the elements that match in KEY and VALUE for the new one /// </summary> public void Replace(CfgNodeValue <TK, TV> oldKeyVal, CfgNodeValue <TK, TV> newKeyVal) { lock (_lock) { var index = AllItems.IndexOf(oldKeyVal); if (index >= 0) { AllItems[index] = newKeyVal; if (SingleItems.ContainsKey(oldKeyVal.Key)) { if (oldKeyVal.Key.Equals(newKeyVal.Key)) { SingleItems[oldKeyVal.Key] = newKeyVal; } else { SingleItems.Remove(oldKeyVal.Key); SingleItems.Add(newKeyVal.Key, newKeyVal); } } else { if (RepeatedItems.ContainsKey(oldKeyVal.Key)) { if (oldKeyVal.Key.Equals(newKeyVal.Key)) { for (var i = 0; i < RepeatedItems[oldKeyVal.Key].Count; i++) { if (RepeatedItems[oldKeyVal.Key][i].Equals(oldKeyVal)) { RepeatedItems[oldKeyVal.Key][i] = newKeyVal; } } } else { Remove(oldKeyVal); Add(newKeyVal); } } } } } }
/// <summary> /// Removes a specific value /// </summary> public void Remove(CfgNodeValue <TK, TV> keyVal) { lock (_lock) { AllItems.RemoveAll(v => v.Value.Equals(keyVal)); if (SingleItems.ContainsKey(keyVal.Key)) { SingleItems.Remove(keyVal.Key); } else if (RepeatedItems.ContainsKey(keyVal.Key)) { RepeatedItems[keyVal.Key].RemoveAll(v => v.Equals(keyVal)); if (!RepeatedItems[keyVal.Key].Any()) { RepeatedItems.Remove(keyVal.Key); } } } }