// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc8, %rsp
movl %ecx, 0x14(%rsp)
movq %rdx, %r15
movq %rdi, %rbx
leaq 0x8(%rdi), %rax
andl $0x0, 0x8(%rdi)
andq $0x0, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
andq $0x0, 0x28(%rdi)
movq %rsi, %r14
movq 0x18(%rdx), %r13
leaq 0x8(%rdx), %rbp
leaq 0x78(%rsp), %r12
cmpq %rbp, %r13
je 0x5989
movl 0x28(%r13), %eax
cmpl 0xdc(%r14), %eax
jge 0x597c
leaq 0x20(%r13), %rsi
incl %eax
movl %eax, 0x78(%rsp)
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5d98
movq %r13, %rdi
callq 0x21a0
movq %rax, %r13
jmp 0x5955
movq %rbp, 0x20(%rsp)
movq 0x18(%r15), %rax
leaq 0x78(%rsp), %r15
leaq 0x40(%rsp), %r12
cmpq 0x20(%rsp), %rax
je 0x5b5a
movq %r15, %rbp
movq 0x20(%rax), %rsi
leaq 0x60(%rsp), %rdi
movq %rax, %r15
callq 0x475e
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq %r15, 0x28(%rsp)
leaq 0x28(%r15), %rcx
movq %rcx, 0x8(%rsp)
movq %rbp, %r15
cmpq 0x30(%rsp), %rax
je 0x5b41
movq %rax, 0x38(%rsp)
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rsp), %ecx
cmpl %ecx, 0x30(%rax)
jne 0x5af2
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x5636
movq %rax, %r13
movl 0x28(%rax), %eax
movl %eax, 0x4(%rsp)
testb $0x1, %dl
jne 0x5a34
movq 0x8(%rsp), %rcx
movl (%rcx), %ecx
cmpl %ecx, %eax
jle 0x5a34
movl %ecx, 0x28(%r13)
movl %ecx, 0x4(%rsp)
movq %r15, %rdi
callq 0x5d7c
addq $0x20, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x601a
movl 0x4(%rsp), %eax
cmpl 0xdc(%r14), %eax
jge 0x5b2b
movq 0x88(%rsp), %rcx
cmpq %rcx, 0xa8(%rsp)
je 0x5b2b
incl %eax
movl %eax, 0x4(%rsp)
movq (%rcx), %rsi
leaq 0x48(%rsp), %rdi
callq 0x475e
movq 0x48(%rsp), %r13
movq 0x50(%rsp), %r15
cmpq %r15, %r13
je 0x5ad8
movq (%r13), %rax
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x4(%rsp), %rdx
callq 0x5636
movl 0x4(%rsp), %ecx
testb $0x1, %dl
jne 0x5abe
cmpl %ecx, 0x28(%rax)
jle 0x5abe
movl %ecx, 0x28(%rax)
cmpl 0xdc(%r14), %ecx
jge 0x5ad2
movq %rbp, %rdi
movq %r12, %rsi
callq 0x601a
addq $0x8, %r13
jmp 0x5a8f
leaq 0x48(%rsp), %rdi
callq 0x56fe
movq %rbp, %r15
movq %rbp, %rdi
callq 0x624e
jmp 0x5a4b
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpl 0xdc(%r14), %eax
jge 0x5b33
incl %eax
movl %eax, 0x78(%rsp)
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x5636
testb $0x1, %dl
jne 0x5b33
movl 0x78(%rsp), %ecx
cmpl %ecx, 0x28(%rax)
jle 0x5b33
movl %ecx, 0x28(%rax)
jmp 0x5b33
movq %r15, %rdi
callq 0x62a4
movq 0x38(%rsp), %rax
addq $0x8, %rax
jmp 0x59db
leaq 0x60(%rsp), %rdi
callq 0x56fe
movq 0x28(%rsp), %rdi
callq 0x21a0
jmp 0x599c
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5b71
movq %rax, %r14
jmp 0x5ba7
jmp 0x5b7c
jmp 0x5b7c
jmp 0x5b81
movq %rax, %r14
jmp 0x5b9d
movq %rax, %r14
jmp 0x5b93
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x56fe
leaq 0x78(%rsp), %rdi
callq 0x62a4
leaq 0x60(%rsp), %rdi
callq 0x56fe
movq %rbx, %rdi
callq 0x4022
movq %r14, %rdi
callq 0x2280
nop