summaryrefslogtreecommitdiff
path: root/src/jvm/clojure/lang/PersistentListIdentityMap.java
blob: 1eea34f3491e6e2f245ba83d15ff87e3321ee7d4 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
 *   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.
 **/

package clojure.lang;

import java.util.Iterator;

/**
 * Implementation of persistent map on a linked list
 * Uses identity (==) comparison, vs equals() of PersistentListMap

 * Note that instances of this class are constant values
 * i.e. add/remove etc return new values
 *
 * Lookups/changes are linear, so only appropriate for _very_small_ maps
 * PersistentArrayMap is generally faster, but this class avoids the double allocation,
 * and so is better/faster as a bucket for hash tables
 *
 * null keys and values are ok, but you won't be able to distinguish a null value via get - use contains/find
 *
 * code duplication here is kind of gross, but most efficient
 */

public class PersistentListIdentityMap implements IPersistentMap, IMapEntry, ISeq, ISequential
{

static public PersistentListIdentityMap EMPTY = new PersistentListIdentityMap();

static public PersistentListIdentityMap create(Object key, Object val){
	return new Tail(key, val);
}

public Object key(){
	return null;
}

public Object val(){
	return null;
}

PersistentListIdentityMap next(){
    return this;
    }

public int count(){
	return 0;
}

public boolean contains(Object key){
	return false;
}

public IMapEntry find(Object key){
	return null;
}

public IPersistentMap add(Object key, Object val) throws Exception {
    return put(key, val);
}

public PersistentListIdentityMap put(Object key, Object val){
	return new Tail(key, val);
}

public PersistentListIdentityMap remove(Object key){
	return this;
}

public Object get(Object key){
	return null;
}

public int capacity(){
	return 0;
}

public Object first() {
    return null;
}

public ISeq rest() {
    return null;
}

public ISeq seq() {
    return null;
}


static class Iter implements Iterator{
	PersistentListIdentityMap e;

	Iter(PersistentListIdentityMap e)
	{
	this.e = e;
	}

	public boolean hasNext(){
		return e != EMPTY;
	}

	public Object next(){
		PersistentListIdentityMap ret = e;
		e = e.next();
		return ret;
	}

	public void remove(){
		throw new UnsupportedOperationException();
	}
}

public Iterator iterator(){
	return new Iter(this);
}

static class Tail extends PersistentListIdentityMap {
	final Object _key;
	final Object _val;

	Tail(Object key,Object val){
		this._key = key;
		this._val = val;
		}

    PersistentListIdentityMap next(){
        return EMPTY;
    }

	public int count(){
		return 1;
	}

	public Object get(Object key){
		if(key ==_key)
			return _val;
		return null;
	}

	public int capacity(){
		return 1;
	}

	public Object key(){
		return _key;
	}

	public Object val(){
		return _val;
	}

	public boolean contains(Object key){
		return key ==_key;
	}

	public IMapEntry find(Object key){
		if(key ==_key)
			return this;
		return null;
	}

	public PersistentListIdentityMap add(Object key, Object val) throws Exception{
		if(key == _key)
			{
            throw new Exception("Key already present");
			}
		return new Link(key,val,this);
	}

    public PersistentListIdentityMap put(Object key, Object val){
		if(key == _key)  //replace
			{
			if(val == _val)
				return this;
			return new Tail(key,val);
			}
		return new Link(key,val,this);
	}

	public PersistentListIdentityMap remove(Object key){
		if(key == _key)
			return EMPTY;
		return this;
	}

    public Object first() {
        return this;
    }

    public ISeq rest() {
        return null;
    }

    public ISeq seq() {
        return this;
    }
}

static class Link extends PersistentListIdentityMap {
	final Object _key;
	final Object _val;
	final PersistentListIdentityMap _rest;

	Link(Object key,Object val,PersistentListIdentityMap next){
		this._key = key;
		this._val = val;
		this._rest = next;
		}

	public Object key(){
		return _key;
	}

	public Object val(){
		return _val;
	}

	PersistentListIdentityMap next(){
        return _rest;
        }

	public int count(){
		return 1 + _rest.count();
	}

	public boolean contains(Object key){
		return find(key) != null;
	}

	public IMapEntry find(Object key){
		if(key ==_key)
			return this;
		return _rest.find(key);
	}

	public PersistentListIdentityMap add(Object key, Object val) throws Exception{
		IMapEntry e = find(key);
		if(e != null)
			{
            throw new Exception("Key already present");
			}
		return new Link(key,val,this);
	}

    public PersistentListIdentityMap put(Object key, Object val){
		IMapEntry e = find(key);
		if(e != null)
			{
			if(e.val() == val)
				return this;
			return create(_key,_val,remove(key));
			}
		return new Link(key,val,this);
	}

	public PersistentListIdentityMap remove(Object key){
		if(key == _key)
			return _rest;
		PersistentListIdentityMap r = _rest.remove(key);
		if(r == _rest)  //not there
			return this;
		return create(_key,_val,r);
	}

	public Object get(Object key){
		IMapEntry e = find(key);
		if(e != null)
			return e.val();
		return null;
	}

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

    public Object first() {
        return this;
    }

    public ISeq rest() {
        return _rest;
    }

    public ISeq seq() {
        return this;
    }

    PersistentListIdentityMap create(Object k,Object v,PersistentListIdentityMap r){
		if(r == EMPTY)
			return new Tail(k,v);
		return new Link(k, v, r);
	}

}

}