rapidyaml  0.9.0
parse and emit YAML, and do it fast
c4::yml::Tree Class Reference

#include <tree.hpp>

Classes

struct  lookup_result
 

Public Member Functions

id_type _claim ()
 
construction and assignment
 Tree ()
 
 Tree (Callbacks const &cb)
 
 Tree (id_type node_capacity, size_t arena_capacity=0)
 
 Tree (id_type node_capacity, size_t arena_capacity, Callbacks const &cb)
 
 ~Tree ()
 
 Tree (Tree const &that)
 
 Tree (Tree &&that) noexcept
 
Treeoperator= (Tree const &that)
 
Treeoperator= (Tree &&that) noexcept
 
memory and sizing
void reserve (id_type node_capacity)
 
void clear ()
 clear the tree and zero every node More...
 
void clear_arena ()
 
bool empty () const
 
id_type size () const
 
id_type capacity () const
 
id_type slack () const
 
Callbacks const & callbacks () const
 
void callbacks (Callbacks const &cb)
 
node getters
id_type id (NodeData const *n) const
 get the index of a node belonging to this tree. n can be nullptr, in which case NONE is returned More...
 
NodeDataget (id_type node)
 get a pointer to a node's NodeData. i can be NONE, in which case a nullptr is returned More...
 
NodeData const * get (id_type node) const
 get a pointer to a node's NodeData. i can be NONE, in which case a nullptr is returned. More...
 
NodeData_p (id_type node)
 An if-less form of get() that demands a valid node index. This function is implementation only; use at your own risk. More...
 
NodeData const * _p (id_type node) const
 An if-less form of get() that demands a valid node index. This function is implementation only; use at your own risk. More...
 
id_type root_id ()
 Get the id of the root node. More...
 
id_type root_id () const
 Get the id of the root node. More...
 
NodeRef ref (id_type node)
 Get a NodeRef of a node by id. More...
 
ConstNodeRef ref (id_type node) const
 Get a NodeRef of a node by id. More...
 
ConstNodeRef cref (id_type node) const
 Get a NodeRef of a node by id. More...
 
NodeRef rootref ()
 Get the root as a NodeRef. More...
 
ConstNodeRef rootref () const
 Get the root as a ConstNodeRef. More...
 
ConstNodeRef crootref () const
 Get the root as a ConstNodeRef. More...
 
NodeRef docref (id_type i)
 get the i-th document of the stream More...
 
ConstNodeRef docref (id_type i) const
 get the i-th document of the stream More...
 
ConstNodeRef cdocref (id_type i) const
 get the i-th document of the stream More...
 
NodeRef operator[] (csubstr key)
 find a root child by name, return it as a NodeRef More...
 
ConstNodeRef operator[] (csubstr key) const
 find a root child by name, return it as a NodeRef More...
 
NodeRef operator[] (id_type i)
 find a root child by index: return the root node's i-th child as a NodeRef More...
 
ConstNodeRef operator[] (id_type i) const
 find a root child by index: return the root node's i-th child as a NodeRef More...
 
node property getters
NodeType type (id_type node) const
 
const char * type_str (id_type node) const
 
csubstr const & key (id_type node) const
 
csubstr const & key_tag (id_type node) const
 
csubstr const & key_ref (id_type node) const
 
csubstr const & key_anchor (id_type node) const
 
NodeScalar const & keysc (id_type node) const
 
csubstr const & val (id_type node) const
 
csubstr const & val_tag (id_type node) const
 
csubstr const & val_ref (id_type node) const
 
csubstr const & val_anchor (id_type node) const
 
NodeScalar const & valsc (id_type node) const
 
node type predicates
bool type_has_any (id_type node, NodeType_e bits) const
 
bool type_has_all (id_type node, NodeType_e bits) const
 
bool type_has_none (id_type node, NodeType_e bits) const
 
bool is_stream (id_type node) const
 
bool is_doc (id_type node) const
 
bool is_container (id_type node) const
 
bool is_map (id_type node) const
 
bool is_seq (id_type node) const
 
bool has_key (id_type node) const
 
bool has_val (id_type node) const
 
bool is_val (id_type node) const
 
bool is_keyval (id_type node) const
 
bool has_key_tag (id_type node) const
 
bool has_val_tag (id_type node) const
 
bool has_key_anchor (id_type node) const
 
bool has_val_anchor (id_type node) const
 
bool has_anchor (id_type node) const
 
bool is_key_ref (id_type node) const
 
bool is_val_ref (id_type node) const
 
bool is_ref (id_type node) const
 
bool parent_is_seq (id_type node) const
 
bool parent_is_map (id_type node) const
 
bool has_anchor (id_type node, csubstr a) const
 true when the node has an anchor named a More...
 
bool key_is_null (id_type node) const
 true if the node key is empty, or its scalar verifies scalar_is_null(). More...
 
bool val_is_null (id_type node) const
 true if the node val is empty, or its scalar verifies scalar_is_null(). More...
 
bool is_key_unfiltered (id_type node) const
 true if the key was a scalar requiring filtering and was left unfiltered during the parsing (see ParserOptions) More...
 
bool is_val_unfiltered (id_type node) const
 true if the val was a scalar requiring filtering and was left unfiltered during the parsing (see ParserOptions) More...
 
bool is_key_anchor (id_type node) const
 
bool is_val_anchor (id_type node) const
 
bool is_anchor (id_type node) const
 
bool is_anchor_or_ref (id_type node) const
 
hierarchy getters
id_type parent (id_type node) const
 
id_type prev_sibling (id_type node) const
 
id_type next_sibling (id_type node) const
 
id_type num_children (id_type node) const
 O(num_children) More...
 
id_type child_pos (id_type node, id_type ch) const
 
id_type first_child (id_type node) const
 
id_type last_child (id_type node) const
 
id_type child (id_type node, id_type pos) const
 
id_type find_child (id_type node, csubstr const &key) const
 
id_type num_siblings (id_type node) const
 O(num_siblings) More...
 
id_type num_other_siblings (id_type node) const
 does not count with this More...
 
id_type sibling_pos (id_type node, id_type sib) const
 
id_type first_sibling (id_type node) const
 
id_type last_sibling (id_type node) const
 
id_type sibling (id_type node, id_type pos) const
 
id_type find_sibling (id_type node, csubstr const &key) const
 
id_type doc (id_type i) const
 gets the i document node index. More...
 
id_type depth_asc (id_type node) const
 O(log(num_tree_nodes)) get the ascending depth of the node: number of levels between root and node. More...
 
id_type depth_desc (id_type node) const
 O(num_tree_nodes) get the descending depth of the node: number of levels between node and deepest child. More...
 
node style predicates and modifiers. see the corresponding predicate in NodeType
bool is_container_styled (id_type node) const
 
bool is_block (id_type node) const
 
bool is_flow_sl (id_type node) const
 
bool is_flow_ml (id_type node) const
 
bool is_flow (id_type node) const
 
bool is_key_styled (id_type node) const
 
bool is_val_styled (id_type node) const
 
bool is_key_literal (id_type node) const
 
bool is_val_literal (id_type node) const
 
bool is_key_folded (id_type node) const
 
bool is_val_folded (id_type node) const
 
bool is_key_squo (id_type node) const
 
bool is_val_squo (id_type node) const
 
bool is_key_dquo (id_type node) const
 
bool is_val_dquo (id_type node) const
 
bool is_key_plain (id_type node) const
 
bool is_val_plain (id_type node) const
 
bool is_key_quoted (id_type node) const
 
bool is_val_quoted (id_type node) const
 
bool is_quoted (id_type node) const
 
void set_container_style (id_type node, NodeType_e style)
 
void set_key_style (id_type node, NodeType_e style)
 
void set_val_style (id_type node, NodeType_e style)
 
node type modifiers
void to_keyval (id_type node, csubstr key, csubstr val, type_bits more_flags=0)
 
void to_map (id_type node, csubstr key, type_bits more_flags=0)
 
void to_seq (id_type node, csubstr key, type_bits more_flags=0)
 
void to_val (id_type node, csubstr val, type_bits more_flags=0)
 
void to_map (id_type node, type_bits more_flags=0)
 
void to_seq (id_type node, type_bits more_flags=0)
 
void to_doc (id_type node, type_bits more_flags=0)
 
void to_stream (id_type node, type_bits more_flags=0)
 
void set_key (id_type node, csubstr key)
 
void set_val (id_type node, csubstr val)
 
void set_key_tag (id_type node, csubstr tag)
 
void set_val_tag (id_type node, csubstr tag)
 
void set_key_anchor (id_type node, csubstr anchor)
 
void set_val_anchor (id_type node, csubstr anchor)
 
void set_key_ref (id_type node, csubstr ref)
 
void set_val_ref (id_type node, csubstr ref)
 
void rem_key_anchor (id_type node)
 
void rem_val_anchor (id_type node)
 
void rem_key_ref (id_type node)
 
void rem_val_ref (id_type node)
 
void rem_anchor_ref (id_type node)
 
tree modifiers
void reorder ()
 reorder the tree in memory so that all the nodes are stored in a linear sequence when visited in depth-first order. More...
 
anchors and references/aliases
void resolve (ReferenceResolver *rr, bool clear_anchors=true)
 Resolve references (aliases <- anchors), by forwarding to ReferenceResolver::resolve(); refer to ReferenceResolver::resolve() for further details. More...
 
void resolve (bool clear_anchors=true)
 Resolve references (aliases <- anchors), by forwarding to ReferenceResolver::resolve(); refer to ReferenceResolver::resolve() for further details. More...
 
modifying hierarchy
id_type insert_child (id_type parent, id_type after)
 create and insert a new child of parent. More...
 
id_type prepend_child (id_type parent)
 create and insert a node as the first child of parent More...
 
id_type append_child (id_type parent)
 create and insert a node as the last child of parent More...
 
id_type _append_child__unprotected (id_type parent)
 
id_type insert_sibling (id_type node, id_type after)
 create and insert a new sibling of n. insert after "after" More...
 
id_type prepend_sibling (id_type node)
 create and insert a node as the first node of parent More...
 
id_type append_sibling (id_type node)
 
void remove (id_type node)
 remove an entire branch at once: ie remove the children and the node itself More...
 
void remove_children (id_type node)
 remove all the node's children, but keep the node itself More...
 
bool change_type (id_type node, NodeType type)
 change the type of the node to one of MAP, SEQ or VAL. More...
 
bool change_type (id_type node, type_bits type)
 
void move (id_type node, id_type after)
 change the node's position in the parent More...
 
void move (id_type node, id_type new_parent, id_type after)
 change the node's parent and position More...
 
id_type move (Tree *src, id_type node, id_type new_parent, id_type after)
 change the node's parent and position to a different tree More...
 
void set_root_as_stream ()
 ensure the first node is a stream. More...
 
id_type duplicate (id_type node, id_type new_parent, id_type after)
 recursively duplicate a node from this tree into a new parent, placing it after one of its children More...
 
id_type duplicate (Tree const *src, id_type node, id_type new_parent, id_type after)
 recursively duplicate a node from a different tree into a new parent, placing it after one of its children More...
 
id_type duplicate_children (id_type node, id_type parent, id_type after)
 recursively duplicate the node's children (but not the node) More...
 
id_type duplicate_children (Tree const *src, id_type node, id_type parent, id_type after)
 recursively duplicate the node's children (but not the node), where the node is from a different tree More...
 
id_type duplicate_children_no_rep (id_type node, id_type parent, id_type after)
 duplicate the node's children (but not the node) in a new parent, but omit repetitions where a duplicated node has the same key (in maps) or value (in seqs). More...
 
id_type duplicate_children_no_rep (Tree const *src, id_type node, id_type parent, id_type after)
 
void duplicate_contents (id_type node, id_type where)
 
void duplicate_contents (Tree const *src, id_type node, id_type where)
 
void merge_with (Tree const *src, id_type src_node=NONE, id_type dst_root=NONE)
 
lookup
lookup_result lookup_path (csubstr path, id_type start=NONE) const
 for example foo.bar[0].baz More...
 
id_type lookup_path_or_modify (csubstr default_value, csubstr path, id_type start=NONE)
 defaulted lookup: lookup path; if the lookup fails, recursively modify the tree so that the corresponding lookup_path() would return the default value. More...
 
id_type lookup_path_or_modify (Tree const *src, id_type src_node, csubstr path, id_type start=NONE)
 defaulted lookup: lookup path; if the lookup fails, recursively modify the tree so that the corresponding lookup_path() would return the branch src_node (from the tree src). More...
 

Public Attributes

NodeDatam_buf
 
id_type m_cap
 
id_type m_size
 
id_type m_free_head
 
id_type m_free_tail
 
substr m_arena
 
size_t m_arena_pos
 
Callbacks m_callbacks
 
TagDirective m_tag_directives [RYML_MAX_TAG_DIRECTIVES]
 

tag directives

typedef TagDirective const * tag_directive_const_iterator
 
typedef c4::yml::TagDirectiveRange TagDirectiveProxy
 
void resolve_tags ()
 
void normalize_tags ()
 
void normalize_tags_long ()
 
id_type num_tag_directives () const
 
bool add_tag_directive (csubstr directive)
 
id_type add_tag_directive (TagDirective const &td)
 
void clear_tag_directives ()
 
size_t resolve_tag (substr output, csubstr tag, id_type node_id) const
 resolve the given tag, appearing at node_id. More...
 
csubstr resolve_tag_sub (substr output, csubstr tag, id_type node_id) const
 
TagDirective const * begin_tag_directives () const
 
TagDirective const * end_tag_directives () const
 
c4::yml::TagDirectiveRange tag_directives () const
 

hierarchy predicates

bool is_root (id_type node) const
 
bool has_parent (id_type node) const
 
bool is_ancestor (id_type node, id_type ancestor) const
 true when ancestor is parent or parent of a parent of node More...
 
bool empty (id_type node) const
 true when key and val are empty, and has no children More...
 
bool has_child (id_type node, id_type ch) const
 true if node has a child with id ch More...
 
bool has_child (id_type node, csubstr key) const
 true if node has a child with key key More...
 
bool has_children (id_type node) const
 true if node has any children key More...
 
bool has_sibling (id_type node, id_type sib) const
 true if node has a sibling with id sib More...
 
bool has_sibling (id_type node, csubstr key) const
 true if one of the node's siblings has the given key More...
 
bool has_other_siblings (id_type node) const
 true if node is not a single child More...
 
static bool has_siblings (id_type)
 

internal string arena

size_t arena_pos () const
 get the current size of the tree's internal arena More...
 
size_t arena_size () const
 get the current size of the tree's internal arena More...
 
size_t arena_capacity () const
 get the current capacity of the tree's internal arena More...
 
size_t arena_slack () const
 get the current slack of the tree's internal arena More...
 
csubstr arena () const
 get the current arena More...
 
substr arena ()
 get the current arena More...
 
bool in_arena (csubstr s) const
 return true if the given substring is part of the tree's string arena More...
 
template<class T >
auto to_arena (T const &a) -> typename std::enable_if< std::is_floating_point< T >::value, csubstr >::type
 serialize the given floating-point variable to the tree's arena, growing it as needed to accomodate the serialization. More...
 
template<class T >
auto to_arena (T const &a) -> typename std::enable_if<!std::is_floating_point< T >::value, csubstr >::type
 serialize the given non-floating-point variable to the tree's arena, growing it as needed to accomodate the serialization. More...
 
csubstr to_arena (csubstr a)
 serialize the given csubstr to the tree's arena, growing the arena as needed to accomodate the serialization. More...
 
csubstr to_arena (const char *s)
 
substr copy_to_arena (csubstr s)
 copy the given substr to the tree's arena, growing it by the required size More...
 
substr alloc_arena (size_t sz)
 grow the tree's string arena by the given size and return a substr of the added portion More...
 
void reserve_arena (size_t arena_cap)
 ensure the tree's internal string arena is at least the given capacity More...
 
static csubstr to_arena (std::nullptr_t)
 

Detailed Description

Definition at line 200 of file tree.hpp.

Member Typedef Documentation

◆ tag_directive_const_iterator

Definition at line 610 of file tree.hpp.

◆ TagDirectiveProxy

Constructor & Destructor Documentation

◆ Tree() [1/6]

c4::yml::Tree::Tree ( )
inline

Definition at line 207 of file tree.hpp.

207 : Tree(get_callbacks()) {}
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:118

◆ Tree() [2/6]

c4::yml::Tree::Tree ( Callbacks const &  cb)

Definition at line 84 of file tree.cpp.

85  : m_buf(nullptr)
86  , m_cap(0)
87  , m_size(0)
88  , m_free_head(NONE)
89  , m_free_tail(NONE)
90  , m_arena()
91  , m_arena_pos(0)
92  , m_callbacks(cb)
94 {
95 }
NodeData * m_buf
Definition: tree.hpp:1302
id_type m_free_head
Definition: tree.hpp:1307
size_t m_arena_pos
Definition: tree.hpp:1311
TagDirective m_tag_directives[RYML_MAX_TAG_DIRECTIVES]
Definition: tree.hpp:1315
id_type m_size
Definition: tree.hpp:1305
id_type m_cap
Definition: tree.hpp:1303
substr m_arena
Definition: tree.hpp:1310
id_type m_free_tail
Definition: tree.hpp:1308
Callbacks m_callbacks
Definition: tree.hpp:1313
@ NONE
an index to none
Definition: common.hpp:260

◆ Tree() [3/6]

c4::yml::Tree::Tree ( id_type  node_capacity,
size_t  arena_capacity = 0 
)
inline

Definition at line 209 of file tree.hpp.

209 : Tree(node_capacity, arena_capacity, get_callbacks()) {}
size_t arena_capacity() const
get the current capacity of the tree's internal arena
Definition: tree.hpp:779

◆ Tree() [4/6]

c4::yml::Tree::Tree ( id_type  node_capacity,
size_t  arena_capacity,
Callbacks const &  cb 
)

Definition at line 97 of file tree.cpp.

98  : Tree(cb)
99 {
100  reserve(node_capacity);
102 }
void reserve_arena(size_t arena_cap)
ensure the tree's internal string arena is at least the given capacity
Definition: tree.hpp:948
void reserve(id_type node_capacity)
Definition: tree.cpp:254

References arena_capacity(), reserve(), and reserve_arena().

◆ ~Tree()

c4::yml::Tree::~Tree ( )

Definition at line 104 of file tree.cpp.

105 {
106  _free();
107 }

◆ Tree() [5/6]

c4::yml::Tree::Tree ( Tree const &  that)

Definition at line 110 of file tree.cpp.

110  : Tree(that.m_callbacks)
111 {
112  _copy(that);
113 }

◆ Tree() [6/6]

c4::yml::Tree::Tree ( Tree &&  that)
noexcept

Definition at line 126 of file tree.cpp.

126  : Tree(that.m_callbacks)
127 {
128  _move(that);
129 }

Member Function Documentation

◆ operator=() [1/2]

Tree & c4::yml::Tree::operator= ( Tree const &  that)

Definition at line 115 of file tree.cpp.

116 {
117  if(&that != this)
118  {
119  _free();
120  m_callbacks = that.m_callbacks;
121  _copy(that);
122  }
123  return *this;
124 }

References m_callbacks.

◆ operator=() [2/2]

Tree & c4::yml::Tree::operator= ( Tree &&  that)
noexcept

Definition at line 131 of file tree.cpp.

132 {
133  if(&that != this)
134  {
135  _free();
136  m_callbacks = that.m_callbacks;
137  _move(that);
138  }
139  return *this;
140 }

◆ reserve()

void c4::yml::Tree::reserve ( id_type  node_capacity)

Definition at line 254 of file tree.cpp.

255 {
256  if(cap > m_cap)
257  {
258  NodeData *buf = _RYML_CB_ALLOC_HINT(m_callbacks, NodeData, (size_t)cap, m_buf);
259  if(m_buf)
260  {
261  memcpy(buf, m_buf, (size_t)m_cap * sizeof(NodeData));
262  _RYML_CB_FREE(m_callbacks, m_buf, NodeData, (size_t)m_cap);
263  }
264  id_type first = m_cap, del = cap - m_cap;
265  m_cap = cap;
266  m_buf = buf;
267  _clear_range(first, del);
268  if(m_free_head != NONE)
269  {
270  _RYML_CB_ASSERT(m_callbacks, m_buf != nullptr);
271  _RYML_CB_ASSERT(m_callbacks, m_free_tail != NONE);
274  m_free_tail = cap-1;
275  }
276  else
277  {
278  _RYML_CB_ASSERT(m_callbacks, m_free_tail == NONE);
279  m_free_head = first;
280  m_free_tail = cap-1;
281  }
282  _RYML_CB_ASSERT(m_callbacks, m_free_head == NONE || (m_free_head >= 0 && m_free_head < cap));
283  _RYML_CB_ASSERT(m_callbacks, m_free_tail == NONE || (m_free_tail >= 0 && m_free_tail < cap));
284 
285  if( ! m_size)
286  _claim_root();
287  }
288 }
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:253
id_type m_next_sibling
Definition: tree.hpp:190
id_type m_prev_sibling
Definition: tree.hpp:191

References m_buf, m_callbacks, m_cap, m_free_head, m_free_tail, c4::yml::NodeData::m_next_sibling, c4::yml::NodeData::m_prev_sibling, m_size, and c4::yml::NONE.

Referenced by Tree(), _claim(), sample::sample_global_allocator(), sample::sample_parse_reuse_tree(), and sample::sample_parse_reuse_tree_and_parser().

◆ clear()

void c4::yml::Tree::clear ( )

clear the tree and zero every node

Note
does NOT clear the arena
See also
clear_arena()

Definition at line 292 of file tree.cpp.

293 {
294  _clear_range(0, m_cap);
295  m_size = 0;
296  if(m_buf)
297  {
298  _RYML_CB_ASSERT(m_callbacks, m_cap >= 0);
299  m_free_head = 0;
300  m_free_tail = m_cap-1;
301  _claim_root();
302  }
303  else
304  {
305  m_free_head = NONE;
306  m_free_tail = NONE;
307  }
308  for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
309  m_tag_directives[i] = {};
310 }
#define RYML_MAX_TAG_DIRECTIVES
the maximum number of tag directives in a Tree
Definition: tag.hpp:19

References m_buf, m_callbacks, m_cap, m_free_head, m_free_tail, m_size, m_tag_directives, c4::yml::NONE, and RYML_MAX_TAG_DIRECTIVES.

Referenced by sample::sample_empty_null_values(), sample::sample_parse_reuse_tree(), and sample::sample_parse_reuse_tree_and_parser().

◆ clear_arena()

void c4::yml::Tree::clear_arena ( )
inline

Definition at line 233 of file tree.hpp.

233 { m_arena_pos = 0; }

Referenced by sample::sample_empty_null_values(), and sample::sample_parse_reuse_tree().

◆ empty() [1/2]

bool c4::yml::Tree::empty ( ) const
inline

◆ size()

id_type c4::yml::Tree::size ( ) const
inline

◆ capacity()

id_type c4::yml::Tree::capacity ( ) const
inline

Definition at line 238 of file tree.hpp.

238 { return m_cap; }

Referenced by c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EventHandlerTree::reset().

◆ slack()

id_type c4::yml::Tree::slack ( ) const
inline

Definition at line 239 of file tree.hpp.

239 { RYML_ASSERT(m_cap >= m_size); return m_cap - m_size; }

◆ callbacks() [1/2]

◆ callbacks() [2/2]

void c4::yml::Tree::callbacks ( Callbacks const &  cb)
inline

Definition at line 242 of file tree.hpp.

242 { m_callbacks = cb; }

◆ id()

id_type c4::yml::Tree::id ( NodeData const *  n) const
inline

get the index of a node belonging to this tree. n can be nullptr, in which case NONE is returned

Definition at line 253 of file tree.hpp.

254  {
255  if( ! n)
256  return NONE;
257  _RYML_CB_ASSERT(m_callbacks, n >= m_buf && n < m_buf + m_cap);
258  return static_cast<id_type>(n - m_buf);
259  }

References c4::yml::NONE.

◆ get() [1/2]

NodeData* c4::yml::Tree::get ( id_type  node)
inline

get a pointer to a node's NodeData. i can be NONE, in which case a nullptr is returned

Definition at line 263 of file tree.hpp.

264  {
265  if(node == NONE)
266  return nullptr;
267  _RYML_CB_ASSERT(m_callbacks, node >= 0 && node < m_cap);
268  return m_buf + node;
269  }

References c4::yml::NONE.

Referenced by find_child(), and remove_children().

◆ get() [2/2]

NodeData const* c4::yml::Tree::get ( id_type  node) const
inline

get a pointer to a node's NodeData. i can be NONE, in which case a nullptr is returned.

Definition at line 272 of file tree.hpp.

273  {
274  if(node == NONE)
275  return nullptr;
276  _RYML_CB_ASSERT(m_callbacks, node >= 0 && node < m_cap);
277  return m_buf + node;
278  }

References c4::yml::NONE.

◆ _p() [1/2]

NodeData* c4::yml::Tree::_p ( id_type  node)
inline

An if-less form of get() that demands a valid node index. This function is implementation only; use at your own risk.

Definition at line 282 of file tree.hpp.

282 { _RYML_CB_ASSERT(m_callbacks, node != NONE && node >= 0 && node < m_cap); return m_buf + node; } // NOLINT(readability-make-member-function-const)

References c4::yml::NONE.

Referenced by change_type(), find_child(), merge_with(), set_root_as_stream(), to_doc(), to_keyval(), to_map(), to_seq(), to_stream(), and to_val().

◆ _p() [2/2]

NodeData const* c4::yml::Tree::_p ( id_type  node) const
inline

An if-less form of get() that demands a valid node index. This function is implementation only; use at your own risk.

Definition at line 285 of file tree.hpp.

285 { _RYML_CB_ASSERT(m_callbacks, node != NONE && node >= 0 && node < m_cap); return m_buf + node; }

References c4::yml::NONE.

◆ root_id() [1/2]

◆ root_id() [2/2]

id_type c4::yml::Tree::root_id ( ) const
inline

Get the id of the root node.

Definition at line 290 of file tree.hpp.

290 { _RYML_CB_ASSERT(m_callbacks, m_cap > 0 && m_size > 0); return 0; }

◆ ref() [1/2]

NodeRef c4::yml::Tree::ref ( id_type  node)

Get a NodeRef of a node by id.

Definition at line 35 of file tree.cpp.

36 {
37  _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
38  return NodeRef(this, id);
39 }

References c4::yml::NONE.

◆ ref() [2/2]

ConstNodeRef c4::yml::Tree::ref ( id_type  node) const

Get a NodeRef of a node by id.

Definition at line 40 of file tree.cpp.

41 {
42  _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
43  return ConstNodeRef(this, id);
44 }

References c4::yml::NONE.

◆ cref()

ConstNodeRef c4::yml::Tree::cref ( id_type  node) const

Get a NodeRef of a node by id.

Definition at line 45 of file tree.cpp.

46 {
47  _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
48  return ConstNodeRef(this, id);
49 }

References c4::yml::NONE.

◆ rootref() [1/2]

◆ rootref() [2/2]

ConstNodeRef c4::yml::Tree::rootref ( ) const

Get the root as a ConstNodeRef.

Definition at line 25 of file tree.cpp.

26 {
27  return ConstNodeRef(this, root_id());
28 }

◆ crootref()

ConstNodeRef c4::yml::Tree::crootref ( ) const

◆ docref() [1/2]

NodeRef c4::yml::Tree::docref ( id_type  i)

get the i-th document of the stream

Note
i is NOT the node id, but the doc position within the stream

Definition at line 69 of file tree.cpp.

70 {
71  return ref(doc(i));
72 }
NodeRef ref(id_type node)
Get a NodeRef of a node by id.
Definition: tree.cpp:35
id_type doc(id_type i) const
gets the i document node index.
Definition: tree.hpp:481

Referenced by sample::sample_docs().

◆ docref() [2/2]

ConstNodeRef c4::yml::Tree::docref ( id_type  i) const

get the i-th document of the stream

Note
i is NOT the node id, but the doc position within the stream

Definition at line 73 of file tree.cpp.

74 {
75  return cref(doc(i));
76 }
ConstNodeRef cref(id_type node) const
Get a NodeRef of a node by id.
Definition: tree.cpp:45

◆ cdocref()

ConstNodeRef c4::yml::Tree::cdocref ( id_type  i) const

get the i-th document of the stream

Note
i is NOT the node id, but the doc position within the stream

Definition at line 77 of file tree.cpp.

78 {
79  return cref(doc(i));
80 }

◆ operator[]() [1/4]

NodeRef c4::yml::Tree::operator[] ( csubstr  key)

find a root child by name, return it as a NodeRef

Note
requires the root to be a map.

Definition at line 51 of file tree.cpp.

52 {
53  return rootref()[key];
54 }
NodeRef rootref()
Get the root as a NodeRef.
Definition: tree.cpp:21
csubstr const & key(id_type node) const
Definition: tree.hpp:340

References c4::yml::key().

◆ operator[]() [2/4]

ConstNodeRef c4::yml::Tree::operator[] ( csubstr  key) const

find a root child by name, return it as a NodeRef

Note
requires the root to be a map.

Definition at line 55 of file tree.cpp.

56 {
57  return rootref()[key];
58 }

References c4::yml::key().

◆ operator[]() [3/4]

NodeRef c4::yml::Tree::operator[] ( id_type  i)

find a root child by index: return the root node's i-th child as a NodeRef

Note
i is NOT the node id, but the child's position

Definition at line 60 of file tree.cpp.

61 {
62  return rootref()[i];
63 }

◆ operator[]() [4/4]

ConstNodeRef c4::yml::Tree::operator[] ( id_type  i) const

find a root child by index: return the root node's i-th child as a NodeRef

Note
i is NOT the node id, but the child's position

Definition at line 64 of file tree.cpp.

65 {
66  return rootref()[i];
67 }

◆ type()

NodeType c4::yml::Tree::type ( id_type  node) const
inline

Definition at line 337 of file tree.hpp.

337 { return _p(node)->m_type; }
NodeData * _p(id_type node)
An if-less form of get() that demands a valid node index. This function is implementation only; use a...
Definition: tree.hpp:282
NodeType m_type
Definition: tree.hpp:182

Referenced by change_type(), and c4::yml::TagDirective::create_from_str().

◆ type_str()

const char* c4::yml::Tree::type_str ( id_type  node) const
inline

Definition at line 338 of file tree.hpp.

338 { return NodeType::type_str(_p(node)->m_type); }
const char * type_str() const noexcept
return a preset string based on the node type
Definition: node_type.hpp:150

◆ key()

csubstr const& c4::yml::Tree::key ( id_type  node) const
inline

Definition at line 340 of file tree.hpp.

340 { _RYML_CB_ASSERT(m_callbacks, has_key(node)); return _p(node)->m_key.scalar; }
bool has_key(id_type node) const
Definition: tree.hpp:368
NodeScalar m_key
Definition: tree.hpp:184
csubstr scalar
Definition: tree.hpp:66

Referenced by duplicate_children_no_rep(), lookup_path_or_modify(), merge_with(), sample::sample_base64(), sample::sample_quick_overview(), sample::sample_tree_arena(), to_keyval(), to_map(), and to_seq().

◆ key_tag()

csubstr const& c4::yml::Tree::key_tag ( id_type  node) const
inline

Definition at line 341 of file tree.hpp.

341 { _RYML_CB_ASSERT(m_callbacks, has_key_tag(node)); return _p(node)->m_key.tag; }
bool has_key_tag(id_type node) const
Definition: tree.hpp:372

◆ key_ref()

csubstr const& c4::yml::Tree::key_ref ( id_type  node) const
inline

Definition at line 342 of file tree.hpp.

342 { _RYML_CB_ASSERT(m_callbacks, is_key_ref(node)); return _p(node)->m_key.anchor; }
bool is_key_ref(id_type node) const
Definition: tree.hpp:377
csubstr anchor
Definition: tree.hpp:67

◆ key_anchor()

csubstr const& c4::yml::Tree::key_anchor ( id_type  node) const
inline

Definition at line 343 of file tree.hpp.

343 { _RYML_CB_ASSERT(m_callbacks, has_key_anchor(node)); return _p(node)->m_key.anchor; }
bool has_key_anchor(id_type node) const
Definition: tree.hpp:374

◆ keysc()

NodeScalar const& c4::yml::Tree::keysc ( id_type  node) const
inline

Definition at line 344 of file tree.hpp.

344 { _RYML_CB_ASSERT(m_callbacks, has_key(node)); return _p(node)->m_key; }

◆ val()

csubstr const& c4::yml::Tree::val ( id_type  node) const
inline

Definition at line 346 of file tree.hpp.

346 { _RYML_CB_ASSERT(m_callbacks, has_val(node)); return _p(node)->m_val.scalar; }
bool has_val(id_type node) const
Definition: tree.hpp:369
NodeScalar m_val
Definition: tree.hpp:185

Referenced by sample::sample_base64(), sample::sample_docs(), sample::sample_tree_arena(), to_keyval(), and to_val().

◆ val_tag()

csubstr const& c4::yml::Tree::val_tag ( id_type  node) const
inline

Definition at line 347 of file tree.hpp.

347 { _RYML_CB_ASSERT(m_callbacks, has_val_tag(node)); return _p(node)->m_val.tag; }
bool has_val_tag(id_type node) const
Definition: tree.hpp:373

◆ val_ref()

csubstr const& c4::yml::Tree::val_ref ( id_type  node) const
inline

Definition at line 348 of file tree.hpp.

348 { _RYML_CB_ASSERT(m_callbacks, is_val_ref(node)); return _p(node)->m_val.anchor; }
bool is_val_ref(id_type node) const
Definition: tree.hpp:378

◆ val_anchor()

csubstr const& c4::yml::Tree::val_anchor ( id_type  node) const
inline

Definition at line 349 of file tree.hpp.

349 { _RYML_CB_ASSERT(m_callbacks, has_val_anchor(node)); return _p(node)->m_val.anchor; }
bool has_val_anchor(id_type node) const
Definition: tree.hpp:375

◆ valsc()

NodeScalar const& c4::yml::Tree::valsc ( id_type  node) const
inline

Definition at line 350 of file tree.hpp.

350 { _RYML_CB_ASSERT(m_callbacks, has_val(node)); return _p(node)->m_val; }

◆ type_has_any()

bool c4::yml::Tree::type_has_any ( id_type  node,
NodeType_e  bits 
) const
inline

Definition at line 359 of file tree.hpp.

359 { return _p(node)->m_type.has_any(bits); }
bool has_any(NodeType_e t) const noexcept
Definition: node_type.hpp:128

◆ type_has_all()

bool c4::yml::Tree::type_has_all ( id_type  node,
NodeType_e  bits 
) const
inline

Definition at line 360 of file tree.hpp.

360 { return _p(node)->m_type.has_all(bits); }
bool has_all(NodeType_e t) const noexcept
Definition: node_type.hpp:129

◆ type_has_none()

bool c4::yml::Tree::type_has_none ( id_type  node,
NodeType_e  bits 
) const
inline

Definition at line 361 of file tree.hpp.

361 { return _p(node)->m_type.has_none(bits); }
bool has_none(NodeType_e t) const noexcept
Definition: node_type.hpp:130

◆ is_stream()

bool c4::yml::Tree::is_stream ( id_type  node) const
inline

Definition at line 363 of file tree.hpp.

363 { return _p(node)->m_type.is_stream(); }
bool is_stream() const noexcept
Definition: node_type.hpp:165

Referenced by c4::yml::TagDirective::create_from_str(), sample::sample_docs(), and set_root_as_stream().

◆ is_doc()

bool c4::yml::Tree::is_doc ( id_type  node) const
inline

Definition at line 364 of file tree.hpp.

364 { return _p(node)->m_type.is_doc(); }
bool is_doc() const noexcept
Definition: node_type.hpp:166

Referenced by sample::sample_docs().

◆ is_container()

bool c4::yml::Tree::is_container ( id_type  node) const
inline

Definition at line 365 of file tree.hpp.

365 { return _p(node)->m_type.is_container(); }
bool is_container() const noexcept
Definition: node_type.hpp:167

◆ is_map()

bool c4::yml::Tree::is_map ( id_type  node) const
inline

◆ is_seq()

bool c4::yml::Tree::is_seq ( id_type  node) const
inline

Definition at line 367 of file tree.hpp.

367 { return _p(node)->m_type.is_seq(); }
bool is_seq() const noexcept
Definition: node_type.hpp:169

Referenced by change_type(), duplicate_children_no_rep(), merge_with(), sample::sample_docs(), and sample::sample_quick_overview().

◆ has_key()

bool c4::yml::Tree::has_key ( id_type  node) const
inline

Definition at line 368 of file tree.hpp.

368 { return _p(node)->m_type.has_key(); }
bool has_key() const noexcept
Definition: node_type.hpp:170

Referenced by change_type(), merge_with(), c4::yml::EventHandlerTree::reset(), and set_root_as_stream().

◆ has_val()

bool c4::yml::Tree::has_val ( id_type  node) const
inline

Definition at line 369 of file tree.hpp.

369 { return _p(node)->m_type.has_val(); }
bool has_val() const noexcept
Definition: node_type.hpp:171

Referenced by merge_with().

◆ is_val()

bool c4::yml::Tree::is_val ( id_type  node) const
inline

Definition at line 370 of file tree.hpp.

370 { return _p(node)->m_type.is_val(); }
bool is_val() const noexcept
Definition: node_type.hpp:172

Referenced by change_type(), merge_with(), and set_root_as_stream().

◆ is_keyval()

bool c4::yml::Tree::is_keyval ( id_type  node) const
inline

Definition at line 371 of file tree.hpp.

371 { return _p(node)->m_type.is_keyval(); }
bool is_keyval() const noexcept
Definition: node_type.hpp:173

Referenced by merge_with().

◆ has_key_tag()

bool c4::yml::Tree::has_key_tag ( id_type  node) const
inline

Definition at line 372 of file tree.hpp.

372 { return _p(node)->m_type.has_key_tag(); }
bool has_key_tag() const noexcept
Definition: node_type.hpp:176

◆ has_val_tag()

bool c4::yml::Tree::has_val_tag ( id_type  node) const
inline

Definition at line 373 of file tree.hpp.

373 { return _p(node)->m_type.has_val_tag(); }
bool has_val_tag() const noexcept
Definition: node_type.hpp:177

◆ has_key_anchor()

bool c4::yml::Tree::has_key_anchor ( id_type  node) const
inline

Definition at line 374 of file tree.hpp.

374 { return _p(node)->m_type.has_key_anchor(); }
bool has_key_anchor() const noexcept
Definition: node_type.hpp:178

◆ has_val_anchor()

bool c4::yml::Tree::has_val_anchor ( id_type  node) const
inline

Definition at line 375 of file tree.hpp.

375 { return _p(node)->m_type.has_val_anchor(); }
bool has_val_anchor() const noexcept
Definition: node_type.hpp:179

◆ has_anchor() [1/2]

bool c4::yml::Tree::has_anchor ( id_type  node) const
inline

Definition at line 376 of file tree.hpp.

376 { return _p(node)->m_type.has_anchor(); }
bool has_anchor() const noexcept
Definition: node_type.hpp:180

◆ is_key_ref()

bool c4::yml::Tree::is_key_ref ( id_type  node) const
inline

Definition at line 377 of file tree.hpp.

377 { return _p(node)->m_type.is_key_ref(); }
bool is_key_ref() const noexcept
Definition: node_type.hpp:181

◆ is_val_ref()

bool c4::yml::Tree::is_val_ref ( id_type  node) const
inline

Definition at line 378 of file tree.hpp.

378 { return _p(node)->m_type.is_val_ref(); }
bool is_val_ref() const noexcept
Definition: node_type.hpp:182

◆ is_ref()

bool c4::yml::Tree::is_ref ( id_type  node) const
inline

Definition at line 379 of file tree.hpp.

379 { return _p(node)->m_type.is_ref(); }
bool is_ref() const noexcept
Definition: node_type.hpp:183

◆ parent_is_seq()

bool c4::yml::Tree::parent_is_seq ( id_type  node) const
inline

Definition at line 381 of file tree.hpp.

381 { _RYML_CB_ASSERT(m_callbacks, has_parent(node)); return is_seq(_p(node)->m_parent); }
bool is_seq(id_type node) const
Definition: tree.hpp:367
bool has_parent(id_type node) const
Definition: tree.hpp:417

Referenced by to_seq().

◆ parent_is_map()

bool c4::yml::Tree::parent_is_map ( id_type  node) const
inline

Definition at line 382 of file tree.hpp.

382 { _RYML_CB_ASSERT(m_callbacks, has_parent(node)); return is_map(_p(node)->m_parent); }
bool is_map(id_type node) const
Definition: tree.hpp:366

Referenced by lookup_path_or_modify(), to_keyval(), to_map(), to_seq(), and to_val().

◆ has_anchor() [2/2]

bool c4::yml::Tree::has_anchor ( id_type  node,
csubstr  a 
) const
inline

true when the node has an anchor named a

Definition at line 385 of file tree.hpp.

385 { return _p(node)->m_key.anchor == a || _p(node)->m_val.anchor == a; }

◆ key_is_null()

bool c4::yml::Tree::key_is_null ( id_type  node) const
inline

true if the node key is empty, or its scalar verifies scalar_is_null().

Warning
the node must verify Tree::has_key() (asserted) (ie must be a member of a map)
See also
https://github.com/biojppm/rapidyaml/issues/413

Definition at line 390 of file tree.hpp.

390 { _RYML_CB_ASSERT(m_callbacks, has_key(node)); NodeData const* C4_RESTRICT n = _p(node); return !n->m_type.is_key_quoted() && (n->m_type.key_is_null() || scalar_is_null(n->m_key.scalar)); }
bool scalar_is_null(csubstr s) noexcept
YAML-sense query of nullity.
Definition: node_type.hpp:255

References c4::yml::scalar_is_null().

◆ val_is_null()

bool c4::yml::Tree::val_is_null ( id_type  node) const
inline

true if the node val is empty, or its scalar verifies scalar_is_null().

Warning
the node must verify Tree::has_val() (asserted) (ie must be a scalar / must not be a container)
See also
https://github.com/biojppm/rapidyaml/issues/413

Definition at line 394 of file tree.hpp.

394 { _RYML_CB_ASSERT(m_callbacks, has_val(node)); NodeData const* C4_RESTRICT n = _p(node); return !n->m_type.is_val_quoted() && (n->m_type.val_is_null() || scalar_is_null(n->m_val.scalar)); }

References c4::yml::scalar_is_null().

◆ is_key_unfiltered()

bool c4::yml::Tree::is_key_unfiltered ( id_type  node) const
inline

true if the key was a scalar requiring filtering and was left unfiltered during the parsing (see ParserOptions)

Definition at line 398 of file tree.hpp.

398 { return _p(node)->m_type.is_key_unfiltered(); }
bool is_key_unfiltered() const noexcept
Definition: node_type.hpp:185

◆ is_val_unfiltered()

bool c4::yml::Tree::is_val_unfiltered ( id_type  node) const
inline

true if the val was a scalar requiring filtering and was left unfiltered during the parsing (see ParserOptions)

Definition at line 401 of file tree.hpp.

401 { return _p(node)->m_type.is_val_unfiltered(); }
bool is_val_unfiltered() const noexcept
Definition: node_type.hpp:186

◆ is_key_anchor()

bool c4::yml::Tree::is_key_anchor ( id_type  node) const
inline

Definition at line 403 of file tree.hpp.

403 { return _p(node)->m_type.has_key_anchor(); }

◆ is_val_anchor()

bool c4::yml::Tree::is_val_anchor ( id_type  node) const
inline

Definition at line 404 of file tree.hpp.

404 { return _p(node)->m_type.has_val_anchor(); }

◆ is_anchor()

bool c4::yml::Tree::is_anchor ( id_type  node) const
inline

Definition at line 405 of file tree.hpp.

405 { return _p(node)->m_type.has_anchor(); }

◆ is_anchor_or_ref()

bool c4::yml::Tree::is_anchor_or_ref ( id_type  node) const
inline

Definition at line 406 of file tree.hpp.

406 { return _p(node)->m_type.has_anchor() || _p(node)->m_type.is_ref(); }

◆ is_root()

bool c4::yml::Tree::is_root ( id_type  node) const
inline

Definition at line 415 of file tree.hpp.

415 { _RYML_CB_ASSERT(m_callbacks, _p(node)->m_parent != NONE || node == 0); return _p(node)->m_parent == NONE; }
id_type m_parent
Definition: tree.hpp:187

References c4::yml::NONE.

Referenced by c4::yml::TagDirective::create_from_str(), depth_asc(), duplicate(), move(), c4::yml::EventHandlerTree::reset(), and sample::sample_docs().

◆ has_parent()

bool c4::yml::Tree::has_parent ( id_type  node) const
inline

Definition at line 417 of file tree.hpp.

417 { return _p(node)->m_parent != NONE; }

References c4::yml::NONE.

◆ is_ancestor()

bool c4::yml::Tree::is_ancestor ( id_type  node,
id_type  ancestor 
) const

true when ancestor is parent or parent of a parent of node

Definition at line 1252 of file tree.cpp.

1253 {
1254  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1255  id_type p = parent(node);
1256  while(p != NONE)
1257  {
1258  if(p == ancestor)
1259  return true;
1260  p = parent(p);
1261  }
1262  return false;
1263 }
id_type parent(id_type node) const
Definition: tree.hpp:457

References m_callbacks, c4::yml::NONE, and parent().

Referenced by duplicate_children_no_rep().

◆ empty() [2/2]

bool c4::yml::Tree::empty ( id_type  node) const
inline

true when key and val are empty, and has no children

Definition at line 423 of file tree.hpp.

423 { return ! has_children(node) && _p(node)->m_key.empty() && (( ! (_p(node)->m_type & VAL)) || _p(node)->m_val.empty()); }
bool has_children(id_type node) const
true if node has any children key
Definition: tree.hpp:430
@ VAL
a scalar: has a scalar (ie string) value, possibly empty. must be a leaf node, and cannot be MAP or S...
Definition: node_type.hpp:34
bool empty() const noexcept
Definition: tree.hpp:94

References c4::yml::VAL.

◆ has_child() [1/2]

bool c4::yml::Tree::has_child ( id_type  node,
id_type  ch 
) const
inline

true if node has a child with id ch

Definition at line 426 of file tree.hpp.

426 { return _p(ch)->m_parent == node; }

Referenced by duplicate_children(), and duplicate_children_no_rep().

◆ has_child() [2/2]

bool c4::yml::Tree::has_child ( id_type  node,
csubstr  key 
) const
inline

true if node has a child with key key

Definition at line 428 of file tree.hpp.

428 { return find_child(node, key) != NONE; }
id_type find_child(id_type node, csubstr const &key) const
Definition: tree.cpp:1192

References c4::yml::key(), and c4::yml::NONE.

◆ has_children()

bool c4::yml::Tree::has_children ( id_type  node) const
inline

true if node has any children key

Definition at line 430 of file tree.hpp.

430 { return _p(node)->m_first_child != NONE; }
id_type m_first_child
Definition: tree.hpp:188

References c4::yml::NONE.

Referenced by merge_with(), set_root_as_stream(), to_doc(), to_keyval(), to_map(), to_seq(), to_stream(), and to_val().

◆ has_sibling() [1/2]

bool c4::yml::Tree::has_sibling ( id_type  node,
id_type  sib 
) const
inline

true if node has a sibling with id sib

Definition at line 433 of file tree.hpp.

433 { return _p(node)->m_parent == _p(sib)->m_parent; }

Referenced by move().

◆ has_sibling() [2/2]

bool c4::yml::Tree::has_sibling ( id_type  node,
csubstr  key 
) const
inline

true if one of the node's siblings has the given key

Definition at line 435 of file tree.hpp.

435 { return find_sibling(node, key) != NONE; }
id_type find_sibling(id_type node, csubstr const &key) const
Definition: tree.hpp:479

References c4::yml::key(), and c4::yml::NONE.

◆ has_other_siblings()

bool c4::yml::Tree::has_other_siblings ( id_type  node) const
inline

true if node is not a single child

Definition at line 437 of file tree.hpp.

438  {
439  NodeData const *n = _p(node);
440  if(C4_LIKELY(n->m_parent != NONE))
441  {
442  n = _p(n->m_parent);
443  return n->m_first_child != n->m_last_child;
444  }
445  return false;
446  }

References c4::yml::NodeData::m_first_child, c4::yml::NodeData::m_last_child, c4::yml::NodeData::m_parent, and c4::yml::NONE.

◆ has_siblings()

static bool c4::yml::Tree::has_siblings ( id_type  )
inlinestatic

Definition at line 448 of file tree.hpp.

448 { return true; }

◆ parent()

id_type c4::yml::Tree::parent ( id_type  node) const
inline

◆ prev_sibling()

id_type c4::yml::Tree::prev_sibling ( id_type  node) const
inline

Definition at line 459 of file tree.hpp.

459 { return _p(node)->m_prev_sibling; }

Referenced by duplicate_children_no_rep().

◆ next_sibling()

id_type c4::yml::Tree::next_sibling ( id_type  node) const
inline

◆ num_children()

id_type c4::yml::Tree::num_children ( id_type  node) const

O(num_children)

Definition at line 1146 of file tree.cpp.

1147 {
1148  id_type count = 0;
1149  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1150  ++count;
1151  return count;
1152 }
id_type first_child(id_type node) const
Definition: tree.hpp:465
id_type next_sibling(id_type node) const
Definition: tree.hpp:460

References first_child(), next_sibling(), and c4::yml::NONE.

Referenced by sample::sample_docs().

◆ child_pos()

id_type c4::yml::Tree::child_pos ( id_type  node,
id_type  ch 
) const

Definition at line 1166 of file tree.cpp.

1167 {
1168  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1169  id_type count = 0;
1170  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1171  {
1172  if(i == ch)
1173  return count;
1174  ++count;
1175  }
1176  return NONE;
1177 }

References first_child(), m_callbacks, next_sibling(), and c4::yml::NONE.

◆ first_child()

id_type c4::yml::Tree::first_child ( id_type  node) const
inline

◆ last_child()

id_type c4::yml::Tree::last_child ( id_type  node) const
inline

Definition at line 466 of file tree.hpp.

466 { return _p(node)->m_last_child; }
id_type m_last_child
Definition: tree.hpp:189

Referenced by duplicate_contents().

◆ child()

id_type c4::yml::Tree::child ( id_type  node,
id_type  pos 
) const

Definition at line 1154 of file tree.cpp.

1155 {
1156  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1157  id_type count = 0;
1158  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1159  {
1160  if(count++ == pos)
1161  return i;
1162  }
1163  return NONE;
1164 }

References first_child(), m_callbacks, next_sibling(), and c4::yml::NONE.

Referenced by _claim(), and sample::sample_docs().

◆ find_child()

id_type c4::yml::Tree::find_child ( id_type  node,
csubstr const &  key 
) const

Definition at line 1192 of file tree.cpp.

1193 {
1194  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1195  _RYML_CB_ASSERT(m_callbacks, is_map(node));
1196  if(get(node)->m_first_child == NONE)
1197  {
1198  _RYML_CB_ASSERT(m_callbacks, _p(node)->m_last_child == NONE);
1199  return NONE;
1200  }
1201  else
1202  {
1203  _RYML_CB_ASSERT(m_callbacks, _p(node)->m_last_child != NONE);
1204  }
1205  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1206  {
1207  if(_p(i)->m_key.scalar == name)
1208  {
1209  return i;
1210  }
1211  }
1212  return NONE;
1213 }
NodeData * get(id_type node)
get a pointer to a node's NodeData. i can be NONE, in which case a nullptr is returned
Definition: tree.hpp:263

References _p(), first_child(), get(), is_map(), m_callbacks, c4::yml::NodeData::m_key, next_sibling(), c4::yml::NONE, and c4::yml::NodeScalar::scalar.

Referenced by merge_with(), sample::sample_docs(), and sample::sample_quick_overview().

◆ num_siblings()

id_type c4::yml::Tree::num_siblings ( id_type  node) const
inline

O(num_siblings)

counts with this

Definition at line 472 of file tree.hpp.

472 { return is_root(node) ? 1 : num_children(_p(node)->m_parent); }
bool is_root(id_type node) const
Definition: tree.hpp:415
id_type num_children(id_type node) const
O(num_children)
Definition: tree.cpp:1146

◆ num_other_siblings()

id_type c4::yml::Tree::num_other_siblings ( id_type  node) const
inline

does not count with this

Definition at line 474 of file tree.hpp.

474 { id_type ns = num_siblings(node); _RYML_CB_ASSERT(m_callbacks, ns > 0); return ns-1; }
id_type num_siblings(id_type node) const
O(num_siblings)
Definition: tree.hpp:472

◆ sibling_pos()

id_type c4::yml::Tree::sibling_pos ( id_type  node,
id_type  sib 
) const
inline

Definition at line 475 of file tree.hpp.

475 { _RYML_CB_ASSERT(m_callbacks, ! is_root(node) || node == root_id()); return child_pos(_p(node)->m_parent, sib); }
id_type child_pos(id_type node, id_type ch) const
Definition: tree.cpp:1166

◆ first_sibling()

id_type c4::yml::Tree::first_sibling ( id_type  node) const
inline

Definition at line 476 of file tree.hpp.

476 { return is_root(node) ? node : _p(_p(node)->m_parent)->m_first_child; }

Referenced by sample::sample_quick_overview().

◆ last_sibling()

id_type c4::yml::Tree::last_sibling ( id_type  node) const
inline

Definition at line 477 of file tree.hpp.

477 { return is_root(node) ? node : _p(_p(node)->m_parent)->m_last_child; }

◆ sibling()

id_type c4::yml::Tree::sibling ( id_type  node,
id_type  pos 
) const
inline

Definition at line 478 of file tree.hpp.

478 { return child(_p(node)->m_parent, pos); }
id_type child(id_type node, id_type pos) const
Definition: tree.cpp:1154

◆ find_sibling()

id_type c4::yml::Tree::find_sibling ( id_type  node,
csubstr const &  key 
) const
inline

Definition at line 479 of file tree.hpp.

479 { return find_child(_p(node)->m_parent, key); }

References c4::yml::key().

◆ doc()

id_type c4::yml::Tree::doc ( id_type  i) const
inline

gets the i document node index.

requires that the root node is a stream.

Definition at line 481 of file tree.hpp.

Referenced by sample::sample_docs().

◆ depth_asc()

id_type c4::yml::Tree::depth_asc ( id_type  node) const

O(log(num_tree_nodes)) get the ascending depth of the node: number of levels between root and node.

Definition at line 1240 of file tree.cpp.

1241 {
1242  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1243  id_type depth = 0;
1244  while(!is_root(node))
1245  {
1246  ++depth;
1247  node = parent(node);
1248  }
1249  return depth;
1250 }

References is_root(), m_callbacks, c4::yml::NONE, and parent().

◆ depth_desc()

id_type c4::yml::Tree::depth_desc ( id_type  node) const

O(num_tree_nodes) get the descending depth of the node: number of levels between node and deepest child.

Definition at line 1234 of file tree.cpp.

1235 {
1236  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1237  return depth_desc_(*this, node);
1238 }

References m_callbacks, and c4::yml::NONE.

◆ is_container_styled()

bool c4::yml::Tree::is_container_styled ( id_type  node) const
inline

Definition at line 493 of file tree.hpp.

493 { return _p(node)->m_type.is_container_styled(); }
bool is_container_styled() const noexcept
Definition: node_type.hpp:199

◆ is_block()

bool c4::yml::Tree::is_block ( id_type  node) const
inline

Definition at line 494 of file tree.hpp.

494 { return _p(node)->m_type.is_block(); }
bool is_block() const noexcept
Definition: node_type.hpp:200

◆ is_flow_sl()

bool c4::yml::Tree::is_flow_sl ( id_type  node) const
inline

Definition at line 495 of file tree.hpp.

495 { return _p(node)->m_type.is_flow_sl(); }
bool is_flow_sl() const noexcept
Definition: node_type.hpp:201

◆ is_flow_ml()

bool c4::yml::Tree::is_flow_ml ( id_type  node) const
inline

Definition at line 496 of file tree.hpp.

496 { return _p(node)->m_type.is_flow_ml(); }
bool is_flow_ml() const noexcept
Definition: node_type.hpp:202

◆ is_flow()

bool c4::yml::Tree::is_flow ( id_type  node) const
inline

Definition at line 497 of file tree.hpp.

497 { return _p(node)->m_type.is_flow(); }
bool is_flow() const noexcept
Definition: node_type.hpp:203

◆ is_key_styled()

bool c4::yml::Tree::is_key_styled ( id_type  node) const
inline

Definition at line 499 of file tree.hpp.

499 { return _p(node)->m_type.is_key_styled(); }
bool is_key_styled() const noexcept
Definition: node_type.hpp:205

◆ is_val_styled()

bool c4::yml::Tree::is_val_styled ( id_type  node) const
inline

Definition at line 500 of file tree.hpp.

500 { return _p(node)->m_type.is_val_styled(); }
bool is_val_styled() const noexcept
Definition: node_type.hpp:206

◆ is_key_literal()

bool c4::yml::Tree::is_key_literal ( id_type  node) const
inline

Definition at line 501 of file tree.hpp.

501 { return _p(node)->m_type.is_key_literal(); }
bool is_key_literal() const noexcept
Definition: node_type.hpp:207

◆ is_val_literal()

bool c4::yml::Tree::is_val_literal ( id_type  node) const
inline

Definition at line 502 of file tree.hpp.

502 { return _p(node)->m_type.is_val_literal(); }
bool is_val_literal() const noexcept
Definition: node_type.hpp:208

◆ is_key_folded()

bool c4::yml::Tree::is_key_folded ( id_type  node) const
inline

Definition at line 503 of file tree.hpp.

503 { return _p(node)->m_type.is_key_folded(); }
bool is_key_folded() const noexcept
Definition: node_type.hpp:209

◆ is_val_folded()

bool c4::yml::Tree::is_val_folded ( id_type  node) const
inline

Definition at line 504 of file tree.hpp.

504 { return _p(node)->m_type.is_val_folded(); }
bool is_val_folded() const noexcept
Definition: node_type.hpp:210

◆ is_key_squo()

bool c4::yml::Tree::is_key_squo ( id_type  node) const
inline

Definition at line 505 of file tree.hpp.

505 { return _p(node)->m_type.is_key_squo(); }
bool is_key_squo() const noexcept
Definition: node_type.hpp:211

◆ is_val_squo()

bool c4::yml::Tree::is_val_squo ( id_type  node) const
inline

Definition at line 506 of file tree.hpp.

506 { return _p(node)->m_type.is_val_squo(); }
bool is_val_squo() const noexcept
Definition: node_type.hpp:212

◆ is_key_dquo()

bool c4::yml::Tree::is_key_dquo ( id_type  node) const
inline

Definition at line 507 of file tree.hpp.

507 { return _p(node)->m_type.is_key_dquo(); }
bool is_key_dquo() const noexcept
Definition: node_type.hpp:213

◆ is_val_dquo()

bool c4::yml::Tree::is_val_dquo ( id_type  node) const
inline

Definition at line 508 of file tree.hpp.

508 { return _p(node)->m_type.is_val_dquo(); }
bool is_val_dquo() const noexcept
Definition: node_type.hpp:214

◆ is_key_plain()

bool c4::yml::Tree::is_key_plain ( id_type  node) const
inline

Definition at line 509 of file tree.hpp.

509 { return _p(node)->m_type.is_key_plain(); }
bool is_key_plain() const noexcept
Definition: node_type.hpp:215

◆ is_val_plain()

bool c4::yml::Tree::is_val_plain ( id_type  node) const
inline

Definition at line 510 of file tree.hpp.

510 { return _p(node)->m_type.is_val_plain(); }
bool is_val_plain() const noexcept
Definition: node_type.hpp:216

◆ is_key_quoted()

bool c4::yml::Tree::is_key_quoted ( id_type  node) const
inline

Definition at line 511 of file tree.hpp.

511 { return _p(node)->m_type.is_key_quoted(); }
bool is_key_quoted() const noexcept
Definition: node_type.hpp:217

◆ is_val_quoted()

bool c4::yml::Tree::is_val_quoted ( id_type  node) const
inline

Definition at line 512 of file tree.hpp.

512 { return _p(node)->m_type.is_val_quoted(); }
bool is_val_quoted() const noexcept
Definition: node_type.hpp:218

◆ is_quoted()

bool c4::yml::Tree::is_quoted ( id_type  node) const
inline

Definition at line 513 of file tree.hpp.

513 { return _p(node)->m_type.is_quoted(); }
bool is_quoted() const noexcept
Definition: node_type.hpp:219

◆ set_container_style()

void c4::yml::Tree::set_container_style ( id_type  node,
NodeType_e  style 
)
inline

Definition at line 515 of file tree.hpp.

515 { _RYML_CB_ASSERT(m_callbacks, is_container(node)); _p(node)->m_type.set_container_style(style); }
bool is_container(id_type node) const
Definition: tree.hpp:365
void set_container_style(NodeType_e style) noexcept
Definition: node_type.hpp:221

◆ set_key_style()

void c4::yml::Tree::set_key_style ( id_type  node,
NodeType_e  style 
)
inline

Definition at line 516 of file tree.hpp.

516 { _RYML_CB_ASSERT(m_callbacks, has_key(node)); _p(node)->m_type.set_key_style(style); }
void set_key_style(NodeType_e style) noexcept
Definition: node_type.hpp:222

◆ set_val_style()

void c4::yml::Tree::set_val_style ( id_type  node,
NodeType_e  style 
)
inline

Definition at line 517 of file tree.hpp.

517 { _RYML_CB_ASSERT(m_callbacks, has_val(node)); _p(node)->m_type.set_val_style(style); }
void set_val_style(NodeType_e style) noexcept
Definition: node_type.hpp:223

◆ to_keyval()

void c4::yml::Tree::to_keyval ( id_type  node,
csubstr  key,
csubstr  val,
type_bits  more_flags = 0 
)

Definition at line 1277 of file tree.cpp.

1278 {
1279  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1280  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
1281  _set_flags(node, KEYVAL|more_flags);
1282  _p(node)->m_key = key;
1283  _p(node)->m_val = val;
1284 }
csubstr const & val(id_type node) const
Definition: tree.hpp:346
bool parent_is_map(id_type node) const
Definition: tree.hpp:382

References _p(), has_children(), key(), c4::yml::KEYVAL, m_callbacks, c4::yml::NodeData::m_key, c4::yml::NodeData::m_val, c4::yml::NONE, parent(), parent_is_map(), and val().

Referenced by lookup_path_or_modify().

◆ to_map() [1/2]

void c4::yml::Tree::to_map ( id_type  node,
csubstr  key,
type_bits  more_flags = 0 
)

Definition at line 1295 of file tree.cpp.

1296 {
1297  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1298  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
1299  _set_flags(node, KEY|MAP|more_flags);
1300  _p(node)->m_key = key;
1301  _p(node)->m_val.clear();
1302 }
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:35
@ KEY
is member of a map
Definition: node_type.hpp:33
void clear() noexcept
Definition: tree.hpp:96

References _p(), c4::yml::NodeScalar::clear(), has_children(), c4::yml::KEY, key(), m_callbacks, c4::yml::NodeData::m_key, c4::yml::NodeData::m_val, c4::yml::MAP, c4::yml::NONE, parent(), and parent_is_map().

Referenced by merge_with().

◆ to_seq() [1/2]

void c4::yml::Tree::to_seq ( id_type  node,
csubstr  key,
type_bits  more_flags = 0 
)

Definition at line 1313 of file tree.cpp.

1314 {
1315  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1316  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
1317  _set_flags(node, KEY|SEQ|more_flags);
1318  _p(node)->m_key = key;
1319  _p(node)->m_val.clear();
1320 }
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:36

References _p(), c4::yml::NodeScalar::clear(), has_children(), c4::yml::KEY, key(), m_callbacks, c4::yml::NodeData::m_key, c4::yml::NodeData::m_val, c4::yml::NONE, parent(), parent_is_map(), and c4::yml::SEQ.

Referenced by merge_with().

◆ to_val()

void c4::yml::Tree::to_val ( id_type  node,
csubstr  val,
type_bits  more_flags = 0 
)

Definition at line 1268 of file tree.cpp.

1269 {
1270  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1271  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || ! parent_is_map(node));
1272  _set_flags(node, VAL|more_flags);
1273  _p(node)->m_key.clear();
1274  _p(node)->m_val = val;
1275 }

References _p(), c4::yml::NodeScalar::clear(), has_children(), m_callbacks, c4::yml::NodeData::m_key, c4::yml::NodeData::m_val, c4::yml::NONE, parent(), parent_is_map(), c4::yml::VAL, and val().

Referenced by lookup_path_or_modify().

◆ to_map() [2/2]

void c4::yml::Tree::to_map ( id_type  node,
type_bits  more_flags = 0 
)

Definition at line 1286 of file tree.cpp.

1287 {
1288  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1289  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || ! parent_is_map(node)); // parent must not have children with keys
1290  _set_flags(node, MAP|more_flags);
1291  _p(node)->m_key.clear();
1292  _p(node)->m_val.clear();
1293 }

References _p(), c4::yml::NodeScalar::clear(), has_children(), m_callbacks, c4::yml::NodeData::m_key, c4::yml::NodeData::m_val, c4::yml::MAP, c4::yml::NONE, parent(), and parent_is_map().

◆ to_seq() [2/2]

void c4::yml::Tree::to_seq ( id_type  node,
type_bits  more_flags = 0 
)

Definition at line 1304 of file tree.cpp.

1305 {
1306  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1307  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_seq(node));
1308  _set_flags(node, SEQ|more_flags);
1309  _p(node)->m_key.clear();
1310  _p(node)->m_val.clear();
1311 }
bool parent_is_seq(id_type node) const
Definition: tree.hpp:381

References _p(), c4::yml::NodeScalar::clear(), has_children(), m_callbacks, c4::yml::NodeData::m_key, c4::yml::NodeData::m_val, c4::yml::NONE, parent(), parent_is_seq(), and c4::yml::SEQ.

◆ to_doc()

void c4::yml::Tree::to_doc ( id_type  node,
type_bits  more_flags = 0 
)

Definition at line 1322 of file tree.cpp.

1323 {
1324  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1325  _set_flags(node, DOC|more_flags);
1326  _p(node)->m_key.clear();
1327  _p(node)->m_val.clear();
1328 }
@ DOC
a document
Definition: node_type.hpp:37

References _p(), c4::yml::NodeScalar::clear(), c4::yml::DOC, has_children(), m_callbacks, c4::yml::NodeData::m_key, and c4::yml::NodeData::m_val.

◆ to_stream()

void c4::yml::Tree::to_stream ( id_type  node,
type_bits  more_flags = 0 
)

Definition at line 1330 of file tree.cpp.

1331 {
1332  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1333  _set_flags(node, STREAM|more_flags);
1334  _p(node)->m_key.clear();
1335  _p(node)->m_val.clear();
1336 }
@ STREAM
a stream: a seq of docs
Definition: node_type.hpp:38

References _p(), c4::yml::NodeScalar::clear(), has_children(), m_callbacks, c4::yml::NodeData::m_key, c4::yml::NodeData::m_val, and c4::yml::STREAM.

◆ set_key()

void c4::yml::Tree::set_key ( id_type  node,
csubstr  key 
)
inline

Definition at line 535 of file tree.hpp.

535 { _RYML_CB_ASSERT(m_callbacks, has_key(node)); _p(node)->m_key.scalar = key; }

References c4::yml::key().

◆ set_val()

void c4::yml::Tree::set_val ( id_type  node,
csubstr  val 
)
inline

Definition at line 536 of file tree.hpp.

536 { _RYML_CB_ASSERT(m_callbacks, has_val(node)); _p(node)->m_val.scalar = val; }

Referenced by sample::sample_tree_arena().

◆ set_key_tag()

void c4::yml::Tree::set_key_tag ( id_type  node,
csubstr  tag 
)
inline

Definition at line 538 of file tree.hpp.

538 { _RYML_CB_ASSERT(m_callbacks, has_key(node)); _p(node)->m_key.tag = tag; _add_flags(node, KEYTAG); }
@ KEYTAG
the key has a tag
Definition: node_type.hpp:43

References c4::yml::KEYTAG.

◆ set_val_tag()

void c4::yml::Tree::set_val_tag ( id_type  node,
csubstr  tag 
)
inline

Definition at line 539 of file tree.hpp.

539 { _RYML_CB_ASSERT(m_callbacks, has_val(node) || is_container(node)); _p(node)->m_val.tag = tag; _add_flags(node, VALTAG); }
@ VALTAG
the val has a tag
Definition: node_type.hpp:44

References c4::yml::VALTAG.

◆ set_key_anchor()

void c4::yml::Tree::set_key_anchor ( id_type  node,
csubstr  anchor 
)
inline

Definition at line 541 of file tree.hpp.

541 { _RYML_CB_ASSERT(m_callbacks, ! is_key_ref(node)); _p(node)->m_key.anchor = anchor.triml('&'); _add_flags(node, KEYANCH); }
@ KEYANCH
the key has an &anchor
Definition: node_type.hpp:41

References c4::yml::KEYANCH.

◆ set_val_anchor()

void c4::yml::Tree::set_val_anchor ( id_type  node,
csubstr  anchor 
)
inline

Definition at line 542 of file tree.hpp.

542 { _RYML_CB_ASSERT(m_callbacks, ! is_val_ref(node)); _p(node)->m_val.anchor = anchor.triml('&'); _add_flags(node, VALANCH); }
@ VALANCH
the val has an &anchor
Definition: node_type.hpp:42

References c4::yml::VALANCH.

◆ set_key_ref()

void c4::yml::Tree::set_key_ref ( id_type  node,
csubstr  ref 
)
inline

Definition at line 543 of file tree.hpp.

543 { _RYML_CB_ASSERT(m_callbacks, ! has_key_anchor(node)); NodeData* C4_RESTRICT n = _p(node); n->m_key.set_ref_maybe_replacing_scalar(ref, n->m_type.has_key()); _add_flags(node, KEY|KEYREF); }
@ KEYREF
a *reference: the key references an &anchor
Definition: node_type.hpp:39
bool has_key() const RYML_NOEXCEPT
Forward to Tree::has_key().
Definition: node.hpp:240

References c4::yml::KEY, and c4::yml::KEYREF.

◆ set_val_ref()

void c4::yml::Tree::set_val_ref ( id_type  node,
csubstr  ref 
)
inline

Definition at line 544 of file tree.hpp.

544 { _RYML_CB_ASSERT(m_callbacks, ! has_val_anchor(node)); NodeData* C4_RESTRICT n = _p(node); n->m_val.set_ref_maybe_replacing_scalar(ref, n->m_type.has_val()); _add_flags(node, VAL|VALREF); }
@ VALREF
a *reference: the val references an &anchor
Definition: node_type.hpp:40
bool has_val() const RYML_NOEXCEPT
Forward to Tree::has_val().
Definition: node.hpp:239

References c4::yml::VAL, and c4::yml::VALREF.

◆ rem_key_anchor()

void c4::yml::Tree::rem_key_anchor ( id_type  node)
inline

Definition at line 546 of file tree.hpp.

546 { _p(node)->m_key.anchor.clear(); _rem_flags(node, KEYANCH); }

References c4::yml::KEYANCH.

◆ rem_val_anchor()

void c4::yml::Tree::rem_val_anchor ( id_type  node)
inline

Definition at line 547 of file tree.hpp.

547 { _p(node)->m_val.anchor.clear(); _rem_flags(node, VALANCH); }

References c4::yml::VALANCH.

◆ rem_key_ref()

void c4::yml::Tree::rem_key_ref ( id_type  node)
inline

Definition at line 548 of file tree.hpp.

548 { _p(node)->m_key.anchor.clear(); _rem_flags(node, KEYREF); }

References c4::yml::KEYREF.

◆ rem_val_ref()

void c4::yml::Tree::rem_val_ref ( id_type  node)
inline

Definition at line 549 of file tree.hpp.

549 { _p(node)->m_val.anchor.clear(); _rem_flags(node, VALREF); }

References c4::yml::VALREF.

◆ rem_anchor_ref()

void c4::yml::Tree::rem_anchor_ref ( id_type  node)
inline

Definition at line 550 of file tree.hpp.

550 { _p(node)->m_key.anchor.clear(); _p(node)->m_val.anchor.clear(); _rem_flags(node, KEYANCH|VALANCH|KEYREF|VALREF); }

References c4::yml::KEYANCH, c4::yml::KEYREF, c4::yml::VALANCH, and c4::yml::VALREF.

◆ reorder()

void c4::yml::Tree::reorder ( )

reorder the tree in memory so that all the nodes are stored in a linear sequence when visited in depth-first order.

This will invalidate existing ids, since the node id is its position in the tree's node array.

Definition at line 539 of file tree.cpp.

540 {
541  id_type r = root_id();
542  _do_reorder(&r, 0);
543 }

References root_id().

◆ resolve() [1/2]

void c4::yml::Tree::resolve ( ReferenceResolver rr,
bool  clear_anchors = true 
)

Resolve references (aliases <- anchors), by forwarding to ReferenceResolver::resolve(); refer to ReferenceResolver::resolve() for further details.

Definition at line 1136 of file tree.cpp.

1137 {
1138  if(m_size == 0)
1139  return;
1140  rr->resolve(this, clear_anchors);
1141 }

References m_size.

Referenced by resolve(), and sample::sample_anchors_and_aliases().

◆ resolve() [2/2]

void c4::yml::Tree::resolve ( bool  clear_anchors = true)

Resolve references (aliases <- anchors), by forwarding to ReferenceResolver::resolve(); refer to ReferenceResolver::resolve() for further details.

This overload uses a throwaway resolver object.

Definition at line 1128 of file tree.cpp.

1129 {
1130  if(m_size == 0)
1131  return;
1132  ReferenceResolver rr;
1133  resolve(&rr, clear_anchors);
1134 }
void resolve(ReferenceResolver *rr, bool clear_anchors=true)
Resolve references (aliases <- anchors), by forwarding to ReferenceResolver::resolve(); refer to Refe...
Definition: tree.cpp:1136

References m_size, and resolve().

◆ resolve_tags()

void c4::yml::Tree::resolve_tags ( )

Definition at line 1467 of file tree.cpp.

1468 {
1469  if(empty())
1470  return;
1471  size_t needed_size = _count_resolved_tags_size(this, root_id());
1472  if(needed_size)
1473  reserve_arena(arena_size() + needed_size);
1474  _resolve_tags(this, root_id());
1475 }
bool empty() const
Definition: tree.hpp:235
size_t arena_size() const
get the current size of the tree's internal arena
Definition: tree.hpp:777

References arena_size(), empty(), reserve_arena(), and root_id().

Referenced by sample::sample_tag_directives().

◆ normalize_tags()

void c4::yml::Tree::normalize_tags ( )

Definition at line 1477 of file tree.cpp.

1478 {
1479  if(empty())
1480  return;
1481  _normalize_tags(this, root_id());
1482 }

References empty(), and root_id().

Referenced by sample::sample_tags().

◆ normalize_tags_long()

void c4::yml::Tree::normalize_tags_long ( )

Definition at line 1484 of file tree.cpp.

1485 {
1486  if(empty())
1487  return;
1488  _normalize_tags_long(this, root_id());
1489 }

References empty(), and root_id().

Referenced by sample::sample_tags().

◆ num_tag_directives()

id_type c4::yml::Tree::num_tag_directives ( ) const

Definition at line 1340 of file tree.cpp.

1341 {
1342  // this assumes we have a very small number of tag directives
1343  for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
1344  if(m_tag_directives[i].handle.empty())
1345  return i;
1346  return RYML_MAX_TAG_DIRECTIVES;
1347 }

References m_tag_directives, and RYML_MAX_TAG_DIRECTIVES.

Referenced by add_tag_directive().

◆ add_tag_directive() [1/2]

bool c4::yml::Tree::add_tag_directive ( csubstr  directive)

Definition at line 1369 of file tree.cpp.

1370 {
1371  TagDirective td;
1372  if(td.create_from_str(directive_, this))
1373  {
1374  add_tag_directive(td);
1375  return true;
1376  }
1377  return false;
1378 }
bool add_tag_directive(csubstr directive)
Definition: tree.cpp:1369

References c4::yml::TagDirective::create_from_str().

◆ add_tag_directive() [2/2]

id_type c4::yml::Tree::add_tag_directive ( TagDirective const &  td)

Definition at line 1355 of file tree.cpp.

1356 {
1357  _RYML_CB_CHECK(m_callbacks, !td.handle.empty());
1358  _RYML_CB_CHECK(m_callbacks, !td.prefix.empty());
1359  _RYML_CB_CHECK(m_callbacks, td.handle.begins_with('!'));
1360  _RYML_CB_CHECK(m_callbacks, td.handle.ends_with('!'));
1361  // https://yaml.org/spec/1.2.2/#rule-ns-word-char
1362  _RYML_CB_CHECK(m_callbacks, td.handle == '!' || td.handle == "!!" || td.handle.trim('!').first_not_of("01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-") == npos);
1363  id_type pos = num_tag_directives();
1364  _RYML_CB_CHECK(m_callbacks, pos < RYML_MAX_TAG_DIRECTIVES);
1365  m_tag_directives[pos] = td;
1366  return pos;
1367 }
id_type num_tag_directives() const
Definition: tree.cpp:1340
@ npos
a null string position
Definition: common.hpp:267

References c4::yml::TagDirective::handle, m_callbacks, m_tag_directives, c4::yml::npos, num_tag_directives(), c4::yml::TagDirective::prefix, and RYML_MAX_TAG_DIRECTIVES.

◆ clear_tag_directives()

void c4::yml::Tree::clear_tag_directives ( )

Definition at line 1349 of file tree.cpp.

1350 {
1351  for(TagDirective &td : m_tag_directives)
1352  td = {};
1353 }

References m_tag_directives.

◆ resolve_tag()

size_t c4::yml::Tree::resolve_tag ( substr  output,
csubstr  tag,
id_type  node_id 
) const

resolve the given tag, appearing at node_id.

Write the result into output.

Returns
the number of characters required for the resolved tag

Definition at line 1380 of file tree.cpp.

1381 {
1382  // lookup from the end. We want to find the first directive that
1383  // matches the tag and has a target node id leq than the given
1384  // node_id.
1385  for(id_type i = RYML_MAX_TAG_DIRECTIVES-1; i != (id_type)-1; --i)
1386  {
1387  auto const& td = m_tag_directives[i];
1388  if(td.handle.empty())
1389  continue;
1390  if(tag.begins_with(td.handle) && td.next_node_id <= node_id)
1391  return td.transform(tag, output, m_callbacks);
1392  }
1393  if(tag.begins_with('!'))
1394  {
1395  if(is_custom_tag(tag))
1396  {
1397  _RYML_CB_ERR(m_callbacks, "tag directive not found");
1398  }
1399  }
1400  return 0; // return 0 to signal that the tag is local and cannot be resolved
1401 }
bool is_custom_tag(csubstr tag)
Definition: tag.cpp:9
size_t transform(csubstr tag, substr output, Callbacks const &callbacks) const
Definition: tag.cpp:243

References c4::yml::is_custom_tag(), m_callbacks, m_tag_directives, RYML_MAX_TAG_DIRECTIVES, and c4::yml::TagDirective::transform().

◆ resolve_tag_sub()

csubstr c4::yml::Tree::resolve_tag_sub ( substr  output,
csubstr  tag,
id_type  node_id 
) const
inline

Definition at line 600 of file tree.hpp.

601  {
602  size_t needed = resolve_tag(output, tag, node_id);
603  return needed <= output.len ? output.first(needed) : output;
604  }
size_t resolve_tag(substr output, csubstr tag, id_type node_id) const
resolve the given tag, appearing at node_id.
Definition: tree.cpp:1380

◆ begin_tag_directives()

TagDirective const* c4::yml::Tree::begin_tag_directives ( ) const
inline

Definition at line 606 of file tree.hpp.

606 { return m_tag_directives; }

◆ end_tag_directives()

TagDirective const* c4::yml::Tree::end_tag_directives ( ) const
inline

Definition at line 607 of file tree.hpp.

607 { return m_tag_directives + num_tag_directives(); }

◆ tag_directives()

c4::yml::TagDirectiveRange c4::yml::Tree::tag_directives ( ) const
inline

Definition at line 608 of file tree.hpp.

TagDirective const * end_tag_directives() const
Definition: tree.hpp:607
TagDirective const * begin_tag_directives() const
Definition: tree.hpp:606

◆ insert_child()

id_type c4::yml::Tree::insert_child ( id_type  parent,
id_type  after 
)
inline

create and insert a new child of parent.

insert after the (to-be) sibling after, which must be a child of parent. To insert as the first child, set after to NONE

Definition at line 623 of file tree.hpp.

624  {
625  _RYML_CB_ASSERT(m_callbacks, parent != NONE);
626  _RYML_CB_ASSERT(m_callbacks, is_container(parent) || is_root(parent));
627  _RYML_CB_ASSERT(m_callbacks, after == NONE || (_p(after)->m_parent == parent));
628  id_type child = _claim();
629  _set_hierarchy(child, parent, after);
630  return child;
631  }
id_type _claim()
Definition: tree.cpp:377

References c4::yml::NONE.

◆ prepend_child()

id_type c4::yml::Tree::prepend_child ( id_type  parent)
inline

create and insert a node as the first child of parent

Definition at line 633 of file tree.hpp.

633 { return insert_child(parent, NONE); }
id_type insert_child(id_type parent, id_type after)
create and insert a new child of parent.
Definition: tree.hpp:623

References c4::yml::NONE.

◆ append_child()

id_type c4::yml::Tree::append_child ( id_type  parent)
inline

create and insert a node as the last child of parent

Definition at line 635 of file tree.hpp.

635 { return insert_child(parent, _p(parent)->m_last_child); }

Referenced by merge_with(), and set_root_as_stream().

◆ _append_child__unprotected()

id_type c4::yml::Tree::_append_child__unprotected ( id_type  parent)
inline

Definition at line 636 of file tree.hpp.

637  {
638  id_type child = _claim();
639  _set_hierarchy(child, parent, _p(parent)->m_last_child);
640  return child;
641  }

◆ insert_sibling()

id_type c4::yml::Tree::insert_sibling ( id_type  node,
id_type  after 
)
inline

create and insert a new sibling of n. insert after "after"

Definition at line 656 of file tree.hpp.

657  {
658  return insert_child(_p(node)->m_parent, after);
659  }

◆ prepend_sibling()

id_type c4::yml::Tree::prepend_sibling ( id_type  node)
inline

create and insert a node as the first node of parent

Definition at line 661 of file tree.hpp.

661 { return prepend_child(_p(node)->m_parent); }
id_type prepend_child(id_type parent)
create and insert a node as the first child of parent
Definition: tree.hpp:633

◆ append_sibling()

id_type c4::yml::Tree::append_sibling ( id_type  node)
inline

Definition at line 662 of file tree.hpp.

662 { return append_child(_p(node)->m_parent); }
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:635

◆ remove()

void c4::yml::Tree::remove ( id_type  node)
inline

remove an entire branch at once: ie remove the children and the node itself

Definition at line 667 of file tree.hpp.

668  {
669  remove_children(node);
670  _release(node);
671  }
void remove_children(id_type node)
remove all the node's children, but keep the node itself
Definition: tree.cpp:857

Referenced by duplicate_children_no_rep(), and move().

◆ remove_children()

void c4::yml::Tree::remove_children ( id_type  node)

remove all the node's children, but keep the node itself

Definition at line 857 of file tree.cpp.

858 {
859  _RYML_CB_ASSERT(m_callbacks, get(node) != nullptr);
860  id_type ich = get(node)->m_first_child;
861  while(ich != NONE)
862  {
863  remove_children(ich);
864  _RYML_CB_ASSERT(m_callbacks, get(ich) != nullptr);
865  id_type next = get(ich)->m_next_sibling;
866  _release(ich);
867  if(ich == get(node)->m_last_child)
868  break;
869  ich = next;
870  }
871 }

References get(), m_callbacks, c4::yml::NodeData::m_first_child, c4::yml::NodeData::m_next_sibling, and c4::yml::NONE.

Referenced by change_type(), and merge_with().

◆ change_type() [1/2]

bool c4::yml::Tree::change_type ( id_type  node,
NodeType  type 
)

change the type of the node to one of MAP, SEQ or VAL.

type must have one and only one of MAP,SEQ,VAL; type may possibly have KEY, but if it does, then the node must also have KEY. Changing to the same type is a no-op. Otherwise, changing to a different type will initialize the node with an empty value of the desired type: changing to VAL will initialize with a null scalar (~), changing to MAP will initialize with an empty map ({}), and changing to SEQ will initialize with an empty seq ([]).

Definition at line 873 of file tree.cpp.

874 {
875  _RYML_CB_ASSERT(m_callbacks, type.is_val() || type.is_map() || type.is_seq());
876  _RYML_CB_ASSERT(m_callbacks, type.is_val() + type.is_map() + type.is_seq() == 1);
877  _RYML_CB_ASSERT(m_callbacks, type.has_key() == has_key(node) || (has_key(node) && !type.has_key()));
878  NodeData *d = _p(node);
879  if(type.is_map() && is_map(node))
880  return false;
881  else if(type.is_seq() && is_seq(node))
882  return false;
883  else if(type.is_val() && is_val(node))
884  return false;
885  d->m_type = (d->m_type & (~(MAP|SEQ|VAL))) | type;
886  remove_children(node);
887  return true;
888 }
NodeType type(id_type node) const
Definition: tree.hpp:337
bool is_val(id_type node) const
Definition: tree.hpp:370

References _p(), c4::yml::NodeType::has_key(), has_key(), c4::yml::NodeType::is_map(), is_map(), c4::yml::NodeType::is_seq(), is_seq(), c4::yml::NodeType::is_val(), is_val(), m_callbacks, c4::yml::NodeData::m_type, c4::yml::MAP, remove_children(), c4::yml::SEQ, type(), and c4::yml::VAL.

◆ change_type() [2/2]

bool c4::yml::Tree::change_type ( id_type  node,
type_bits  type 
)
inline

Definition at line 687 of file tree.hpp.

688  {
689  return change_type(node, (NodeType)type);
690  }
bool change_type(id_type node, NodeType type)
change the type of the node to one of MAP, SEQ or VAL.
Definition: tree.cpp:873

◆ move() [1/3]

void c4::yml::Tree::move ( id_type  node,
id_type  after 
)

change the node's position in the parent

Definition at line 782 of file tree.cpp.

783 {
784  _RYML_CB_ASSERT(m_callbacks, node != NONE);
785  _RYML_CB_ASSERT(m_callbacks, node != after);
786  _RYML_CB_ASSERT(m_callbacks, ! is_root(node));
787  _RYML_CB_ASSERT(m_callbacks, (after == NONE) || (has_sibling(node, after) && has_sibling(after, node)));
788 
789  _rem_hierarchy(node);
790  _set_hierarchy(node, parent(node), after);
791 }
bool has_sibling(id_type node, id_type sib) const
true if node has a sibling with id sib
Definition: tree.hpp:433

References has_sibling(), is_root(), m_callbacks, c4::yml::NONE, and parent().

Referenced by duplicate_children_no_rep(), c4::yml::NodeRef::move(), and set_root_as_stream().

◆ move() [2/3]

void c4::yml::Tree::move ( id_type  node,
id_type  new_parent,
id_type  after 
)

change the node's parent and position

Definition at line 795 of file tree.cpp.

796 {
797  _RYML_CB_ASSERT(m_callbacks, node != NONE);
798  _RYML_CB_ASSERT(m_callbacks, node != after);
799  _RYML_CB_ASSERT(m_callbacks, new_parent != NONE);
800  _RYML_CB_ASSERT(m_callbacks, new_parent != node);
801  _RYML_CB_ASSERT(m_callbacks, new_parent != after);
802  _RYML_CB_ASSERT(m_callbacks, ! is_root(node));
803 
804  _rem_hierarchy(node);
805  _set_hierarchy(node, new_parent, after);
806 }

References is_root(), m_callbacks, and c4::yml::NONE.

◆ move() [3/3]

id_type c4::yml::Tree::move ( Tree src,
id_type  node,
id_type  new_parent,
id_type  after 
)

change the node's parent and position to a different tree

Returns
the index of the new node in the destination tree

Definition at line 808 of file tree.cpp.

809 {
810  _RYML_CB_ASSERT(m_callbacks, src != nullptr);
811  _RYML_CB_ASSERT(m_callbacks, node != NONE);
812  _RYML_CB_ASSERT(m_callbacks, new_parent != NONE);
813  _RYML_CB_ASSERT(m_callbacks, new_parent != after);
814 
815  id_type dup = duplicate(src, node, new_parent, after);
816  src->remove(node);
817  return dup;
818 }
id_type duplicate(id_type node, id_type new_parent, id_type after)
recursively duplicate a node from this tree into a new parent, placing it after one of its children
Definition: tree.cpp:892

References duplicate(), m_callbacks, c4::yml::NONE, and remove().

◆ set_root_as_stream()

void c4::yml::Tree::set_root_as_stream ( )

ensure the first node is a stream.

Eg, change this tree

DOCMAP MAP KEYVAL KEYVAL SEQ VAL

to

STREAM DOCMAP MAP KEYVAL KEYVAL SEQ VAL

If the root is already a stream, this is a no-op.

Definition at line 820 of file tree.cpp.

821 {
822  id_type root = root_id();
823  if(is_stream(root))
824  return;
825  // don't use _add_flags() because it's checked and will fail
826  if(!has_children(root))
827  {
828  if(is_val(root))
829  {
830  _p(root)->m_type.add(SEQ);
831  id_type next_doc = append_child(root);
832  _copy_props_wo_key(next_doc, root);
833  _p(next_doc)->m_type.add(DOC);
834  _p(next_doc)->m_type.rem(SEQ);
835  }
836  _p(root)->m_type = STREAM;
837  return;
838  }
839  _RYML_CB_ASSERT(m_callbacks, !has_key(root));
840  id_type next_doc = append_child(root);
841  _copy_props_wo_key(next_doc, root);
842  _add_flags(next_doc, DOC);
843  for(id_type prev = NONE, ch = first_child(root), next = next_sibling(ch); ch != NONE; )
844  {
845  if(ch == next_doc)
846  break;
847  move(ch, next_doc, prev);
848  prev = ch;
849  ch = next;
850  next = next_sibling(next);
851  }
852  _p(root)->m_type = STREAM;
853 }
void move(id_type node, id_type after)
change the node's position in the parent
Definition: tree.cpp:782
bool is_stream(id_type node) const
Definition: tree.hpp:363
void rem(NodeType_e t) noexcept
Definition: node_type.hpp:134
void add(NodeType_e t) noexcept
Definition: node_type.hpp:133

References _p(), c4::yml::NodeType::add(), append_child(), c4::yml::DOC, first_child(), has_children(), has_key(), is_stream(), is_val(), m_callbacks, c4::yml::NodeData::m_type, move(), next_sibling(), c4::yml::NONE, c4::yml::NodeType::rem(), root_id(), c4::yml::SEQ, and c4::yml::STREAM.

◆ duplicate() [1/2]

id_type c4::yml::Tree::duplicate ( id_type  node,
id_type  new_parent,
id_type  after 
)

recursively duplicate a node from this tree into a new parent, placing it after one of its children

Returns
the index of the copy

Definition at line 892 of file tree.cpp.

893 {
894  return duplicate(this, node, parent, after);
895 }

References parent().

Referenced by c4::yml::NodeRef::duplicate(), duplicate_children(), duplicate_children_no_rep(), and move().

◆ duplicate() [2/2]

id_type c4::yml::Tree::duplicate ( Tree const *  src,
id_type  node,
id_type  new_parent,
id_type  after 
)

recursively duplicate a node from a different tree into a new parent, placing it after one of its children

Returns
the index of the copy

Definition at line 897 of file tree.cpp.

898 {
899  _RYML_CB_ASSERT(m_callbacks, src != nullptr);
900  _RYML_CB_ASSERT(m_callbacks, node != NONE);
901  _RYML_CB_ASSERT(m_callbacks, parent != NONE);
902  _RYML_CB_ASSERT(m_callbacks, ! src->is_root(node));
903 
904  id_type copy = _claim();
905 
906  _copy_props(copy, src, node);
907  _set_hierarchy(copy, parent, after);
908  duplicate_children(src, node, copy, NONE);
909 
910  return copy;
911 }
id_type duplicate_children(id_type node, id_type parent, id_type after)
recursively duplicate the node's children (but not the node)
Definition: tree.cpp:914

References _claim(), duplicate_children(), is_root(), m_callbacks, c4::yml::NONE, and parent().

◆ duplicate_children() [1/2]

id_type c4::yml::Tree::duplicate_children ( id_type  node,
id_type  parent,
id_type  after 
)

recursively duplicate the node's children (but not the node)

Returns
the index of the last duplicated child

Definition at line 914 of file tree.cpp.

915 {
916  return duplicate_children(this, node, parent, after);
917 }

References parent().

Referenced by duplicate(), c4::yml::NodeRef::duplicate_children(), and duplicate_contents().

◆ duplicate_children() [2/2]

id_type c4::yml::Tree::duplicate_children ( Tree const *  src,
id_type  node,
id_type  parent,
id_type  after 
)

recursively duplicate the node's children (but not the node), where the node is from a different tree

Returns
the index of the last duplicated child

Definition at line 919 of file tree.cpp.

920 {
921  _RYML_CB_ASSERT(m_callbacks, src != nullptr);
922  _RYML_CB_ASSERT(m_callbacks, node != NONE);
923  _RYML_CB_ASSERT(m_callbacks, parent != NONE);
924  _RYML_CB_ASSERT(m_callbacks, after == NONE || has_child(parent, after));
925 
926  id_type prev = after;
927  for(id_type i = src->first_child(node); i != NONE; i = src->next_sibling(i))
928  {
929  prev = duplicate(src, i, parent, prev);
930  }
931 
932  return prev;
933 }
bool has_child(id_type node, id_type ch) const
true if node has a child with id ch
Definition: tree.hpp:426

References duplicate(), first_child(), has_child(), m_callbacks, next_sibling(), c4::yml::NONE, and parent().

◆ duplicate_children_no_rep() [1/2]

id_type c4::yml::Tree::duplicate_children_no_rep ( id_type  node,
id_type  parent,
id_type  after 
)

duplicate the node's children (but not the node) in a new parent, but omit repetitions where a duplicated node has the same key (in maps) or value (in seqs).

If one of the duplicated children has the same key (in maps) or value (in seqs) as one of the parent's children, the one that is placed closest to the end will prevail.

Definition at line 951 of file tree.cpp.

952 {
953  return duplicate_children_no_rep(this, node, parent, after);
954 }
id_type duplicate_children_no_rep(id_type node, id_type parent, id_type after)
duplicate the node's children (but not the node) in a new parent, but omit repetitions where a duplic...
Definition: tree.cpp:951

References parent().

◆ duplicate_children_no_rep() [2/2]

id_type c4::yml::Tree::duplicate_children_no_rep ( Tree const *  src,
id_type  node,
id_type  parent,
id_type  after 
)

Definition at line 956 of file tree.cpp.

957 {
958  _RYML_CB_ASSERT(m_callbacks, node != NONE);
959  _RYML_CB_ASSERT(m_callbacks, parent != NONE);
960  _RYML_CB_ASSERT(m_callbacks, after == NONE || has_child(parent, after));
961 
962  // don't loop using pointers as there may be a relocation
963 
964  // find the position where "after" is
965  id_type after_pos = NONE;
966  if(after != NONE)
967  {
968  for(id_type i = first_child(parent), icount = 0; i != NONE; ++icount, i = next_sibling(i))
969  {
970  if(i == after)
971  {
972  after_pos = icount;
973  break;
974  }
975  }
976  _RYML_CB_ASSERT(m_callbacks, after_pos != NONE);
977  }
978 
979  // for each child to be duplicated...
980  id_type prev = after;
981  for(id_type i = src->first_child(node); i != NONE; i = src->next_sibling(i))
982  {
983  _c4dbgpf("duplicate_no_rep: {} -> {}/{}", i, parent, prev);
984  _RYML_CB_CHECK(m_callbacks, this != src || (parent != i && !is_ancestor(parent, i)));
985  if(is_seq(parent))
986  {
987  _c4dbgpf("duplicate_no_rep: {} is seq", parent);
988  prev = duplicate(src, i, parent, prev);
989  }
990  else
991  {
992  _c4dbgpf("duplicate_no_rep: {} is map", parent);
993  _RYML_CB_ASSERT(m_callbacks, is_map(parent));
994  // does the parent already have a node with key equal to that of the current duplicate?
995  id_type dstnode_dup = NONE, dstnode_dup_pos = NONE;
996  {
997  csubstr srckey = src->key(i);
998  for(id_type j = first_child(parent), jcount = 0; j != NONE; ++jcount, j = next_sibling(j))
999  {
1000  if(key(j) == srckey)
1001  {
1002  _c4dbgpf("duplicate_no_rep: found matching key '{}' src={}/{} dst={}/{}", srckey, node, i, parent, j);
1003  dstnode_dup = j;
1004  dstnode_dup_pos = jcount;
1005  break;
1006  }
1007  }
1008  }
1009  _c4dbgpf("duplicate_no_rep: dstnode_dup={} dstnode_dup_pos={} after_pos={}", dstnode_dup, dstnode_dup_pos, after_pos);
1010  if(dstnode_dup == NONE) // there is no repetition; just duplicate
1011  {
1012  _c4dbgpf("duplicate_no_rep: no repetition, just duplicate i={} parent={} prev={}", i, parent, prev);
1013  prev = duplicate(src, i, parent, prev);
1014  }
1015  else // yes, there is a repetition
1016  {
1017  if(after_pos != NONE && dstnode_dup_pos <= after_pos)
1018  {
1019  // the dst duplicate is located before the node which will be inserted,
1020  // and will be overridden by the duplicate. So replace it.
1021  _c4dbgpf("duplicate_no_dstnode_dup: replace {}/{} with {}/{}", parent, dstnode_dup, node, i);
1022  if(prev == dstnode_dup)
1023  prev = prev_sibling(dstnode_dup);
1024  remove(dstnode_dup);
1025  prev = duplicate(src, i, parent, prev);
1026  }
1027  else if(prev == NONE)
1028  {
1029  _c4dbgpf("duplicate_no_dstnode_dup: {}=prev <- {}", prev, dstnode_dup);
1030  // first iteration with prev = after = NONE and dstnode_dupetition
1031  prev = dstnode_dup;
1032  }
1033  else if(dstnode_dup != prev)
1034  {
1035  // dstnode_dup is located after the node which will be inserted
1036  // and overrides it. So move the dstnode_dup into this node's place.
1037  _c4dbgpf("duplicate_no_dstnode_dup: move({}, {})", dstnode_dup, prev);
1038  move(dstnode_dup, prev);
1039  prev = dstnode_dup;
1040  }
1041  } // there's a dstnode_dupetition
1042  }
1043  }
1044 
1045  return prev;
1046 }
id_type prev_sibling(id_type node) const
Definition: tree.hpp:459
bool is_ancestor(id_type node, id_type ancestor) const
true when ancestor is parent or parent of a parent of node
Definition: tree.cpp:1252
void remove(id_type node)
remove an entire branch at once: ie remove the children and the node itself
Definition: tree.hpp:667

References duplicate(), first_child(), has_child(), is_ancestor(), is_map(), is_seq(), key(), m_callbacks, move(), next_sibling(), c4::yml::NONE, parent(), prev_sibling(), and remove().

◆ duplicate_contents() [1/2]

void c4::yml::Tree::duplicate_contents ( id_type  node,
id_type  where 
)

Definition at line 936 of file tree.cpp.

937 {
938  duplicate_contents(this, node, where);
939 }
void duplicate_contents(id_type node, id_type where)
Definition: tree.cpp:936

◆ duplicate_contents() [2/2]

void c4::yml::Tree::duplicate_contents ( Tree const *  src,
id_type  node,
id_type  where 
)

Definition at line 941 of file tree.cpp.

942 {
943  _RYML_CB_ASSERT(m_callbacks, src != nullptr);
944  _RYML_CB_ASSERT(m_callbacks, node != NONE);
945  _RYML_CB_ASSERT(m_callbacks, where != NONE);
946  _copy_props_wo_key(where, src, node);
947  duplicate_children(src, node, where, last_child(where));
948 }
id_type last_child(id_type node) const
Definition: tree.hpp:466

References duplicate_children(), last_child(), m_callbacks, and c4::yml::NONE.

◆ merge_with()

void c4::yml::Tree::merge_with ( Tree const *  src,
id_type  src_node = NONE,
id_type  dst_root = NONE 
)

Definition at line 1051 of file tree.cpp.

1052 {
1053  _RYML_CB_ASSERT(m_callbacks, src != nullptr);
1054  if(src_node == NONE)
1055  src_node = src->root_id();
1056  if(dst_node == NONE)
1057  dst_node = root_id();
1058  _RYML_CB_ASSERT(m_callbacks, src->has_val(src_node) || src->is_seq(src_node) || src->is_map(src_node));
1059  if(src->has_val(src_node))
1060  {
1061  type_bits mask_src = ~STYLE; // keep the existing style if it is already a val
1062  if( ! has_val(dst_node))
1063  {
1064  if(has_children(dst_node))
1065  remove_children(dst_node);
1066  mask_src |= VAL_STYLE; // copy the src style
1067  }
1068  if(src->is_keyval(src_node))
1069  {
1070  _copy_props(dst_node, src, src_node, mask_src);
1071  }
1072  else
1073  {
1074  _RYML_CB_ASSERT(m_callbacks, src->is_val(src_node));
1075  _copy_props_wo_key(dst_node, src, src_node, mask_src);
1076  }
1077  }
1078  else if(src->is_seq(src_node))
1079  {
1080  if( ! is_seq(dst_node))
1081  {
1082  if(has_children(dst_node))
1083  remove_children(dst_node);
1084  _clear_type(dst_node);
1085  if(src->has_key(src_node))
1086  to_seq(dst_node, src->key(src_node));
1087  else
1088  to_seq(dst_node);
1089  _p(dst_node)->m_type = src->_p(src_node)->m_type;
1090  }
1091  for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
1092  {
1093  id_type dch = append_child(dst_node);
1094  _copy_props_wo_key(dch, src, sch);
1095  merge_with(src, sch, dch);
1096  }
1097  }
1098  else
1099  {
1100  _RYML_CB_ASSERT(m_callbacks, src->is_map(src_node));
1101  if( ! is_map(dst_node))
1102  {
1103  if(has_children(dst_node))
1104  remove_children(dst_node);
1105  _clear_type(dst_node);
1106  if(src->has_key(src_node))
1107  to_map(dst_node, src->key(src_node));
1108  else
1109  to_map(dst_node);
1110  _p(dst_node)->m_type = src->_p(src_node)->m_type;
1111  }
1112  for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
1113  {
1114  id_type dch = find_child(dst_node, src->key(sch));
1115  if(dch == NONE)
1116  {
1117  dch = append_child(dst_node);
1118  _copy_props(dch, src, sch);
1119  }
1120  merge_with(src, sch, dch);
1121  }
1122  }
1123 }
void to_seq(id_type node, csubstr key, type_bits more_flags=0)
Definition: tree.cpp:1313
void to_map(id_type node, csubstr key, type_bits more_flags=0)
Definition: tree.cpp:1295
void merge_with(Tree const *src, id_type src_node=NONE, id_type dst_root=NONE)
Definition: tree.cpp:1051
uint32_t type_bits
the integral type necessary to cover all the bits for NodeType_e
Definition: node_type.hpp:26
@ VAL_STYLE
mask of all the scalar styles for val (not container styles!)
Definition: node_type.hpp:89

References _p(), append_child(), find_child(), first_child(), has_children(), has_key(), has_val(), is_keyval(), is_map(), is_seq(), is_val(), key(), m_callbacks, c4::yml::NodeData::m_type, next_sibling(), c4::yml::NONE, remove_children(), root_id(), c4::yml::STYLE, to_map(), to_seq(), and c4::yml::VAL_STYLE.

Referenced by lookup_path_or_modify().

◆ arena_pos()

size_t c4::yml::Tree::arena_pos ( ) const
inline

get the current size of the tree's internal arena

Definition at line 775 of file tree.hpp.

775 { return m_arena_pos; }

◆ arena_size()

size_t c4::yml::Tree::arena_size ( ) const
inline

get the current size of the tree's internal arena

Definition at line 777 of file tree.hpp.

777 { return m_arena_pos; }

Referenced by resolve_tags().

◆ arena_capacity()

size_t c4::yml::Tree::arena_capacity ( ) const
inline

get the current capacity of the tree's internal arena

Definition at line 779 of file tree.hpp.

779 { return m_arena.len; }

Referenced by Tree(), and sample::sample_tree_arena().

◆ arena_slack()

size_t c4::yml::Tree::arena_slack ( ) const
inline

get the current slack of the tree's internal arena

Definition at line 781 of file tree.hpp.

781 { _RYML_CB_ASSERT(m_callbacks, m_arena.len >= m_arena_pos); return m_arena.len - m_arena_pos; }

◆ arena() [1/2]

csubstr c4::yml::Tree::arena ( ) const
inline

get the current arena

Definition at line 784 of file tree.hpp.

784 { return m_arena.first(m_arena_pos); }

Referenced by sample::sample_empty_null_values(), sample::sample_fundamental_types(), sample::sample_quick_overview(), and sample::sample_tree_arena().

◆ arena() [2/2]

substr c4::yml::Tree::arena ( )
inline

get the current arena

Definition at line 786 of file tree.hpp.

786 { return m_arena.first(m_arena_pos); } // NOLINT(readability-make-member-function-const)

◆ in_arena()

bool c4::yml::Tree::in_arena ( csubstr  s) const
inline

return true if the given substring is part of the tree's string arena

Definition at line 789 of file tree.hpp.

790  {
791  return m_arena.is_super(s);
792  }

Referenced by sample::sample_quick_overview().

◆ to_arena() [1/5]

template<class T >
auto c4::yml::Tree::to_arena ( T const &  a) -> typename std::enable_if<std::is_floating_point<T>::value, csubstr>::type
inline

serialize the given floating-point variable to the tree's arena, growing it as needed to accomodate the serialization.

Note
Growing the arena may cause relocation of the entire existing arena, and thus change the contents of individual nodes, and thus cost O(numnodes)+O(arenasize). To avoid this cost, ensure that the arena is reserved to an appropriate size using Tree::reserve_arena().
See also
alloc_arena()

Definition at line 805 of file tree.hpp.

807  {
808  substr rem(m_arena.sub(m_arena_pos));
809  size_t num = to_chars_float(rem, a);
810  if(num > rem.len)
811  {
812  rem = _grow_arena(num);
813  num = to_chars_float(rem, a);
814  _RYML_CB_ASSERT(m_callbacks, num <= rem.len);
815  }
816  rem = _request_span(num);
817  return rem;
818  }
size_t to_chars_float(substr buf, T val)
encode a floating point value to a string.
Definition: tree.hpp:1412

References c4::yml::to_chars_float().

Referenced by sample::sample_fundamental_types(), and sample::sample_tree_arena().

◆ to_arena() [2/5]

template<class T >
auto c4::yml::Tree::to_arena ( T const &  a) -> typename std::enable_if<!std::is_floating_point<T>::value, csubstr>::type
inline

serialize the given non-floating-point variable to the tree's arena, growing it as needed to accomodate the serialization.

Note
Growing the arena may cause relocation of the entire existing arena, and thus change the contents of individual nodes, and thus cost O(numnodes)+O(arenasize). To avoid this cost, ensure that the arena is reserved to an appropriate size using Tree::reserve_arena().
See also
alloc_arena()

Definition at line 831 of file tree.hpp.

833  {
834  substr rem(m_arena.sub(m_arena_pos));
835  size_t num = to_chars(rem, a);
836  if(num > rem.len)
837  {
838  rem = _grow_arena(num);
839  num = to_chars(rem, a);
840  _RYML_CB_ASSERT(m_callbacks, num <= rem.len);
841  }
842  rem = _request_span(num);
843  return rem;
844  }
size_t to_chars(substr buf, uint8_t v) noexcept
Definition: charconv.hpp:2328

References sample::to_chars().

◆ to_arena() [3/5]

csubstr c4::yml::Tree::to_arena ( csubstr  a)
inline

serialize the given csubstr to the tree's arena, growing the arena as needed to accomodate the serialization.

Note
Growing the arena may cause relocation of the entire existing arena, and thus change the contents of individual nodes, and thus cost O(numnodes)+O(arenasize). To avoid this cost, ensure that the arena is reserved to an appropriate size using Tree::reserve_arena().
See also
alloc_arena()

Definition at line 856 of file tree.hpp.

857  {
858  if(a.len > 0)
859  {
860  substr rem(m_arena.sub(m_arena_pos));
861  size_t num = to_chars(rem, a);
862  if(num > rem.len)
863  {
864  rem = _grow_arena(num);
865  num = to_chars(rem, a);
866  _RYML_CB_ASSERT(m_callbacks, num <= rem.len);
867  }
868  return _request_span(num);
869  }
870  else
871  {
872  if(a.str == nullptr)
873  {
874  return csubstr{};
875  }
876  else if(m_arena.str == nullptr)
877  {
878  // Arena is empty and we want to store a non-null
879  // zero-length string.
880  // Even though the string has zero length, we need
881  // some "memory" to store a non-nullptr string
882  _grow_arena(1);
883  }
884  return _request_span(0);
885  }
886  }

References sample::to_chars().

◆ to_arena() [4/5]

csubstr c4::yml::Tree::to_arena ( const char *  s)
inline

Definition at line 887 of file tree.hpp.

888  {
889  return to_arena(to_csubstr(s));
890  }
auto to_arena(T const &a) -> typename std::enable_if< std::is_floating_point< T >::value, csubstr >::type
serialize the given floating-point variable to the tree's arena, growing it as needed to accomodate t...
Definition: tree.hpp:805
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2186

References c4::to_csubstr().

◆ to_arena() [5/5]

static csubstr c4::yml::Tree::to_arena ( std::nullptr_t  )
inlinestatic

Definition at line 891 of file tree.hpp.

892  {
893  return csubstr{};
894  }

◆ copy_to_arena()

substr c4::yml::Tree::copy_to_arena ( csubstr  s)
inline

copy the given substr to the tree's arena, growing it by the required size

Note
Growing the arena may cause relocation of the entire existing arena, and thus change the contents of individual nodes, and thus cost O(numnodes)+O(arenasize). To avoid this cost, ensure that the arena is reserved to an appropriate size before using Tree::reserve_arena()
See also
reserve_arena()
alloc_arena()

Definition at line 908 of file tree.hpp.

909  {
910  substr cp = alloc_arena(s.len);
911  _RYML_CB_ASSERT(m_callbacks, cp.len == s.len);
912  _RYML_CB_ASSERT(m_callbacks, !s.overlaps(cp));
913  #if (!defined(__clang__)) && (defined(__GNUC__) && __GNUC__ >= 10)
914  C4_SUPPRESS_WARNING_GCC_PUSH
915  C4_SUPPRESS_WARNING_GCC("-Wstringop-overflow=") // no need for terminating \0
916  C4_SUPPRESS_WARNING_GCC("-Wrestrict") // there's an assert to ensure no violation of restrict behavior
917  #endif
918  if(s.len)
919  memcpy(cp.str, s.str, s.len);
920  #if (!defined(__clang__)) && (defined(__GNUC__) && __GNUC__ >= 10)
921  C4_SUPPRESS_WARNING_GCC_POP
922  #endif
923  return cp;
924  }
substr alloc_arena(size_t sz)
grow the tree's string arena by the given size and return a substr of the added portion
Definition: tree.hpp:936

Referenced by c4::yml::parse_in_arena(), c4::yml::parse_json_in_arena(), and sample::sample_tree_arena().

◆ alloc_arena()

substr c4::yml::Tree::alloc_arena ( size_t  sz)
inline

grow the tree's string arena by the given size and return a substr of the added portion

Note
Growing the arena may cause relocation of the entire existing arena, and thus change the contents of individual nodes, and thus cost O(numnodes)+O(arenasize). To avoid this cost, ensure that the arena is reserved to an appropriate size using .reserve_arena().
See also
reserve_arena()

Definition at line 936 of file tree.hpp.

937  {
938  if(sz > arena_slack())
939  _grow_arena(sz - arena_slack());
940  substr s = _request_span(sz);
941  return s;
942  }
size_t arena_slack() const
get the current slack of the tree's internal arena
Definition: tree.hpp:781

Referenced by sample::sample_tree_arena().

◆ reserve_arena()

void c4::yml::Tree::reserve_arena ( size_t  arena_cap)
inline

ensure the tree's internal string arena is at least the given capacity

Warning
This operation may be expensive, with a potential complexity of O(numNodes)+O(arenasize).
Growing the arena may cause relocation of the entire existing arena, and thus change the contents of individual nodes.

Definition at line 948 of file tree.hpp.

949  {
950  if(arena_cap > m_arena.len)
951  {
952  substr buf;
953  buf.str = (char*) m_callbacks.m_allocate(arena_cap, m_arena.str, m_callbacks.m_user_data);
954  buf.len = arena_cap;
955  if(m_arena.str)
956  {
957  _RYML_CB_ASSERT(m_callbacks, m_arena.len >= 0);
958  _relocate(buf); // does a memcpy and changes nodes using the arena
960  }
961  m_arena = buf;
962  }
963  }
void * m_user_data
Definition: common.hpp:377
pfn_allocate m_allocate
Definition: common.hpp:378
pfn_free m_free
Definition: common.hpp:379

Referenced by Tree(), resolve_tags(), sample::sample_global_allocator(), sample::sample_parse_reuse_tree(), and sample::sample_tree_arena().

◆ lookup_path()

Tree::lookup_result c4::yml::Tree::lookup_path ( csubstr  path,
id_type  start = NONE 
) const

for example foo.bar[0].baz

Definition at line 1514 of file tree.cpp.

1515 {
1516  if(start == NONE)
1517  start = root_id();
1518  lookup_result r(path, start);
1519  if(path.empty())
1520  return r;
1521  _lookup_path(&r);
1522  if(r.target == NONE && r.closest == start)
1523  r.closest = NONE;
1524  return r;
1525 }

References c4::yml::Tree::lookup_result::closest, c4::yml::NONE, root_id(), and c4::yml::Tree::lookup_result::target.

◆ lookup_path_or_modify() [1/2]

id_type c4::yml::Tree::lookup_path_or_modify ( csubstr  default_value,
csubstr  path,
id_type  start = NONE 
)

defaulted lookup: lookup path; if the lookup fails, recursively modify the tree so that the corresponding lookup_path() would return the default value.

See also
lookup_path()

Definition at line 1527 of file tree.cpp.

1528 {
1529  id_type target = _lookup_path_or_create(path, start);
1530  if(parent_is_map(target))
1531  to_keyval(target, key(target), default_value);
1532  else
1533  to_val(target, default_value);
1534  return target;
1535 }
void to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flags=0)
Definition: tree.cpp:1277
void to_val(id_type node, csubstr val, type_bits more_flags=0)
Definition: tree.cpp:1268

References key(), parent_is_map(), to_keyval(), and to_val().

◆ lookup_path_or_modify() [2/2]

id_type c4::yml::Tree::lookup_path_or_modify ( Tree const *  src,
id_type  src_node,
csubstr  path,
id_type  start = NONE 
)

defaulted lookup: lookup path; if the lookup fails, recursively modify the tree so that the corresponding lookup_path() would return the branch src_node (from the tree src).

See also
lookup_path()

Definition at line 1537 of file tree.cpp.

1538 {
1539  id_type target = _lookup_path_or_create(path, start);
1540  merge_with(src, src_node, target);
1541  return target;
1542 }

References merge_with().

◆ _claim()

id_type c4::yml::Tree::_claim ( )

Definition at line 377 of file tree.cpp.

378 {
379  if(m_free_head == NONE || m_buf == nullptr)
380  {
381  id_type sz = 2 * m_cap;
382  sz = sz ? sz : 16;
383  reserve(sz);
384  _RYML_CB_ASSERT(m_callbacks, m_free_head != NONE);
385  }
386 
387  _RYML_CB_ASSERT(m_callbacks, m_size < m_cap);
388  _RYML_CB_ASSERT(m_callbacks, m_free_head >= 0 && m_free_head < m_cap);
389 
390  id_type ichild = m_free_head;
391  NodeData *child = m_buf + ichild;
392 
393  ++m_size;
394  m_free_head = child->m_next_sibling;
395  if(m_free_head == NONE)
396  {
397  m_free_tail = NONE;
398  _RYML_CB_ASSERT(m_callbacks, m_size == m_cap);
399  }
400 
401  _clear(ichild);
402 
403  return ichild;
404 }

References child(), m_buf, m_callbacks, m_cap, m_free_head, m_free_tail, m_size, c4::yml::NONE, and reserve().

Referenced by duplicate().

Member Data Documentation

◆ m_buf

NodeData* c4::yml::Tree::m_buf

Definition at line 1302 of file tree.hpp.

Referenced by _claim(), clear(), and reserve().

◆ m_cap

id_type c4::yml::Tree::m_cap

Definition at line 1303 of file tree.hpp.

Referenced by _claim(), clear(), and reserve().

◆ m_size

id_type c4::yml::Tree::m_size

Definition at line 1305 of file tree.hpp.

Referenced by _claim(), clear(), reserve(), and resolve().

◆ m_free_head

id_type c4::yml::Tree::m_free_head

Definition at line 1307 of file tree.hpp.

Referenced by _claim(), clear(), and reserve().

◆ m_free_tail

id_type c4::yml::Tree::m_free_tail

Definition at line 1308 of file tree.hpp.

Referenced by _claim(), clear(), and reserve().

◆ m_arena

substr c4::yml::Tree::m_arena

Definition at line 1310 of file tree.hpp.

◆ m_arena_pos

size_t c4::yml::Tree::m_arena_pos

Definition at line 1311 of file tree.hpp.

◆ m_callbacks

◆ m_tag_directives

TagDirective c4::yml::Tree::m_tag_directives[RYML_MAX_TAG_DIRECTIVES]

The documentation for this class was generated from the following files: