@@ -984,49 +984,59 @@ id_type Tree::duplicate_children_no_rep(Tree const *src, id_type node, id_type p
984984 _RYML_CB_CHECK (m_callbacks, this != src || (parent != i && !is_ancestor (parent, i)));
985985 if (is_seq (parent))
986986 {
987- prev = duplicate (i, parent, prev);
987+ _c4dbgpf (" duplicate_no_rep: {} is seq" , parent);
988+ prev = duplicate (src, i, parent, prev);
988989 }
989990 else
990991 {
992+ _c4dbgpf (" duplicate_no_rep: {} is map" , parent);
991993 _RYML_CB_ASSERT (m_callbacks, is_map (parent));
992994 // does the parent already have a node with key equal to that of the current duplicate?
993- id_type rep = NONE, rep_pos = NONE;
994- for (id_type j = first_child (parent), jcount = 0 ; j != NONE; ++jcount, j = next_sibling (j))
995+ id_type dstnode_dup = NONE, dstnode_dup_pos = NONE;
995996 {
996- if (key (j) == key (i))
997+ csubstr srckey = src->key (i);
998+ for (id_type j = first_child (parent), jcount = 0 ; j != NONE; ++jcount, j = next_sibling (j))
997999 {
998- rep = j;
999- rep_pos = jcount;
1000- break ;
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+ }
10011007 }
10021008 }
1003- if (rep == NONE) // there is no repetition; just duplicate
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
10041011 {
10051012 _c4dbgpf (" duplicate_no_rep: no repetition, just duplicate i={} parent={} prev={}" , i, parent, prev);
10061013 prev = duplicate (src, i, parent, prev);
10071014 }
10081015 else // yes, there is a repetition
10091016 {
1010- if (after_pos != NONE && rep_pos < after_pos)
1017+ if (after_pos != NONE && dstnode_dup_pos < after_pos)
10111018 {
1012- // rep is located before the node which will be inserted,
1019+ // the dst duplicate is located before the node which will be inserted,
10131020 // and will be overridden by the duplicate. So replace it.
1014- remove (rep);
1021+ _c4dbgpf (" duplicate_no_dstnode_dup: replace {}/{} with {}/{}" , parent, dstnode_dup, node, i);
1022+ remove (dstnode_dup);
10151023 prev = duplicate (src, i, parent, prev);
10161024 }
10171025 else if (prev == NONE)
10181026 {
1019- // first iteration with prev = after = NONE and repetition
1020- prev = rep;
1027+ _c4dbgpf (" duplicate_no_dstnode_dup: {}=prev <- {}" , prev, dstnode_dup);
1028+ // first iteration with prev = after = NONE and dstnode_dupetition
1029+ prev = dstnode_dup;
10211030 }
1022- else if (rep != prev)
1031+ else if (dstnode_dup != prev)
10231032 {
1024- // rep is located after the node which will be inserted
1025- // and overrides it. So move the rep into this node's place.
1026- move (rep, prev);
1027- prev = rep;
1033+ // dstnode_dup is located after the node which will be inserted
1034+ // and overrides it. So move the dstnode_dup into this node's place.
1035+ _c4dbgpf (" duplicate_no_dstnode_dup: move({}, {})" , dstnode_dup, prev);
1036+ move (dstnode_dup, prev);
1037+ prev = dstnode_dup;
10281038 }
1029- } // there's a repetition
1039+ } // there's a dstnode_dupetition
10301040 }
10311041 }
10321042
0 commit comments