Class vector was a mashup between 3D vectors and lua arrays. Fixed that, they're two separate classes now.

This commit is contained in:
2025-03-03 17:18:01 -05:00
parent 551b3fb6b1
commit c9fd224a38

View File

@@ -4,20 +4,20 @@
#include "table.hpp" #include "table.hpp"
#include "source.hpp" #include "source.hpp"
// A quick check to see if a table appears to be a vector. // A quick check to see if a table appears to be a array.
// Does not thoroughly verify the vector. Returns the size // Does not thoroughly verify the array. Returns the size
// of the vector. // of the array.
static int check_vector_quick(LuaCoreStack &LS, LuaSlot vector, LuaSlot tmp) { static int check_array_quick(LuaCoreStack &LS, LuaSlot array, LuaSlot tmp) {
LS.cktable(vector, "vector"); LS.cktable(array, "array");
int nkeys = LS.nkeys(vector); int nkeys = LS.nkeys(array);
if (nkeys > 0) { if (nkeys > 0) {
LS.rawget(tmp, vector, nkeys); LS.rawget(tmp, array, nkeys);
if (LS.isnil(tmp)) { 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)) { if (!LS.isnil(tmp)) {
luaL_error(LS.state(), "Not a valid vector"); luaL_error(LS.state(), "Not a valid array");
} }
} }
return nkeys; return nkeys;
@@ -60,15 +60,15 @@ LuaDefine(table_equal, "table1,table2",
return LS.result(); return LS.result();
} }
LuaDefine(table_isvector, "table", LuaDefine(table_isarray, "table",
"|Return true if the table is a valid vector." "|Return true if the table is a valid array."
"|" "|"
"|A vector is a table that has keys starting with 1, and no gaps." "|A array is a table that has keys starting with 1, and no gaps."
"|The empty table also counts as a valid vector. This function" "|The empty table also counts as a valid array. This function"
"|scans the entire vector to verify its validity, so it takes O(N)" "|scans the entire array to verify its validity, so it takes O(N)"
"|time. See also table.isvectorq" "|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; LuaArg table;
LuaRet result; LuaRet result;
@@ -90,17 +90,17 @@ LuaDefine(table_isvector, "table",
return LS.result(); return LS.result();
} }
LuaDefine(table_isvectorq, "table", LuaDefine(table_isarrayq, "table",
"|Return true if the table is probably a valid vector." "|Return true if the table is probably a valid array."
"|" "|"
"|A vector is a table that has keys starting with 1, and no gaps." "|A array is a table that has keys starting with 1, and no gaps."
"|The empty table also counts as a valid vector. This function" "|The empty table also counts as a valid array. This function"
"|does a constant-time heuristic check: it gets the number of keys" "|does a constant-time heuristic check: it gets the number of keys"
"|in the table, NKEYS. Then it verifies that table[NKEYS] is not" "|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" "|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; LuaArg table;
LuaRet result; LuaRet result;
@@ -127,98 +127,98 @@ LuaDefine(table_isvectorq, "table",
return LS.result(); return LS.result();
} }
LuaDefineAlias(vector_isvector, table_isvector); LuaDefineAlias(array_isarray, table_isarray);
LuaDefine(vector_removeall, "vector,value", LuaDefine(array_removeall, "array,value",
"|Remove all occurrences of value from vector." "|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}." "|{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." "|Returns true if it removed something, false otherwise."
"|") { "|") {
LuaArg vector, value; LuaArg array, value;
LuaRet result; LuaRet result;
LuaVar tmp; LuaVar tmp;
LuaDefStack LS(L, vector, value, result, tmp); LuaDefStack LS(L, array, value, result, tmp);
int nkeys = check_vector_quick(LS, vector, tmp); int nkeys = check_array_quick(LS, array, tmp);
int dest = 1; int dest = 1;
for (int i = 1; i <= nkeys; i++) { for (int i = 1; i <= nkeys; i++) {
LS.rawget(tmp, vector, i); LS.rawget(tmp, array, i);
if (LS.isnil(tmp)) { if (LS.isnil(tmp)) {
luaL_error(L, "not a valid vector"); luaL_error(L, "not a valid array");
return LS.result(); return LS.result();
} }
if (!LS.rawequal(tmp, value)) { if (!LS.rawequal(tmp, value)) {
if (dest < i) { if (dest < i) {
LS.rawset(vector, dest, tmp); LS.rawset(array, dest, tmp);
} }
dest += 1; dest += 1;
} }
} }
LS.set(result, (dest < nkeys)); LS.set(result, (dest < nkeys));
while (dest <= nkeys) { while (dest <= nkeys) {
LS.rawset(vector, dest, LuaNil); LS.rawset(array, dest, LuaNil);
dest += 1; dest += 1;
} }
return LS.result(); return LS.result();
} }
LuaDefine(vector_push, "vector,value", LuaDefine(array_push, "array,value",
"|Push a value onto the end of a vector." "|Push a value onto the end of a array."
"|" "|"
"|Argument must be a valid vector. Appends the value to" "|Argument must be a valid array. Appends the value to"
"|the end of the vector." "|the end of the array."
"|") { "|") {
LuaArg vector, value; LuaArg array, value;
LuaVar tmp; LuaVar tmp;
LuaDefStack LS(L, vector, value, tmp); LuaDefStack LS(L, array, value, tmp);
int nkeys = check_vector_quick(LS, vector, tmp); int nkeys = check_array_quick(LS, array, tmp);
LS.rawset(vector, nkeys + 1, value); LS.rawset(array, nkeys + 1, value);
return LS.result(); return LS.result();
} }
LuaDefine(vector_pop, "vector,value", LuaDefine(array_pop, "array,value",
"|Pop a value from the end of a vector." "|Pop a value from the end of a array."
"|" "|"
"|Argument must be a valid vector. Returns the last value" "|Argument must be a valid array. Returns the last value"
"|from the vector. If the vector is empty, returns nil." "|from the array. If the array is empty, returns nil."
"|") { "|") {
LuaArg vector; LuaArg array;
LuaRet value; LuaRet value;
LuaVar tmp; LuaVar tmp;
LuaDefStack LS(L, vector, value, tmp); LuaDefStack LS(L, array, value, tmp);
int nkeys = check_vector_quick(LS, vector, tmp); int nkeys = check_array_quick(LS, array, tmp);
if (nkeys == 0) { if (nkeys == 0) {
LS.set(value, LuaNil); LS.set(value, LuaNil);
} else { } else {
LS.rawget(value, vector, nkeys); LS.rawget(value, array, nkeys);
LS.rawset(vector, nkeys, LuaNil); LS.rawset(array, nkeys, LuaNil);
} }
return LS.result(); return LS.result();
} }
LuaDefine(vector_find, "vector,value", LuaDefine(array_find, "array,value",
"|Find the first occurence of value in vector." "|Find the first occurence of value in array."
"|" "|"
"|Argument must be a valid vector. Returns the index of the" "|Argument must be a valid array. Returns the index of the"
"|first occurrence of value in vector. If the value is not" "|first occurrence of value in array. If the value is not"
"|found, returns nil." "|found, returns nil."
"|" "|"
"|Searching for 'nil' in a vector is explicitly disallowed, since" "|Searching for 'nil' in a array is explicitly disallowed, since"
"|a valid vector cannot contain nil." "|a valid array cannot contain nil."
"|") { "|") {
LuaArg vector, value; LuaArg array, value;
LuaRet index; LuaRet index;
LuaVar tmp; LuaVar tmp;
LuaDefStack LS(L, vector, value, index, tmp); LuaDefStack LS(L, array, value, index, tmp);
int nkeys = check_vector_quick(LS, vector, tmp); int nkeys = check_array_quick(LS, array, tmp);
if (LS.isnil(value)) { 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; return 0;
} }
for (int i = 1; i <= nkeys; i++) { for (int i = 1; i <= nkeys; i++) {
LS.rawget(tmp, vector, i); LS.rawget(tmp, array, i);
if (LS.rawequal(tmp, value)) { if (LS.rawequal(tmp, value)) {
LS.set(index, i); LS.set(index, i);
return LS.result(); 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 // Given a sortedpairs array, return the (key, value) pairs
// one by one. The first element of the vector is used as a // one by one. The first element of the array is used as a
// counter to keep track of our position. // counter to keep track of our position.
// //
///////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////