rapidyaml  0.9.0
parse and emit YAML, and do it fast
tree.cpp
Go to the documentation of this file.
1 #include "c4/yml/tree.hpp"
2 #include "c4/yml/detail/parser_dbg.hpp"
3 #include "c4/yml/node.hpp"
5 
6 
7 C4_SUPPRESS_WARNING_MSVC_WITH_PUSH(4296/*expression is always 'boolean_value'*/)
8 C4_SUPPRESS_WARNING_MSVC(4702/*unreachable code*/)
9 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wold-style-cast")
10 C4_SUPPRESS_WARNING_GCC("-Wtype-limits")
11 C4_SUPPRESS_WARNING_GCC("-Wuseless-cast")
12 
13 namespace c4 {
14 namespace yml {
15 
16 
17 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
20 
21 NodeRef Tree::rootref()
22 {
23  return NodeRef(this, root_id());
24 }
25 ConstNodeRef Tree::rootref() const
26 {
27  return ConstNodeRef(this, root_id());
28 }
29 
30 ConstNodeRef Tree::crootref() const
31 {
32  return ConstNodeRef(this, root_id());
33 }
34 
35 NodeRef Tree::ref(id_type id)
36 {
37  _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
38  return NodeRef(this, id);
39 }
40 ConstNodeRef Tree::ref(id_type id) const
41 {
42  _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
43  return ConstNodeRef(this, id);
44 }
45 ConstNodeRef Tree::cref(id_type id) const
46 {
47  _RYML_CB_ASSERT(m_callbacks, id != NONE && id >= 0 && id < m_cap);
48  return ConstNodeRef(this, id);
49 }
50 
51 NodeRef Tree::operator[] (csubstr key)
52 {
53  return rootref()[key];
54 }
55 ConstNodeRef Tree::operator[] (csubstr key) const
56 {
57  return rootref()[key];
58 }
59 
60 NodeRef Tree::operator[] (id_type i)
61 {
62  return rootref()[i];
63 }
64 ConstNodeRef Tree::operator[] (id_type i) const
65 {
66  return rootref()[i];
67 }
68 
69 NodeRef Tree::docref(id_type i)
70 {
71  return ref(doc(i));
72 }
73 ConstNodeRef Tree::docref(id_type i) const
74 {
75  return cref(doc(i));
76 }
77 ConstNodeRef Tree::cdocref(id_type i) const
78 {
79  return cref(doc(i));
80 }
81 
82 
83 //-----------------------------------------------------------------------------
84 Tree::Tree(Callbacks const& cb)
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)
93  , m_tag_directives()
94 {
95 }
96 
97 Tree::Tree(id_type node_capacity, size_t arena_capacity, Callbacks const& cb)
98  : Tree(cb)
99 {
100  reserve(node_capacity);
102 }
103 
105 {
106  _free();
107 }
108 
109 
110 Tree::Tree(Tree const& that) : Tree(that.m_callbacks)
111 {
112  _copy(that);
113 }
114 
116 {
117  if(&that != this)
118  {
119  _free();
120  m_callbacks = that.m_callbacks;
121  _copy(that);
122  }
123  return *this;
124 }
125 
126 Tree::Tree(Tree && that) noexcept : Tree(that.m_callbacks)
127 {
128  _move(that);
129 }
130 
131 Tree& Tree::operator= (Tree && that) noexcept
132 {
133  if(&that != this)
134  {
135  _free();
136  m_callbacks = that.m_callbacks;
137  _move(that);
138  }
139  return *this;
140 }
141 
142 void Tree::_free()
143 {
144  if(m_buf)
145  {
146  _RYML_CB_ASSERT(m_callbacks, m_cap > 0);
147  _RYML_CB_FREE(m_callbacks, m_buf, NodeData, (size_t)m_cap);
148  }
149  if(m_arena.str)
150  {
151  _RYML_CB_ASSERT(m_callbacks, m_arena.len > 0);
152  _RYML_CB_FREE(m_callbacks, m_arena.str, char, m_arena.len);
153  }
154  _clear();
155 }
156 
157 
158 C4_SUPPRESS_WARNING_GCC_PUSH
159 #if defined(__GNUC__) && __GNUC__>= 8
160  C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wclass-memaccess") // error: ‘void* memset(void*, int, size_t)’ clearing an object of type ‘class c4::yml::Tree’ with no trivial copy-assignment; use assignment or value-initialization instead
161 #endif
162 
163 void Tree::_clear()
164 {
165  m_buf = nullptr;
166  m_cap = 0;
167  m_size = 0;
168  m_free_head = 0;
169  m_free_tail = 0;
170  m_arena = {};
171  m_arena_pos = 0;
172  for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
173  m_tag_directives[i] = {};
174 }
175 
176 void Tree::_copy(Tree const& that)
177 {
178  _RYML_CB_ASSERT(m_callbacks, m_buf == nullptr);
179  _RYML_CB_ASSERT(m_callbacks, m_arena.str == nullptr);
180  _RYML_CB_ASSERT(m_callbacks, m_arena.len == 0);
181  if(that.m_cap)
182  {
183  m_buf = _RYML_CB_ALLOC_HINT(m_callbacks, NodeData, (size_t)that.m_cap, that.m_buf);
184  memcpy(m_buf, that.m_buf, (size_t)that.m_cap * sizeof(NodeData));
185  }
186  m_cap = that.m_cap;
187  m_size = that.m_size;
188  m_free_head = that.m_free_head;
189  m_free_tail = that.m_free_tail;
190  m_arena_pos = that.m_arena_pos;
191  m_arena = that.m_arena;
192  if(that.m_arena.str)
193  {
194  _RYML_CB_ASSERT(m_callbacks, that.m_arena.len > 0);
195  substr arena;
196  arena.str = _RYML_CB_ALLOC_HINT(m_callbacks, char, that.m_arena.len, that.m_arena.str);
197  arena.len = that.m_arena.len;
198  _relocate(arena); // does a memcpy of the arena and updates nodes using the old arena
199  m_arena = arena;
200  }
201  for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
202  m_tag_directives[i] = that.m_tag_directives[i];
203 }
204 
205 void Tree::_move(Tree & that) noexcept
206 {
207  _RYML_CB_ASSERT(m_callbacks, m_buf == nullptr);
208  _RYML_CB_ASSERT(m_callbacks, m_arena.str == nullptr);
209  _RYML_CB_ASSERT(m_callbacks, m_arena.len == 0);
210  m_buf = that.m_buf;
211  m_cap = that.m_cap;
212  m_size = that.m_size;
213  m_free_head = that.m_free_head;
214  m_free_tail = that.m_free_tail;
215  m_arena = that.m_arena;
216  m_arena_pos = that.m_arena_pos;
217  for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
218  m_tag_directives[i] = that.m_tag_directives[i];
219  that._clear();
220 }
221 
222 void Tree::_relocate(substr next_arena)
223 {
224  _RYML_CB_ASSERT(m_callbacks, next_arena.not_empty());
225  _RYML_CB_ASSERT(m_callbacks, next_arena.len >= m_arena.len);
226  if(m_arena_pos)
227  memcpy(next_arena.str, m_arena.str, m_arena_pos);
228  for(NodeData *C4_RESTRICT n = m_buf, *e = m_buf + m_cap; n != e; ++n)
229  {
230  if(in_arena(n->m_key.scalar))
231  n->m_key.scalar = _relocated(n->m_key.scalar, next_arena);
232  if(in_arena(n->m_key.tag))
233  n->m_key.tag = _relocated(n->m_key.tag, next_arena);
234  if(in_arena(n->m_key.anchor))
235  n->m_key.anchor = _relocated(n->m_key.anchor, next_arena);
236  if(in_arena(n->m_val.scalar))
237  n->m_val.scalar = _relocated(n->m_val.scalar, next_arena);
238  if(in_arena(n->m_val.tag))
239  n->m_val.tag = _relocated(n->m_val.tag, next_arena);
240  if(in_arena(n->m_val.anchor))
241  n->m_val.anchor = _relocated(n->m_val.anchor, next_arena);
242  }
243  for(TagDirective &C4_RESTRICT td : m_tag_directives)
244  {
245  if(in_arena(td.prefix))
246  td.prefix = _relocated(td.prefix, next_arena);
247  if(in_arena(td.handle))
248  td.handle = _relocated(td.handle, next_arena);
249  }
250 }
251 
252 
253 //-----------------------------------------------------------------------------
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 }
289 
290 
291 //-----------------------------------------------------------------------------
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 }
311 
312 void Tree::_claim_root()
313 {
314  id_type r = _claim();
315  _RYML_CB_ASSERT(m_callbacks, r == 0);
316  _set_hierarchy(r, NONE, NONE);
317 }
318 
319 
320 //-----------------------------------------------------------------------------
321 void Tree::_clear_range(id_type first, id_type num)
322 {
323  if(num == 0)
324  return; // prevent overflow when subtracting
325  _RYML_CB_ASSERT(m_callbacks, first >= 0 && first + num <= m_cap);
326  memset(m_buf + first, 0, (size_t)num * sizeof(NodeData)); // TODO we should not need this
327  for(id_type i = first, e = first + num; i < e; ++i)
328  {
329  _clear(i);
330  NodeData *n = m_buf + i;
331  n->m_prev_sibling = i - 1;
332  n->m_next_sibling = i + 1;
333  }
334  m_buf[first + num - 1].m_next_sibling = NONE;
335 }
336 
337 C4_SUPPRESS_WARNING_GCC_POP
338 
339 
340 //-----------------------------------------------------------------------------
341 void Tree::_release(id_type i)
342 {
343  _RYML_CB_ASSERT(m_callbacks, i >= 0 && i < m_cap);
344 
345  _rem_hierarchy(i);
346  _free_list_add(i);
347  _clear(i);
348 
349  --m_size;
350 }
351 
352 //-----------------------------------------------------------------------------
353 // add to the front of the free list
354 void Tree::_free_list_add(id_type i)
355 {
356  _RYML_CB_ASSERT(m_callbacks, i >= 0 && i < m_cap);
357  NodeData &C4_RESTRICT w = m_buf[i];
358 
359  w.m_parent = NONE;
360  w.m_next_sibling = m_free_head;
361  w.m_prev_sibling = NONE;
362  if(m_free_head != NONE)
364  m_free_head = i;
365  if(m_free_tail == NONE)
367 }
368 
369 void Tree::_free_list_rem(id_type i)
370 {
371  if(m_free_head == i)
373  _rem_hierarchy(i);
374 }
375 
376 //-----------------------------------------------------------------------------
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 }
405 
406 //-----------------------------------------------------------------------------
407 
408 C4_SUPPRESS_WARNING_GCC_PUSH
409 C4_SUPPRESS_WARNING_CLANG_PUSH
410 C4_SUPPRESS_WARNING_CLANG("-Wnull-dereference")
411 #if defined(__GNUC__)
412 #if (__GNUC__ >= 6)
413 C4_SUPPRESS_WARNING_GCC("-Wnull-dereference")
414 #endif
415 #if (__GNUC__ > 9)
416 C4_SUPPRESS_WARNING_GCC("-Wanalyzer-fd-leak")
417 #endif
418 #endif
419 
420 void Tree::_set_hierarchy(id_type ichild, id_type iparent, id_type iprev_sibling)
421 {
422  _RYML_CB_ASSERT(m_callbacks, ichild >= 0 && ichild < m_cap);
423  _RYML_CB_ASSERT(m_callbacks, iparent == NONE || (iparent >= 0 && iparent < m_cap));
424  _RYML_CB_ASSERT(m_callbacks, iprev_sibling == NONE || (iprev_sibling >= 0 && iprev_sibling < m_cap));
425 
426  NodeData *C4_RESTRICT child = _p(ichild);
427 
428  child->m_parent = iparent;
429  child->m_prev_sibling = NONE;
430  child->m_next_sibling = NONE;
431 
432  if(iparent == NONE)
433  {
434  _RYML_CB_ASSERT(m_callbacks, ichild == 0);
435  _RYML_CB_ASSERT(m_callbacks, iprev_sibling == NONE);
436  }
437 
438  if(iparent == NONE)
439  return;
440 
441  id_type inext_sibling = iprev_sibling != NONE ? next_sibling(iprev_sibling) : first_child(iparent);
442  NodeData *C4_RESTRICT parent = get(iparent);
443  NodeData *C4_RESTRICT psib = get(iprev_sibling);
444  NodeData *C4_RESTRICT nsib = get(inext_sibling);
445 
446  if(psib)
447  {
448  _RYML_CB_ASSERT(m_callbacks, next_sibling(iprev_sibling) == id(nsib));
449  child->m_prev_sibling = id(psib);
450  psib->m_next_sibling = id(child);
451  _RYML_CB_ASSERT(m_callbacks, psib->m_prev_sibling != psib->m_next_sibling || psib->m_prev_sibling == NONE);
452  }
453 
454  if(nsib)
455  {
456  _RYML_CB_ASSERT(m_callbacks, prev_sibling(inext_sibling) == id(psib));
457  child->m_next_sibling = id(nsib);
458  nsib->m_prev_sibling = id(child);
459  _RYML_CB_ASSERT(m_callbacks, nsib->m_prev_sibling != nsib->m_next_sibling || nsib->m_prev_sibling == NONE);
460  }
461 
462  if(parent->m_first_child == NONE)
463  {
464  _RYML_CB_ASSERT(m_callbacks, parent->m_last_child == NONE);
465  parent->m_first_child = id(child);
466  parent->m_last_child = id(child);
467  }
468  else
469  {
470  if(child->m_next_sibling == parent->m_first_child)
471  parent->m_first_child = id(child);
472 
473  if(child->m_prev_sibling == parent->m_last_child)
474  parent->m_last_child = id(child);
475  }
476 }
477 
478 C4_SUPPRESS_WARNING_GCC_POP
479 C4_SUPPRESS_WARNING_CLANG_POP
480 
481 
482 //-----------------------------------------------------------------------------
483 void Tree::_rem_hierarchy(id_type i)
484 {
485  _RYML_CB_ASSERT(m_callbacks, i >= 0 && i < m_cap);
486 
487  NodeData &C4_RESTRICT w = m_buf[i];
488 
489  // remove from the parent
490  if(w.m_parent != NONE)
491  {
492  NodeData &C4_RESTRICT p = m_buf[w.m_parent];
493  if(p.m_first_child == i)
494  {
495  p.m_first_child = w.m_next_sibling;
496  }
497  if(p.m_last_child == i)
498  {
499  p.m_last_child = w.m_prev_sibling;
500  }
501  }
502 
503  // remove from the used list
504  if(w.m_prev_sibling != NONE)
505  {
506  NodeData *C4_RESTRICT prev = get(w.m_prev_sibling);
507  prev->m_next_sibling = w.m_next_sibling;
508  }
509  if(w.m_next_sibling != NONE)
510  {
511  NodeData *C4_RESTRICT next = get(w.m_next_sibling);
512  next->m_prev_sibling = w.m_prev_sibling;
513  }
514 }
515 
516 //-----------------------------------------------------------------------------
517 /** @cond dev */
518 id_type Tree::_do_reorder(id_type *node, id_type count)
519 {
520  // swap this node if it's not in place
521  if(*node != count)
522  {
523  _swap(*node, count);
524  *node = count;
525  }
526  ++count; // bump the count from this node
527 
528  // now descend in the hierarchy
529  for(id_type i = first_child(*node); i != NONE; i = next_sibling(i))
530  {
531  // this child may have been relocated to a different index,
532  // so get an updated version
533  count = _do_reorder(&i, count);
534  }
535  return count;
536 }
537 /** @endcond */
538 
540 {
541  id_type r = root_id();
542  _do_reorder(&r, 0);
543 }
544 
545 
546 //-----------------------------------------------------------------------------
547 /** @cond dev */
548 void Tree::_swap(id_type n_, id_type m_)
549 {
550  _RYML_CB_ASSERT(m_callbacks, (parent(n_) != NONE) || type(n_) == NOTYPE);
551  _RYML_CB_ASSERT(m_callbacks, (parent(m_) != NONE) || type(m_) == NOTYPE);
552  NodeType tn = type(n_);
553  NodeType tm = type(m_);
554  if(tn != NOTYPE && tm != NOTYPE)
555  {
556  _swap_props(n_, m_);
557  _swap_hierarchy(n_, m_);
558  }
559  else if(tn == NOTYPE && tm != NOTYPE)
560  {
561  _copy_props(n_, m_);
562  _free_list_rem(n_);
563  _copy_hierarchy(n_, m_);
564  _clear(m_);
565  _free_list_add(m_);
566  }
567  else if(tn != NOTYPE && tm == NOTYPE)
568  {
569  _copy_props(m_, n_);
570  _free_list_rem(m_);
571  _copy_hierarchy(m_, n_);
572  _clear(n_);
573  _free_list_add(n_);
574  }
575  else
576  {
577  C4_NEVER_REACH();
578  }
579 }
580 
581 //-----------------------------------------------------------------------------
582 void Tree::_swap_hierarchy(id_type ia, id_type ib)
583 {
584  if(ia == ib) return;
585 
586  for(id_type i = first_child(ia); i != NONE; i = next_sibling(i))
587  {
588  if(i == ib || i == ia)
589  continue;
590  _p(i)->m_parent = ib;
591  }
592 
593  for(id_type i = first_child(ib); i != NONE; i = next_sibling(i))
594  {
595  if(i == ib || i == ia)
596  continue;
597  _p(i)->m_parent = ia;
598  }
599 
600  auto & C4_RESTRICT a = *_p(ia);
601  auto & C4_RESTRICT b = *_p(ib);
602  auto & C4_RESTRICT pa = *_p(a.m_parent);
603  auto & C4_RESTRICT pb = *_p(b.m_parent);
604 
605  if(&pa == &pb)
606  {
607  if((pa.m_first_child == ib && pa.m_last_child == ia)
608  ||
609  (pa.m_first_child == ia && pa.m_last_child == ib))
610  {
611  std::swap(pa.m_first_child, pa.m_last_child);
612  }
613  else
614  {
615  bool changed = false;
616  if(pa.m_first_child == ia)
617  {
618  pa.m_first_child = ib;
619  changed = true;
620  }
621  if(pa.m_last_child == ia)
622  {
623  pa.m_last_child = ib;
624  changed = true;
625  }
626  if(pb.m_first_child == ib && !changed)
627  {
628  pb.m_first_child = ia;
629  }
630  if(pb.m_last_child == ib && !changed)
631  {
632  pb.m_last_child = ia;
633  }
634  }
635  }
636  else
637  {
638  if(pa.m_first_child == ia)
639  pa.m_first_child = ib;
640  if(pa.m_last_child == ia)
641  pa.m_last_child = ib;
642  if(pb.m_first_child == ib)
643  pb.m_first_child = ia;
644  if(pb.m_last_child == ib)
645  pb.m_last_child = ia;
646  }
647  std::swap(a.m_first_child , b.m_first_child);
648  std::swap(a.m_last_child , b.m_last_child);
649 
650  if(a.m_prev_sibling != ib && b.m_prev_sibling != ia &&
651  a.m_next_sibling != ib && b.m_next_sibling != ia)
652  {
653  if(a.m_prev_sibling != NONE && a.m_prev_sibling != ib)
654  _p(a.m_prev_sibling)->m_next_sibling = ib;
655  if(a.m_next_sibling != NONE && a.m_next_sibling != ib)
656  _p(a.m_next_sibling)->m_prev_sibling = ib;
657  if(b.m_prev_sibling != NONE && b.m_prev_sibling != ia)
658  _p(b.m_prev_sibling)->m_next_sibling = ia;
659  if(b.m_next_sibling != NONE && b.m_next_sibling != ia)
660  _p(b.m_next_sibling)->m_prev_sibling = ia;
661  std::swap(a.m_prev_sibling, b.m_prev_sibling);
662  std::swap(a.m_next_sibling, b.m_next_sibling);
663  }
664  else
665  {
666  if(a.m_next_sibling == ib) // n will go after m
667  {
668  _RYML_CB_ASSERT(m_callbacks, b.m_prev_sibling == ia);
669  if(a.m_prev_sibling != NONE)
670  {
671  _RYML_CB_ASSERT(m_callbacks, a.m_prev_sibling != ib);
672  _p(a.m_prev_sibling)->m_next_sibling = ib;
673  }
674  if(b.m_next_sibling != NONE)
675  {
676  _RYML_CB_ASSERT(m_callbacks, b.m_next_sibling != ia);
677  _p(b.m_next_sibling)->m_prev_sibling = ia;
678  }
679  id_type ns = b.m_next_sibling;
680  b.m_prev_sibling = a.m_prev_sibling;
681  b.m_next_sibling = ia;
682  a.m_prev_sibling = ib;
683  a.m_next_sibling = ns;
684  }
685  else if(a.m_prev_sibling == ib) // m will go after n
686  {
687  _RYML_CB_ASSERT(m_callbacks, b.m_next_sibling == ia);
688  if(b.m_prev_sibling != NONE)
689  {
690  _RYML_CB_ASSERT(m_callbacks, b.m_prev_sibling != ia);
691  _p(b.m_prev_sibling)->m_next_sibling = ia;
692  }
693  if(a.m_next_sibling != NONE)
694  {
695  _RYML_CB_ASSERT(m_callbacks, a.m_next_sibling != ib);
696  _p(a.m_next_sibling)->m_prev_sibling = ib;
697  }
698  id_type ns = b.m_prev_sibling;
699  a.m_prev_sibling = b.m_prev_sibling;
700  a.m_next_sibling = ib;
701  b.m_prev_sibling = ia;
702  b.m_next_sibling = ns;
703  }
704  else
705  {
706  C4_NEVER_REACH();
707  }
708  }
709  _RYML_CB_ASSERT(m_callbacks, a.m_next_sibling != ia);
710  _RYML_CB_ASSERT(m_callbacks, a.m_prev_sibling != ia);
711  _RYML_CB_ASSERT(m_callbacks, b.m_next_sibling != ib);
712  _RYML_CB_ASSERT(m_callbacks, b.m_prev_sibling != ib);
713 
714  if(a.m_parent != ib && b.m_parent != ia)
715  {
716  std::swap(a.m_parent, b.m_parent);
717  }
718  else
719  {
720  if(a.m_parent == ib && b.m_parent != ia)
721  {
722  a.m_parent = b.m_parent;
723  b.m_parent = ia;
724  }
725  else if(a.m_parent != ib && b.m_parent == ia)
726  {
727  b.m_parent = a.m_parent;
728  a.m_parent = ib;
729  }
730  else
731  {
732  C4_NEVER_REACH();
733  }
734  }
735 }
736 
737 //-----------------------------------------------------------------------------
738 void Tree::_copy_hierarchy(id_type dst_, id_type src_)
739 {
740  auto const& C4_RESTRICT src = *_p(src_);
741  auto & C4_RESTRICT dst = *_p(dst_);
742  auto & C4_RESTRICT prt = *_p(src.m_parent);
743  for(id_type i = src.m_first_child; i != NONE; i = next_sibling(i))
744  {
745  _p(i)->m_parent = dst_;
746  }
747  if(src.m_prev_sibling != NONE)
748  {
749  _p(src.m_prev_sibling)->m_next_sibling = dst_;
750  }
751  if(src.m_next_sibling != NONE)
752  {
753  _p(src.m_next_sibling)->m_prev_sibling = dst_;
754  }
755  if(prt.m_first_child == src_)
756  {
757  prt.m_first_child = dst_;
758  }
759  if(prt.m_last_child == src_)
760  {
761  prt.m_last_child = dst_;
762  }
763  dst.m_parent = src.m_parent;
764  dst.m_first_child = src.m_first_child;
765  dst.m_last_child = src.m_last_child;
766  dst.m_prev_sibling = src.m_prev_sibling;
767  dst.m_next_sibling = src.m_next_sibling;
768 }
769 
770 //-----------------------------------------------------------------------------
771 void Tree::_swap_props(id_type n_, id_type m_)
772 {
773  NodeData &C4_RESTRICT n = *_p(n_);
774  NodeData &C4_RESTRICT m = *_p(m_);
775  std::swap(n.m_type, m.m_type);
776  std::swap(n.m_key, m.m_key);
777  std::swap(n.m_val, m.m_val);
778 }
779 /** @endcond */
780 
781 //-----------------------------------------------------------------------------
782 void Tree::move(id_type node, id_type after)
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 }
792 
793 //-----------------------------------------------------------------------------
794 
795 void Tree::move(id_type node, id_type new_parent, id_type after)
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 }
807 
808 id_type Tree::move(Tree *src, id_type node, id_type new_parent, id_type after)
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 }
819 
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 }
854 
855 
856 //-----------------------------------------------------------------------------
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 }
872 
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 }
889 
890 
891 //-----------------------------------------------------------------------------
893 {
894  return duplicate(this, node, parent, after);
895 }
896 
897 id_type Tree::duplicate(Tree const* src, id_type node, id_type parent, id_type after)
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 }
912 
913 //-----------------------------------------------------------------------------
915 {
916  return duplicate_children(this, node, parent, after);
917 }
918 
919 id_type Tree::duplicate_children(Tree const* src, id_type node, id_type parent, id_type after)
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 }
934 
935 //-----------------------------------------------------------------------------
937 {
938  duplicate_contents(this, node, where);
939 }
940 
941 void Tree::duplicate_contents(Tree const *src, id_type node, id_type where)
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 }
949 
950 //-----------------------------------------------------------------------------
952 {
953  return duplicate_children_no_rep(this, node, parent, after);
954 }
955 
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 }
1047 
1048 
1049 //-----------------------------------------------------------------------------
1050 
1051 void Tree::merge_with(Tree const *src, id_type src_node, id_type dst_node)
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 }
1124 
1125 
1126 //-----------------------------------------------------------------------------
1127 
1128 void Tree::resolve(bool clear_anchors)
1129 {
1130  if(m_size == 0)
1131  return;
1132  ReferenceResolver rr;
1133  resolve(&rr, clear_anchors);
1134 }
1135 
1136 void Tree::resolve(ReferenceResolver *C4_RESTRICT rr, bool clear_anchors)
1137 {
1138  if(m_size == 0)
1139  return;
1140  rr->resolve(this, clear_anchors);
1141 }
1142 
1143 
1144 //-----------------------------------------------------------------------------
1145 
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 }
1153 
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 }
1165 
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 }
1178 
1179 #if defined(__clang__)
1180 # pragma clang diagnostic push
1181 # pragma GCC diagnostic ignored "-Wnull-dereference"
1182 #elif defined(__GNUC__)
1183 # pragma GCC diagnostic push
1184 # if __GNUC__ >= 6
1185 # pragma GCC diagnostic ignored "-Wnull-dereference"
1186 # endif
1187 # if __GNUC__ > 9
1188 # pragma GCC diagnostic ignored "-Wanalyzer-null-dereference"
1189 # endif
1190 #endif
1191 
1192 id_type Tree::find_child(id_type node, csubstr const& name) const
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 }
1214 
1215 #if defined(__clang__)
1216 # pragma clang diagnostic pop
1217 #elif defined(__GNUC__)
1218 # pragma GCC diagnostic pop
1219 #endif
1220 
1221 namespace {
1222 id_type depth_desc_(Tree const& C4_RESTRICT t, id_type id, id_type currdepth=0, id_type maxdepth=0)
1223 {
1224  maxdepth = currdepth > maxdepth ? currdepth : maxdepth;
1225  for(id_type child = t.first_child(id); child != NONE; child = t.next_sibling(child))
1226  {
1227  const id_type d = depth_desc_(t, child, currdepth+1, maxdepth);
1228  maxdepth = d > maxdepth ? d : maxdepth;
1229  }
1230  return maxdepth;
1231 }
1232 }
1233 
1235 {
1236  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1237  return depth_desc_(*this, node);
1238 }
1239 
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 }
1251 
1252 bool Tree::is_ancestor(id_type node, id_type ancestor) const
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 }
1264 
1265 
1266 //-----------------------------------------------------------------------------
1267 
1268 void Tree::to_val(id_type node, csubstr val, type_bits more_flags)
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 }
1276 
1277 void Tree::to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flags)
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 }
1285 
1286 void Tree::to_map(id_type node, type_bits more_flags)
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 }
1294 
1295 void Tree::to_map(id_type node, csubstr key, type_bits more_flags)
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 }
1303 
1304 void Tree::to_seq(id_type node, type_bits more_flags)
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 }
1312 
1313 void Tree::to_seq(id_type node, csubstr key, type_bits more_flags)
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 }
1321 
1322 void Tree::to_doc(id_type node, type_bits more_flags)
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 }
1329 
1330 void Tree::to_stream(id_type node, type_bits more_flags)
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 }
1337 
1338 
1339 //-----------------------------------------------------------------------------
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 }
1348 
1350 {
1351  for(TagDirective &td : m_tag_directives)
1352  td = {};
1353 }
1354 
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 }
1368 
1369 bool Tree::add_tag_directive(csubstr directive_)
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 }
1379 
1380 size_t Tree::resolve_tag(substr output, csubstr tag, id_type node_id) const
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 }
1402 
1403 namespace {
1404 csubstr _transform_tag(Tree *t, csubstr tag, id_type node)
1405 {
1406  _c4dbgpf("[{}] resolving tag ~~~{}~~~", node, tag);
1407  size_t required_size = t->resolve_tag(substr{}, tag, node);
1408  if(!required_size)
1409  {
1410  if(tag.begins_with("!<"))
1411  tag = tag.sub(1);
1412  _c4dbgpf("[{}] resolved tag: ~~~{}~~~", node, tag);
1413  return tag;
1414  }
1415  const char *prev_arena = t->arena().str;(void)prev_arena;
1416  substr buf = t->alloc_arena(required_size);
1417  _RYML_CB_ASSERT(t->m_callbacks, t->arena().str == prev_arena);
1418  size_t actual_size = t->resolve_tag(buf, tag, node);
1419  _RYML_CB_ASSERT(t->m_callbacks, actual_size <= required_size);
1420  _c4dbgpf("[{}] resolved tag: ~~~{}~~~", node, buf.first(actual_size));
1421  return buf.first(actual_size);
1422 }
1423 void _resolve_tags(Tree *t, id_type node)
1424 {
1425  NodeData *C4_RESTRICT d = t->_p(node);
1426  if(d->m_type & KEYTAG)
1427  d->m_key.tag = _transform_tag(t, d->m_key.tag, node);
1428  if(d->m_type & VALTAG)
1429  d->m_val.tag = _transform_tag(t, d->m_val.tag, node);
1430  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1431  _resolve_tags(t, child);
1432 }
1433 size_t _count_resolved_tags_size(Tree const* t, id_type node)
1434 {
1435  size_t sz = 0;
1436  NodeData const* C4_RESTRICT d = t->_p(node);
1437  if(d->m_type & KEYTAG)
1438  sz += t->resolve_tag(substr{}, d->m_key.tag, node);
1439  if(d->m_type & VALTAG)
1440  sz += t->resolve_tag(substr{}, d->m_val.tag, node);
1441  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1442  sz += _count_resolved_tags_size(t, child);
1443  return sz;
1444 }
1445 void _normalize_tags(Tree *t, id_type node)
1446 {
1447  NodeData *C4_RESTRICT d = t->_p(node);
1448  if(d->m_type & KEYTAG)
1449  d->m_key.tag = normalize_tag(d->m_key.tag);
1450  if(d->m_type & VALTAG)
1451  d->m_val.tag = normalize_tag(d->m_val.tag);
1452  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1453  _normalize_tags(t, child);
1454 }
1455 void _normalize_tags_long(Tree *t, id_type node)
1456 {
1457  NodeData *C4_RESTRICT d = t->_p(node);
1458  if(d->m_type & KEYTAG)
1459  d->m_key.tag = normalize_tag_long(d->m_key.tag);
1460  if(d->m_type & VALTAG)
1461  d->m_val.tag = normalize_tag_long(d->m_val.tag);
1462  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1463  _normalize_tags_long(t, child);
1464 }
1465 } // namespace
1466 
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 }
1476 
1478 {
1479  if(empty())
1480  return;
1481  _normalize_tags(this, root_id());
1482 }
1483 
1485 {
1486  if(empty())
1487  return;
1488  _normalize_tags_long(this, root_id());
1489 }
1490 
1491 
1492 //-----------------------------------------------------------------------------
1493 
1495 {
1496  csubstr p = path.first(path_pos);
1497  if(p.ends_with('.'))
1498  p = p.first(p.len-1);
1499  return p;
1500 }
1501 
1503 {
1504  return path.sub(path_pos);
1505 }
1506 
1507 void Tree::_advance(lookup_result *r, size_t more)
1508 {
1509  r->path_pos += more;
1510  if(r->path.sub(r->path_pos).begins_with('.'))
1511  ++r->path_pos;
1512 }
1513 
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 }
1526 
1527 id_type Tree::lookup_path_or_modify(csubstr default_value, csubstr path, id_type start)
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 }
1536 
1537 id_type Tree::lookup_path_or_modify(Tree const *src, id_type src_node, csubstr path, id_type start)
1538 {
1539  id_type target = _lookup_path_or_create(path, start);
1540  merge_with(src, src_node, target);
1541  return target;
1542 }
1543 
1544 id_type Tree::_lookup_path_or_create(csubstr path, id_type start)
1545 {
1546  if(start == NONE)
1547  start = root_id();
1548  lookup_result r(path, start);
1549  _lookup_path(&r);
1550  if(r.target != NONE)
1551  {
1552  C4_ASSERT(r.unresolved().empty());
1553  return r.target;
1554  }
1555  _lookup_path_modify(&r);
1556  return r.target;
1557 }
1558 
1559 void Tree::_lookup_path(lookup_result *r) const
1560 {
1561  C4_ASSERT( ! r->unresolved().empty());
1562  _lookup_path_token parent{"", type(r->closest)};
1563  id_type node;
1564  do
1565  {
1566  node = _next_node(r, &parent);
1567  if(node != NONE)
1568  r->closest = node;
1569  if(r->unresolved().empty())
1570  {
1571  r->target = node;
1572  return;
1573  }
1574  } while(node != NONE);
1575 }
1576 
1577 void Tree::_lookup_path_modify(lookup_result *r)
1578 {
1579  C4_ASSERT( ! r->unresolved().empty());
1580  _lookup_path_token parent{"", type(r->closest)};
1581  id_type node;
1582  do
1583  {
1584  node = _next_node_modify(r, &parent);
1585  if(node != NONE)
1586  r->closest = node;
1587  if(r->unresolved().empty())
1588  {
1589  r->target = node;
1590  return;
1591  }
1592  } while(node != NONE);
1593 }
1594 
1595 id_type Tree::_next_node(lookup_result * r, _lookup_path_token *parent) const
1596 {
1597  _lookup_path_token token = _next_token(r, *parent);
1598  if( ! token)
1599  return NONE;
1600 
1601  id_type node = NONE;
1602  csubstr prev = token.value;
1603  if(token.type == MAP || token.type == SEQ)
1604  {
1605  _RYML_CB_ASSERT(m_callbacks, !token.value.begins_with('['));
1606  //_RYML_CB_ASSERT(m_callbacks, is_container(r->closest) || r->closest == NONE);
1607  _RYML_CB_ASSERT(m_callbacks, is_map(r->closest));
1608  node = find_child(r->closest, token.value);
1609  }
1610  else if(token.type == KEYVAL)
1611  {
1612  _RYML_CB_ASSERT(m_callbacks, r->unresolved().empty());
1613  if(is_map(r->closest))
1614  node = find_child(r->closest, token.value);
1615  }
1616  else if(token.type == KEY)
1617  {
1618  _RYML_CB_ASSERT(m_callbacks, token.value.begins_with('[') && token.value.ends_with(']'));
1619  token.value = token.value.offs(1, 1).trim(' ');
1620  id_type idx = 0;
1621  _RYML_CB_CHECK(m_callbacks, from_chars(token.value, &idx));
1622  node = child(r->closest, idx);
1623  }
1624  else
1625  {
1626  C4_NEVER_REACH();
1627  }
1628 
1629  if(node != NONE)
1630  {
1631  *parent = token;
1632  }
1633  else
1634  {
1635  csubstr p = r->path.sub(r->path_pos > 0 ? r->path_pos - 1 : r->path_pos);
1636  r->path_pos -= prev.len;
1637  if(p.begins_with('.'))
1638  r->path_pos -= 1u;
1639  }
1640 
1641  return node;
1642 }
1643 
1644 id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
1645 {
1646  _lookup_path_token token = _next_token(r, *parent);
1647  if( ! token)
1648  return NONE;
1649 
1650  id_type node = NONE;
1651  if(token.type == MAP || token.type == SEQ)
1652  {
1653  _RYML_CB_ASSERT(m_callbacks, !token.value.begins_with('['));
1654  //_RYML_CB_ASSERT(m_callbacks, is_container(r->closest) || r->closest == NONE);
1655  if( ! is_container(r->closest))
1656  {
1657  if(has_key(r->closest))
1658  to_map(r->closest, key(r->closest));
1659  else
1660  to_map(r->closest);
1661  }
1662  else
1663  {
1664  if(is_map(r->closest))
1665  node = find_child(r->closest, token.value);
1666  else
1667  {
1668  id_type pos = NONE;
1669  _RYML_CB_CHECK(m_callbacks, c4::atox(token.value, &pos));
1670  _RYML_CB_ASSERT(m_callbacks, pos != NONE);
1671  node = child(r->closest, pos);
1672  }
1673  }
1674  if(node == NONE)
1675  {
1676  _RYML_CB_ASSERT(m_callbacks, is_map(r->closest));
1677  node = append_child(r->closest);
1678  NodeData *n = _p(node);
1679  n->m_key.scalar = token.value;
1680  n->m_type.add(KEY);
1681  }
1682  }
1683  else if(token.type == KEYVAL)
1684  {
1685  _RYML_CB_ASSERT(m_callbacks, r->unresolved().empty());
1686  if(is_map(r->closest))
1687  {
1688  node = find_child(r->closest, token.value);
1689  if(node == NONE)
1690  node = append_child(r->closest);
1691  }
1692  else
1693  {
1694  _RYML_CB_ASSERT(m_callbacks, !is_seq(r->closest));
1695  _add_flags(r->closest, MAP);
1696  node = append_child(r->closest);
1697  }
1698  NodeData *n = _p(node);
1699  n->m_key.scalar = token.value;
1700  n->m_val.scalar = "";
1701  n->m_type.add(KEYVAL);
1702  }
1703  else if(token.type == KEY)
1704  {
1705  _RYML_CB_ASSERT(m_callbacks, token.value.begins_with('[') && token.value.ends_with(']'));
1706  token.value = token.value.offs(1, 1).trim(' ');
1707  id_type idx;
1708  if( ! from_chars(token.value, &idx))
1709  return NONE;
1710  if( ! is_container(r->closest))
1711  {
1712  if(has_key(r->closest))
1713  {
1714  csubstr k = key(r->closest);
1715  _clear_type(r->closest);
1716  to_seq(r->closest, k);
1717  }
1718  else
1719  {
1720  _clear_type(r->closest);
1721  to_seq(r->closest);
1722  }
1723  }
1724  _RYML_CB_ASSERT(m_callbacks, is_container(r->closest));
1725  node = child(r->closest, idx);
1726  if(node == NONE)
1727  {
1728  _RYML_CB_ASSERT(m_callbacks, num_children(r->closest) <= idx);
1729  for(id_type i = num_children(r->closest); i <= idx; ++i)
1730  {
1731  node = append_child(r->closest);
1732  if(i < idx)
1733  {
1734  if(is_map(r->closest))
1735  to_keyval(node, /*"~"*/{}, /*"~"*/{});
1736  else if(is_seq(r->closest))
1737  to_val(node, /*"~"*/{});
1738  }
1739  }
1740  }
1741  }
1742  else
1743  {
1744  C4_NEVER_REACH();
1745  }
1746 
1747  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1748  *parent = token;
1749  return node;
1750 }
1751 
1752 /* types of tokens:
1753  * - seeing "map." ---> "map"/MAP
1754  * - finishing "scalar" ---> "scalar"/KEYVAL
1755  * - seeing "seq[n]" ---> "seq"/SEQ (--> "[n]"/KEY)
1756  * - seeing "[n]" ---> "[n]"/KEY
1757  */
1758 Tree::_lookup_path_token Tree::_next_token(lookup_result *r, _lookup_path_token const& parent) const
1759 {
1760  csubstr unres = r->unresolved();
1761  if(unres.empty())
1762  return {};
1763 
1764  // is it an indexation like [0], [1], etc?
1765  if(unres.begins_with('['))
1766  {
1767  size_t pos = unres.find(']');
1768  if(pos == csubstr::npos)
1769  return {};
1770  csubstr idx = unres.first(pos + 1);
1771  _advance(r, pos + 1);
1772  return {idx, KEY};
1773  }
1774 
1775  // no. so it must be a name
1776  size_t pos = unres.first_of(".[");
1777  if(pos == csubstr::npos)
1778  {
1779  _advance(r, unres.len);
1780  NodeType t;
1781  if(( ! parent) || parent.type.is_seq())
1782  return {unres, VAL};
1783  return {unres, KEYVAL};
1784  }
1785 
1786  // it's either a map or a seq
1787  _RYML_CB_ASSERT(m_callbacks, unres[pos] == '.' || unres[pos] == '[');
1788  if(unres[pos] == '.')
1789  {
1790  _RYML_CB_ASSERT(m_callbacks, pos != 0);
1791  _advance(r, pos + 1);
1792  return {unres.first(pos), MAP};
1793  }
1794 
1795  _RYML_CB_ASSERT(m_callbacks, unres[pos] == '[');
1796  _advance(r, pos);
1797  return {unres.first(pos), SEQ};
1798 }
1799 
1800 
1801 } // namespace yml
1802 } // namespace c4
1803 
1804 
1805 C4_SUPPRESS_WARNING_GCC_CLANG_POP
1806 C4_SUPPRESS_WARNING_MSVC_POP
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:839
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:980
NodeData * m_buf
Definition: tree.hpp:1302
void move(id_type node, id_type after)
change the node's position in the parent
Definition: tree.cpp:782
void reserve_arena(size_t arena_cap)
ensure the tree's internal string arena is at least the given capacity
Definition: tree.hpp:948
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
void clear()
clear the tree and zero every node
Definition: tree.cpp:292
id_type m_free_head
Definition: tree.hpp:1307
lookup_result lookup_path(csubstr path, id_type start=NONE) const
for example foo.bar[0].baz
Definition: tree.cpp:1514
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 correspon...
Definition: tree.cpp:1527
id_type first_child(id_type node) const
Definition: tree.hpp:465
bool is_stream(id_type node) const
Definition: tree.hpp:363
NodeType type(id_type node) const
Definition: tree.hpp:337
void set_root_as_stream()
ensure the first node is a stream.
Definition: tree.cpp:820
id_type prev_sibling(id_type node) const
Definition: tree.hpp:459
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
size_t m_arena_pos
Definition: tree.hpp:1311
bool is_map(id_type node) const
Definition: tree.hpp:366
void clear_tag_directives()
Definition: tree.cpp:1349
Tree & operator=(Tree const &that)
Definition: tree.cpp:115
void to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flags=0)
Definition: tree.cpp:1277
bool is_root(id_type node) const
Definition: tree.hpp:415
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
bool is_keyval(id_type node) const
Definition: tree.hpp:371
TagDirective m_tag_directives[RYML_MAX_TAG_DIRECTIVES]
Definition: tree.hpp:1315
void reserve(id_type node_capacity)
Definition: tree.cpp:254
id_type m_size
Definition: tree.hpp:1305
void to_doc(id_type node, type_bits more_flags=0)
Definition: tree.cpp:1322
bool has_key(id_type node) const
Definition: tree.hpp:368
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.
Definition: tree.cpp:1240
bool in_arena(csubstr s) const
return true if the given substring is part of the tree's string arena
Definition: tree.hpp:789
id_type parent(id_type node) const
Definition: tree.hpp:457
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
void resolve_tags()
Definition: tree.cpp:1467
bool add_tag_directive(csubstr directive)
Definition: tree.cpp:1369
bool is_val(id_type node) const
Definition: tree.hpp:370
bool empty() const
Definition: tree.hpp:235
id_type child_pos(id_type node, id_type ch) const
Definition: tree.cpp:1166
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:635
void to_stream(id_type node, type_bits more_flags=0)
Definition: tree.cpp:1330
bool has_sibling(id_type node, id_type sib) const
true if node has a sibling with id sib
Definition: tree.hpp:433
id_type next_sibling(id_type node) const
Definition: tree.hpp:460
void to_seq(id_type node, csubstr key, type_bits more_flags=0)
Definition: tree.cpp:1313
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 chi...
Definition: tree.cpp:1234
id_type num_tag_directives() const
Definition: tree.cpp:1340
bool parent_is_seq(id_type node) const
Definition: tree.hpp:381
csubstr const & key(id_type node) const
Definition: tree.hpp:340
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
id_type m_cap
Definition: tree.hpp:1303
id_type last_child(id_type node) const
Definition: tree.hpp:466
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
Definition: tree.hpp:253
substr m_arena
Definition: tree.hpp:1310
void normalize_tags()
Definition: tree.cpp:1477
void reorder()
reorder the tree in memory so that all the nodes are stored in a linear sequence when visited in dept...
Definition: tree.cpp:539
void remove_children(id_type node)
remove all the node's children, but keep the node itself
Definition: tree.cpp:857
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
bool has_val(id_type node) const
Definition: tree.hpp:369
void to_map(id_type node, csubstr key, type_bits more_flags=0)
Definition: tree.cpp:1295
size_t arena_capacity() const
get the current capacity of the tree's internal arena
Definition: tree.hpp:779
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
id_type m_free_tail
Definition: tree.hpp:1308
void normalize_tags_long()
Definition: tree.cpp:1484
csubstr const & val(id_type node) const
Definition: tree.hpp:346
void resolve(ReferenceResolver *rr, bool clear_anchors=true)
Resolve references (aliases <- anchors), by forwarding to ReferenceResolver::resolve(); refer to Refe...
Definition: tree.cpp:1136
bool is_seq(id_type node) const
Definition: tree.hpp:367
bool parent_is_map(id_type node) const
Definition: tree.hpp:382
void remove(id_type node)
remove an entire branch at once: ie remove the children and the node itself
Definition: tree.hpp:667
id_type find_child(id_type node, csubstr const &key) const
Definition: tree.cpp:1192
void duplicate_contents(id_type node, id_type where)
Definition: tree.cpp:936
id_type root_id()
Get the id of the root node.
Definition: tree.hpp:288
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
id_type num_children(id_type node) const
O(num_children)
Definition: tree.cpp:1146
csubstr arena() const
get the current arena
Definition: tree.hpp:784
void merge_with(Tree const *src, id_type src_node=NONE, id_type dst_root=NONE)
Definition: tree.cpp:1051
id_type _claim()
Definition: tree.cpp:377
bool is_container(id_type node) const
Definition: tree.hpp:365
size_t arena_size() const
get the current size of the tree's internal arena
Definition: tree.hpp:777
id_type child(id_type node, id_type pos) const
Definition: tree.cpp:1154
bool has_child(id_type node, id_type ch) const
true if node has a child with id ch
Definition: tree.hpp:426
Callbacks m_callbacks
Definition: tree.hpp:1313
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
void to_val(id_type node, csubstr val, type_bits more_flags=0)
Definition: tree.cpp:1268
bool has_children(id_type node) const
true if node has any children key
Definition: tree.hpp:430
bool atox(csubstr s, uint8_t *v) noexcept
Definition: charconv.hpp:2288
bool from_chars(csubstr buf, uint8_t *v) noexcept
Definition: charconv.hpp:2363
uint32_t type_bits
the integral type necessary to cover all the bits for NodeType_e
Definition: node_type.hpp:26
@ NOTYPE
no node type or style is set
Definition: node_type.hpp:32
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:35
@ STREAM
a stream: a seq of docs
Definition: node_type.hpp:38
@ KEY
is member of a map
Definition: node_type.hpp:33
@ VAL_STYLE
mask of all the scalar styles for val (not container styles!)
Definition: node_type.hpp:89
@ KEYTAG
the key has a tag
Definition: node_type.hpp:43
@ 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
@ VALTAG
the val has a tag
Definition: node_type.hpp:44
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:36
@ DOC
a document
Definition: node_type.hpp:37
Key< K > key(K &k)
Definition: node.hpp:43
bool is_custom_tag(csubstr tag)
Definition: tag.cpp:9
csubstr normalize_tag_long(csubstr tag)
Definition: tag.cpp:31
csubstr normalize_tag(csubstr tag)
Definition: tag.cpp:19
#define RYML_MAX_TAG_DIRECTIVES
the maximum number of tag directives in a Tree
Definition: tag.hpp:19
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
@ npos
a null string position
Definition: common.hpp:267
@ NONE
an index to none
Definition: common.hpp:260
Definition: common.cpp:12
Node classes.
a c-style callbacks class.
Definition: common.hpp:376
contains the data for each YAML node.
Definition: tree.hpp:181
NodeType m_type
Definition: tree.hpp:182
id_type m_next_sibling
Definition: tree.hpp:190
id_type m_parent
Definition: tree.hpp:187
NodeScalar m_key
Definition: tree.hpp:184
id_type m_prev_sibling
Definition: tree.hpp:191
id_type m_first_child
Definition: tree.hpp:188
NodeScalar m_val
Definition: tree.hpp:185
csubstr scalar
Definition: tree.hpp:66
void clear() noexcept
Definition: tree.hpp:96
wraps a NodeType_e element with some syntactic sugar and predicates
Definition: node_type.hpp:117
bool has_key() const noexcept
Definition: node_type.hpp:170
bool is_seq() const noexcept
Definition: node_type.hpp:169
void rem(NodeType_e t) noexcept
Definition: node_type.hpp:134
bool is_map() const noexcept
Definition: node_type.hpp:168
void add(NodeType_e t) noexcept
Definition: node_type.hpp:133
bool is_val() const noexcept
Definition: node_type.hpp:172
Reusable object to resolve references/aliases in a Tree.
bool create_from_str(csubstr directive_)
leaves next_node_id unfilled
Definition: tag.cpp:204
csubstr handle
Eg.
Definition: tag.hpp:60
size_t transform(csubstr tag, substr output, Callbacks const &callbacks) const
Definition: tag.cpp:243
csubstr prefix
Eg.
Definition: tag.hpp:62
csubstr unresolved() const
get the part ot the input path that was unresolved
Definition: tree.cpp:1502
csubstr resolved() const
get the part ot the input path that was resolved
Definition: tree.cpp:1494