using System;
using System.Collections.Generic;
namespace Semmle.Util
{
///
/// A dictionary which performs an action when items are added to the dictionary.
/// The order in which keys and actions are added does not matter.
///
///
///
public class ActionMap
{
public void Add(Key key, Value value)
{
Action a;
if (actions.TryGetValue(key, out a))
a(value);
values[key] = value;
}
public void OnAdd(Key key, Action action)
{
Action a;
if (actions.TryGetValue(key, out a))
{
actions[key] = a + action;
}
else
{
actions.Add(key, action);
}
Value val;
if (values.TryGetValue(key, out val))
{
action(val);
}
}
// Action associated with each key.
readonly Dictionary> actions = new Dictionary>();
// Values associated with each key.
readonly Dictionary values = new Dictionary();
}
}