summaryrefslogtreecommitdiff
path: root/src/cli/runtime/PersistentArrayMap.cs
blob: 15a67d7370f46b29ba6c3305f40156e933fa6c23 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 *   Copyright (c) Rich Hickey. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
 *   which can be found in the file CPL.TXT at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

using System;
using System.Collections;

namespace clojure.lang
{

/**
 * Simple implementation of persistent map on an array

 * Note that instances of this class are constant values
 * i.e. add/remove etc return new values
 *
 * Copies array on every change, so only appropriate for _very_small_ maps
 *
 * null keys and values are ok, but you won't be able to distinguish a null value via get - use contains/find
 */

public class PersistentArrayMap : Obj, IPersistentMap, IPersistentCollection {

internal readonly Object[] array;

	public static PersistentArrayMap EMPTY = new PersistentArrayMap();
	
	internal const int HASHTABLE_THRESHOLD = 42;

protected PersistentArrayMap(){
    this.array = RT.EMPTY_ARRAY;
}

virtual internal PersistentArrayMap create(params Object[] init){
	PersistentArrayMap ret = new PersistentArrayMap(init);
	ret._meta = _meta;
	return ret;
}

virtual internal IPersistentMap createHT(Object[] init){
	PersistentHashtableMap ret = new PersistentHashtableMap(init);
	ret._meta = _meta;
	return ret;
	}
	
public override Obj withMeta(IPersistentMap meta)
	{
	Obj ret = (Obj)MemberwiseClone();
	ret._meta = meta;
	return ret;
	} 
/**
 * This ctor captures/aliases the passed array, so do not modify later
 * @param init {key1,val1,key2,val2,...}
 */
public PersistentArrayMap(params Object[] init){
    this.array = init;
}

public int count() {
    return array.Length/2;
}

public bool contains(Object key){
	return indexOf(key) >= 0;
}

public IMapEntry find(Object key) {
    int i = indexOf(key);
    if(i >= 0)
        return new Iter(array,i);
    return null;
}

public IPersistentMap add(Object key, Object val) {
    int i = indexOf(key);
    Object[] newArray;
    if(i >= 0)
        {
		throw new Exception("Key already present");
        }
    else //didn't have key, grow
        {
		if (array.Length > HASHTABLE_THRESHOLD)
			return createHT(array).add(key, val);
		newArray = new Object[array.Length + 2];
        if(array.Length > 0)
            Array.Copy(array,0,newArray,2,array.Length);
        newArray[0] = key;
        newArray[1] = val;
        }
    return create(newArray);
    }

public IPersistentMap put(Object key, Object val) {
    int i = indexOf(key);
    Object[] newArray;
    if(i >= 0) //already have key, same-sized replacement
        {
        if(array[i+1] == val) //no change, no op
            return this;
        newArray = (Object[])array.Clone();
        newArray[i+1] = val;
        }
    else //didn't have key, grow
        {
		if (array.Length > HASHTABLE_THRESHOLD)
			return createHT(array).put(key, val);
		newArray = new Object[array.Length + 2];
        if(array.Length > 0)
            Array.Copy(array,0,newArray,2,array.Length);
        newArray[0] = key;
        newArray[1] = val;
        }
    return create(newArray);
}

public IPersistentMap remove(Object key) {
    int i = indexOf(key);
    if(i >= 0) //have key, will remove
        {
		int newlen = array.Length - 2;
		if (newlen == 0)
			return empty();
		Object[] newArray = new Object[newlen];
        for(int s=0,d=0;s<array.Length;s += 2)
            {
            if(!equalKey(array[s],key)) //skip removal key
                {
                newArray[d] = array[s];
                newArray[d+1] = array[s+1];
                d += 2;
                }
            }
        return create(newArray);
        }
    //don't have key, no op
    return this;
}

virtual public IPersistentMap empty() {
    if(_meta == null)
        return EMPTY;
    PersistentArrayMap ret = new PersistentArrayMap();
    ret._meta = _meta;
    return ret;
}

public Object get(Object key) {
    int i = indexOf(key);
    if(i >= 0)
        return array[i + 1];
    return null;
}

public int capacity() {
    return count();
}

int indexOf(Object key){
    for(int i=0;i<array.Length;i+=2)
        {
        if(equalKey(array[i],key))
            return i;
        }
    return -1;
}

internal virtual bool equalKey(Object k1,Object k2){
    if(k1 == null)
        return k2 == null;
    return k1.Equals(k2);
}

public IEnumerator GetEnumerator() {
    return new Iter(array);
}

public ISeq seq() {
    if(array.Length > 0)
        return new Seq(array,0);
    return null;
}

internal class Seq : ISeq, IMapEntry{
    readonly Object[] array;
	readonly int i;

    internal Seq(Object[] array, int i){
        this.array = array;
        this.i = i;
    }

    public Object key() {
        return array[i];
    }

    public Object val() {
        return array[i+1];
    }

    public Object first() {
        return this;
    }

    public ISeq rest() {
        if(i+2 < array.Length)
            return new Seq(array, i + 2);
        return null;
    }
}
internal class Iter : IEnumerator,IMapEntry{
    Object[] array;
    int i;

    //for iterator
    internal Iter(Object[] array):  this(array,-2)
    {
    }

    //for find
    internal Iter(Object[] array, int i){
        this.array = array;
        this.i = i;
    }

    public Object key() {
        return array[i];
    }

    public Object val() {
        return array[i+1];
    }

#region IEnumerator Members

public object Current
	{
	get {return this;}
	}

public bool MoveNext()
	{
	i += 2;
	return i < array.Length - 2;
	}

public void Reset()
	{
	throw new Exception("The method or operation is not implemented.");
	}

#endregion
	}
}


}