// File: /workspace/github_cpp_makefile_star2_O3/ChristalC[P]Online-Outlier-Detection/lof.cpp
// Function: Lof::addPoint(std::vector<double, std::allocator<double>>)
bool Lof::addPoint(vector<double> new_pos) {
  Point* new_point = new Point(new_pos);
  all_points.push_back(new_point);
  point_count++;
  // If lof_data.dimension is not fixed (== 0), set it to dimension of new_point.
  if (dimension == 0) {
    dimension = new_point->getDimension();
  }
  // If lof_data.dimension is fixed, compare with new_point dimension to confirm consistency
  else if (dimension != new_point->getDimension()) {
    cout << "New point dimension is inconsistent. End the program!" << endl;
    return false;
  }
  auto it = all_points.begin();
  for (; it != prev(all_points.end()); it++) {
    (*it)->addNewNeighbor(new_point);
    new_point->insertNeighbor(*it);
  }
  bool is_outlier = false;
  // If group_size is set (!= 0), update stats of the new point
  // If group_size is fixed, which means decision is needed on if the new_point is an outlier
  if (group_size != 0) {
    new_point->updateStats();
    double lof_val = new_point->getLofVal();
    if (lof_val > threshold) {
      cout << "Found an outlier: ";
      new_point->displayPoint();
    }
    removeFrontPoint();
  }
  // If the group_size is not fixed, count if the new_point is outside the first window
  // If it is, then we need to decide group_size and threshold at this point
  // If not, no action or decision needed
  else {  
    if (getPointCount() == window_size + 1) {
      int best_group_size = decideGroupSize();
      changeGroupSize(best_group_size);

      double th = decideThreshold();
      setThreshold(th);

      // Decide if the newly added point is an outlier based on group_size and threshold
      double lof_val = new_point->getLofVal();
      if (lof_val > threshold) {
        cout << "Found an outlier: ";
        new_point->displayPoint();
      }
      removeFrontPoint();
    }
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %r14
movl $0x70, %edi
callq 0x3270
movq %rax, %rbx
movq %rsp, %rdi
movq %r15, %rsi
callq 0x44ae
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x371c
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4069
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x18(%r14), %r15
movl $0x18, %edi
callq 0x3270
movq %rbx, 0x10(%rax)
movq %rax, %rdi
movq %r15, %rsi
callq 0x3060
incq 0x28(%r14)
incl 0xc(%r14)
movl (%r14), %eax
movl 0x18(%rbx), %ecx
testl %eax, %eax
je 0x40e5
cmpl %ecx, %eax
je 0x40e8
movq 0x5f2d(%rip), %rbx      # 0x9fd0
leaq 0x2f9e(%rip), %rsi      # 0x7048
movl $0x35, %edx
movq %rbx, %rdi
callq 0x32b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
xorl %ebp, %ebp
jmp 0x41ba
movl %ecx, (%r14)
movq 0x18(%r14), %r15
cmpq 0x20(%r14), %r15
je 0x410f
movq 0x10(%r15), %rdi
movq %rbx, %rsi
callq 0x385a
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x37a6
movq (%r15), %r15
jmp 0x40ec
cmpl $0x0, 0x8(%r14)
je 0x4158
movq %rbx, %rdi
callq 0x38e6
movsd 0x68(%rbx), %xmm0
ucomisd 0x10(%r14), %xmm0
jbe 0x414b
movq 0x5e9e(%rip), %rdi      # 0x9fd0
leaq 0x2f45(%rip), %rsi      # 0x707e
movl $0x12, %edx
callq 0x32b0
movq %rbx, %rdi
callq 0x3b8e
movq %r14, %rdi
callq 0x41fa
movb $0x1, %bpl
jmp 0x41ba
movl 0x4(%r14), %eax
incl %eax
movb $0x1, %bpl
cmpl %eax, 0xc(%r14)
jne 0x41ba
movq %r14, %rdi
callq 0x4288
movq %r14, %rdi
movl %eax, %esi
callq 0x42f8
movq %r14, %rdi
callq 0x4378
movsd %xmm0, 0x10(%r14)
movsd 0x68(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x41b2
movq 0x5e37(%rip), %rdi      # 0x9fd0
leaq 0x2ede(%rip), %rsi      # 0x707e
movl $0x12, %edx
callq 0x32b0
movq %rbx, %rdi
callq 0x3b8e
movq %r14, %rdi
callq 0x41fa
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x41e5
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
jmp 0x41e5
movq %rax, %r14
movl $0x70, %esi
movq %rbx, %rdi
callq 0x3280
movq %r14, %rdi
callq 0x3460