rapidyaml  0.8.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  if(is_seq(parent))
984  {
985  prev = duplicate(i, parent, prev);
986  }
987  else
988  {
989  _RYML_CB_ASSERT(m_callbacks, is_map(parent));
990  // does the parent already have a node with key equal to that of the current duplicate?
991  id_type rep = NONE, rep_pos = NONE;
992  for(id_type j = first_child(parent), jcount = 0; j != NONE; ++jcount, j = next_sibling(j))
993  {
994  if(key(j) == key(i))
995  {
996  rep = j;
997  rep_pos = jcount;
998  break;
999  }
1000  }
1001  if(rep == NONE) // there is no repetition; just duplicate
1002  {
1003  prev = duplicate(src, i, parent, prev);
1004  }
1005  else // yes, there is a repetition
1006  {
1007  if(after_pos != NONE && rep_pos < after_pos)
1008  {
1009  // rep is located before the node which will be inserted,
1010  // and will be overridden by the duplicate. So replace it.
1011  remove(rep);
1012  prev = duplicate(src, i, parent, prev);
1013  }
1014  else if(prev == NONE)
1015  {
1016  // first iteration with prev = after = NONE and repetition
1017  prev = rep;
1018  }
1019  else if(rep != prev)
1020  {
1021  // rep is located after the node which will be inserted
1022  // and overrides it. So move the rep into this node's place.
1023  move(rep, prev);
1024  prev = rep;
1025  }
1026  } // there's a repetition
1027  }
1028  }
1029 
1030  return prev;
1031 }
1032 
1033 
1034 //-----------------------------------------------------------------------------
1035 
1036 void Tree::merge_with(Tree const *src, id_type src_node, id_type dst_node)
1037 {
1038  _RYML_CB_ASSERT(m_callbacks, src != nullptr);
1039  if(src_node == NONE)
1040  src_node = src->root_id();
1041  if(dst_node == NONE)
1042  dst_node = root_id();
1043  _RYML_CB_ASSERT(m_callbacks, src->has_val(src_node) || src->is_seq(src_node) || src->is_map(src_node));
1044  if(src->has_val(src_node))
1045  {
1046  type_bits mask_src = ~STYLE; // keep the existing style if it is already a val
1047  if( ! has_val(dst_node))
1048  {
1049  if(has_children(dst_node))
1050  remove_children(dst_node);
1051  mask_src |= VAL_STYLE; // copy the src style
1052  }
1053  if(src->is_keyval(src_node))
1054  {
1055  _copy_props(dst_node, src, src_node, mask_src);
1056  }
1057  else
1058  {
1059  _RYML_CB_ASSERT(m_callbacks, src->is_val(src_node));
1060  _copy_props_wo_key(dst_node, src, src_node, mask_src);
1061  }
1062  }
1063  else if(src->is_seq(src_node))
1064  {
1065  if( ! is_seq(dst_node))
1066  {
1067  if(has_children(dst_node))
1068  remove_children(dst_node);
1069  _clear_type(dst_node);
1070  if(src->has_key(src_node))
1071  to_seq(dst_node, src->key(src_node));
1072  else
1073  to_seq(dst_node);
1074  _p(dst_node)->m_type = src->_p(src_node)->m_type;
1075  }
1076  for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
1077  {
1078  id_type dch = append_child(dst_node);
1079  _copy_props_wo_key(dch, src, sch);
1080  merge_with(src, sch, dch);
1081  }
1082  }
1083  else
1084  {
1085  _RYML_CB_ASSERT(m_callbacks, src->is_map(src_node));
1086  if( ! is_map(dst_node))
1087  {
1088  if(has_children(dst_node))
1089  remove_children(dst_node);
1090  _clear_type(dst_node);
1091  if(src->has_key(src_node))
1092  to_map(dst_node, src->key(src_node));
1093  else
1094  to_map(dst_node);
1095  _p(dst_node)->m_type = src->_p(src_node)->m_type;
1096  }
1097  for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
1098  {
1099  id_type dch = find_child(dst_node, src->key(sch));
1100  if(dch == NONE)
1101  {
1102  dch = append_child(dst_node);
1103  _copy_props(dch, src, sch);
1104  }
1105  merge_with(src, sch, dch);
1106  }
1107  }
1108 }
1109 
1110 
1111 //-----------------------------------------------------------------------------
1112 
1114 {
1115  if(m_size == 0)
1116  return;
1117  ReferenceResolver rr;
1118  resolve(&rr);
1119 }
1120 
1121 void Tree::resolve(ReferenceResolver *C4_RESTRICT rr)
1122 {
1123  if(m_size == 0)
1124  return;
1125  rr->resolve(this);
1126 }
1127 
1128 
1129 //-----------------------------------------------------------------------------
1130 
1132 {
1133  id_type count = 0;
1134  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1135  ++count;
1136  return count;
1137 }
1138 
1140 {
1141  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1142  id_type count = 0;
1143  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1144  {
1145  if(count++ == pos)
1146  return i;
1147  }
1148  return NONE;
1149 }
1150 
1152 {
1153  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1154  id_type count = 0;
1155  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1156  {
1157  if(i == ch)
1158  return count;
1159  ++count;
1160  }
1161  return NONE;
1162 }
1163 
1164 #if defined(__clang__)
1165 # pragma clang diagnostic push
1166 # pragma GCC diagnostic ignored "-Wnull-dereference"
1167 #elif defined(__GNUC__)
1168 # pragma GCC diagnostic push
1169 # if __GNUC__ >= 6
1170 # pragma GCC diagnostic ignored "-Wnull-dereference"
1171 # endif
1172 # if __GNUC__ > 9
1173 # pragma GCC diagnostic ignored "-Wanalyzer-null-dereference"
1174 # endif
1175 #endif
1176 
1177 id_type Tree::find_child(id_type node, csubstr const& name) const
1178 {
1179  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1180  _RYML_CB_ASSERT(m_callbacks, is_map(node));
1181  if(get(node)->m_first_child == NONE)
1182  {
1183  _RYML_CB_ASSERT(m_callbacks, _p(node)->m_last_child == NONE);
1184  return NONE;
1185  }
1186  else
1187  {
1188  _RYML_CB_ASSERT(m_callbacks, _p(node)->m_last_child != NONE);
1189  }
1190  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1191  {
1192  if(_p(i)->m_key.scalar == name)
1193  {
1194  return i;
1195  }
1196  }
1197  return NONE;
1198 }
1199 
1200 #if defined(__clang__)
1201 # pragma clang diagnostic pop
1202 #elif defined(__GNUC__)
1203 # pragma GCC diagnostic pop
1204 #endif
1205 
1206 namespace {
1207 id_type depth_desc_(Tree const& C4_RESTRICT t, id_type id, id_type currdepth=0, id_type maxdepth=0)
1208 {
1209  maxdepth = currdepth > maxdepth ? currdepth : maxdepth;
1210  for(id_type child = t.first_child(id); child != NONE; child = t.next_sibling(child))
1211  {
1212  const id_type d = depth_desc_(t, child, currdepth+1, maxdepth);
1213  maxdepth = d > maxdepth ? d : maxdepth;
1214  }
1215  return maxdepth;
1216 }
1217 }
1218 
1220 {
1221  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1222  return depth_desc_(*this, node);
1223 }
1224 
1226 {
1227  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1228  id_type depth = 0;
1229  while(!is_root(node))
1230  {
1231  ++depth;
1232  node = parent(node);
1233  }
1234  return depth;
1235 }
1236 
1237 
1238 //-----------------------------------------------------------------------------
1239 
1240 void Tree::to_val(id_type node, csubstr val, type_bits more_flags)
1241 {
1242  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1243  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || ! parent_is_map(node));
1244  _set_flags(node, VAL|more_flags);
1245  _p(node)->m_key.clear();
1246  _p(node)->m_val = val;
1247 }
1248 
1249 void Tree::to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flags)
1250 {
1251  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1252  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
1253  _set_flags(node, KEYVAL|more_flags);
1254  _p(node)->m_key = key;
1255  _p(node)->m_val = val;
1256 }
1257 
1258 void Tree::to_map(id_type node, type_bits more_flags)
1259 {
1260  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1261  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || ! parent_is_map(node)); // parent must not have children with keys
1262  _set_flags(node, MAP|more_flags);
1263  _p(node)->m_key.clear();
1264  _p(node)->m_val.clear();
1265 }
1266 
1267 void Tree::to_map(id_type node, csubstr key, type_bits more_flags)
1268 {
1269  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1270  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
1271  _set_flags(node, KEY|MAP|more_flags);
1272  _p(node)->m_key = key;
1273  _p(node)->m_val.clear();
1274 }
1275 
1276 void Tree::to_seq(id_type node, type_bits more_flags)
1277 {
1278  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1279  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_seq(node));
1280  _set_flags(node, SEQ|more_flags);
1281  _p(node)->m_key.clear();
1282  _p(node)->m_val.clear();
1283 }
1284 
1285 void Tree::to_seq(id_type node, csubstr key, type_bits more_flags)
1286 {
1287  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1288  _RYML_CB_ASSERT(m_callbacks, parent(node) == NONE || parent_is_map(node));
1289  _set_flags(node, KEY|SEQ|more_flags);
1290  _p(node)->m_key = key;
1291  _p(node)->m_val.clear();
1292 }
1293 
1294 void Tree::to_doc(id_type node, type_bits more_flags)
1295 {
1296  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1297  _set_flags(node, DOC|more_flags);
1298  _p(node)->m_key.clear();
1299  _p(node)->m_val.clear();
1300 }
1301 
1302 void Tree::to_stream(id_type node, type_bits more_flags)
1303 {
1304  _RYML_CB_ASSERT(m_callbacks, ! has_children(node));
1305  _set_flags(node, STREAM|more_flags);
1306  _p(node)->m_key.clear();
1307  _p(node)->m_val.clear();
1308 }
1309 
1310 
1311 //-----------------------------------------------------------------------------
1313 {
1314  // this assumes we have a very small number of tag directives
1315  for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
1316  if(m_tag_directives[i].handle.empty())
1317  return i;
1318  return RYML_MAX_TAG_DIRECTIVES;
1319 }
1320 
1322 {
1323  for(TagDirective &td : m_tag_directives)
1324  td = {};
1325 }
1326 
1328 {
1329  _RYML_CB_CHECK(m_callbacks, !td.handle.empty());
1330  _RYML_CB_CHECK(m_callbacks, !td.prefix.empty());
1331  _RYML_CB_CHECK(m_callbacks, td.handle.begins_with('!'));
1332  _RYML_CB_CHECK(m_callbacks, td.handle.ends_with('!'));
1333  // https://yaml.org/spec/1.2.2/#rule-ns-word-char
1334  _RYML_CB_CHECK(m_callbacks, td.handle == '!' || td.handle == "!!" || td.handle.trim('!').first_not_of("01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-") == npos);
1335  id_type pos = num_tag_directives();
1336  _RYML_CB_CHECK(m_callbacks, pos < RYML_MAX_TAG_DIRECTIVES);
1337  m_tag_directives[pos] = td;
1338  return pos;
1339 }
1340 
1341 bool Tree::add_tag_directive(csubstr directive_)
1342 {
1343  TagDirective td;
1344  if(td.create_from_str(directive_, this))
1345  {
1346  add_tag_directive(td);
1347  return true;
1348  }
1349  return false;
1350 }
1351 
1352 size_t Tree::resolve_tag(substr output, csubstr tag, id_type node_id) const
1353 {
1354  // lookup from the end. We want to find the first directive that
1355  // matches the tag and has a target node id leq than the given
1356  // node_id.
1357  for(id_type i = RYML_MAX_TAG_DIRECTIVES-1; i != (id_type)-1; --i)
1358  {
1359  auto const& td = m_tag_directives[i];
1360  if(td.handle.empty())
1361  continue;
1362  if(tag.begins_with(td.handle) && td.next_node_id <= node_id)
1363  return td.transform(tag, output, m_callbacks);
1364  }
1365  if(tag.begins_with('!'))
1366  {
1367  if(is_custom_tag(tag))
1368  {
1369  _RYML_CB_ERR(m_callbacks, "tag directive not found");
1370  }
1371  }
1372  return 0; // return 0 to signal that the tag is local and cannot be resolved
1373 }
1374 
1375 namespace {
1376 csubstr _transform_tag(Tree *t, csubstr tag, id_type node)
1377 {
1378  _c4dbgpf("[{}] resolving tag ~~~{}~~~", node, tag);
1379  size_t required_size = t->resolve_tag(substr{}, tag, node);
1380  if(!required_size)
1381  {
1382  if(tag.begins_with("!<"))
1383  tag = tag.sub(1);
1384  _c4dbgpf("[{}] resolved tag: ~~~{}~~~", node, tag);
1385  return tag;
1386  }
1387  const char *prev_arena = t->arena().str;(void)prev_arena;
1388  substr buf = t->alloc_arena(required_size);
1389  _RYML_CB_ASSERT(t->m_callbacks, t->arena().str == prev_arena);
1390  size_t actual_size = t->resolve_tag(buf, tag, node);
1391  _RYML_CB_ASSERT(t->m_callbacks, actual_size <= required_size);
1392  _c4dbgpf("[{}] resolved tag: ~~~{}~~~", node, buf.first(actual_size));
1393  return buf.first(actual_size);
1394 }
1395 void _resolve_tags(Tree *t, id_type node)
1396 {
1397  NodeData *C4_RESTRICT d = t->_p(node);
1398  if(d->m_type & KEYTAG)
1399  d->m_key.tag = _transform_tag(t, d->m_key.tag, node);
1400  if(d->m_type & VALTAG)
1401  d->m_val.tag = _transform_tag(t, d->m_val.tag, node);
1402  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1403  _resolve_tags(t, child);
1404 }
1405 size_t _count_resolved_tags_size(Tree const* t, id_type node)
1406 {
1407  size_t sz = 0;
1408  NodeData const* C4_RESTRICT d = t->_p(node);
1409  if(d->m_type & KEYTAG)
1410  sz += t->resolve_tag(substr{}, d->m_key.tag, node);
1411  if(d->m_type & VALTAG)
1412  sz += t->resolve_tag(substr{}, d->m_val.tag, node);
1413  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1414  sz += _count_resolved_tags_size(t, child);
1415  return sz;
1416 }
1417 void _normalize_tags(Tree *t, id_type node)
1418 {
1419  NodeData *C4_RESTRICT d = t->_p(node);
1420  if(d->m_type & KEYTAG)
1421  d->m_key.tag = normalize_tag(d->m_key.tag);
1422  if(d->m_type & VALTAG)
1423  d->m_val.tag = normalize_tag(d->m_val.tag);
1424  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1425  _normalize_tags(t, child);
1426 }
1427 void _normalize_tags_long(Tree *t, id_type node)
1428 {
1429  NodeData *C4_RESTRICT d = t->_p(node);
1430  if(d->m_type & KEYTAG)
1431  d->m_key.tag = normalize_tag_long(d->m_key.tag);
1432  if(d->m_type & VALTAG)
1433  d->m_val.tag = normalize_tag_long(d->m_val.tag);
1434  for(id_type child = t->first_child(node); child != NONE; child = t->next_sibling(child))
1435  _normalize_tags_long(t, child);
1436 }
1437 } // namespace
1438 
1440 {
1441  if(empty())
1442  return;
1443  size_t needed_size = _count_resolved_tags_size(this, root_id());
1444  if(needed_size)
1445  reserve_arena(arena_size() + needed_size);
1446  _resolve_tags(this, root_id());
1447 }
1448 
1450 {
1451  if(empty())
1452  return;
1453  _normalize_tags(this, root_id());
1454 }
1455 
1457 {
1458  if(empty())
1459  return;
1460  _normalize_tags_long(this, root_id());
1461 }
1462 
1463 
1464 //-----------------------------------------------------------------------------
1465 
1467 {
1468  csubstr p = path.first(path_pos);
1469  if(p.ends_with('.'))
1470  p = p.first(p.len-1);
1471  return p;
1472 }
1473 
1475 {
1476  return path.sub(path_pos);
1477 }
1478 
1479 void Tree::_advance(lookup_result *r, size_t more)
1480 {
1481  r->path_pos += more;
1482  if(r->path.sub(r->path_pos).begins_with('.'))
1483  ++r->path_pos;
1484 }
1485 
1487 {
1488  if(start == NONE)
1489  start = root_id();
1490  lookup_result r(path, start);
1491  if(path.empty())
1492  return r;
1493  _lookup_path(&r);
1494  if(r.target == NONE && r.closest == start)
1495  r.closest = NONE;
1496  return r;
1497 }
1498 
1499 id_type Tree::lookup_path_or_modify(csubstr default_value, csubstr path, id_type start)
1500 {
1501  id_type target = _lookup_path_or_create(path, start);
1502  if(parent_is_map(target))
1503  to_keyval(target, key(target), default_value);
1504  else
1505  to_val(target, default_value);
1506  return target;
1507 }
1508 
1509 id_type Tree::lookup_path_or_modify(Tree const *src, id_type src_node, csubstr path, id_type start)
1510 {
1511  id_type target = _lookup_path_or_create(path, start);
1512  merge_with(src, src_node, target);
1513  return target;
1514 }
1515 
1516 id_type Tree::_lookup_path_or_create(csubstr path, id_type start)
1517 {
1518  if(start == NONE)
1519  start = root_id();
1520  lookup_result r(path, start);
1521  _lookup_path(&r);
1522  if(r.target != NONE)
1523  {
1524  C4_ASSERT(r.unresolved().empty());
1525  return r.target;
1526  }
1527  _lookup_path_modify(&r);
1528  return r.target;
1529 }
1530 
1531 void Tree::_lookup_path(lookup_result *r) const
1532 {
1533  C4_ASSERT( ! r->unresolved().empty());
1534  _lookup_path_token parent{"", type(r->closest)};
1535  id_type node;
1536  do
1537  {
1538  node = _next_node(r, &parent);
1539  if(node != NONE)
1540  r->closest = node;
1541  if(r->unresolved().empty())
1542  {
1543  r->target = node;
1544  return;
1545  }
1546  } while(node != NONE);
1547 }
1548 
1549 void Tree::_lookup_path_modify(lookup_result *r)
1550 {
1551  C4_ASSERT( ! r->unresolved().empty());
1552  _lookup_path_token parent{"", type(r->closest)};
1553  id_type node;
1554  do
1555  {
1556  node = _next_node_modify(r, &parent);
1557  if(node != NONE)
1558  r->closest = node;
1559  if(r->unresolved().empty())
1560  {
1561  r->target = node;
1562  return;
1563  }
1564  } while(node != NONE);
1565 }
1566 
1567 id_type Tree::_next_node(lookup_result * r, _lookup_path_token *parent) const
1568 {
1569  _lookup_path_token token = _next_token(r, *parent);
1570  if( ! token)
1571  return NONE;
1572 
1573  id_type node = NONE;
1574  csubstr prev = token.value;
1575  if(token.type == MAP || token.type == SEQ)
1576  {
1577  _RYML_CB_ASSERT(m_callbacks, !token.value.begins_with('['));
1578  //_RYML_CB_ASSERT(m_callbacks, is_container(r->closest) || r->closest == NONE);
1579  _RYML_CB_ASSERT(m_callbacks, is_map(r->closest));
1580  node = find_child(r->closest, token.value);
1581  }
1582  else if(token.type == KEYVAL)
1583  {
1584  _RYML_CB_ASSERT(m_callbacks, r->unresolved().empty());
1585  if(is_map(r->closest))
1586  node = find_child(r->closest, token.value);
1587  }
1588  else if(token.type == KEY)
1589  {
1590  _RYML_CB_ASSERT(m_callbacks, token.value.begins_with('[') && token.value.ends_with(']'));
1591  token.value = token.value.offs(1, 1).trim(' ');
1592  id_type idx = 0;
1593  _RYML_CB_CHECK(m_callbacks, from_chars(token.value, &idx));
1594  node = child(r->closest, idx);
1595  }
1596  else
1597  {
1598  C4_NEVER_REACH();
1599  }
1600 
1601  if(node != NONE)
1602  {
1603  *parent = token;
1604  }
1605  else
1606  {
1607  csubstr p = r->path.sub(r->path_pos > 0 ? r->path_pos - 1 : r->path_pos);
1608  r->path_pos -= prev.len;
1609  if(p.begins_with('.'))
1610  r->path_pos -= 1u;
1611  }
1612 
1613  return node;
1614 }
1615 
1616 id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
1617 {
1618  _lookup_path_token token = _next_token(r, *parent);
1619  if( ! token)
1620  return NONE;
1621 
1622  id_type node = NONE;
1623  if(token.type == MAP || token.type == SEQ)
1624  {
1625  _RYML_CB_ASSERT(m_callbacks, !token.value.begins_with('['));
1626  //_RYML_CB_ASSERT(m_callbacks, is_container(r->closest) || r->closest == NONE);
1627  if( ! is_container(r->closest))
1628  {
1629  if(has_key(r->closest))
1630  to_map(r->closest, key(r->closest));
1631  else
1632  to_map(r->closest);
1633  }
1634  else
1635  {
1636  if(is_map(r->closest))
1637  node = find_child(r->closest, token.value);
1638  else
1639  {
1640  id_type pos = NONE;
1641  _RYML_CB_CHECK(m_callbacks, c4::atox(token.value, &pos));
1642  _RYML_CB_ASSERT(m_callbacks, pos != NONE);
1643  node = child(r->closest, pos);
1644  }
1645  }
1646  if(node == NONE)
1647  {
1648  _RYML_CB_ASSERT(m_callbacks, is_map(r->closest));
1649  node = append_child(r->closest);
1650  NodeData *n = _p(node);
1651  n->m_key.scalar = token.value;
1652  n->m_type.add(KEY);
1653  }
1654  }
1655  else if(token.type == KEYVAL)
1656  {
1657  _RYML_CB_ASSERT(m_callbacks, r->unresolved().empty());
1658  if(is_map(r->closest))
1659  {
1660  node = find_child(r->closest, token.value);
1661  if(node == NONE)
1662  node = append_child(r->closest);
1663  }
1664  else
1665  {
1666  _RYML_CB_ASSERT(m_callbacks, !is_seq(r->closest));
1667  _add_flags(r->closest, MAP);
1668  node = append_child(r->closest);
1669  }
1670  NodeData *n = _p(node);
1671  n->m_key.scalar = token.value;
1672  n->m_val.scalar = "";
1673  n->m_type.add(KEYVAL);
1674  }
1675  else if(token.type == KEY)
1676  {
1677  _RYML_CB_ASSERT(m_callbacks, token.value.begins_with('[') && token.value.ends_with(']'));
1678  token.value = token.value.offs(1, 1).trim(' ');
1679  id_type idx;
1680  if( ! from_chars(token.value, &idx))
1681  return NONE;
1682  if( ! is_container(r->closest))
1683  {
1684  if(has_key(r->closest))
1685  {
1686  csubstr k = key(r->closest);
1687  _clear_type(r->closest);
1688  to_seq(r->closest, k);
1689  }
1690  else
1691  {
1692  _clear_type(r->closest);
1693  to_seq(r->closest);
1694  }
1695  }
1696  _RYML_CB_ASSERT(m_callbacks, is_container(r->closest));
1697  node = child(r->closest, idx);
1698  if(node == NONE)
1699  {
1700  _RYML_CB_ASSERT(m_callbacks, num_children(r->closest) <= idx);
1701  for(id_type i = num_children(r->closest); i <= idx; ++i)
1702  {
1703  node = append_child(r->closest);
1704  if(i < idx)
1705  {
1706  if(is_map(r->closest))
1707  to_keyval(node, /*"~"*/{}, /*"~"*/{});
1708  else if(is_seq(r->closest))
1709  to_val(node, /*"~"*/{});
1710  }
1711  }
1712  }
1713  }
1714  else
1715  {
1716  C4_NEVER_REACH();
1717  }
1718 
1719  _RYML_CB_ASSERT(m_callbacks, node != NONE);
1720  *parent = token;
1721  return node;
1722 }
1723 
1724 /* types of tokens:
1725  * - seeing "map." ---> "map"/MAP
1726  * - finishing "scalar" ---> "scalar"/KEYVAL
1727  * - seeing "seq[n]" ---> "seq"/SEQ (--> "[n]"/KEY)
1728  * - seeing "[n]" ---> "[n]"/KEY
1729  */
1730 Tree::_lookup_path_token Tree::_next_token(lookup_result *r, _lookup_path_token const& parent) const
1731 {
1732  csubstr unres = r->unresolved();
1733  if(unres.empty())
1734  return {};
1735 
1736  // is it an indexation like [0], [1], etc?
1737  if(unres.begins_with('['))
1738  {
1739  size_t pos = unres.find(']');
1740  if(pos == csubstr::npos)
1741  return {};
1742  csubstr idx = unres.first(pos + 1);
1743  _advance(r, pos + 1);
1744  return {idx, KEY};
1745  }
1746 
1747  // no. so it must be a name
1748  size_t pos = unres.first_of(".[");
1749  if(pos == csubstr::npos)
1750  {
1751  _advance(r, unres.len);
1752  NodeType t;
1753  if(( ! parent) || parent.type.is_seq())
1754  return {unres, VAL};
1755  return {unres, KEYVAL};
1756  }
1757 
1758  // it's either a map or a seq
1759  _RYML_CB_ASSERT(m_callbacks, unres[pos] == '.' || unres[pos] == '[');
1760  if(unres[pos] == '.')
1761  {
1762  _RYML_CB_ASSERT(m_callbacks, pos != 0);
1763  _advance(r, pos + 1);
1764  return {unres.first(pos), MAP};
1765  }
1766 
1767  _RYML_CB_ASSERT(m_callbacks, unres[pos] == '[');
1768  _advance(r, pos);
1769  return {unres.first(pos), SEQ};
1770 }
1771 
1772 
1773 } // namespace yml
1774 } // namespace c4
1775 
1776 
1777 C4_SUPPRESS_WARNING_GCC_CLANG_POP
1778 C4_SUPPRESS_WARNING_MSVC_POP
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:838
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:979
NodeData * m_buf
Definition: tree.hpp:1309
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:955
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:1314
lookup_result lookup_path(csubstr path, id_type start=NONE) const
for example foo.bar[0].baz
Definition: tree.cpp:1486
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:1499
id_type first_child(id_type node) const
Definition: tree.hpp:462
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:456
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:1318
bool is_map(id_type node) const
Definition: tree.hpp:366
void clear_tag_directives()
Definition: tree.cpp:1321
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:1249
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:943
bool is_keyval(id_type node) const
Definition: tree.hpp:371
TagDirective m_tag_directives[RYML_MAX_TAG_DIRECTIVES]
Definition: tree.hpp:1322
void reserve(id_type node_capacity)
Definition: tree.cpp:254
id_type m_size
Definition: tree.hpp:1312
void to_doc(id_type node, type_bits more_flags=0)
Definition: tree.cpp:1294
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:1225
bool in_arena(csubstr s) const
return true if the given substring is part of the tree's string arena
Definition: tree.hpp:796
id_type parent(id_type node) const
Definition: tree.hpp:454
size_t resolve_tag(substr output, csubstr tag, id_type node_id) const
resolve the given tag, appearing at node_id.
Definition: tree.cpp:1352
void resolve_tags()
Definition: tree.cpp:1439
bool add_tag_directive(csubstr directive)
Definition: tree.cpp:1341
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:1151
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:642
void to_stream(id_type node, type_bits more_flags=0)
Definition: tree.cpp:1302
bool has_sibling(id_type node, id_type sib) const
true if node has a sibling with id sib
Definition: tree.hpp:430
id_type next_sibling(id_type node) const
Definition: tree.hpp:457
void to_seq(id_type node, csubstr key, type_bits more_flags=0)
Definition: tree.cpp:1285
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:1219
id_type num_tag_directives() const
Definition: tree.cpp:1312
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:1310
id_type last_child(id_type node) const
Definition: tree.hpp:463
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:1317
void normalize_tags()
Definition: tree.cpp:1449
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 resolve()
Resolve references using a throw-away resolver.
Definition: tree.cpp:1113
void remove_children(id_type node)
remove all the node's children, but keep the node itself
Definition: tree.cpp:857
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:1267
size_t arena_capacity() const
get the current capacity of the tree's internal arena
Definition: tree.hpp:786
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:1315
void normalize_tags_long()
Definition: tree.cpp:1456
csubstr const & val(id_type node) const
Definition: tree.hpp:346
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:674
id_type find_child(id_type node, csubstr const &key) const
Definition: tree.cpp:1177
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:1131
csubstr arena() const
get the current arena
Definition: tree.hpp:791
void merge_with(Tree const *src, id_type src_node=NONE, id_type dst_root=NONE)
Definition: tree.cpp:1036
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:784
id_type child(id_type node, id_type pos) const
Definition: tree.cpp:1139
bool has_child(id_type node, id_type ch) const
true if node has a child with id ch
Definition: tree.hpp:423
Callbacks m_callbacks
Definition: tree.hpp:1320
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:1240
bool has_children(id_type node) const
true if node has any children key
Definition: tree.hpp:427
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 the 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:1474
csubstr resolved() const
get the part ot the input path that was resolved
Definition: tree.cpp:1466