Class vector was a mashup between 3D vectors and lua arrays. Fixed that, they're two separate classes now.
This commit is contained in:
@@ -4,20 +4,20 @@
|
||||
#include "table.hpp"
|
||||
#include "source.hpp"
|
||||
|
||||
// A quick check to see if a table appears to be a vector.
|
||||
// Does not thoroughly verify the vector. Returns the size
|
||||
// of the vector.
|
||||
static int check_vector_quick(LuaCoreStack &LS, LuaSlot vector, LuaSlot tmp) {
|
||||
LS.cktable(vector, "vector");
|
||||
int nkeys = LS.nkeys(vector);
|
||||
// A quick check to see if a table appears to be a array.
|
||||
// Does not thoroughly verify the array. Returns the size
|
||||
// of the array.
|
||||
static int check_array_quick(LuaCoreStack &LS, LuaSlot array, LuaSlot tmp) {
|
||||
LS.cktable(array, "array");
|
||||
int nkeys = LS.nkeys(array);
|
||||
if (nkeys > 0) {
|
||||
LS.rawget(tmp, vector, nkeys);
|
||||
LS.rawget(tmp, array, nkeys);
|
||||
if (LS.isnil(tmp)) {
|
||||
luaL_error(LS.state(), "Not a valid vector");
|
||||
luaL_error(LS.state(), "Not a valid array");
|
||||
}
|
||||
LS.rawget(tmp, vector, nkeys + 1);
|
||||
LS.rawget(tmp, array, nkeys + 1);
|
||||
if (!LS.isnil(tmp)) {
|
||||
luaL_error(LS.state(), "Not a valid vector");
|
||||
luaL_error(LS.state(), "Not a valid array");
|
||||
}
|
||||
}
|
||||
return nkeys;
|
||||
@@ -60,15 +60,15 @@ LuaDefine(table_equal, "table1,table2",
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(table_isvector, "table",
|
||||
"|Return true if the table is a valid vector."
|
||||
LuaDefine(table_isarray, "table",
|
||||
"|Return true if the table is a valid array."
|
||||
"|"
|
||||
"|A vector is a table that has keys starting with 1, and no gaps."
|
||||
"|The empty table also counts as a valid vector. This function"
|
||||
"|scans the entire vector to verify its validity, so it takes O(N)"
|
||||
"|time. See also table.isvectorq"
|
||||
"|A array is a table that has keys starting with 1, and no gaps."
|
||||
"|The empty table also counts as a valid array. This function"
|
||||
"|scans the entire array to verify its validity, so it takes O(N)"
|
||||
"|time. See also table.isarrayq"
|
||||
"|"
|
||||
"|The functions vector.isvector and table.isvector are identical."
|
||||
"|The functions array.isarray and table.isarray are identical."
|
||||
"|") {
|
||||
LuaArg table;
|
||||
LuaRet result;
|
||||
@@ -90,17 +90,17 @@ LuaDefine(table_isvector, "table",
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(table_isvectorq, "table",
|
||||
"|Return true if the table is probably a valid vector."
|
||||
LuaDefine(table_isarrayq, "table",
|
||||
"|Return true if the table is probably a valid array."
|
||||
"|"
|
||||
"|A vector is a table that has keys starting with 1, and no gaps."
|
||||
"|The empty table also counts as a valid vector. This function"
|
||||
"|A array is a table that has keys starting with 1, and no gaps."
|
||||
"|The empty table also counts as a valid array. This function"
|
||||
"|does a constant-time heuristic check: it gets the number of keys"
|
||||
"|in the table, NKEYS. Then it verifies that table[NKEYS] is not"
|
||||
"|nil and that table[NKEYS+1] is nil. If these things are both"
|
||||
"|true, the table is very likely a valid vector."
|
||||
"|true, the table is very likely a valid array."
|
||||
"|"
|
||||
"|The functions vector.isvectorq and table.isvectorq are identical."
|
||||
"|The functions array.isarrayq and table.isarrayq are identical."
|
||||
"|") {
|
||||
LuaArg table;
|
||||
LuaRet result;
|
||||
@@ -127,98 +127,98 @@ LuaDefine(table_isvectorq, "table",
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefineAlias(vector_isvector, table_isvector);
|
||||
LuaDefineAlias(array_isarray, table_isarray);
|
||||
|
||||
LuaDefine(vector_removeall, "vector,value",
|
||||
"|Remove all occurrences of value from vector."
|
||||
LuaDefine(array_removeall, "array,value",
|
||||
"|Remove all occurrences of value from array."
|
||||
"|"
|
||||
"|For example, if you remove the number 3 from the vector"
|
||||
"|For example, if you remove the number 3 from the array"
|
||||
"|{1,2,3,4,5,4,3,2,1} you get {1,2,4,5,4,2,1}."
|
||||
"|"
|
||||
"|Returns true if it removed something, false otherwise."
|
||||
"|") {
|
||||
LuaArg vector, value;
|
||||
LuaArg array, value;
|
||||
LuaRet result;
|
||||
LuaVar tmp;
|
||||
LuaDefStack LS(L, vector, value, result, tmp);
|
||||
int nkeys = check_vector_quick(LS, vector, tmp);
|
||||
LuaDefStack LS(L, array, value, result, tmp);
|
||||
int nkeys = check_array_quick(LS, array, tmp);
|
||||
int dest = 1;
|
||||
for (int i = 1; i <= nkeys; i++) {
|
||||
LS.rawget(tmp, vector, i);
|
||||
LS.rawget(tmp, array, i);
|
||||
if (LS.isnil(tmp)) {
|
||||
luaL_error(L, "not a valid vector");
|
||||
luaL_error(L, "not a valid array");
|
||||
return LS.result();
|
||||
}
|
||||
if (!LS.rawequal(tmp, value)) {
|
||||
if (dest < i) {
|
||||
LS.rawset(vector, dest, tmp);
|
||||
LS.rawset(array, dest, tmp);
|
||||
}
|
||||
dest += 1;
|
||||
}
|
||||
}
|
||||
LS.set(result, (dest < nkeys));
|
||||
while (dest <= nkeys) {
|
||||
LS.rawset(vector, dest, LuaNil);
|
||||
LS.rawset(array, dest, LuaNil);
|
||||
dest += 1;
|
||||
}
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(vector_push, "vector,value",
|
||||
"|Push a value onto the end of a vector."
|
||||
LuaDefine(array_push, "array,value",
|
||||
"|Push a value onto the end of a array."
|
||||
"|"
|
||||
"|Argument must be a valid vector. Appends the value to"
|
||||
"|the end of the vector."
|
||||
"|Argument must be a valid array. Appends the value to"
|
||||
"|the end of the array."
|
||||
"|") {
|
||||
LuaArg vector, value;
|
||||
LuaArg array, value;
|
||||
LuaVar tmp;
|
||||
LuaDefStack LS(L, vector, value, tmp);
|
||||
int nkeys = check_vector_quick(LS, vector, tmp);
|
||||
LS.rawset(vector, nkeys + 1, value);
|
||||
LuaDefStack LS(L, array, value, tmp);
|
||||
int nkeys = check_array_quick(LS, array, tmp);
|
||||
LS.rawset(array, nkeys + 1, value);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(vector_pop, "vector,value",
|
||||
"|Pop a value from the end of a vector."
|
||||
LuaDefine(array_pop, "array,value",
|
||||
"|Pop a value from the end of a array."
|
||||
"|"
|
||||
"|Argument must be a valid vector. Returns the last value"
|
||||
"|from the vector. If the vector is empty, returns nil."
|
||||
"|Argument must be a valid array. Returns the last value"
|
||||
"|from the array. If the array is empty, returns nil."
|
||||
"|") {
|
||||
LuaArg vector;
|
||||
LuaArg array;
|
||||
LuaRet value;
|
||||
LuaVar tmp;
|
||||
LuaDefStack LS(L, vector, value, tmp);
|
||||
int nkeys = check_vector_quick(LS, vector, tmp);
|
||||
LuaDefStack LS(L, array, value, tmp);
|
||||
int nkeys = check_array_quick(LS, array, tmp);
|
||||
if (nkeys == 0) {
|
||||
LS.set(value, LuaNil);
|
||||
} else {
|
||||
LS.rawget(value, vector, nkeys);
|
||||
LS.rawset(vector, nkeys, LuaNil);
|
||||
LS.rawget(value, array, nkeys);
|
||||
LS.rawset(array, nkeys, LuaNil);
|
||||
}
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(vector_find, "vector,value",
|
||||
"|Find the first occurence of value in vector."
|
||||
LuaDefine(array_find, "array,value",
|
||||
"|Find the first occurence of value in array."
|
||||
"|"
|
||||
"|Argument must be a valid vector. Returns the index of the"
|
||||
"|first occurrence of value in vector. If the value is not"
|
||||
"|Argument must be a valid array. Returns the index of the"
|
||||
"|first occurrence of value in array. If the value is not"
|
||||
"|found, returns nil."
|
||||
"|"
|
||||
"|Searching for 'nil' in a vector is explicitly disallowed, since"
|
||||
"|a valid vector cannot contain nil."
|
||||
"|Searching for 'nil' in a array is explicitly disallowed, since"
|
||||
"|a valid array cannot contain nil."
|
||||
"|") {
|
||||
LuaArg vector, value;
|
||||
LuaArg array, value;
|
||||
LuaRet index;
|
||||
LuaVar tmp;
|
||||
LuaDefStack LS(L, vector, value, index, tmp);
|
||||
int nkeys = check_vector_quick(LS, vector, tmp);
|
||||
LuaDefStack LS(L, array, value, index, tmp);
|
||||
int nkeys = check_array_quick(LS, array, tmp);
|
||||
if (LS.isnil(value)) {
|
||||
luaL_error(L, "cannot search for nil in a vector");
|
||||
luaL_error(L, "cannot search for nil in a array");
|
||||
return 0;
|
||||
}
|
||||
for (int i = 1; i <= nkeys; i++) {
|
||||
LS.rawget(tmp, vector, i);
|
||||
LS.rawget(tmp, array, i);
|
||||
if (LS.rawequal(tmp, value)) {
|
||||
LS.set(index, i);
|
||||
return LS.result();
|
||||
@@ -705,8 +705,8 @@ bool table_getpairs(LuaCoreStack &LS0, LuaSlot tab, LuaSlot pairs, bool sort) {
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Given a sortedpairs vector, return the (key, value) pairs
|
||||
// one by one. The first element of the vector is used as a
|
||||
// Given a sortedpairs array, return the (key, value) pairs
|
||||
// one by one. The first element of the array is used as a
|
||||
// counter to keep track of our position.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user