Skip to content

Commit

Permalink
Change node_map type from map<ptr,ptr> to vector<pair<ptr,ptr>> (jbed…
Browse files Browse the repository at this point in the history
…er#386)

* Change node_map type from map<ptr,ptr> to vector<pair<ptr,ptr>>

Map nodes are now iterated over in document order.

* Change insert_map_pair to always append

Always append in insert_map_pair even if the key is already present.
This breaks the behavior of force_insert which now always inserts KVs
even if the key is already present. The first insert for duplicated keys
now takes precedence for lookups.
  • Loading branch information
c0nk authored and jbeder committed Jul 18, 2016
1 parent f74ae54 commit f0b15cd
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/yaml-cpp/node/detail/node_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class YAML_CPP_API node_data {
mutable std::size_t m_seqSize;

// map
typedef std::map<node*, node*> node_map;
typedef std::vector<std::pair<node*, node*>> node_map;
node_map m_map;

typedef std::pair<node*, node*> kv_pair;
Expand Down
2 changes: 1 addition & 1 deletion include/yaml-cpp/node/detail/node_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct node_iterator_value : public std::pair<V*, V*> {
};

typedef std::vector<node*> node_seq;
typedef std::map<node*, node*> node_map;
typedef std::vector<std::pair<node*, node*>> node_map;

template <typename V>
struct node_iterator_type {
Expand Down
5 changes: 3 additions & 2 deletions src/node_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,10 @@ void node_data::reset_map() {
}

void node_data::insert_map_pair(node& key, node& value) {
m_map[&key] = &value;
m_map.emplace_back(&key, &value);

if (!key.is_defined() || !value.is_defined())
m_undefinedPairs.push_back(kv_pair(&key, &value));
m_undefinedPairs.emplace_back(&key, &value);
}

void node_data::convert_to_map(shared_memory_holder pMemory) {
Expand Down
4 changes: 2 additions & 2 deletions test/node/node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ TEST(NodeTest, MapForceInsert) {

node.force_insert(k2, v2);
EXPECT_EQ("v1", node["k1"].as<std::string>());
EXPECT_EQ("v2", node["k2"].as<std::string>());
EXPECT_EQ(2, node.size());
EXPECT_EQ("v1", node["k2"].as<std::string>());
EXPECT_EQ(3, node.size());
}

TEST(NodeTest, UndefinedConstNodeWithFallback) {
Expand Down

0 comments on commit f0b15cd

Please sign in to comment.