Skip to content

Optimize removing negligible values #688

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

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
2 changes: 2 additions & 0 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ Json::Value obj_value(Json::objectValue); // {}
/** \brief Remove the named map member.

Update 'removed' iff removed.
'removed' can be NULL if removed value is negligible.
Copy link
Contributor

@BillyDonahue BillyDonahue Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word "negligible" is almost misused here as it implies a small size. The removed value might be ignorable, but not because of its size. Ignorable would be a more plain choice. But see other comments as I think this optionally-null parameter isn't the best choice.

\param key may contain embedded nulls.
\return true iff removed (no exceptions)
*/
Expand All @@ -544,6 +545,7 @@ Json::Value obj_value(Json::objectValue); // {}

O(n) expensive operations.
Update 'removed' iff removed.
'removed' can be NULL if removed value is negligible.
\return true iff removed (no exceptions)
*/
bool removeIndex(ArrayIndex i, Value* removed);
Expand Down
6 changes: 4 additions & 2 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,8 @@ bool Value::removeMember(const char* key, const char* cend, Value* removed)
ObjectValues::iterator it = value_.map_->find(actualKey);
if (it == value_.map_->end())
return false;
*removed = it->second;
if (removed)
*removed = it->second;
value_.map_->erase(it);
return true;
}
Expand Down Expand Up @@ -1219,7 +1220,8 @@ bool Value::removeIndex(ArrayIndex index, Value* removed) {
if (it == value_.map_->end()) {
return false;
}
*removed = it->second;
if (removed)
*removed = it->second;
ArrayIndex oldSize = size();
// shift left all items left, into the place of the "removed"
for (ArrayIndex i = index; i < (oldSize - 1); ++i){
Expand Down
15 changes: 15 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ JSONTEST_FIXTURE(ValueTest, objects) {
did = object1_.removeMember("some other id", &got);
JSONTEST_ASSERT_EQUAL(Json::Value("bar"), got);
JSONTEST_ASSERT_EQUAL(false, did);

// Remove negligible value.
object1_["negligible"] = "foo";
Json::ArrayIndex objectSize = object1_.size();
JSONTEST_ASSERT_EQUAL(Json::Value("foo"), object1_["negligible"]);
JSONTEST_ASSERT_EQUAL(true, object1_.removeMember("negligible", NULL));
JSONTEST_ASSERT_EQUAL(objectSize - 1, object1_.size());
JSONTEST_ASSERT_EQUAL(false, object1_.removeMember("negligible", NULL));
JSONTEST_ASSERT_EQUAL(objectSize - 1, object1_.size());
}

JSONTEST_FIXTURE(ValueTest, arrays) {
Expand Down Expand Up @@ -263,7 +272,13 @@ JSONTEST_FIXTURE(ValueTest, arrays) {
JSONTEST_ASSERT_EQUAL(true, array1_.removeIndex(2, &got));
JSONTEST_ASSERT_EQUAL(Json::Value(17), got);
JSONTEST_ASSERT_EQUAL(false, array1_.removeIndex(2, &got)); // gone now

// Remove negligible value.
Json::ArrayIndex arraySize = array1_.size();
JSONTEST_ASSERT_EQUAL(true, array1_.removeIndex(0, NULL));
JSONTEST_ASSERT_EQUAL(arraySize -1, array1_.size());
}

JSONTEST_FIXTURE(ValueTest, arrayIssue252)
{
int count = 5;
Expand Down