-
Notifications
You must be signed in to change notification settings - Fork 47
/
RList.java
333 lines (291 loc) · 9.08 KB
/
RList.java
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package org.rosuda.REngine;
// REngine library - Java client interface to R
// Copyright (C) 2004,2007,2008 Simon Urbanek
import java.util.*;
/** implementation of R-lists<br>
All lists (dotted-pair lists, language lists, expressions and vectors) are regarded as named generic vectors.
Note: This implementation has changed radically in Rserve 0.5!
This class inofficially implements the Map interface. Unfortunately a conflict in the Java iterface classes Map and List doesn't allow us to implement both officially. Most prominently the Map 'remove' method had to be renamed to removeByKey.
@version $Id$
*/
public class RList extends Vector implements List {
public Vector names;
/** constructs an empty list */
public RList() { super(); names=null; }
/** constructs an initialized, unnamed list
* @param contents - an array of {@link REXP}s to use as contents of this list */
public RList(REXP[] contents) {
super(contents.length);
int i=0;
while (i<contents.length)
super.add(contents[i++]);
names=null;
}
public RList(int initSize, boolean hasNames) {
super(initSize);
names=null;
if (hasNames) names=new Vector(initSize);
}
/** constructs an initialized, unnamed list
* @param contents - a {@link Collection} of {@link REXP}s to use as contents of this list */
public RList(Collection contents) {
super(contents);
names=null;
}
/** constructs an initialized, named list. The length of the contents vector determines the length of the list.
* @param contents - an array of {@link REXP}s to use as contents of this list
* @param names - an array of {@link String}s to use as names */
public RList(REXP[] contents, String[] names) {
this(contents);
if (names!=null && names.length>0) {
this.names=new Vector(names.length);
int i = 0;
while (i < names.length) this.names.add(names[i++]);
while (this.names.size()<size()) this.names.add(null);
}
}
/** constructs an initialized, named list. The size of the contents collection determines the length of the list.
* @param contents - a {@link Collection} of {@link REXP}s to use as contents of this list
* @param names - an array of {@link String}s to use as names */
public RList(Collection contents, String[] names) {
this(contents);
if (names!=null && names.length>0) {
this.names=new Vector(names.length);
int i = 0;
while (i < names.length) this.names.add(names[i++]);
while (this.names.size()<size()) this.names.add(null);
}
}
/** constructs an initialized, named list. The size of the contents collection determines the length of the list.
* @param contents - a {@link Collection} of {@link REXP}s to use as contents of this list
* @param names - an {@link Collection} of {@link String}s to use as names */
public RList(Collection contents, Collection names) {
this(contents);
if (names!=null && names.size()>0) {
this.names=new Vector(names);
while (this.names.size()<size()) this.names.add(null);
}
}
/** checks whether this list is named or unnamed
* @return <code>true</code> if this list is named, <code>false</code> otherwise */
public boolean isNamed() {
return names!=null;
}
/** get xpression given a key
@param v key
@return value which corresponds to the given key or
<code>null</code> if the list is unnamed or key not found */
public REXP at(String v) {
if (names==null) return null;
int i = names.indexOf(v);
if (i < 0) return null;
return (REXP)elementAt(i);
}
/** get element at the specified position
@param i index
@return value at the index or <code>null</code> if the index is out of bounds */
public REXP at(int i) {
return (i>=0 && i<size())?(REXP)elementAt(i):null;
}
/** return the key (name) at a given index
@param i index
@return ket at the index - can be <code>null</code> is the list is unnamed or the index is out of range */
public String keyAt(int i) {
return (names==null || i<0 || i>=names.size())?null:(String)names.get(i);
}
/** set key at the given index. Using this method automatically makes the list a named one even if the key is <code>null</code>. Out of range operations are undefined (currently no-ops)
@param i index
@param value key name */
public void setKeyAt(int i, String value) {
if (i < 0) return;
if (names==null)
names = new Vector();
if (names.size() < size()) names.setSize(size());
if (i < size()) names.set(i, value);
}
/** returns all keys of the list
* @return array containing all keys or <code>null</code> if list unnamed */
public String[] keys() {
if (names==null) return null;
int i = 0;
String k[] = new String[names.size()];
while (i < k.length) { k[i] = keyAt(i); i++; };
return k;
}
// --- overrides that sync names
public void add(int index, Object element) {
super.add(index, element);
if (names==null) return;
names.add(index, null);
}
public boolean add(Object element) {
super.add(element);
if (names != null)
names.add(null);
return true;
}
public boolean addAll(Collection c) {
boolean ch = super.addAll(c);
if (names==null) return ch;
int l = size();
while (names.size()<l) names.add(null);
return ch;
}
public boolean addAll(int index, Collection c) {
boolean ch = super.addAll(index, c);
if (names==null) return ch;
int l = c.size();
while (l-- > 0) names.add(index, null);
return ch;
}
public void clear() {
super.clear();
names=null;
}
public Object clone() {
return new RList(this, names);
}
public Object remove(int index) {
Object o = super.remove(index);
if (names != null) {
names.remove(index);
if (size()==0) names=null;
}
return o;
}
public boolean remove(Object elem) {
int i = indexOf(elem);
if (i<0) return false;
remove(i);
if (size()==0) names=null;
return true;
}
public boolean removeAll(Collection c) {
if (names==null) return super.removeAll(c);
boolean changed=false;
Iterator it = c.iterator();
while (it.hasNext())
changed|=remove(it.next());
return changed;
}
public boolean retainAll(Collection c) {
if (names==null) return super.retainAll(c);
boolean rm[] = new boolean[size()];
boolean changed=false;
int i = 0;
while (i<rm.length) {
changed|=rm[i]=!c.contains(get(i));
i++;
}
while (i>0) {
i--;
if (rm[i]) remove(i);
}
return changed;
}
// --- old API mapping
public void removeAllElements() { clear(); }
public void insertElementAt(Object obj, int index) { add(index, obj); }
public void addElement(Object obj) { add(obj); }
public void removeElementAt(int index) { remove(index); }
public boolean removeElement(Object obj) { return remove(obj); }
// --- Map interface
public boolean containsKey(Object key) {
return (names==null)?false:names.contains(key);
}
public boolean containsValue(Object value) {
return contains(value);
}
/** NOTE: THIS IS UNIMPLEMENTED and always returns <code>null</code>! Due to the fact that R lists are not proper maps we canot maintain a set-view of the list */
public Set entrySet() {
return null;
}
public Object get(Object key) {
return at((String)key);
}
/** Note: sinde RList is not really a Map, the returned set is only an approximation as it cannot reference duplicate or null names that may exist in the list */
public Set keySet() {
if (names==null) return null;
return new HashSet(names);
}
public Object put(Object key, Object value) {
if (key==null) {
add(value);
return null;
}
if (names != null) {
int p = names.indexOf(key);
if (p >= 0)
return super.set(p, value);
}
int i = size();
super.add(value);
if (names==null)
names = new Vector(i+1);
while (names.size() < i) names.add(null);
names.add(key);
return null;
}
public void putAll(Map t) {
if (t==null) return;
// NOTE: this if branch is dead since RList cannot inherit from Map
if (t instanceof RList) { // we need some more sophistication for RLists as they may have null-names which we append
RList l = (RList) t;
if (names==null) {
addAll(l);
return;
}
int n = l.size();
int i = 0;
while (i < n) {
String key = l.keyAt(i);
if (key==null)
add(l.at(i));
else
put(key, l.at(i));
i++;
}
} else {
Set ks = t.keySet();
Iterator i = ks.iterator();
while (i.hasNext()) {
Object key = i.next();
put(key, t.get(key));
}
}
}
public void putAll(RList t) {
if (t == null) return;
RList l = (RList) t;
if (names==null) {
addAll(l);
return;
}
int n = l.size();
int i = 0;
while (i < n) {
String key = l.keyAt(i);
if (key == null)
add(l.at(i));
else
put(key, l.at(i));
i++;
}
}
public Object removeByKey(Object key) {
if (names==null) return null;
int i = names.indexOf(key);
if (i<0) return null;
Object o = elementAt(i);
removeElementAt(i);
names.removeElementAt(i);
return o;
}
public Collection values() {
return this;
}
// other
public String toString() {
return "RList"+super.toString()+"{"+(isNamed()?"named,":"")+size()+"}";
}
}