comparison src/luan/modules/TableLuan.java @ 1333:25746915a241

merge Luan and LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 22:33:40 -0700
parents f41919741100
children c88b486a9511
comparison
equal deleted inserted replaced
1332:11b7e11f9ed5 1333:25746915a241
3 import java.util.Comparator; 3 import java.util.Comparator;
4 import java.util.List; 4 import java.util.List;
5 import java.util.ArrayList; 5 import java.util.ArrayList;
6 import java.util.Arrays; 6 import java.util.Arrays;
7 import luan.Luan; 7 import luan.Luan;
8 import luan.LuanState;
9 import luan.LuanTable; 8 import luan.LuanTable;
10 import luan.LuanFunction; 9 import luan.LuanFunction;
11 import luan.LuanException; 10 import luan.LuanException;
12 import luan.LuanRuntimeException; 11 import luan.LuanRuntimeException;
13 import luan.LuanMethod; 12 import luan.LuanMethod;
14 13
15 14
16 public final class TableLuan { 15 public final class TableLuan {
17 16
18 public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException { 17 public static String concat(Luan luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException {
19 int first = i==null ? 1 : i; 18 int first = i==null ? 1 : i;
20 int last = j==null ? list.length() : j; 19 int last = j==null ? list.length() : j;
21 StringBuilder buf = new StringBuilder(); 20 StringBuilder buf = new StringBuilder();
22 for( int k=first; k<=last; k++ ) { 21 for( int k=first; k<=last; k++ ) {
23 Object val = list.get(k); 22 Object val = list.get(k);
46 45
47 private static interface LessThan { 46 private static interface LessThan {
48 public boolean isLessThan(Object o1,Object o2); 47 public boolean isLessThan(Object o1,Object o2);
49 } 48 }
50 49
51 public static void sort(final LuanState luan,LuanTable list,final LuanFunction comp) throws LuanException { 50 public static void sort(final Luan luan,LuanTable list,final LuanFunction comp) throws LuanException {
52 if( list.getMetatable() != null ) 51 if( list.getMetatable() != null )
53 throw new LuanException("can't sort a table with a metatable"); 52 throw new LuanException("can't sort a table with a metatable");
54 final LessThan lt; 53 final LessThan lt;
55 if( comp==null ) { 54 if( comp==null ) {
56 lt = new LessThan() { 55 lt = new LessThan() {
82 } catch(LuanRuntimeException e) { 81 } catch(LuanRuntimeException e) {
83 throw (LuanException)e.getCause(); 82 throw (LuanException)e.getCause();
84 } 83 }
85 } 84 }
86 85
87 public static LuanTable pack(LuanState luan,Object... args) throws LuanException { 86 public static LuanTable pack(Luan luan,Object... args) throws LuanException {
88 LuanTable tbl = new LuanTable(luan,Arrays.asList(args)); 87 LuanTable tbl = new LuanTable(luan,Arrays.asList(args));
89 tbl.rawPut( "n", args.length ); 88 tbl.rawPut( "n", args.length );
90 return tbl; 89 return tbl;
91 } 90 }
92 91
104 list.add( tbl.get(i) ); 103 list.add( tbl.get(i) );
105 } 104 }
106 return list.toArray(); 105 return list.toArray();
107 } 106 }
108 107
109 public static LuanTable copy(LuanState luan,LuanTable list,Integer from,Integer to) { 108 public static LuanTable copy(Luan luan,LuanTable list,Integer from,Integer to) {
110 if( from == null ) 109 if( from == null )
111 return new LuanTable(list); 110 return new LuanTable(list);
112 if( to == null ) 111 if( to == null )
113 to = list.rawLength(); 112 to = list.rawLength();
114 return list.rawSubList(from,to); 113 return list.rawSubList(from,to);
128 127
129 public static int size(LuanTable tbl) throws LuanException { 128 public static int size(LuanTable tbl) throws LuanException {
130 return tbl.rawSize(); 129 return tbl.rawSize();
131 } 130 }
132 131
133 public static LuanTable toTable(LuanState luan,Object obj) { 132 public static LuanTable toTable(Luan luan,Object obj) {
134 return luan.toTable(obj); 133 return luan.toTable(obj);
135 } 134 }
136 135
137 } 136 }