// File: /workspace/github_cpp_makefile_star2_O3/ruankotovich[P]TextCorrection-Trie/src/trie.hpp
// Function: trie::Trie_t::buildNewSet(std::set<trie::ActiveNode_t, std::less<trie::ActiveNode_t>, std::allocator<trie::ActiveNode_t>>&, unsigned int)
std::set<ActiveNode_t> buildNewSet(std::set<ActiveNode_t>& set, unsigned int curChar)
    {
      std::set<ActiveNode_t> activeNodeSet;

      /*
      ** Considering node N
      ** We can transform the content of this node for the currentWord by first adding a character. (Prebuilt Addiction)
      */
      // std::cout << "\nBuilding node for " << m_reverseCharacterMap[curChar] << '\n';
      for (auto curActiveNode = set.begin(); curActiveNode != set.end(); curActiveNode++) {
        if (curActiveNode->editDistance < m_fuzzyLimitThreshold) {
          // std::cout << " * Updating distance for node " << m_reverseCharacterMap[curActiveNode->node->getContent()] << " from " << curActiveNode->editDistance << " to " << curActiveNode->editDistance + 1 << '\n';
          activeNodeSet.emplace(curActiveNode->node, curActiveNode->editDistance + 1);
        }
      }

      /*
      ** Considering no+de P (child of N)
      ** -  C1. Different character from `curChar` : (Posbuilt Substitution Addiction)
      **    We can transform this character of P for the current character by substitution, relative to ED(N), then, we have to add P to
      **    the set if and only if ED(N)+1 [previously calculated and updated] < threshold
      **
      ** -  C2. Equals character from `curChar` : (Posbuilt Match Addiction)
      **    If this node character match with `curChar`, it means that we got to add it with ED(N) (indeed, when we got a match, we have ED=0) [1]
      **    Once doing it, we have to add the descendant nodes of P (called here `PChild`) with distance PChild = ED(P) + DeepthFrom(P, PChild) < threshold
      **
      ** Addendum [1] - I must update the distance only if the new distance is lower than the last distance (it happens when a previously father node have been calculated)
      */

      for (auto curActiveNode = set.begin(); curActiveNode != set.end(); curActiveNode++) {

        // std::cout << "\nScanning children of node " << m_reverseCharacterMap[curActiveNode->node->getContent()] << '\n';

        for (auto childOfcurActiveNode : curActiveNode->node->getChildren()) {
          // std::cout << " * Node " << m_reverseCharacterMap[childOfcurActiveNode->getContent()] << " found ";
          if (childOfcurActiveNode->getContent() != curChar) { // case 1
            // std::cout << " and haven't matched with the character,";
            if (curActiveNode->editDistance < m_fuzzyLimitThreshold) { // verify if ED(N)+1 < P
              // std::cout << " but it's edit distance can be increased and it would be added to the set";
              int newEditDistance = curActiveNode->editDistance + 1; // the new edit distance for the child

              // retrieves to add the child, return FALSE if it was not possible, cuz the child was already added before (so, i've to verify which edit distance is lower : the current or the previous?)
              auto childIteratorOnSet = activeNodeSet.emplace(childOfcurActiveNode, newEditDistance);

              // if the child was not added (it was cuz' there was a activeNode marked on this node before) and the last ED is bigger than the new ED
              if (!childIteratorOnSet.second) {
                if (childIteratorOnSet.first->editDistance > newEditDistance) {
                  childIteratorOnSet.first->editDistance = newEditDistance; // guaranteed by addendum 1
                }
              }
            }
            // std::cout << '\n';
          } else { // case 2
            // std::cout << " and it matches with the character, so will be added to the set\n";
            // retrieves to add the child, return FALSE if it was not possible, cuz the child was already added before (so, i've to verify which edit distance is lower : the current or the previous?)
            auto childIteratorOnSet = activeNodeSet.emplace(childOfcurActiveNode, curActiveNode->editDistance);

            int currentChildDistance = childIteratorOnSet.first->editDistance;

            if (!childIteratorOnSet.second) { /* processor thanks! */
              if (childIteratorOnSet.first->editDistance > curActiveNode->editDistance) {
                currentChildDistance = childIteratorOnSet.first->editDistance = curActiveNode->editDistance;
              }
            }

            /*
            **  I've to fetch the children and the entire set of children to the active node if
            **  and only if the dist( father, getChildRecursive(father)) ) < threshold;
            */
            // std::cout << "\t\tIt's children will be verifieds to be added to the set : \n";

            std::queue<TrieNode_t*> toRecover; // a queue to save which node is on the way
            toRecover.push(childIteratorOnSet.first->node); // adding the current matched node to the queue

            // while the distance is lesser than the limit and we got some node to recover...
            while (currentChildDistance < m_fuzzyLimitThreshold && !toRecover.empty()) {
              // recover the current node from the queue
              TrieNode_t*& currentNode = toRecover.front();
              // std::cout << "\t\tCurrent node : " << m_reverseCharacterMap[currentNode->getContent()] << '\n';
              // and update the distance to one more
              ++currentChildDistance;

              // for each child of the current node
              for (auto child : currentNode->getChildren()) {

                // we add this child to the active node set, once we can face it as a addiction operation inside the boundary imposed by the search
                auto currentChildIterator = activeNodeSet.emplace(child, currentChildDistance);
                // std::cout << "\t\t * Children " << m_reverseCharacterMap[currentChildIterator.first->node->getContent()] << " added to the set ";
                // if there was this child within the activeNode, we have to keep the minor operation distance
                if (!currentChildIterator.second) {
                  // std::cout << "by update/permanence ";
                  if (currentChildIterator.first->editDistance > currentChildDistance) {
                    currentChildIterator.first->editDistance = currentChildDistance;
                  }
                }
                // std::cout<<" with edit distance " << currentChildIterator.first->editDistance << '\n';

                // and put it if and only if the currentDistance is lesser than the thresould (the memory and processment thank!)
                if (currentChildDistance < m_fuzzyLimitThreshold) {
                  toRecover.push(child);
                }
              }
              toRecover.pop();
            }
          }
        }
      }
      // std::cout << "\n\n";
      return activeNodeSet;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %ecx, 0x1c(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rdi), %rax
movl $0x0, 0x8(%rdi)
xorl %ecx, %ecx
movq %rcx, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
movq %rcx, 0x28(%rdi)
movq 0x18(%rdx), %rbp
leaq 0x8(%rdx), %r13
cmpq %r13, %rbp
je 0x5dea
movq %rdx, %r15
leaq 0x20(%rsp), %r12
movl 0x28(%rbp), %eax
cmpl 0xdc(%r14), %eax
jge 0x5dd6
leaq 0x20(%rbp), %rsi
incl %eax
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, %rdx
callq 0x6302
movq %rbp, %rdi
callq 0x21b0
movq %rax, %rbp
cmpq %r13, %rax
jne 0x5db5
movq 0x18(%r15), %rbp
movq %r13, 0xc0(%rsp)
cmpq %r13, %rbp
je 0x60e6
movq %rbp, %rax
leaq 0x80(%rsp), %r15
leaq 0xc(%rsp), %rbp
movq %rax, 0x78(%rsp)
movq 0x20(%rax), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x4e0a
movq 0xa8(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpq %rax, %rdi
je 0x60b9
movq 0x78(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0xd0(%rsp)
movq (%rdi), %rax
movq %rax, 0x88(%rsp)
movl 0x1c(%rsp), %ecx
cmpl %ecx, 0x30(%rax)
jne 0x5ef6
movq %rbx, %rdi
leaq 0x88(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x5a36
movq %rax, %r12
movl 0x28(%rax), %eax
movl %eax, 0xc(%rsp)
testb $0x1, %dl
jne 0x5ea8
movq 0x10(%rsp), %rcx
movl (%rcx), %ecx
cmpl %ecx, %eax
jle 0x5ea8
movl %ecx, 0x28(%r12)
movl %ecx, 0xc(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x63b0
addq $0x20, %r12
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x5f43
movq (%r12), %rcx
movq %rcx, (%rax)
addq $0x8, 0x50(%rsp)
jmp 0x5f50
movq 0x10(%rsp), %rax
movl (%rax), %eax
cmpl 0xdc(%r14), %eax
jge 0x6097
incl %eax
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
leaq 0x88(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x5a36
testb $0x1, %dl
jne 0x6097
movl 0x20(%rsp), %ecx
cmpl %ecx, 0x28(%rax)
jle 0x6097
movl %ecx, 0x28(%rax)
jmp 0x6097
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x6526
movl 0xc(%rsp), %ecx
cmpl 0xdc(%r14), %ecx
jge 0x608d
movq 0x30(%rsp), %rax
cmpq %rax, 0x50(%rsp)
je 0x608d
incl %ecx
movl %ecx, 0xc(%rsp)
movq (%rax), %rsi
leaq 0x90(%rsp), %rdi
callq 0x4e0a
movq 0x90(%rsp), %r12
movq 0x98(%rsp), %r13
cmpq %r13, %r12
je 0x6014
movq (%r12), %rax
movq %rax, 0x80(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x5a36
movl 0xc(%rsp), %ecx
testb $0x1, %dl
jne 0x5fc7
cmpl %ecx, 0x28(%rax)
jle 0x5fc7
movl %ecx, 0x28(%rax)
cmpl 0xdc(%r14), %ecx
jge 0x6003
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x5ff6
movq 0x80(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x50(%rsp)
jmp 0x6003
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x6526
addq $0x8, %r12
cmpq %r13, %r12
jne 0x5f9c
movq 0x90(%rsp), %r12
testq %r12, %r12
je 0x602c
movq 0xa0(%rsp), %rsi
subq %r12, %rsi
movq %r12, %rdi
callq 0x2180
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x6045
addq $0x8, %rax
jmp 0x6077
movq 0x38(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x2180
movq 0x48(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x38(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x30(%rsp)
movl 0xc(%rsp), %ecx
cmpl 0xdc(%r14), %ecx
jl 0x5f66
leaq 0x20(%rsp), %rdi
callq 0x673a
movq 0xd0(%rsp), %rdi
addq $0x8, %rdi
cmpq 0xc8(%rsp), %rdi
jne 0x5e50
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x60ce
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x78(%rsp), %rdi
callq 0x21b0
cmpq 0xc0(%rsp), %rax
jne 0x5e0b
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x60fd
movq %rax, %r14
jmp 0x6159
jmp 0x610d
jmp 0x6108
jmp 0x6108
movq %rax, %r14
jmp 0x613c
movq %rax, %r14
jmp 0x6132
movq %rax, %r14
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x6132
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x20(%rsp), %rdi
callq 0x673a
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x6159
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x4766
movq %r14, %rdi
callq 0x2270
nop