Mercurial Hosting > luan
comparison src/luan/modules/TableLuan.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | core/src/luan/modules/TableLuan.java@8e8c30b72e9b |
children | 6b8ea0a9b7c9 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
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.LuanException; | |
12 import luan.LuanRuntimeException; | |
13 import luan.LuanMethod; | |
14 import luan.LuanPropertyMeta; | |
15 | |
16 | |
17 public final class TableLuan { | |
18 | |
19 public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException { | |
20 int first = i==null ? 1 : i; | |
21 int last = j==null ? list.length(luan) : j; | |
22 StringBuilder buf = new StringBuilder(); | |
23 for( int k=first; k<=last; k++ ) { | |
24 Object val = list.get(luan,k); | |
25 if( val==null ) | |
26 break; | |
27 if( sep!=null && k > first ) | |
28 buf.append(sep); | |
29 String s = luan.toString(val); | |
30 buf.append(s); | |
31 } | |
32 return buf.toString(); | |
33 } | |
34 | |
35 public static void insert(LuanTable list,int pos,Object value) throws LuanException { | |
36 Utils.checkNotNull(list); | |
37 if( list.getMetatable() != null ) | |
38 throw new LuanException("can't insert into a table with a metatable"); | |
39 list.rawInsert(pos,value); | |
40 } | |
41 | |
42 public static Object remove(LuanTable list,int pos) throws LuanException { | |
43 if( list.getMetatable() != null ) | |
44 throw new LuanException("can't remove from a table with a metatable"); | |
45 return list.rawRemove(pos); | |
46 } | |
47 | |
48 private static interface LessThan { | |
49 public boolean isLessThan(Object o1,Object o2); | |
50 } | |
51 | |
52 public static void sort(final LuanState luan,LuanTable list,final LuanFunction comp) throws LuanException { | |
53 if( list.getMetatable() != null ) | |
54 throw new LuanException("can't sort a table with a metatable"); | |
55 final LessThan lt; | |
56 if( comp==null ) { | |
57 lt = new LessThan() { | |
58 public boolean isLessThan(Object o1,Object o2) { | |
59 try { | |
60 return Luan.isLessThan(luan,o1,o2); | |
61 } catch(LuanException e) { | |
62 throw new LuanRuntimeException(e); | |
63 } | |
64 } | |
65 }; | |
66 } else { | |
67 lt = new LessThan() { | |
68 public boolean isLessThan(Object o1,Object o2) { | |
69 try { | |
70 return Luan.checkBoolean(Luan.first(comp.call(luan,new Object[]{o1,o2}))); | |
71 } catch(LuanException e) { | |
72 throw new LuanRuntimeException(e); | |
73 } | |
74 } | |
75 }; | |
76 } | |
77 try { | |
78 list.rawSort( new Comparator<Object>() { | |
79 public int compare(Object o1,Object o2) { | |
80 return lt.isLessThan(o1,o2) ? -1 : lt.isLessThan(o2,o1) ? 1 : 0; | |
81 } | |
82 } ); | |
83 } catch(LuanRuntimeException e) { | |
84 throw (LuanException)e.getCause(); | |
85 } | |
86 } | |
87 | |
88 public static LuanTable pack(Object... args) { | |
89 LuanTable tbl = new LuanTable(Arrays.asList(args)); | |
90 tbl.rawPut( "n", args.length ); | |
91 return tbl; | |
92 } | |
93 | |
94 @LuanMethod public static Object[] unpack(LuanState luan,LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException { | |
95 int from = iFrom!=null ? iFrom : 1; | |
96 int to = iTo!=null ? iTo : tbl.length(luan); | |
97 List<Object> list = new ArrayList<Object>(); | |
98 for( int i=from; i<=to; i++ ) { | |
99 list.add( tbl.get(luan,i) ); | |
100 } | |
101 return list.toArray(); | |
102 } | |
103 | |
104 public static LuanTable copy(LuanTable list,Integer from,Integer to) { | |
105 if( from == null ) | |
106 return new LuanTable(list); | |
107 if( to == null ) | |
108 to = list.rawLength(); | |
109 return list.rawSubList(from,to); | |
110 } | |
111 | |
112 public static LuanTable new_property_table() { | |
113 return LuanPropertyMeta.INSTANCE.newTable(); | |
114 } | |
115 | |
116 public static void clear(LuanTable tbl) { | |
117 tbl.rawClear(); | |
118 } | |
119 | |
120 } |