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

View File

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

View File

@@ -1,3 +1,4 @@
#include <algorithm>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
@@ -199,15 +200,10 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType,
bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) { bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) {
// TODO: check for non-printable characters? // TODO: check for non-printable characters?
for (char ch : str) { return std::none_of(str.begin(), str.end(), [=](char ch) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) { return (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) ||
return false; (ch == '\n');
} });
if (ch == '\n') {
return false;
}
}
return true;
} }
bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType, 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? // TODO: check for non-printable characters?
for (char ch : str) { return std::none_of(str.begin(), str.end(), [=](char ch) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) { return (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch)));
return false; });
}
}
return true;
} }
void WriteDoubleQuoteEscapeSequence(ostream_wrapper& out, int codePoint) { 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; it = jt;
} }
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { auto it =
if (it->first->is(key)) { std::find_if(m_map.begin(), m_map.end(),
m_map.erase(it); [&](std::pair<YAML::detail::node*, YAML::detail::node*> j) {
return true; return (j.first->is(key));
} });
if (it != m_map.end()) {
m_map.erase(it);
return true;
} }
return false; return false;