Code optimization for array expression (from 4.x)

This commit is contained in:
NovaRain
2020-10-11 10:55:58 +08:00
parent b1f847afbc
commit 466b79f5e8
3 changed files with 39 additions and 33 deletions
+29 -29
View File
@@ -19,7 +19,7 @@ ArrayKeysMap savedArrays;
// auto-incremented ID
DWORD nextArrayID = 1;
// special array ID for array expressions
// special array ID for array expressions, contains the ID number of the currently created array
DWORD stackArrayId;
static char get_all_arrays_special_key[] = "...all_arrays...";
@@ -311,6 +311,10 @@ int GetNumArrays() {
return arrays.size();
}
bool ArrayExist(DWORD id) {
return (arrays.find(id) != arrays.end());
}
void GetArrays(int* _arrays) {
int pos = 0;
array_citr itr = arrays.begin();
@@ -414,13 +418,14 @@ DWORD __stdcall getScriptTypeBySfallType(DWORD dataType) {
}
DWORD __stdcall CreateArray(int len, DWORD flags) {
sArrayVar var;
var.flags = (flags & 0xFFFFFFFE); // reset 1 bit
flags = (flags & ~1); // reset 1 bit
if (len < 0) {
var.flags |= ARRAYFLAG_ASSOC;
flags |= ARRAYFLAG_ASSOC;
} else if (len > ARRAY_MAX_SIZE) {
len = ARRAY_MAX_SIZE; // safecheck
}
sArrayVar var(len, flags);
if (!var.isAssoc()) {
var.val.resize(len);
}
@@ -526,18 +531,14 @@ DWORD __stdcall GetArray(DWORD id, DWORD key, DWORD keyType, DWORD* resultType)
return 0;
}
void __stdcall SetArray(DWORD id, DWORD key, DWORD keyType, DWORD val, DWORD valType, DWORD allowUnset) {
void __stdcall setArray(DWORD id, DWORD key, DWORD keyType, DWORD val, DWORD valType, DWORD allowUnset) {
keyType = getSfallTypeByScriptType(keyType);
valType = getSfallTypeByScriptType(valType);
if (arrays.find(id) == arrays.end()) {
return;
}
int el;
sArrayVar &arr = arrays[id];
if (arrays[id].isAssoc()) {
if (arr.isAssoc()) {
sArrayElement sEl(key, keyType);
ArrayKeysMap::iterator elIter = arr.keyHash.find(sEl);
el = (elIter != arr.keyHash.end())
int el = (elIter != arr.keyHash.end())
? elIter->second
: -1;
@@ -568,26 +569,26 @@ void __stdcall SetArray(DWORD id, DWORD key, DWORD keyType, DWORD val, DWORD val
arr.val[el].setByType(key, keyType); // copy data
arr.keyHash[arr.val[el]] = el;
}
arr.val[el+1].setByType(val, valType);
arr.val[el + 1].setByType(val, valType);
}
} else {
// only update normal array if key is an integer and within array size
el = key;
if (keyType == DATATYPE_INT && arr.size() > el) {
arr.val[el].setByType(val, valType);
} else if (keyType == DATATYPE_INT) { // only update normal array if key is an integer and within array size
size_t index = key;
if (arr.val.size() > index) {
arr.val[index].setByType(val, valType);
}
}
}
void __stdcall SetArray(DWORD id, DWORD key, DWORD keyType, DWORD val, DWORD valType, DWORD allowUnset) {
if (!ArrayExist(id)) return;
setArray(id, key, keyType, val, valType, allowUnset);
}
int __stdcall LenArray(DWORD id) {
array_itr it = arrays.find(id);
return (it != arrays.end()) ? it->second.size() : -1;
}
bool __stdcall ArrayExist(DWORD id) {
return (arrays.find(id) != arrays.end());
}
template <class T>
static void ListSort(std::vector<T> &arr, int type) {
switch (type) {
@@ -784,15 +785,14 @@ void __stdcall SaveArray(DWORD key, DWORD keyType, DWORD id) {
Should always return 0!
*/
long __stdcall StackArray(DWORD key, DWORD keyType, DWORD val, DWORD valType) {
DWORD id = stackArrayId;
if (id == 0 || arrays.find(id) == arrays.end()) {
return 0;
if (stackArrayId == 0 || !ArrayExist(stackArrayId)) return 0;
if (!arrays[stackArrayId].isAssoc()) { // automatically resize array to fit one new element
size_t size = arrays[stackArrayId].val.size();
if (size >= ARRAY_MAX_SIZE) return 0;
if (key >= size) arrays[stackArrayId].val.resize(size + 10); // resize with a small margin
}
if (!arrays[id].isAssoc()) {
// automatically resize array to fit one new element
ResizeArray(id, arrays[id].size() + 1);
}
SetArray(id, key, keyType, val, valType, 0);
setArray(stackArrayId, key, keyType, val, valType, 0);
return 0;
}
+9 -3
View File
@@ -103,6 +103,10 @@ public:
sArrayVar() : flags(0), key() {}
sArrayVar(int len, int flag) : flags(flag), key() {
if (len > 0) val.reserve(len + 10);
}
bool isAssoc() const {
return (flags & ARRAYFLAG_ASSOC);
}
@@ -148,6 +152,7 @@ extern ArrayKeysMap savedArrays;
long LoadArrays(HANDLE h);
void SaveArrays(HANDLE h);
int GetNumArrays();
bool ArrayExist(DWORD id);
void GetArrays(int* arrays);
void DEGetArray(int id, DWORD* types, char* data);
@@ -176,14 +181,15 @@ DWORD __stdcall GetArrayKey(DWORD id, int index, DWORD* resultType);
// get array element by index (list) or key (map)
DWORD __stdcall GetArray(DWORD id, DWORD key, DWORD keyType, DWORD* resultType);
// set array element by index or key
// set array element by index or key (with checking the existence of the array ID)
void __stdcall SetArray(DWORD id, DWORD key, DWORD keyType, DWORD val, DWORD valType, DWORD allowUnset);
// set array element by index or key
void __stdcall setArray(DWORD id, DWORD key, DWORD keyType, DWORD val, DWORD valType, DWORD allowUnset);
// number of elements in list or pairs in map
int __stdcall LenArray(DWORD id);
bool __stdcall ArrayExist(DWORD id);
// change array size (only works with list)
void __stdcall ResizeArray(DWORD id, int newlen);
+1 -1
View File
@@ -108,7 +108,7 @@ notstring1:
push ecx; // arg 3: key type
push esi; // arg 2: key
push edi; // arg 1: arrayID
call SetArray
call SetArray;
end:
popaop;
retn;