using System;
using System.Collections;
namespace BCG
{
///
/// An N-domensional hashtable , providing add and retrieve features
/// for the time being. Does not deal with null keys right now
///
///
[Serializable]
public class HashTableND
{
private int myDim, myCount = 0;
private IDictionary myRootDic = new Hashtable ();
public HashTableND (int dim) {myDim = dim;}
public object add (object[] keys, object val)
{
ArrayList t = new ArrayList (keys);
t.Add (val);
return add (t.ToArray());
}
///
/// Adds a value to the hashtable. the size of the arg array should be dim+1, with the
/// the first dim entries corresponding to keys and the last to the value to be added
///
///
public object add (object[] os)
{
if (os.Length != myDim+1)
throw new ApplicationException ("size of arg array not right");
else
{
myCount ++;
IDictionary d = myRootDic;
for (int i = 0 ; ; i++)
{
if (i < myDim - 1)
{
if (!d.Contains (os[i]))
d.Add (os[i], new Hashtable ());
d = d[os[i]] as IDictionary;
}
else
{
d[os[i]] = os[i+1];
return os [myDim];
}
}
}
}
///
/// Retrieves values from the hastable, with the arg array specifying the keys.
///
///
///
public object getValue (params object[] keys)
{
IDictionary d = myRootDic;
for (int i = 0; ; i++)
if (i < keys.Length-1)
{
d = d[keys[i]] as IDictionary;
if (d == null)
return null;
}
else
return d[keys[i]];
}
public void setValue (object[] keys, object val)
{
ArrayList t = new ArrayList (keys);
t.Add (val);
setValue (t.ToArray());
}
public void setValue (object[] os)
{
if (os.Length != myDim+1)
throw new ApplicationException ("size of arg array not right");
IDictionary d = myRootDic;
for (int i = 0; i < myDim-1; i++)
d = d[os[i]] as IDictionary;
d[os[myDim-1]] = os[myDim];
}
public void remove (params object[] keys)
{
myCount --;
IDictionary d = myRootDic;
for (int i = 0; ; i++)
if (i < keys.Length-1)
d = d[keys[i]] as IDictionary;
else
{
d.Remove (keys[i]);
return;
}
}
public int count {get {return myCount;}}
public ICollection topLevelKeys { get {return myRootDic.Keys;}}
}
}