namespace Ngs.Collections
{
// Copyright (c) Microsoft Corporation. All rights reserved.
// This source code is made available under the terms of the Microsoft Public License (MS-PL)
using System;
using System.Collections;
using System.Collections.Generic;
public class ScopedDictionary
{
ScopedDictionary previous;
Dictionary map;
public ScopedDictionary(ScopedDictionary previous)
{
this.previous = previous;
this.map = new Dictionary();
}
public ScopedDictionary(ScopedDictionary previous, IEnumerable> 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 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 scope = this; scope != null; scope = scope.previous)
{
if (scope.map.ContainsKey(key))
return true;
}
return false;
}
}
}