comparison core/src/luan/LuanTableImpl.java @ 284:8870840251ea

use LinkedHashMap in LuanTable to preserve order git-svn-id: https://luan-java.googlecode.com/svn/trunk@285 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 02 Dec 2014 03:55:33 +0000
parents ef39bc4d3f70
children 9fb523472035
comparison
equal deleted inserted replaced
283:b669cdaf54b7 284:8870840251ea
2 2
3 import java.util.Iterator; 3 import java.util.Iterator;
4 import java.util.ListIterator; 4 import java.util.ListIterator;
5 import java.util.Map; 5 import java.util.Map;
6 import java.util.AbstractMap; 6 import java.util.AbstractMap;
7 import java.util.HashMap;
8 import java.util.List; 7 import java.util.List;
9 import java.util.ArrayList; 8 import java.util.ArrayList;
10 import java.util.Collections; 9 import java.util.Collections;
11 import java.util.Comparator; 10 import java.util.Comparator;
12 import java.util.Set; 11 import java.util.Set;
22 21
23 public LuanTableImpl() {} 22 public LuanTableImpl() {}
24 /* 23 /*
25 public LuanTableImpl(LuanTableImpl tbl) { 24 public LuanTableImpl(LuanTableImpl tbl) {
26 if( tbl.map != null ) 25 if( tbl.map != null )
27 this.map = new HashMap<Object,Object>(tbl.map); 26 this.map = newMap(tbl.map);
28 if( tbl.list != null ) 27 if( tbl.list != null )
29 this.list = new ArrayList<Object>(tbl.list); 28 this.list = new ArrayList<Object>(tbl.list);
30 } 29 }
31 */ 30 */
32 LuanTableImpl(List<Object> list) { 31 LuanTableImpl(List<Object> list) {
33 this.list = list; 32 this.list = list;
34 this.map = new HashMap<Object,Object>(); 33 this.map = newMap();
35 map.put("n",list.size()); 34 map.put("n",list.size());
36 for( int i=0; i<list.size(); i++ ) { 35 for( int i=0; i<list.size(); i++ ) {
37 if( list.get(i) == null ) { 36 if( list.get(i) == null ) {
38 listToMap(i); 37 listToMap(i);
39 break; 38 break;
49 } 48 }
50 this.map = map; 49 this.map = map;
51 } 50 }
52 51
53 LuanTableImpl(Set<Object> set) { 52 LuanTableImpl(Set<Object> set) {
54 map = new HashMap<Object,Object>(); 53 map = newMap();
55 for( Object obj : set ) { 54 for( Object obj : set ) {
56 if( obj != null ) 55 if( obj != null )
57 map.put(obj,Boolean.TRUE); 56 map.put(obj,Boolean.TRUE);
58 } 57 }
59 } 58 }
62 return new LuanTableImpl(); 61 return new LuanTableImpl();
63 } 62 }
64 63
65 @Override public void deepenClone(LuanTableImpl clone,DeepCloner cloner) { 64 @Override public void deepenClone(LuanTableImpl clone,DeepCloner cloner) {
66 if( map != null ) { 65 if( map != null ) {
67 clone.map = new HashMap<Object,Object>(); 66 clone.map = newMap();
68 for( Map.Entry<Object,Object> entry : map.entrySet() ) { 67 for( Map.Entry<Object,Object> entry : map.entrySet() ) {
69 clone.map.put( cloner.get(entry.getKey()), cloner.get(entry.getValue()) ); 68 clone.map.put( cloner.get(entry.getKey()), cloner.get(entry.getValue()) );
70 } 69 }
71 } 70 }
72 if( list != null ) { 71 if( list != null ) {
88 } 87 }
89 88
90 @Override public Map<Object,Object> asMap() { 89 @Override public Map<Object,Object> asMap() {
91 if( list == null || list.isEmpty() ) 90 if( list == null || list.isEmpty() )
92 return map!=null ? map : Collections.emptyMap(); 91 return map!=null ? map : Collections.emptyMap();
93 Map<Object,Object> rtn = map!=null ? new HashMap<Object,Object>(map) : new HashMap<Object,Object>(); 92 Map<Object,Object> rtn = map!=null ? newMap(map) : newMap();
94 for( ListIterator iter = list.listIterator(); iter.hasNext(); ) { 93 for( ListIterator iter = list.listIterator(); iter.hasNext(); ) {
95 int i = iter.nextIndex(); 94 int i = iter.nextIndex();
96 rtn.put(i+1,iter.next()); 95 rtn.put(i+1,iter.next());
97 } 96 }
98 return rtn; 97 return rtn;
220 return; 219 return;
221 } 220 }
222 } 221 }
223 } 222 }
224 if( map==null ) { 223 if( map==null ) {
225 map = new HashMap<Object,Object>(); 224 map = newMap();
226 } 225 }
227 if( key instanceof Number && !(key instanceof Double) ) { 226 if( key instanceof Number && !(key instanceof Double) ) {
228 Number n = (Number)key; 227 Number n = (Number)key;
229 key = Double.valueOf(n.doubleValue()); 228 key = Double.valueOf(n.doubleValue());
230 } 229 }
251 while( list.size() > from ) { 250 while( list.size() > from ) {
252 int i = list.size() - 1; 251 int i = list.size() - 1;
253 Object v = list.remove(i); 252 Object v = list.remove(i);
254 if( v != null ) { 253 if( v != null ) {
255 if( map==null ) 254 if( map==null )
256 map = new HashMap<Object,Object>(); 255 map = newMap();
257 map.put(i+1,v); 256 map.put(i+1,v);
258 } 257 }
259 } 258 }
260 } 259 }
261 } 260 }