comparison src/luan/modules/TableLuan.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents c88b486a9511
children 225808b90cee
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
7 import luan.Luan; 7 import luan.Luan;
8 import luan.LuanTable; 8 import luan.LuanTable;
9 import luan.LuanFunction; 9 import luan.LuanFunction;
10 import luan.LuanException; 10 import luan.LuanException;
11 import luan.LuanRuntimeException; 11 import luan.LuanRuntimeException;
12 import luan.LuanMethod;
13 12
14 13
15 public final class TableLuan { 14 public final class TableLuan {
16 15
17 public static String concat(LuanTable list,String sep,Integer i,Integer j) throws LuanException { 16 public static String concat(LuanTable list,String sep,Integer i,Integer j) throws LuanException {
45 44
46 private static interface LessThan { 45 private static interface LessThan {
47 public boolean isLessThan(Object o1,Object o2); 46 public boolean isLessThan(Object o1,Object o2);
48 } 47 }
49 48
50 public static void sort(final Luan luan,LuanTable list,final LuanFunction comp) throws LuanException { 49 public static void sort(LuanTable list,final LuanFunction comp) throws LuanException {
51 if( list.getMetatable() != null ) 50 if( list.getMetatable() != null )
52 throw new LuanException("can't sort a table with a metatable"); 51 throw new LuanException("can't sort a table with a metatable");
53 final LessThan lt; 52 final LessThan lt;
54 if( comp==null ) { 53 if( comp==null ) {
55 lt = new LessThan() { 54 lt = new LessThan() {
56 public boolean isLessThan(Object o1,Object o2) { 55 public boolean isLessThan(Object o1,Object o2) {
57 try { 56 try {
58 return luan.isLessThan(o1,o2); 57 return Luan.isLessThan(o1,o2);
59 } catch(LuanException e) { 58 } catch(LuanException e) {
60 throw new LuanRuntimeException(e); 59 throw new LuanRuntimeException(e);
61 } 60 }
62 } 61 }
63 }; 62 };
64 } else { 63 } else {
65 lt = new LessThan() { 64 lt = new LessThan() {
66 public boolean isLessThan(Object o1,Object o2) { 65 public boolean isLessThan(Object o1,Object o2) {
67 try { 66 try {
68 return Luan.checkBoolean(Luan.first(comp.call(luan,new Object[]{o1,o2}))); 67 return Luan.checkBoolean(Luan.first(comp.call(o1,o2)));
69 } catch(LuanException e) { 68 } catch(LuanException e) {
70 throw new LuanRuntimeException(e); 69 throw new LuanRuntimeException(e);
71 } 70 }
72 } 71 }
73 }; 72 };
87 LuanTable tbl = new LuanTable(luan,Arrays.asList(args)); 86 LuanTable tbl = new LuanTable(luan,Arrays.asList(args));
88 tbl.rawPut( "n", args.length ); 87 tbl.rawPut( "n", args.length );
89 return tbl; 88 return tbl;
90 } 89 }
91 90
92 @LuanMethod public static Object[] unpack(LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException { 91 public static Object[] unpack(LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException {
93 int from = iFrom!=null ? iFrom : 1; 92 int from = iFrom!=null ? iFrom : 1;
94 int to; 93 int to;
95 if( iTo != null ) { 94 if( iTo != null ) {
96 to = iTo; 95 to = iTo;
97 } else { 96 } else {
103 list.add( tbl.get(i) ); 102 list.add( tbl.get(i) );
104 } 103 }
105 return list.toArray(); 104 return list.toArray();
106 } 105 }
107 106
108 public static LuanTable copy(Luan luan,LuanTable list,Integer from,Integer to) { 107 public static LuanTable copy(LuanTable list,Integer from,Integer to) {
109 if( from == null ) 108 if( from == null )
110 return new LuanTable(list); 109 return new LuanTable(list);
111 if( to == null ) 110 if( to == null )
112 to = list.rawLength(); 111 to = list.rawLength();
113 return list.rawSubList(from,to); 112 return list.rawSubList(from,to);