manual algorithm conversions (#891)

Signed-off-by: Rosen Penev <rosenp@gmail.com>
This commit is contained in:
Rosen Penev
2020-06-15 13:10:09 -07:00
committed by GitHub
parent a808c1f44a
commit 41001d1bf9
5 changed files with 54 additions and 62 deletions

View File

@@ -225,9 +225,8 @@ template <typename K, typename V, typename C, typename A>
struct convert<std::map<K, V, C, A>> {
static Node encode(const std::map<K, V, C, A>& rhs) {
Node node(NodeType::Map);
for (typename std::map<K, V, C, A>::const_iterator it = rhs.begin();
it != rhs.end(); ++it)
node.force_insert(it->first, it->second);
for (const auto& it : rhs)
node.force_insert(it.first, it.second);
return node;
}
@@ -236,12 +235,12 @@ struct convert<std::map<K, V, C, A>> {
return false;
rhs.clear();
for (const_iterator it = node.begin(); it != node.end(); ++it)
for (const auto& it : node)
#if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3:
rhs[it->first.template as<K>()] = it->second.template as<V>();
rhs[it.first.template as<K>()] = it.second.template as<V>();
#else
rhs[it->first.as<K>()] = it->second.as<V>();
rhs[it.first.as<K>()] = it.second.as<V>();
#endif
return true;
}
@@ -252,9 +251,7 @@ template <typename T, typename A>
struct convert<std::vector<T, A>> {
static Node encode(const std::vector<T, A>& rhs) {
Node node(NodeType::Sequence);
for (typename std::vector<T, A>::const_iterator it = rhs.begin();
it != rhs.end(); ++it)
node.push_back(*it);
std::copy(rhs.begin(), rhs.end(), std::back_inserter(rhs));
return node;
}
@@ -263,12 +260,12 @@ struct convert<std::vector<T, A>> {
return false;
rhs.clear();
for (const_iterator it = node.begin(); it != node.end(); ++it)
for (const auto& it : node)
#if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3:
rhs.push_back(it->template as<T>());
rhs.push_back(it.template as<T>());
#else
rhs.push_back(it->as<T>());
rhs.push_back(it.as<T>());
#endif
return true;
}
@@ -279,9 +276,7 @@ template <typename T, typename A>
struct convert<std::list<T,A>> {
static Node encode(const std::list<T,A>& rhs) {
Node node(NodeType::Sequence);
for (typename std::list<T,A>::const_iterator it = rhs.begin();
it != rhs.end(); ++it)
node.push_back(*it);
std::copy(rhs.begin(), rhs.end(), std::back_inserter(rhs));
return node;
}
@@ -290,12 +285,12 @@ struct convert<std::list<T,A>> {
return false;
rhs.clear();
for (const_iterator it = node.begin(); it != node.end(); ++it)
for (const auto& it : node)
#if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3:
rhs.push_back(it->template as<T>());
rhs.push_back(it.template as<T>());
#else
rhs.push_back(it->as<T>());
rhs.push_back(it.as<T>());
#endif
return true;
}
@@ -306,9 +301,7 @@ template <typename T, std::size_t N>
struct convert<std::array<T, N>> {
static Node encode(const std::array<T, N>& rhs) {
Node node(NodeType::Sequence);
for (const auto& element : rhs) {
node.push_back(element);
}
std::copy(rhs.begin(), rhs.end(), std::back_inserter(rhs));
return node;
}

View File

@@ -9,6 +9,8 @@
#include "yaml-cpp/node/detail/node.h"
#include "yaml-cpp/node/detail/node_data.h"
#include <algorithm>
#include <type_traits>
namespace YAML {
@@ -125,13 +127,11 @@ inline node* node_data::get(const Key& key,
throw BadSubscript(m_mark, key);
}
for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->equals(key, pMemory)) {
return it->second;
}
}
auto it = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) {
return m.first->equals(key, pMemory);
});
return nullptr;
return it != m_map.end() ? it->second : nullptr;
}
template <typename Key>
@@ -153,10 +153,12 @@ inline node& node_data::get(const Key& key, shared_memory_holder pMemory) {
throw BadSubscript(m_mark, key);
}
for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->equals(key, pMemory)) {
return *it->second;
}
auto it = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) {
return m.first->equals(key, pMemory);
});
if (it != m_map.end()) {
return it->second;
}
node& k = convert_to_node(key, pMemory);
@@ -169,7 +171,9 @@ template <typename Key>
inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) {
if (m_type == NodeType::Sequence) {
return remove_idx<Key>::remove(m_sequence, key, m_seqSize);
} else if (m_type == NodeType::Map) {
}
if (m_type == NodeType::Map) {
kv_pairs::iterator it = m_undefinedPairs.begin();
while (it != m_undefinedPairs.end()) {
kv_pairs::iterator jt = std::next(it);
@@ -179,11 +183,13 @@ inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) {
it = jt;
}
for (node_map::iterator iter = m_map.begin(); iter != m_map.end(); ++iter) {
if (iter->first->equals(key, pMemory)) {
m_map.erase(iter);
return true;
}
auto iter = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) {
return m.first->equals(key, pMemory);
});
if (iter != m_map.end()) {
m_map.erase(iter);
return true;
}
}

View File

@@ -16,11 +16,7 @@ std::string tolower(const std::string& str) {
template <typename T>
bool IsEntirely(const std::string& str, T func) {
for (char ch : str)
if (!func(ch))
return false;
return true;
return std::all_of(str.begin(), str.end(), [=](char ch) { return func(ch); });
}
// IsFlexibleCase

View File

@@ -1,3 +1,4 @@
#include <algorithm>
#include <iomanip>
#include <sstream>
@@ -199,15 +200,10 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType,
bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) {
// TODO: check for non-printable characters?
for (char ch : str) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) {
return false;
}
if (ch == '\n') {
return false;
}
}
return true;
return std::none_of(str.begin(), str.end(), [=](char ch) {
return (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) ||
(ch == '\n');
});
}
bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType,
@@ -217,12 +213,9 @@ bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType,
}
// TODO: check for non-printable characters?
for (char ch : str) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) {
return false;
}
}
return true;
return std::none_of(str.begin(), str.end(), [=](char ch) {
return (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch)));
});
}
void WriteDoubleQuoteEscapeSequence(ostream_wrapper& out, int codePoint) {

View File

@@ -252,11 +252,15 @@ bool node_data::remove(node& key, shared_memory_holder /* pMemory */) {
it = jt;
}
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->is(key)) {
m_map.erase(it);
return true;
}
auto it =
std::find_if(m_map.begin(), m_map.end(),
[&](std::pair<YAML::detail::node*, YAML::detail::node*> j) {
return (j.first->is(key));
});
if (it != m_map.end()) {
m_map.erase(it);
return true;
}
return false;