org.luaj.vm
Class LTable

java.lang.Object
  extended by org.luaj.vm.LValue
      extended by org.luaj.vm.LTable
Direct Known Subclasses:
LWeakTable

public class LTable
extends LValue

Simple implementation of table structure for Lua VM. Maintains both an array part and a hash part. Does not attempt to achieve the same performance as the C version. Java code can put values in the table or get values out (bypassing the metatable, if there is one) using put() and get(). There are specializations of put() and get() for integers and Strings to avoid allocating wrapper objects when possible. remove() methods are private: setting a key's value to nil is the correct way to remove an entry from the table.


Field Summary
protected  java.lang.Object[] array
           
protected  LValue[] hashKeys
           
protected  java.lang.Object[] hashValues
           
 
Fields inherited from class org.luaj.vm.LValue
TM_INDEX, TM_METATABLE, TM_MODE, TM_NEWINDEX
 
Constructor Summary
LTable()
          Construct an empty LTable with no initial capacity.
LTable(int narray, int nhash)
          Construct an empty LTable that is expected to contain entries with keys in the range 1 ..
 
Method Summary
 void arrayExpand(int newLength)
           
 void arrayPresize(int minSize)
           
protected  boolean checkLoadFactor()
           
 boolean containsKey(int key)
          Return true if the table contains an entry with the given integer-valued key, false if not.
 boolean containsKey(LValue key)
          Return true if the table contains an entry with the given key, false if not.
 LValue foreach(LuaState vm, LFunction function, boolean indexedonly)
          Executes the given f over all elements of table.
 LValue get(int key)
          Utility method for retrieving an integer-keyed value
 LValue get(LValue key)
          Utility method to directly get the value in a table, without metatable calls.
 int getArrayCapacity()
          Get capacity of hash part
 int getHashCapacity()
          Get capacity of hash part
 LValue[] getKeys()
          Deprecated. this is not scalable. Does a linear search through the table.
protected  void hashClearSlot(int i)
           
 int hashFindSlot(LValue key)
           
 java.lang.Object hashGet(LValue key)
           
protected  void hashRemove(LValue key)
           
 void hashSet(LValue key, java.lang.Object value)
           
 boolean isTable()
          Return true if this is a LTable
 LTable luaGetMetatable()
          Valid for tables
 int luaGetType()
          Valid for all types: return the int value identifying the type of this value
 void luaInsertPos(int ikey, LValue value)
          Insert element at a position in the list, shifting contiguous elements up.
 int luaLength()
          Try to find a boundary in table `t'.
 LValue luaMaxN()
          Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices.
 LValue luaRemovePos(int ikey)
          Remove an element from the list, moving contiguous elements down
 LTable luaSetMetatable(LValue metatable)
          Valid for tables
 void luaSort(LuaState vm, LValue compare)
           
 boolean next(LuaState vm, LValue key, boolean indexedonly)
          Leave key,value pair on top, or nil if at end of list.
protected  LValue normalizeGet(java.lang.Object val)
          Check for null, and convert to nilor leave alone
protected  java.lang.Object normalizePut(LValue val)
          Check for nil, and convert to null or leave alone
 void put(int key, LValue val)
          Method for putting an integer-keyed value.
 void put(LValue key, LValue val)
          Generic put method for all types of keys, but does not use the metatable.
 void put(java.lang.String key, int val)
          Utility method for putting a string key, int value directly, typically for initializing a table.
 void put(java.lang.String key, LValue val)
          Utility method for putting a string-keyed value directly, typically for initializing a table.
protected  void rehash()
           
 int size()
          Deprecated. this is not scalable. Does a linear search through the table. Use luaLength() instead.
 java.lang.String toJavaString()
          Convert to a Java String
 
Methods inherited from class org.luaj.vm.LValue
compareError, conversionError, id, isClosure, isFunction, isInteger, isNil, isNumber, isString, isUserData, luaAsString, luaBinCmpDouble, luaBinCmpInteger, luaBinCmpString, luaBinCmpUnknown, luaBinOpDouble, luaBinOpInteger, luaBinOpUnknown, luaConcatTo, luaGetEnv, luaGetTable, luaGetTypeName, luaSetEnv, luaSetTable, luaStackCall, luaToNumber, luaUnaryMinus, toJavaBoolean, toJavaBoxedBoolean, toJavaBoxedByte, toJavaBoxedCharacter, toJavaBoxedDouble, toJavaBoxedFloat, toJavaBoxedInteger, toJavaBoxedLong, toJavaBoxedShort, toJavaByte, toJavaChar, toJavaDouble, toJavaFloat, toJavaInstance, toJavaInt, toJavaLong, toJavaShort, toString, toStrongReference
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

array

protected java.lang.Object[] array

hashKeys

protected LValue[] hashKeys

hashValues

protected java.lang.Object[] hashValues
Constructor Detail

LTable

public LTable()
Construct an empty LTable with no initial capacity.


LTable

public LTable(int narray,
              int nhash)
Construct an empty LTable that is expected to contain entries with keys in the range 1 .. narray and nhash non-integer keys.

Method Detail

isTable

public boolean isTable()
Description copied from class: LValue
Return true if this is a LTable

Overrides:
isTable in class LValue

getArrayCapacity

public int getArrayCapacity()
Get capacity of hash part


getHashCapacity

public int getHashCapacity()
Get capacity of hash part


size

public int size()
Deprecated. this is not scalable. Does a linear search through the table. Use luaLength() instead.

Return total number of keys mapped to non-nil values. Not to be confused with luaLength, which returns some number n such that the value at n+1 is nil.


put

public void put(LValue key,
                LValue val)
Generic put method for all types of keys, but does not use the metatable.


put

public void put(int key,
                LValue val)
Method for putting an integer-keyed value. Bypasses the metatable, if any.


put

public void put(java.lang.String key,
                LValue val)
Utility method for putting a string-keyed value directly, typically for initializing a table. Bypasses the metatable, if any.


put

public void put(java.lang.String key,
                int val)
Utility method for putting a string key, int value directly, typically for initializing a table. Bypasses the metatable, if any.


normalizePut

protected java.lang.Object normalizePut(LValue val)
Check for nil, and convert to null or leave alone


get

public LValue get(LValue key)
Utility method to directly get the value in a table, without metatable calls. Must never return null, use LNil.NIL instead.


get

public LValue get(int key)
Utility method for retrieving an integer-keyed value


normalizeGet

protected LValue normalizeGet(java.lang.Object val)
Check for null, and convert to nilor leave alone


containsKey

public boolean containsKey(LValue key)
Return true if the table contains an entry with the given key, false if not. Ignores the metatable.


containsKey

public boolean containsKey(int key)
Return true if the table contains an entry with the given integer-valued key, false if not. Ignores the metatable.


luaLength

public int luaLength()
Try to find a boundary in table `t'. A `boundary' is an integer index such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).

Overrides:
luaLength in class LValue

luaGetMetatable

public LTable luaGetMetatable()
Valid for tables

Overrides:
luaGetMetatable in class LValue
Returns:
null if there is no meta-table

luaSetMetatable

public LTable luaSetMetatable(LValue metatable)
Valid for tables

Overrides:
luaSetMetatable in class LValue
Returns:
this if unchanged, or new LTable if copied using weak table

toJavaString

public java.lang.String toJavaString()
Description copied from class: LValue
Convert to a Java String

Overrides:
toJavaString in class LValue

luaGetType

public int luaGetType()
Description copied from class: LValue
Valid for all types: return the int value identifying the type of this value

Specified by:
luaGetType in class LValue

getKeys

public LValue[] getKeys()
Deprecated. this is not scalable. Does a linear search through the table.

Helper method to get all the keys in this table in an array. Meant to be used instead of keys() (which returns an enumeration) when an array is more convenient. Note that for a very large table, getting an Enumeration instead would be more space efficient.


luaInsertPos

public void luaInsertPos(int ikey,
                         LValue value)
Insert element at a position in the list, shifting contiguous elements up.


luaRemovePos

public LValue luaRemovePos(int ikey)
Remove an element from the list, moving contiguous elements down

Parameters:
pos - position to remove, or 0 to remove last element

luaMaxN

public LValue luaMaxN()
Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)

Returns:
LValue that is the largest int

luaSort

public void luaSort(LuaState vm,
                    LValue compare)

next

public boolean next(LuaState vm,
                    LValue key,
                    boolean indexedonly)
Leave key,value pair on top, or nil if at end of list.

Parameters:
vm - the LuaState to leave the values on
indexedonly - TODO
index - index to start search
Returns:
true if next exists, false if at end of list

foreach

public LValue foreach(LuaState vm,
                      LFunction function,
                      boolean indexedonly)
Executes the given f over all elements of table. For each element, f is called with the index and respective value as arguments. If f returns a non-nil value, then the loop is broken, and this value is returned as the final value of foreach.

Parameters:
vm -
function -
indexedonly - is a table.foreachi() call, not a table.foreach() call
Returns:

arrayExpand

public void arrayExpand(int newLength)

arrayPresize

public void arrayPresize(int minSize)

hashSet

public void hashSet(LValue key,
                    java.lang.Object value)

hashGet

public java.lang.Object hashGet(LValue key)

hashFindSlot

public int hashFindSlot(LValue key)

hashRemove

protected void hashRemove(LValue key)

hashClearSlot

protected void hashClearSlot(int i)

checkLoadFactor

protected boolean checkLoadFactor()

rehash

protected void rehash()


Copyright © 2007-2013 Luaj.org. All Rights Reserved.