changeset 431:3ffe8ba5b297

TableLuan cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2015 21:12:48 -0600
parents f28320fd671d
children d9df6d6cb927
files core/src/luan/LuanJavaFunction.java core/src/luan/LuanTable.java core/src/luan/modules/IoLuan.java core/src/luan/modules/TableLuan.java
diffstat 4 files changed, 26 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
diff -r f28320fd671d -r 3ffe8ba5b297 core/src/luan/LuanJavaFunction.java
--- a/core/src/luan/LuanJavaFunction.java	Sat May 02 20:35:26 2015 -0600
+++ b/core/src/luan/LuanJavaFunction.java	Sat May 02 21:12:48 2015 -0600
@@ -402,12 +402,24 @@
 			if( obj instanceof Map ) {
 				@SuppressWarnings("unchecked")
 				Map<Object,Object> map = (Map<Object,Object>)obj;
-				return new LuanTable(map);
+				LuanTable tbl = new LuanTable();
+				for( Map.Entry<Object,Object> entry : map.entrySet() ) {
+					Object key = entry.getKey();
+					Object value = entry.getValue();
+					if( key != null && value != null )
+						tbl.rawPut(key,value);
+				}
+				return tbl;
 			}
 			if( obj instanceof Set ) {
 				@SuppressWarnings("unchecked")
 				Set<Object> set = (Set<Object>)obj;
-				return new LuanTable(set);
+				LuanTable tbl = new LuanTable();
+				for( Object el : set ) {
+					if( el != null )
+						tbl.rawPut(el,Boolean.TRUE);
+				}
+				return tbl;
 			}
 			Class cls = obj.getClass();
 			if( cls.isArray() && !cls.getComponentType().isPrimitive() ) {
diff -r f28320fd671d -r 3ffe8ba5b297 core/src/luan/LuanTable.java
--- a/core/src/luan/LuanTable.java	Sat May 02 20:35:26 2015 -0600
+++ b/core/src/luan/LuanTable.java	Sat May 02 21:12:48 2015 -0600
@@ -35,23 +35,6 @@
 		}
 	}
 
-	LuanTable(Map<Object,Object> map) {
-		map.remove(null);
-		for( Iterator<Object> i=map.values().iterator(); i.hasNext(); ) {
-			if( i.next() == null )
-				i.remove();
-		}
-		this.map = map;
-	}
-
-	LuanTable(Set<Object> set) {
-		map = newMap();
-		for( Object obj : set ) {
-			if( obj != null )
-				map.put(obj,Boolean.TRUE);
-		}
-	}
-
 	public LuanTable(LuanTable tbl) {
 		if( tbl.map != null && !tbl.map.isEmpty() )
 			this.map = new LinkedHashMap<Object,Object>(tbl.map);
@@ -233,25 +216,25 @@
 		return list;
 	}
 
-	public void insert(int pos,Object value) {
+	public void rawInsert(int pos,Object value) {
 		if( value==null )
 			throw new IllegalArgumentException("can't insert a nil value");
 		list().add(pos-1,value);
 		mapToList();
 	}
 
-	public void add(Object value) {
+	public void rawAdd(Object value) {
 		if( value==null )
 			throw new IllegalArgumentException("can't add a nil value");
 		list().add(value);
 		mapToList();
 	}
 
-	public Object remove(int pos) {
+	public Object rawRemove(int pos) {
 		return list().remove(pos-1);
 	}
 
-	public void sort(Comparator<Object> cmp) {
+	public void rawSort(Comparator<Object> cmp) {
 		Collections.sort(list(),cmp);
 	}
 
@@ -306,7 +289,7 @@
 		};
 	}
 
-	public LuanTable subList(int from,int to) {
+	public LuanTable rawSubList(int from,int to) {
 		LuanTable tbl = shallowClone();
 		tbl.list = new ArrayList<Object>(list().subList(from-1,to-1));
 		return tbl;
@@ -336,14 +319,10 @@
 
 	// from AbstractLuanTable
 
-	protected final Map<Object,Object> newMap() {
+	private Map<Object,Object> newMap() {
 		return new LinkedHashMap<Object,Object>();
 	}
 
-	public boolean isEmpty() {
-		return isList() && length()==0;
-	}
-
 	public boolean isSet() {
 		for( Map.Entry<Object,Object> entry : this ) {
 			if( !entry.getValue().equals(Boolean.TRUE) )
diff -r f28320fd671d -r 3ffe8ba5b297 core/src/luan/modules/IoLuan.java
--- a/core/src/luan/modules/IoLuan.java	Sat May 02 20:35:26 2015 -0600
+++ b/core/src/luan/modules/IoLuan.java	Sat May 02 21:12:48 2015 -0600
@@ -417,7 +417,7 @@
 				return null;
 			LuanTable list = new LuanTable();
 			for( File f : files ) {
-				list.add(new LuanFile(luan,f).table());
+				list.rawAdd(new LuanFile(luan,f).table());
 			}
 			return list;
 		}
diff -r f28320fd671d -r 3ffe8ba5b297 core/src/luan/modules/TableLuan.java
--- a/core/src/luan/modules/TableLuan.java	Sat May 02 20:35:26 2015 -0600
+++ b/core/src/luan/modules/TableLuan.java	Sat May 02 21:12:48 2015 -0600
@@ -35,11 +35,11 @@
 	}
 
 	public static void insert(LuanTable list,int pos,Object value){
-		list.insert(pos,value);
+		list.rawInsert(pos,value);
 	}
 
 	public static Object remove(LuanTable list,int pos) {
-		return list.remove(pos);
+		return list.rawRemove(pos);
 	}
 
 	private static interface LessThan {
@@ -70,7 +70,7 @@
 			};
 		}
 		try {
-			list.sort( new Comparator<Object>() {
+			list.rawSort( new Comparator<Object>() {
 				public int compare(Object o1,Object o2) {
 					return lt.isLessThan(o1,o2) ? -1 : lt.isLessThan(o2,o1) ? 1 : 0;
 				}
@@ -88,7 +88,7 @@
 			if( v==null ) {
 				hasNull = true;
 			} else if( !hasNull ) {
-				tbl.add(v);
+				tbl.rawAdd(v);
 			} else {
 				tbl.rawPut(i+1,v);
 			}
@@ -108,7 +108,7 @@
 	}
 
 	public static LuanTable sub_list(LuanTable list,int from,int to) {
-		return list.subList(from,to);
+		return list.rawSubList(from,to);
 	}
 
 	public static LuanTable clone(LuanTable tbl) {