Add assoc set array
This commit is contained in:
parent
d092ed31dc
commit
517deb57e6
|
@ -126,15 +126,39 @@ public:
|
||||||
|
|
||||||
void SetArray(const SFallScriptValue& key, const SFallScriptValue& val, bool allowUnset)
|
void SetArray(const SFallScriptValue& key, const SFallScriptValue& val, bool allowUnset)
|
||||||
{
|
{
|
||||||
|
auto iter = map.find(key);
|
||||||
|
|
||||||
// TODO: assoc
|
bool lookupMap = (mFlags & SFALL_ARRAYFLAG_CONSTVAL) != 0;
|
||||||
|
|
||||||
// if (key.isInt()) {
|
if (iter != map.end() && lookupMap) {
|
||||||
// auto index = key.asInt();
|
// don't update value of key
|
||||||
// if (index >= 0 && index < size()) {
|
return;
|
||||||
// values[index] = key;
|
}
|
||||||
// }
|
|
||||||
// }
|
if (allowUnset && !lookupMap && val.isInt() && val.asInt() == 0) {
|
||||||
|
// after assigning zero to a key, no need to store it, because "get_array" returns 0 for non-existent keys: try unset
|
||||||
|
if (iter != map.end()) {
|
||||||
|
map.erase(iter);
|
||||||
|
std::vector<SFallScriptValue> newKeys;
|
||||||
|
newKeys.reserve(keys.size() - 1);
|
||||||
|
for (auto keyCandidate : keys) {
|
||||||
|
if (keyCandidate == key) {
|
||||||
|
// skip this key
|
||||||
|
} else {
|
||||||
|
newKeys.push_back(keyCandidate);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
keys = newKeys;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (iter == map.end()) {
|
||||||
|
// size check
|
||||||
|
if (size() >= ARRAY_MAX_SIZE) return;
|
||||||
|
|
||||||
|
keys.push_back(key);
|
||||||
|
map[key] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -86,4 +86,24 @@ bool SFallScriptValue::operator<(SFallScriptValue const& other) const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SFallScriptValue::operator==(SFallScriptValue const& other) const
|
||||||
|
{
|
||||||
|
if (opcode != other.opcode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (opcode) {
|
||||||
|
case VALUE_TYPE_DYNAMIC_STRING:
|
||||||
|
case VALUE_TYPE_STRING:
|
||||||
|
case VALUE_TYPE_PTR:
|
||||||
|
return pointerValue == other.pointerValue;
|
||||||
|
case VALUE_TYPE_INT:
|
||||||
|
return integerValue == other.integerValue;
|
||||||
|
case VALUE_TYPE_FLOAT:
|
||||||
|
return floatValue == other.floatValue;
|
||||||
|
default:
|
||||||
|
throw(std::exception());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -19,6 +19,7 @@ public:
|
||||||
int asInt() const;
|
int asInt() const;
|
||||||
|
|
||||||
bool operator<(SFallScriptValue const& other) const;
|
bool operator<(SFallScriptValue const& other) const;
|
||||||
|
bool operator==(SFallScriptValue const& other) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue