format code with "clang-format" and add a test for removing a pair with a integer key in a map

This commit is contained in:
star 2018-05-08 11:39:55 +08:00
parent 3a9aec8b2a
commit 66c18d5ecf
4 changed files with 42 additions and 88 deletions

View File

@ -58,9 +58,7 @@ 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 */) {
return 0;
}
static bool remove(std::vector<node*>&, const Key&) { return false; }
};
template <typename Key>
@ -68,13 +66,13 @@ 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;
}
static bool remove(std::vector<node*>& sequence, const Key& key) {
if (key >= sequence.size()) {
return false;
} else {
sequence.erase(sequence.begin() + key);
return true;
}
}
};
@ -82,10 +80,10 @@ 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) {
static bool 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;
: false;
}
};
@ -162,29 +160,25 @@ 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::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) {
if (m_type == NodeType::Sequence) {
return remove_idx<Key>::remove(m_sequence, key);
} else if (m_type == NodeType::Map) {
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;
}
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;
}

View File

@ -1,10 +1,10 @@
#include "yaml-cpp/emitter.h"
#include "yaml-cpp/node/emit.h"
#include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/impl.h"
#include "yaml-cpp/emitter.h"
#include "yaml-cpp/node/convert.h"
#include "yaml-cpp/node/iterator.h"
#include "yaml-cpp/node/detail/impl.h"
#include "yaml-cpp/node/emit.h"
#include "yaml-cpp/node/impl.h"
#include "yaml-cpp/node/iterator.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
@ -78,6 +78,16 @@ TEST(NodeTest, MapElementRemoval) {
EXPECT_TRUE(!node["foo"]);
}
TEST(NodeTest, MapIntegerElementRemoval) {
Node node;
node[1] = "hello";
node[2] = 'c';
node["foo"] = "bar";
EXPECT_TRUE(node.IsMap());
node.remove(1);
EXPECT_TRUE(node.IsMap());
}
TEST(NodeTest, SimpleAssignSequence) {
Node node;
node[0] = 10;

View File

@ -14,8 +14,3 @@ 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")

View File

@ -1,45 +0,0 @@
#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] = 'h';
root[1] = 'e';
root[2] = 'l';
root[3] = 'o';
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;
}