/
CatHashList.cs
107 lines (89 loc) · 2.52 KB
/
CatHashList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/// Dedicated to the public domain by Christopher Diggins
/// http://creativecommons.org/licenses/publicdomain/
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Cat
{
class HashList : ICatObject
{
HashList mNext;
Dictionary<Object, Object> mDict = new Dictionary<object,object>();
public HashList()
{
mNext = this;
}
private HashList(HashList list)
{
mDict = list.mDict;
mNext = list.mNext;
list.mNext = this;
}
private HashList MakeCopy()
{
HashList result = new HashList();
result.mDict = new Dictionary<object, object>(mDict);
HashList next = mNext;
while (next != this)
{
// Remove "this" from the reference chain
if (next.mNext == this)
next.mNext = this.mNext;
}
return result;
}
public HashList Add(Object key, Object value)
{
HashList result = this;
if (result.mNext != result)
result = MakeCopy();
result.mDict.Add(key, value);
return result;
}
public HashList Set(Object key, Object value)
{
HashList result = this;
if (result.mNext != result)
result = MakeCopy();
result.mDict[key] = value;
return result;
}
public Object Get(Object key)
{
Object result = mDict[key];
return result;
}
public bool ContainsKey(Object key)
{
return mDict.ContainsKey(key);
}
public CatList ToArray()
{
CatList[] a = new CatList[mDict.Count];
int i = 0;
foreach (KeyValuePair<Object, Object> pair in mDict)
{
a[i++] = CatList.MakePair(pair.Value, pair.Key);
}
return new CatList(a);
}
public override string ToString()
{
return "hash_list";
}
#region ICatObject Members
public void pop()
{
}
ICatObject ICatObject.dup()
{
return new HashList(this);
}
public string str()
{
return ToString();
}
#endregion
}
}