comparison src/luan/modules/TableLib.java @ 167:4c0131c2b650

merge luan/lib into modules git-svn-id: https://luan-java.googlecode.com/svn/trunk@168 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:28:32 +0000
parents src/luan/lib/TableLib.java@05f8c21160ef
children
comparison
equal deleted inserted replaced
166:4eaee12f6c65 167:4c0131c2b650
1 package luan.modules;
2
3 import java.util.Comparator;
4 import java.util.List;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import luan.Luan;
8 import luan.LuanState;
9 import luan.LuanTable;
10 import luan.LuanFunction;
11 import luan.LuanJavaFunction;
12 import luan.LuanElement;
13 import luan.LuanException;
14 import luan.LuanRuntimeException;
15
16
17 public final class TableLib {
18
19 public static final LuanFunction LOADER = new LuanFunction() {
20 @Override public Object call(LuanState luan,Object[] args) {
21 LuanTable module = new LuanTable();
22 try {
23 add( module, "concat", LuanState.class, LuanTable.class, String.class, Integer.class, Integer.class );
24 add( module, "insert", LuanState.class, LuanTable.class, Integer.TYPE, Object.class );
25 add( module, "pack", new Object[0].getClass() );
26 add( module, "remove", LuanState.class, LuanTable.class, Integer.TYPE );
27 add( module, "sort", LuanState.class, LuanTable.class, LuanFunction.class );
28 add( module, "sub_list", LuanTable.class, Integer.TYPE, Integer.TYPE );
29 add( module, "unpack", LuanTable.class, Integer.class, Integer.class );
30 } catch(NoSuchMethodException e) {
31 throw new RuntimeException(e);
32 }
33 return module;
34 }
35 };
36
37 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
38 t.put( method, new LuanJavaFunction(TableLib.class.getMethod(method,parameterTypes),null) );
39 }
40
41 public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException {
42 int first = i==null ? 1 : i;
43 int last = j==null ? list.length() : j;
44 StringBuilder buf = new StringBuilder();
45 for( int k=first; k<=last; k++ ) {
46 Object val = list.get(k);
47 if( val==null )
48 break;
49 if( sep!=null && k > first )
50 buf.append(sep);
51 String s = Luan.asString(val);
52 if( s==null )
53 throw luan.exception( "invalid value ("+Luan.type(val)+") at index "+k+" in table for 'concat'" );
54 buf.append(val);
55 }
56 return buf.toString();
57 }
58
59 public static void insert(LuanState luan,LuanTable list,int pos,Object value) throws LuanException {
60 try {
61 list.insert(pos,value);
62 } catch(IndexOutOfBoundsException e) {
63 throw luan.exception(e);
64 }
65 }
66
67 public static Object remove(LuanState luan,LuanTable list,int pos) throws LuanException {
68 try {
69 return list.remove(pos);
70 } catch(IndexOutOfBoundsException e) {
71 throw luan.exception(e);
72 }
73 }
74
75 private static interface LessThan {
76 public boolean isLessThan(Object o1,Object o2);
77 }
78
79 public static void sort(final LuanState luan,LuanTable list,final LuanFunction comp) throws LuanException {
80 final LessThan lt;
81 if( comp==null ) {
82 lt = new LessThan() {
83 public boolean isLessThan(Object o1,Object o2) {
84 try {
85 return luan.isLessThan(o1,o2);
86 } catch(LuanException e) {
87 throw new LuanRuntimeException(e);
88 }
89 }
90 };
91 } else {
92 lt = new LessThan() {
93 public boolean isLessThan(Object o1,Object o2) {
94 try {
95 return Luan.toBoolean(Luan.first(luan.call(comp,"comp-arg",new Object[]{o1,o2})));
96 } catch(LuanException e) {
97 throw new LuanRuntimeException(e);
98 }
99 }
100 };
101 }
102 try {
103 list.sort( new Comparator<Object>() {
104 public int compare(Object o1,Object o2) {
105 return lt.isLessThan(o1,o2) ? -1 : lt.isLessThan(o2,o1) ? 1 : 0;
106 }
107 } );
108 } catch(LuanRuntimeException e) {
109 throw (LuanException)e.getCause();
110 }
111 }
112
113 public static LuanTable pack(Object... args) {
114 return new LuanTable(new ArrayList<Object>(Arrays.asList(args)));
115 }
116
117 public static Object[] unpack(LuanTable tbl,Integer iFrom,Integer iTo) {
118 int from = iFrom!=null ? iFrom : 1;
119 int to = iTo!=null ? iTo : tbl.length();
120 List<Object> list = new ArrayList<Object>();
121 for( int i=from; i<=to; i++ ) {
122 list.add( tbl.get(i) );
123 }
124 return list.toArray();
125 }
126
127 public static LuanTable sub_list(LuanTable list,int from,int to) {
128 return list.subList(from,to);
129 }
130
131 }