Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map lookup for keys #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstdint>
#include <cstring>
#include <map>
#include <unordered_map>
#include <stdexcept>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -101,7 +102,8 @@ class UniValue {
private:
UniValue::VType typ;
std::string val; // numbers are stored as C++ strings
std::vector<std::string> keys;
std::unordered_map<std::string, size_t> keys;
std::vector<std::string> key_vector;
std::vector<UniValue> values;

bool findKey(const std::string& key, size_t& retIdx) const;
Expand Down
25 changes: 12 additions & 13 deletions lib/univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void UniValue::clear()
typ = VNULL;
val.clear();
keys.clear();
key_vector.clear();
values.clear();
}

Expand Down Expand Up @@ -129,7 +130,8 @@ bool UniValue::push_backV(const std::vector<UniValue>& vec)

void UniValue::__pushKV(const std::string& key, const UniValue& val_)
{
keys.push_back(key);
keys[key] = values.size();
key_vector.push_back(key);
values.push_back(val_);
}

Expand All @@ -151,8 +153,8 @@ bool UniValue::pushKVs(const UniValue& obj)
if (typ != VOBJ || obj.typ != VOBJ)
return false;

for (size_t i = 0; i < obj.keys.size(); i++)
__pushKV(obj.keys[i], obj.values.at(i));
for (const auto& [key, i] : obj.keys)
__pushKV(key, obj.values.at(i));

return true;
}
Expand All @@ -163,17 +165,15 @@ void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
return;

kv.clear();
for (size_t i = 0; i < keys.size(); i++)
kv[keys[i]] = values[i];
for (const auto& [key, i] : keys)
kv[key] = values[i];
}

bool UniValue::findKey(const std::string& key, size_t& retIdx) const
{
for (size_t i = 0; i < keys.size(); i++) {
if (keys[i] == key) {
retIdx = i;
return true;
}
if (keys.find(key) != keys.end()) {
retIdx = keys.find(key)->second;
return true;
}

return false;
Expand Down Expand Up @@ -238,9 +238,8 @@ const char *uvTypeName(UniValue::VType t)

const UniValue& find_value(const UniValue& obj, const std::string& name)
{
for (unsigned int i = 0; i < obj.keys.size(); i++)
if (obj.keys[i] == name)
return obj.values.at(i);
if (obj.keys.find(name) != obj.keys.end())
return obj.values[obj.keys.find(name)->second];

return NullUniValue;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/univalue_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const std::vector<std::string>& UniValue::getKeys() const
{
if (typ != VOBJ)
throw std::runtime_error("JSON value is not an object as expected");
return keys;
return key_vector;
}

const std::vector<UniValue>& UniValue::getValues() const
Expand Down
3 changes: 2 additions & 1 deletion lib/univalue_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ bool UniValue::read(const char *raw, size_t size)
case JTOK_STRING: {
if (expect(OBJ_NAME)) {
UniValue *top = stack.back();
top->keys.push_back(tokenVal);
top->keys[tokenVal] = top->values.size();
top->key_vector.push_back(tokenVal);
clearExpect(OBJ_NAME);
setExpect(COLON);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/univalue_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel,
if (prettyIndent)
s += "\n";

for (unsigned int i = 0; i < keys.size(); i++) {
for (unsigned int i = 0; i < key_vector.size(); i++) {
if (prettyIndent)
indentStr(prettyIndent, indentLevel, s);
s += "\"" + json_escape(keys[i]) + "\":";
s += "\"" + json_escape(key_vector[i]) + "\":";
if (prettyIndent)
s += " ";
s += values.at(i).write(prettyIndent, indentLevel + 1);
Expand Down