Mercurial > projects > dwt2
comparison base/src/java/util/ArrayList.d @ 27:1bf55a6eb092
Renamed java tree to base
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 21 Mar 2009 11:33:57 +0100 |
parents | java/src/java/util/ArrayList.d@9b96950f2c3c |
children | fcf926c91ca4 |
comparison
equal
deleted
inserted
replaced
26:f589fc20a5f9 | 27:1bf55a6eb092 |
---|---|
1 module java.util.ArrayList; | |
2 | |
3 import java.lang.all; | |
4 import java.util.AbstractList; | |
5 import java.util.List; | |
6 import java.util.ListIterator; | |
7 import java.util.Collection; | |
8 import java.util.Iterator; | |
9 | |
10 class ArrayList : AbstractList, List { | |
11 private Object[] data; | |
12 | |
13 this(){ | |
14 } | |
15 this(int size){ | |
16 data.length = size; | |
17 data.length = 0; | |
18 } | |
19 this(Collection col){ | |
20 this(cast(int)(col.size*1.1)); | |
21 addAll(col); | |
22 } | |
23 void add(int index, Object element){ | |
24 data.length = data.length +1; | |
25 System.arraycopy( data, index, data, index+1, data.length - index -1 ); | |
26 data[index] = element; | |
27 } | |
28 bool add(Object o){ | |
29 data ~= o; | |
30 return true; | |
31 } | |
32 public bool add(String o){ | |
33 return add(stringcast(o)); | |
34 } | |
35 bool addAll(Collection c){ | |
36 if( c.size() is 0 ) return false; | |
37 uint idx = data.length; | |
38 data.length = data.length + c.size(); | |
39 foreach( o; c ){ | |
40 data[ idx++ ] = o; | |
41 } | |
42 return true; | |
43 } | |
44 bool addAll(int index, Collection c){ | |
45 implMissing( __FILE__, __LINE__ ); | |
46 return false; | |
47 } | |
48 void clear(){ | |
49 data.length = 0; | |
50 } | |
51 ArrayList clone(){ | |
52 implMissing( __FILE__, __LINE__ ); | |
53 return null; | |
54 } | |
55 bool contains(Object o){ | |
56 foreach( v; data ){ | |
57 if( o is v ){ | |
58 return true; | |
59 } | |
60 if(( o is null ) || ( v is null )){ | |
61 continue; | |
62 } | |
63 if( o == v ){ | |
64 return true; | |
65 } | |
66 } | |
67 return false; | |
68 } | |
69 bool contains(String o){ | |
70 return contains(stringcast(o)); | |
71 } | |
72 bool containsAll(Collection c){ | |
73 implMissing( __FILE__, __LINE__ ); | |
74 return false; | |
75 } | |
76 equals_t opEquals(Object o){ | |
77 if( auto other = cast(ArrayList)o ){ | |
78 if( data.length !is other.data.length ){ | |
79 return false; | |
80 } | |
81 for( int i = 0; i < data.length; i++ ){ | |
82 if( data[i] is other.data[i] ){ | |
83 continue; | |
84 } | |
85 if(( data[i] is null ) || ( other.data[i] is null )){ | |
86 return false; | |
87 } | |
88 if( data[i] == other.data[i] ){ | |
89 continue; | |
90 } | |
91 return false; | |
92 } | |
93 return true; | |
94 } | |
95 return false; | |
96 } | |
97 Object get(int index){ | |
98 return data[index]; | |
99 } | |
100 public override hash_t toHash(){ | |
101 return super.toHash(); | |
102 } | |
103 int indexOf(Object o){ | |
104 foreach( i, v; data ){ | |
105 if( data[i] is o ){ | |
106 return i; | |
107 } | |
108 if(( data[i] is null ) || ( o is null )){ | |
109 continue; | |
110 } | |
111 if( data[i] == o ){ | |
112 return i; | |
113 } | |
114 } | |
115 return -1; | |
116 } | |
117 bool isEmpty(){ | |
118 return data.length is 0; | |
119 } | |
120 class LocalIterator : Iterator{ | |
121 int idx = -1; | |
122 public this(){ | |
123 } | |
124 public bool hasNext(){ | |
125 return idx+1 < data.length; | |
126 } | |
127 public Object next(){ | |
128 idx++; | |
129 Object res = data[idx]; | |
130 return res; | |
131 } | |
132 public void remove(){ | |
133 implMissing( __FILE__, __LINE__ ); | |
134 this.outer.remove(idx); | |
135 idx--; | |
136 } | |
137 } | |
138 | |
139 Iterator iterator(){ | |
140 return new LocalIterator(); | |
141 } | |
142 int lastIndexOf(Object o){ | |
143 foreach_reverse( i, v; data ){ | |
144 if( data[i] is o ){ | |
145 return i; | |
146 } | |
147 if(( data[i] is null ) || ( o is null )){ | |
148 continue; | |
149 } | |
150 if( data[i] == o ){ | |
151 return i; | |
152 } | |
153 } | |
154 return -1; | |
155 } | |
156 | |
157 class LocalListIterator : ListIterator { | |
158 int idx_next = 0; | |
159 public bool hasNext(){ | |
160 return idx_next < data.length; | |
161 } | |
162 public Object next(){ | |
163 Object res = data[idx_next]; | |
164 idx_next++; | |
165 return res; | |
166 } | |
167 public void remove(){ | |
168 implMissing( __FILE__, __LINE__ ); | |
169 this.outer.remove(idx_next); | |
170 idx_next--; | |
171 } | |
172 public void add(Object o){ | |
173 implMissing( __FILE__, __LINE__ ); | |
174 } | |
175 public bool add(String o){ | |
176 implMissing( __FILE__, __LINE__ ); | |
177 return false; | |
178 } | |
179 public bool hasPrevious(){ | |
180 return idx_next > 0; | |
181 } | |
182 public int nextIndex(){ | |
183 return idx_next; | |
184 } | |
185 public Object previous(){ | |
186 idx_next--; | |
187 Object res = data[idx_next]; | |
188 return res; | |
189 } | |
190 public int previousIndex(){ | |
191 return idx_next-1; | |
192 } | |
193 public void set(Object o){ | |
194 implMissing( __FILE__, __LINE__ ); | |
195 } | |
196 } | |
197 | |
198 ListIterator listIterator(){ | |
199 return new LocalListIterator(); | |
200 } | |
201 ListIterator listIterator(int index){ | |
202 implMissing( __FILE__, __LINE__ ); | |
203 return null; | |
204 } | |
205 Object remove(int index){ | |
206 Object res = data[index]; | |
207 System.arraycopy( data, index+1, data, index, data.length - index - 1 ); | |
208 data.length = data.length -1; | |
209 return res; | |
210 } | |
211 bool remove(Object o){ | |
212 int idx = -1; | |
213 for( int i = 0; i < data.length; i++ ){ | |
214 if( data[i] is null ? o is null : data[i] == o ){ | |
215 idx = i; | |
216 break; | |
217 } | |
218 } | |
219 if( idx is -1 ){ | |
220 return false; | |
221 } | |
222 for( int i = idx + 1; i < data.length; i++ ){ | |
223 data[i-1] = data[i]; | |
224 } | |
225 data.length = data.length - 1; | |
226 return true; | |
227 } | |
228 public bool remove(String key){ | |
229 return remove(stringcast(key)); | |
230 } | |
231 bool removeAll(Collection c){ | |
232 implMissing( __FILE__, __LINE__ ); | |
233 return false; | |
234 } | |
235 bool retainAll(Collection c){ | |
236 implMissing( __FILE__, __LINE__ ); | |
237 return false; | |
238 } | |
239 protected void removeRange(int fromIndex, int toIndex){ | |
240 implMissing( __FILE__, __LINE__ ); | |
241 } | |
242 Object set(int index, Object element){ | |
243 Object res = data[index]; | |
244 data[index] = element; | |
245 return res; | |
246 } | |
247 int size(){ | |
248 return data.length; | |
249 } | |
250 List subList(int fromIndex, int toIndex){ | |
251 implMissing( __FILE__, __LINE__ ); | |
252 return null; | |
253 } | |
254 Object[] toArray(){ | |
255 return data.dup; | |
256 } | |
257 Object[] toArray(Object[] a){ | |
258 if( data.length <= a.length ){ | |
259 a[ 0 .. data.length ] = data; | |
260 } | |
261 else{ | |
262 return data.dup; | |
263 } | |
264 if( data.length < a.length ){ | |
265 a[data.length] = null; | |
266 } | |
267 return a; | |
268 } | |
269 | |
270 // only for D | |
271 public int opApply (int delegate(ref Object value) dg){ | |
272 foreach( o; data ){ | |
273 auto res = dg( o ); | |
274 if( res ) return res; | |
275 } | |
276 return 0; | |
277 } | |
278 } | |
279 |