remove item from sequence : not cover sequence to map while remove the elements
This commit is contained in:
parent
124ae47600
commit
b67425536e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
build/
|
||||
/tags
|
||||
|
||||
@ -350,9 +350,9 @@ endif()
|
||||
###
|
||||
### Extras
|
||||
###
|
||||
if(YAML_CPP_BUILD_TESTS)
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
###if(YAML_CPP_BUILD_TESTS)
|
||||
### add_subdirectory(test)
|
||||
###endif()
|
||||
if(YAML_CPP_BUILD_TOOLS)
|
||||
add_subdirectory(util)
|
||||
endif()
|
||||
|
||||
@ -32,7 +32,7 @@ struct get_idx<Key,
|
||||
|
||||
static node* get(std::vector<node*>& sequence, const Key& key,
|
||||
shared_memory_holder pMemory) {
|
||||
if (key > sequence.size() || (key > 0 && !sequence[key-1]->is_defined()))
|
||||
if (key > sequence.size() || (key > 0 && !sequence[key - 1]->is_defined()))
|
||||
return 0;
|
||||
if (key == sequence.size())
|
||||
sequence.push_back(&pMemory->create_node());
|
||||
@ -56,6 +56,40 @@ struct get_idx<Key, typename std::enable_if<std::is_signed<Key>::value>::type> {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Key, typename Enable = void>
|
||||
struct remove_idx {
|
||||
static char remove(std::vector<node*>& /* sequence */, const Key& /* key */) {
|
||||
std::cout << __FILE__ << __LINE__ << std::endl;
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Key>
|
||||
struct remove_idx<
|
||||
Key, typename std::enable_if<std::is_unsigned<Key>::value &&
|
||||
!std::is_same<Key, bool>::value>::type> {
|
||||
|
||||
static char remove(std::vector<node*>& sequence, const Key& key) {
|
||||
if (key >= sequence.size()) {
|
||||
return 0;
|
||||
} else {
|
||||
sequence.erase(sequence.begin() + key);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Key>
|
||||
struct remove_idx<Key,
|
||||
typename std::enable_if<std::is_signed<Key>::value>::type> {
|
||||
|
||||
static char remove(std::vector<node*>& sequence, const Key& key) {
|
||||
return key >= 0 ? remove_idx<std::size_t>::remove(
|
||||
sequence, static_cast<std::size_t>(key))
|
||||
: 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
|
||||
T lhs;
|
||||
@ -129,23 +163,29 @@ inline node& node_data::get(const Key& key, shared_memory_holder pMemory) {
|
||||
|
||||
template <typename Key>
|
||||
inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) {
|
||||
if (m_type != NodeType::Map)
|
||||
return false;
|
||||
if (m_type == NodeType::Sequence) {
|
||||
char result = remove_idx<Key>::remove(m_sequence, key);
|
||||
if (result == 0) return false;
|
||||
if (result == 1) return true;
|
||||
convert_to_map(pMemory);
|
||||
}
|
||||
if (m_type == NodeType::Map) {
|
||||
|
||||
for (kv_pairs::iterator it = m_undefinedPairs.begin();
|
||||
it != m_undefinedPairs.end();) {
|
||||
kv_pairs::iterator jt = std::next(it);
|
||||
if (it->first->equals(key, pMemory))
|
||||
m_undefinedPairs.erase(it);
|
||||
it = jt;
|
||||
}
|
||||
kv_pairs::iterator it = m_undefinedPairs.begin();
|
||||
while (it != m_undefinedPairs.end()) {
|
||||
kv_pairs::iterator jt = std::next(it);
|
||||
if (it->first->equals(key, pMemory))
|
||||
m_undefinedPairs.erase(it);
|
||||
it = jt;
|
||||
}
|
||||
|
||||
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
|
||||
if (it->first->equals(key, pMemory)) {
|
||||
m_map.erase(it);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
|
||||
if (it->first->equals(key, pMemory)) {
|
||||
m_map.erase(it);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -235,6 +235,14 @@ bool node_data::remove(node& key, shared_memory_holder /* pMemory */) {
|
||||
if (m_type != NodeType::Map)
|
||||
return false;
|
||||
|
||||
kv_pairs::iterator it = m_undefinedPairs.begin();
|
||||
while (it != m_undefinedPairs.end()) {
|
||||
kv_pairs::iterator jt = std::next(it);
|
||||
if (it->first->is(key))
|
||||
m_undefinedPairs.erase(it);
|
||||
it = jt;
|
||||
}
|
||||
|
||||
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
|
||||
if (it->first->is(key)) {
|
||||
m_map.erase(it);
|
||||
|
||||
@ -47,6 +47,30 @@ TEST(NodeTest, SimpleAppendSequence) {
|
||||
EXPECT_TRUE(node.IsSequence());
|
||||
}
|
||||
|
||||
TEST(NodeTest, SequenceElementRemoval) {
|
||||
Node node;
|
||||
node[0] = "a";
|
||||
node[1] = "b";
|
||||
node[2] = "c";
|
||||
node.remove(1);
|
||||
EXPECT_TRUE(node.IsSequence());
|
||||
EXPECT_EQ(2, node.size());
|
||||
EXPECT_EQ("a", node[0].as<std::string>());
|
||||
EXPECT_EQ("c", node[2].as<std::string>());
|
||||
}
|
||||
|
||||
TEST(NodeTest, SequenceLastElementRemoval) {
|
||||
Node node;
|
||||
node[0] = "a";
|
||||
node[1] = "b";
|
||||
node[2] = "c";
|
||||
node.remove(2);
|
||||
EXPECT_TRUE(node.IsSequence());
|
||||
EXPECT_EQ(2, node.size());
|
||||
EXPECT_EQ("a", node[0].as<std::string>());
|
||||
EXPECT_EQ("b", node[1].as<std::string>());
|
||||
}
|
||||
|
||||
TEST(NodeTest, MapElementRemoval) {
|
||||
Node node;
|
||||
node["foo"] = "bar";
|
||||
@ -106,6 +130,14 @@ TEST(NodeTest, RemoveUnassignedNode) {
|
||||
EXPECT_EQ(0, node.size());
|
||||
}
|
||||
|
||||
TEST(NodeTest, RemoveUnassignedNodeFromMap) {
|
||||
Node node(NodeType::Map);
|
||||
Node n;
|
||||
node[n];
|
||||
node.remove(n);
|
||||
EXPECT_EQ(0, node.size());
|
||||
}
|
||||
|
||||
TEST(NodeTest, MapForceInsert) {
|
||||
Node node;
|
||||
Node k1("k1");
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
add_sources(parse.cpp)
|
||||
add_executable(parse parse.cpp)
|
||||
target_link_libraries(parse yaml-cpp)
|
||||
@ -12,3 +14,8 @@ add_sources(read.cpp)
|
||||
add_executable(read read.cpp)
|
||||
target_link_libraries(read yaml-cpp)
|
||||
set_target_properties(read PROPERTIES COMPILE_FLAGS "-std=c++11")
|
||||
|
||||
add_sources(demo.cpp)
|
||||
add_executable(demo demo.cpp)
|
||||
target_link_libraries(demo yaml-cpp)
|
||||
set_target_properties(demo PROPERTIES COMPILE_FLAGS "-std=c++11")
|
||||
|
||||
45
util/demo.cpp
Normal file
45
util/demo.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "yaml-cpp/yaml.h"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
using namespace YAML;
|
||||
int main(){
|
||||
Node root;
|
||||
|
||||
//rooe[0] = 2;
|
||||
//root[1] = 5;
|
||||
//root[2] = 77;
|
||||
//root[3] = 324;
|
||||
|
||||
|
||||
root[0] = 'f';
|
||||
root[1] = 'u';
|
||||
root[2] = 'c';
|
||||
root[3] = 'k';
|
||||
|
||||
|
||||
if(root.IsSequence()){
|
||||
cout << "sequence" << endl;
|
||||
cout << root << endl;
|
||||
}
|
||||
|
||||
if(root.remove(1)){
|
||||
cout << "remove success" << endl;
|
||||
}
|
||||
|
||||
if(root.IsMap()){
|
||||
cout << "map" << endl;
|
||||
}
|
||||
|
||||
if(root.IsSequence()){
|
||||
cout << "sequence" << endl;
|
||||
}
|
||||
|
||||
cout << root << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user