Exemple #1
0
size_t hash_value(const VarValue& v) {
	switch (v.type()) {
	case VarType::Null:		return 0;
	case VarType::Int:		return hash<Int64>()(v.ToInt64());
	case VarType::Float:	return hash<double>()(v.ToDouble());
	case VarType::Bool:		return hash<bool>()(v.ToBool());
	case VarType::String:	return hash<String>()(v.ToString());
	case VarType::Array:
		{
			size_t r = 0;
			for (int i=0; i<v.size(); ++i)
				r += hash<VarValue>()(v[i]);
			return r;
		}
	case VarType::Map:
		{
			vector<String> keys = v.Keys();
			size_t r = 0;
			for (int i=0; i<keys.size(); ++i)
				r += hash<VarValue>()(v[keys[i]]);
			return r;
		}
	default:
		Throw(E_NOTIMPL);
	}
}
Exemple #2
0
bool VarValue::operator==(const VarValue& v) const {
	if (type() != v.type())
		return false;
	switch (type()) {
	case VarType::Null: return true;
	case VarType::Bool: return ToBool() == v.ToBool();
	case VarType::Int: return ToInt64() == v.ToInt64();
	case VarType::Float: return ToDouble() == v.ToDouble();
	case VarType::String: return ToString() == v.ToString();
	case VarType::Array:
		if (size() != v.size())
			return false;
		for (int i=0; i<size(); ++i)
			if (_self[i] != v[i])
				return false;
		return true;
	case VarType::Map:
		{
			if (size() != v.size())
				return false;
			vector<String> keys = Keys();
			for (int i=0; i<keys.size(); ++i) {
				const String& key = keys[i];
				if (!v.HasKey(key) || _self[key] != v[key])
					return false;
			}
			return true;
		}
	default:
		Throw(E_NOTIMPL);
	}
}