comparison src/luan/LuanTable.java @ 118:735708619119

add Debug.debug() git-svn-id: https://luan-java.googlecode.com/svn/trunk@119 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 01 Jun 2014 07:07:31 +0000
parents 3c404a296995
children cc3a0578edac
comparison
equal deleted inserted replaced
117:e935581cf9fb 118:735708619119
21 21
22 public LuanTable() {} 22 public LuanTable() {}
23 23
24 public LuanTable(List<Object> list) { 24 public LuanTable(List<Object> list) {
25 this.list = list; 25 this.list = list;
26 this.map = new HashMap<Object,Object>();
27 map.put("n",list.size());
28 for( int i=0; i<list.size(); i++ ) {
29 if( list.get(i) == null ) {
30 listToMap(i);
31 break;
32 }
33 }
26 } 34 }
27 35
28 public LuanTable(Map<Object,Object> map) { 36 public LuanTable(Map<Object,Object> map) {
37 map.remove(null);
38 for( Iterator<Object> i=map.values().iterator(); i.hasNext(); ) {
39 if( i.next() == null )
40 i.remove();
41 }
29 this.map = map; 42 this.map = map;
30 } 43 }
31 44
32 public LuanTable(Set<Object> set) { 45 public LuanTable(Set<Object> set) {
33 map = new HashMap<Object,Object>(); 46 map = new HashMap<Object,Object>();
34 for( Object obj : set ) { 47 for( Object obj : set ) {
35 map.put(obj,Boolean.TRUE); 48 if( obj != null )
49 map.put(obj,Boolean.TRUE);
36 } 50 }
37 } 51 }
38 52
39 @Override public LuanTable shallowClone() { 53 @Override public LuanTable shallowClone() {
40 return new LuanTable(); 54 return new LuanTable();
198 } 212 }
199 return null; 213 return null;
200 } else if( i>=0 && i<list.size() ) { 214 } else if( i>=0 && i<list.size() ) {
201 Object old = list.get(i); 215 Object old = list.get(i);
202 list.set(i,val); 216 list.set(i,val);
203 if( val == null && i == list.size()-1 ) { 217 if( val == null ) {
204 while( i>=0 && list.get(i)==null ) { 218 listToMap(i);
205 list.remove(i--);
206 }
207 } 219 }
208 return old; 220 return old;
209 } 221 }
210 } 222 }
211 } 223 }
228 while(true) { 240 while(true) {
229 Object v = map.remove(Double.valueOf(list.size()+1)); 241 Object v = map.remove(Double.valueOf(list.size()+1));
230 if( v == null ) 242 if( v == null )
231 break; 243 break;
232 list.add(v); 244 list.add(v);
245 }
246 }
247 }
248
249 private void listToMap(int from) {
250 if( list != null ) {
251 while( list.size() > from ) {
252 int i = list.size() - 1;
253 Object v = list.remove(i);
254 if( v != null ) {
255 if( map==null )
256 map = new HashMap<Object,Object>();
257 map.put(i+1,v);
258 }
233 } 259 }
234 } 260 }
235 } 261 }
236 262
237 private List<Object> list() { 263 private List<Object> list() {
312 public void remove() { 338 public void remove() {
313 throw new UnsupportedOperationException(); 339 throw new UnsupportedOperationException();
314 } 340 }
315 }; 341 };
316 } 342 }
317 343 /*
318 public Object[] listToArray() { 344 public Object[] listToArray() {
319 return list==null ? new Object[0] : list.toArray(); 345 return list==null ? new Object[0] : list.toArray();
320 } 346 }
321 347 */
322 public LuanTable subList(int from,int to) { 348 public LuanTable subList(int from,int to) {
323 return new LuanTable(new ArrayList<Object>(list().subList(from-1,to-1))); 349 return new LuanTable(new ArrayList<Object>(list().subList(from-1,to-1)));
324 } 350 }
325 351
326 public LuanTable getMetatable() { 352 public LuanTable getMetatable() {