Skip to content

Commit

Permalink
Merge pull request #171 from biojppm/change_type
Browse files Browse the repository at this point in the history
Add change_type()
  • Loading branch information
biojppm committed Dec 9, 2021
2 parents 8c70f1e + ec2c13c commit 4a8fb19
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 56 deletions.
16 changes: 16 additions & 0 deletions changelog/current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

### New features
- Add `Tree::change_type()` and `NodeRef::change_type()` ([PR #171](https://github.com/biojppm/rapidyaml/pull/171)):
```c++
// clears a node and sets its type to a different type (one of `VAL`, `SEQ`, `MAP`):
Tree t = parse("{keyval0: val0, keyval1: val1, keyval2: val2}");
t[0].change_type(VAL);
t[1].change_type(MAP);
t[2].change_type(SEQ);
Tree expected = parse("{keyval0: val0, keyval1: {}, keyval2: []}");
assert(emitrs<std::string>(t) == emitrs<std::string>(expected));
```
### Fixes
- prefer passing `substr` and `csubstr` by value instead of const reference ([PR #171](https://github.com/biojppm/rapidyaml/pull/171))
2 changes: 1 addition & 1 deletion ext/c4core
Submodule c4core updated 65 files
+12 −0 .github/reqs.sh
+4 −0 .github/setenv.sh
+23 −3 .github/vagrant/vagrant-provision.sh
+113 −0 .github/workflows/libcxx.yml
+9 −2 .github/workflows/release.yml
+63 −4 .github/workflows/test.yml
+104 −0 .github/workflows/test_install.yml
+1 −0 .gitignore
+80 −63 CMakeLists.txt
+2 −1 bm/CMakeLists.txt
+36 −0 changelog/current.md
+1 −1 cmake
+6 −4 src/c4/base64.cpp
+3 −2 src/c4/charconv.hpp
+12 −0 src/c4/compiler.hpp
+3 −3 src/c4/ctor_dtor.hpp
+5 −2 src/c4/error.cpp
+7 −3 src/c4/ext/fast_float.hpp
+2,947 −0 src/c4/ext/fast_float_all.h
+6 −6 src/c4/ext/rng/rng.hpp
+16 −23 src/c4/ext/sg14/inplace_function.h
+4 −1 src/c4/platform.hpp
+0 −1 src/c4/preprocessor.hpp
+5 −5 src/c4/span.hpp
+10 −0 src/c4/std/std_fwd.hpp
+14 −12 src/c4/std/string.hpp
+57 −0 src/c4/std/string_fwd.hpp
+3 −0 src/c4/std/tuple.hpp
+35 −29 src/c4/std/vector.hpp
+53 −0 src/c4/std/vector_fwd.hpp
+127 −87 src/c4/substr.hpp
+2 −2 src/c4/time.cpp
+1 −1 src/c4/types.hpp
+2 −1 src/c4/windows.hpp
+7 −3 src/c4/windows_pop.hpp
+5 −0 src/c4/windows_push.hpp
+7 −23 test/CMakeLists.txt
+6 −1 test/c4/libtest/archetypes.hpp
+4 −0 test/c4/test.hpp
+3 −0 test/test_allocator.cpp
+2 −0 test/test_base64.cpp
+2 −0 test/test_bitmask.cpp
+3 −1 test/test_blob.cpp
+2 −0 test/test_char_traits.cpp
+2 −0 test/test_charconv.cpp
+2 −0 test/test_ctor_dtor.cpp
+3 −0 test/test_enum.cpp
+2 −0 test/test_enum_common.hpp
+8 −2 test/test_error.cpp
+3 −0 test/test_error_exception.cpp
+3 −1 test/test_format.cpp
+24 −0 test/test_install/CMakeLists.txt
+2 −0 test/test_memory_resource.cpp
+2 −0 test/test_memory_util.cpp
+2 −0 test/test_preprocessor.cpp
+37 −0 test/test_singleheader/CMakeLists.txt
+2 −0 test/test_singleheader/libc4core_singleheader.cpp
+153 −0 test/test_span.cpp
+97 −0 test/test_std_string.cpp
+113 −2 test/test_std_vector.cpp
+542 −20 test/test_substr.cpp
+2 −0 test/test_szconv.cpp
+2 −0 test/test_type_name.cpp
+2 −0 test/test_types.cpp
+113 −0 tools/amalgamate.py
10 changes: 6 additions & 4 deletions src/c4/yml/detail/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ inline void print_node(NodeRef const& p, int level=0)

inline size_t print_tree(Tree const& p, size_t node=NONE)
{
if(node == NONE)
printf("--------------------------------------\n");
size_t ret = 0;
if(!p.empty())
{
node = p.root_id();
if(node == NONE)
node = p.root_id();
ret = print_node(p, node, 0, 0, true);
}
printf("--------------------------------------\n");
size_t ret = print_node(p, node, 0, 0, true);
printf("#nodes=%zd vs #printed=%zd\n", p.size(), ret);
printf("--------------------------------------\n");
return ret;
Expand Down
60 changes: 35 additions & 25 deletions src/c4/yml/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,26 +243,32 @@ class RYML_EXPORT NodeRef
/** @name node modifiers */
/** @{ */

inline void change_type(NodeType t)
{
_C4RV();
m_tree->change_type(m_id, t);
}

inline void set_type(NodeType t)
{
_C4RV();
m_tree->_set_flags(m_id, t);
}

inline void set_key(csubstr const& key)
inline void set_key(csubstr key)
{
_C4RV();
m_tree->_set_key(m_id, key);
}

inline void set_val(csubstr const& val)
inline void set_val(csubstr val)
{
_C4RV();
m_tree->_set_val(m_id, val);
}

template<class T>
inline size_t set_key_serialized(T const& k)
inline size_t set_key_serialized(T const& C4_RESTRICT k)
{
_C4RV();
csubstr s = m_tree->to_arena(k);
Expand All @@ -271,7 +277,7 @@ class RYML_EXPORT NodeRef
}

template<class T>
inline size_t set_val_serialized(T const& v)
inline size_t set_val_serialized(T const& C4_RESTRICT v)
{
_C4RV();
csubstr s = m_tree->to_arena(v);
Expand All @@ -295,37 +301,37 @@ class RYML_EXPORT NodeRef
* @return the size of base64-decoded blob */
size_t deserialize_val(fmt::base64_wrapper v) const;

inline void set_key_tag(csubstr const& key_tag)
inline void set_key_tag(csubstr key_tag)
{
_C4RV();
m_tree->set_key_tag(m_id, key_tag);
}

inline void set_val_tag(csubstr const& val_tag) const
inline void set_val_tag(csubstr val_tag) const
{
_C4RV();
m_tree->set_val_tag(m_id, val_tag);
}

inline void set_key_anchor(csubstr const& key_anchor)
inline void set_key_anchor(csubstr key_anchor)
{
_C4RV();
m_tree->set_key_anchor(m_id, key_anchor);
}

inline void set_val_anchor(csubstr const& val_anchor) const
inline void set_val_anchor(csubstr val_anchor) const
{
_C4RV();
m_tree->set_val_anchor(m_id, val_anchor);
}

inline void set_key_ref(csubstr const& key_ref)
inline void set_key_ref(csubstr key_ref)
{
_C4RV();
m_tree->set_key_ref(m_id, key_ref);
}

inline void set_val_ref(csubstr const& val_ref) const
inline void set_val_ref(csubstr val_ref) const
{
_C4RV();
m_tree->set_val_ref(m_id, val_ref);
Expand All @@ -334,7 +340,7 @@ class RYML_EXPORT NodeRef
public:

template<class T>
inline csubstr to_arena(T const& s) const
inline csubstr to_arena(T const& C4_RESTRICT s) const
{
_C4RV();
return m_tree->to_arena(s);
Expand All @@ -344,26 +350,30 @@ class RYML_EXPORT NodeRef

inline void clear()
{
if(is_seed()) return;
if(is_seed())
return;
m_tree->remove_children(m_id);
m_tree->_clear(m_id);
}

inline void clear_key()
{
if(is_seed()) return;
if(is_seed())
return;
m_tree->_clear_key(m_id);
}

inline void clear_val()
{
if(is_seed()) return;
if(is_seed())
return;
m_tree->_clear_val(m_id);
}

inline void clear_children()
{
if(is_seed()) return;
if(is_seed())
return;
m_tree->remove_children(m_id);
}

Expand All @@ -372,7 +382,7 @@ class RYML_EXPORT NodeRef
public:

/** O(num_children) */
NodeRef operator[] (csubstr const& k)
NodeRef operator[] (csubstr k)
{
RYML_ASSERT( ! is_seed());
RYML_ASSERT(valid());
Expand All @@ -394,7 +404,7 @@ class RYML_EXPORT NodeRef
public:

/** O(num_children) */
NodeRef const operator[] (csubstr const& k) const
NodeRef const operator[] (csubstr k) const
{
RYML_ASSERT( ! is_seed());
RYML_ASSERT(valid());
Expand Down Expand Up @@ -441,7 +451,7 @@ class RYML_EXPORT NodeRef
_apply(v);
}

inline void operator= (csubstr const& v)
inline void operator= (csubstr v)
{
_apply_seed();
_apply(v);
Expand All @@ -459,7 +469,7 @@ class RYML_EXPORT NodeRef
public:

/** serialize a variable, then assign the result to the node's key */
inline NodeRef& operator<< (csubstr const& s)
inline NodeRef& operator<< (csubstr s)
{
// this overload is needed to prevent ambiguity (there's also
// operator<< for writing a substr to a stream)
Expand All @@ -470,7 +480,7 @@ class RYML_EXPORT NodeRef
}

template<class T>
inline NodeRef& operator<< (T const& v)
inline NodeRef& operator<< (T const& C4_RESTRICT v)
{
_apply_seed();
write(this, v);
Expand All @@ -494,7 +504,7 @@ class RYML_EXPORT NodeRef

/** serialize a variable, then assign the result to the node's key */
template<class T>
inline NodeRef& operator<< (Key<const T> const& v)
inline NodeRef& operator<< (Key<const T> const& C4_RESTRICT v)
{
_apply_seed();
set_key_serialized(v.k);
Expand All @@ -503,7 +513,7 @@ class RYML_EXPORT NodeRef

/** serialize a variable, then assign the result to the node's key */
template<class T>
inline NodeRef& operator<< (Key<T> const& v)
inline NodeRef& operator<< (Key<T> const& C4_RESTRICT v)
{
_apply_seed();
set_key_serialized(v.k);
Expand Down Expand Up @@ -550,7 +560,7 @@ class RYML_EXPORT NodeRef
public:

template<class T>
void get_if(csubstr const& name, T *var) const
void get_if(csubstr name, T *var) const
{
auto ch = find_child(name);
if(ch.valid())
Expand All @@ -560,7 +570,7 @@ class RYML_EXPORT NodeRef
}

template<class T>
void get_if(csubstr const& name, T *var, T const& fallback) const
void get_if(csubstr name, T *var, T fallback) const
{
auto ch = find_child(name);
if(ch.valid())
Expand Down Expand Up @@ -600,7 +610,7 @@ class RYML_EXPORT NodeRef
}
}

inline void _apply(csubstr const& v)
inline void _apply(csubstr v)
{
m_tree->_set_val(m_id, v);
}
Expand Down
43 changes: 39 additions & 4 deletions src/c4/yml/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,41 @@ void Tree::set_root_as_stream()
}


//-----------------------------------------------------------------------------
void Tree::remove_children(size_t node)
{
RYML_ASSERT(get(node) != nullptr);
size_t ich = get(node)->m_first_child;
while(ich != NONE)
{
remove_children(ich);
RYML_ASSERT(get(ich) != nullptr);
size_t next = get(ich)->m_next_sibling;
_release(ich);
if(ich == get(node)->m_last_child)
break;
ich = next;
}
}

bool Tree::change_type(size_t node, NodeType type)
{
RYML_ASSERT(type.is_val() || type.is_map() || type.is_seq());
RYML_ASSERT(type.is_val() + type.is_map() + type.is_seq() == 1);
RYML_ASSERT(type.has_key() == has_key(node) || (has_key(node) && !type.has_key()));
NodeData *d = _p(node);
if(type.is_map() && is_map(node))
return false;
else if(type.is_seq() && is_seq(node))
return false;
else if(type.is_val() && is_val(node))
return false;
d->m_type = (d->m_type & (~(MAP|SEQ|VAL))) | type;
remove_children(node);
return true;
}


//-----------------------------------------------------------------------------
size_t Tree::duplicate(size_t node, size_t parent, size_t after)
{
Expand Down Expand Up @@ -1497,7 +1532,7 @@ size_t Tree::find_child(size_t node, csubstr const& name) const

//-----------------------------------------------------------------------------

void Tree::to_val(size_t node, csubstr const& val, type_bits more_flags)
void Tree::to_val(size_t node, csubstr val, type_bits more_flags)
{
RYML_ASSERT( ! has_children(node));
RYML_ASSERT(parent(node) == NONE || ! parent_is_map(node));
Expand All @@ -1506,7 +1541,7 @@ void Tree::to_val(size_t node, csubstr const& val, type_bits more_flags)
_p(node)->m_val = val;
}

void Tree::to_keyval(size_t node, csubstr const& key, csubstr const& val, type_bits more_flags)
void Tree::to_keyval(size_t node, csubstr key, csubstr val, type_bits more_flags)
{
RYML_ASSERT( ! has_children(node));
RYML_ASSERT(parent(node) == NONE || parent_is_map(node));
Expand All @@ -1524,7 +1559,7 @@ void Tree::to_map(size_t node, type_bits more_flags)
_p(node)->m_val.clear();
}

void Tree::to_map(size_t node, csubstr const& key, type_bits more_flags)
void Tree::to_map(size_t node, csubstr key, type_bits more_flags)
{
RYML_ASSERT( ! has_children(node));
RYML_ASSERT(parent(node) == NONE || parent_is_map(node));
Expand All @@ -1542,7 +1577,7 @@ void Tree::to_seq(size_t node, type_bits more_flags)
_p(node)->m_val.clear();
}

void Tree::to_seq(size_t node, csubstr const& key, type_bits more_flags)
void Tree::to_seq(size_t node, csubstr key, type_bits more_flags)
{
RYML_ASSERT( ! has_children(node));
RYML_ASSERT(parent(node) == NONE || parent_is_map(node));
Expand Down
Loading

0 comments on commit 4a8fb19

Please sign in to comment.