Mercurial Hosting > luan
view core/src/luan/modules/TableLuan.java @ 553:b1256e2d19a3
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 18 Jun 2015 03:14:07 -0600 |
parents | bf5e62a9090c |
children | 22bfd8a2eaee |
line wrap: on
line source
package luan.modules; import java.util.Comparator; import java.util.List; import java.util.ArrayList; import java.util.Arrays; import luan.Luan; import luan.LuanState; import luan.LuanTable; import luan.LuanFunction; import luan.LuanException; import luan.LuanRuntimeException; import luan.LuanMethod; import luan.LuanPropertyMeta; public final class TableLuan { public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException { int first = i==null ? 1 : i; int last = j==null ? list.length(luan) : j; StringBuilder buf = new StringBuilder(); for( int k=first; k<=last; k++ ) { Object val = list.get(luan,k); if( val==null ) break; if( sep!=null && k > first ) buf.append(sep); String s = luan.toString(val); buf.append(s); } return buf.toString(); } public static void insert(LuanState luan,LuanTable list,int pos,Object value) throws LuanException { if( list.getMetatable() != null ) throw luan.exception("can't insert into a table with a metatable"); list.rawInsert(pos,value); } public static Object remove(LuanState luan,LuanTable list,int pos) throws LuanException { if( list.getMetatable() != null ) throw luan.exception("can't remove from a table with a metatable"); return list.rawRemove(pos); } private static interface LessThan { public boolean isLessThan(Object o1,Object o2); } public static void sort(final LuanState luan,LuanTable list,final LuanFunction comp) throws LuanException { if( list.getMetatable() != null ) throw luan.exception("can't sort a table with a metatable"); final LessThan lt; if( comp==null ) { lt = new LessThan() { public boolean isLessThan(Object o1,Object o2) { try { return luan.isLessThan(o1,o2); } catch(LuanException e) { throw new LuanRuntimeException(e); } } }; } else { lt = new LessThan() { public boolean isLessThan(Object o1,Object o2) { try { return luan.checkBoolean(Luan.first(luan.call(comp,"comp-arg",new Object[]{o1,o2}))); } catch(LuanException e) { throw new LuanRuntimeException(e); } } }; } try { list.rawSort( new Comparator<Object>() { public int compare(Object o1,Object o2) { return lt.isLessThan(o1,o2) ? -1 : lt.isLessThan(o2,o1) ? 1 : 0; } } ); } catch(LuanRuntimeException e) { throw (LuanException)e.getCause(); } } public static LuanTable pack(Object... args) { LuanTable tbl = new LuanTable(Arrays.asList(args)); tbl.rawPut( "n", args.length ); return tbl; } @LuanMethod public static Object[] unpack(LuanState luan,LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException { int from = iFrom!=null ? iFrom : 1; int to = iTo!=null ? iTo : tbl.length(luan); List<Object> list = new ArrayList<Object>(); for( int i=from; i<=to; i++ ) { list.add( tbl.get(luan,i) ); } return list.toArray(); } public static LuanTable sub_list(LuanState luan,LuanTable list,int from,int to) throws LuanException { if( list.getMetatable() != null ) throw luan.exception("can't sub_list a table with a metatable"); return list.rawSubList(from,to); } public static LuanTable clone(LuanTable tbl) { return new LuanTable(tbl); } public static LuanTable new_property_table() { return LuanPropertyMeta.INSTANCE.newTable(); } }