// File: 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:
subq $0x238, %rsp            # imm = 0x238
movq %rdi, 0x68(%rsp)
movq %rdi, %rax
movq %rax, 0x70(%rsp)
movq %rdi, 0x230(%rsp)
movq %rsi, 0x228(%rsp)
movq %rdx, 0x220(%rsp)
movl %ecx, 0x21c(%rsp)
movq 0x228(%rsp), %rax
movq %rax, 0x78(%rsp)
movb $0x0, 0x21b(%rsp)
callq 0x40e0
movq 0x220(%rsp), %rdi
callq 0x11bb0
movq %rax, 0x210(%rsp)
movq 0x220(%rsp), %rdi
callq 0x11be0
movq %rax, 0x208(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x11c10
testb $0x1, %al
jne 0x114a5
jmp 0x11566
leaq 0x210(%rsp), %rdi
callq 0x124c0
movq 0x78(%rsp), %rcx
movl 0x8(%rax), %eax
cmpl 0xdc(%rcx), %eax
jge 0x11548
leaq 0x210(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x124c0
movq 0x48(%rsp), %rdi
movq %rax, 0x50(%rsp)
callq 0x124c0
movq 0x68(%rsp), %rdi
movq 0x50(%rsp), %rsi
movl 0x8(%rax), %eax
incl %eax
movl %eax, 0x204(%rsp)
leaq 0x204(%rsp), %rdx
callq 0x124e0
movb %dl, 0x5f(%rsp)
movq %rax, 0x60(%rsp)
jmp 0x11515
movb 0x5f(%rsp), %al
movq 0x60(%rsp), %rcx
movq %rcx, 0x1e0(%rsp)
movb %al, 0x1e8(%rsp)
jmp 0x11548
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
jmp 0x11b67
jmp 0x1154a
leaq 0x210(%rsp), %rdi
xorl %esi, %esi
callq 0x12540
movq %rax, 0x1d8(%rsp)
jmp 0x11472
movq 0x220(%rsp), %rdi
callq 0x11bb0
movq %rax, 0x1d0(%rsp)
movq 0x220(%rsp), %rdi
callq 0x11be0
movq %rax, 0x1c8(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x11c10
testb $0x1, %al
jne 0x115ae
jmp 0x11b33
leaq 0x1d0(%rsp), %rdi
callq 0x124c0
movq (%rax), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0xb550
jmp 0x115cd
leaq 0x1a8(%rsp), %rax
movq %rax, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0xb640
movq %rax, 0x1a0(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0xb670
movq %rax, 0x198(%rsp)
leaq 0x1a0(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0xb6a0
testb $0x1, %al
jne 0x1163d
movl $0x8, 0x194(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0xb930
jmp 0x11b15
leaq 0x1a0(%rsp), %rdi
callq 0xb6e0
movq (%rax), %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rdi
callq 0x12580
movl %eax, 0x44(%rsp)
jmp 0x11668
movl 0x44(%rsp), %eax
cmpl 0x21c(%rsp), %eax
je 0x11774
leaq 0x1d0(%rsp), %rdi
callq 0x124c0
movq 0x78(%rsp), %rcx
movl 0x8(%rax), %eax
cmpl 0xdc(%rcx), %eax
jge 0x1176f
leaq 0x1d0(%rsp), %rdi
callq 0x124c0
movq 0x68(%rsp), %rdi
movl 0x8(%rax), %eax
incl %eax
movl %eax, 0x184(%rsp)
leaq 0x188(%rsp), %rsi
leaq 0x184(%rsp), %rdx
callq 0xb860
movb %dl, 0x37(%rsp)
movq %rax, 0x38(%rsp)
jmp 0x116d8
movb 0x37(%rsp), %al
movq 0x38(%rsp), %rcx
movq %rcx, 0x160(%rsp)
movb %al, 0x168(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x170(%rsp)
movb 0x168(%rsp), %al
movb %al, 0x178(%rsp)
testb $0x1, 0x178(%rsp)
jne 0x1176d
leaq 0x170(%rsp), %rdi
callq 0x124c0
movl 0x8(%rax), %eax
cmpl 0x184(%rsp), %eax
jle 0x1176b
movl 0x184(%rsp), %eax
movl %eax, 0x30(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x124c0
movl 0x30(%rsp), %ecx
movl %ecx, 0x8(%rax)
jmp 0x1176b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
jmp 0x11b06
jmp 0x1176d
jmp 0x1176f
jmp 0x11af2
leaq 0x1d0(%rsp), %rdi
callq 0x124c0
movq 0x68(%rsp), %rdi
movq %rax, %rdx
addq $0x8, %rdx
leaq 0x188(%rsp), %rsi
callq 0xb860
movb %dl, 0x27(%rsp)
movq %rax, 0x28(%rsp)
jmp 0x117a5
movb 0x27(%rsp), %al
movq 0x28(%rsp), %rcx
movq %rcx, 0x140(%rsp)
movb %al, 0x148(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x150(%rsp)
movb 0x148(%rsp), %al
movb %al, 0x158(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x124c0
movl 0x8(%rax), %eax
movl %eax, 0x13c(%rsp)
testb $0x1, 0x158(%rsp)
jne 0x1185d
leaq 0x150(%rsp), %rdi
callq 0x124c0
movl 0x8(%rax), %eax
movl %eax, 0x20(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x124c0
movq %rax, %rcx
movl 0x20(%rsp), %eax
cmpl 0x8(%rcx), %eax
jle 0x1185b
leaq 0x1d0(%rsp), %rdi
callq 0x124c0
movl 0x8(%rax), %eax
movl %eax, 0x1c(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x124c0
movq %rax, %rcx
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%rcx)
movl %eax, 0x13c(%rsp)
jmp 0x1185d
leaq 0xe8(%rsp), %rdi
callq 0x12590
jmp 0x1186c
leaq 0x150(%rsp), %rdi
callq 0x124c0
movq %rax, %rsi
leaq 0xe8(%rsp), %rdi
callq 0x125c0
jmp 0x1188b
jmp 0x1188d
movq 0x78(%rsp), %rdx
movl 0x13c(%rsp), %ecx
xorl %eax, %eax
cmpl 0xdc(%rdx), %ecx
movb %al, 0x1b(%rsp)
jge 0x118c4
leaq 0xe8(%rsp), %rdi
callq 0x125f0
movb %al, 0x1a(%rsp)
jmp 0x118ba
movb 0x1a(%rsp), %al
xorb $-0x1, %al
movb %al, 0x1b(%rsp)
movb 0x1b(%rsp), %al
testb $0x1, %al
jne 0x118d1
jmp 0x11ad4
leaq 0xe8(%rsp), %rdi
callq 0x12610
movq %rax, 0x10(%rsp)
jmp 0x118e5
movq 0x10(%rsp), %rax
movq %rax, 0xe0(%rsp)
movl 0x13c(%rsp), %eax
incl %eax
movl %eax, 0x13c(%rsp)
movq 0xe0(%rsp), %rax
movq (%rax), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xb550
jmp 0x1191c
leaq 0xc0(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xb640
movq %rax, 0xb8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xb670
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xb6a0
testb $0x1, %al
jne 0x119a5
movl $0xc, 0x194(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xb930
jmp 0x11ac0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
jmp 0x11ae3
leaq 0xb8(%rsp), %rdi
callq 0xb6e0
movq 0x68(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
leaq 0xa8(%rsp), %rsi
leaq 0x13c(%rsp), %rdx
callq 0xb860
movb %dl, 0x7(%rsp)
movq %rax, 0x8(%rsp)
jmp 0x119e2
movb 0x7(%rsp), %al
movq 0x8(%rsp), %rcx
movq %rcx, 0x88(%rsp)
movb %al, 0x90(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x98(%rsp)
movb 0x90(%rsp), %al
movb %al, 0xa0(%rsp)
testb $0x1, 0xa0(%rsp)
jne 0x11a7f
leaq 0x98(%rsp), %rdi
callq 0x124c0
movl 0x8(%rax), %eax
cmpl 0x13c(%rsp), %eax
jle 0x11a7d
movl 0x13c(%rsp), %eax
movl %eax, (%rsp)
leaq 0x98(%rsp), %rdi
callq 0x124c0
movl (%rsp), %ecx
movl %ecx, 0x8(%rax)
jmp 0x11a7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f8(%rsp)
movl %eax, 0x1f4(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xb930
jmp 0x11ae3
jmp 0x11a7f
movq 0x78(%rsp), %rcx
movl 0x13c(%rsp), %eax
cmpl 0xdc(%rcx), %eax
jge 0x11aac
leaq 0xe8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x125c0
jmp 0x11aaa
jmp 0x11aac
jmp 0x11aae
leaq 0xb8(%rsp), %rdi
callq 0xb910
jmp 0x11956
leaq 0xe8(%rsp), %rdi
callq 0x12620
jmp 0x11acf
jmp 0x1188d
leaq 0xe8(%rsp), %rdi
callq 0x12630
jmp 0x11af2
leaq 0xe8(%rsp), %rdi
callq 0x12630
jmp 0x11b06
jmp 0x11af4
leaq 0x1a0(%rsp), %rdi
callq 0xb910
jmp 0x11607
leaq 0x1a8(%rsp), %rdi
callq 0xb930
jmp 0x11b67
jmp 0x11b17
leaq 0x1d0(%rsp), %rdi
xorl %esi, %esi
callq 0x12540
movq %rax, 0x80(%rsp)
jmp 0x1157b
movb $0x1, 0x21b(%rsp)
movl $0x1, 0x194(%rsp)
testb $0x1, 0x21b(%rsp)
jne 0x11b5a
movq 0x68(%rsp), %rdi
callq 0x4680
movq 0x70(%rsp), %rax
addq $0x238, %rsp            # imm = 0x238
retq
movq 0x68(%rsp), %rdi
callq 0x4680
movq 0x1f8(%rsp), %rdi
callq 0x2300
nop