using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Signum.Utilities.DataStructures { public class ScopedDictionary<TKey, TValue> { ScopedDictionary<TKey, TValue> previous; Dictionary<TKey, TValue> map; public ScopedDictionary(ScopedDictionary<TKey, TValue> previous) { this.previous = previous; this.map = new Dictionary<TKey, TValue>(); } public ScopedDictionary(ScopedDictionary<TKey, TValue> previous, IEnumerable<KeyValuePair<TKey, TValue>> pairs) : this(previous) { foreach (var p in pairs) { this.map.Add(p.Key, p.Value); } } public void Add(TKey key, TValue value) { this.map.Add(key, value); } public bool TryGetValue(TKey key, out TValue value) { for (ScopedDictionary<TKey, TValue> scope = this; scope != null; scope = scope.previous) { if (scope.map.TryGetValue(key, out value)) return true; } value = default(TValue); return false; } public bool ContainsKey(TKey key) { for (ScopedDictionary<TKey, TValue> scope = this; scope != null; scope = scope.previous) { if (scope.map.ContainsKey(key)) return true; } return false; } } }
By viewing downloads associated with this article you agree to the Terms of use and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)
Math Primers for Programmers