// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
pushq $0x70
popq %rdi
callq 0x2240
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x32b6
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x265c
leaq 0x10(%rsp), %rdi
callq 0x2b4c
leaq 0x8(%rsp), %r15
movq %r14, (%r15)
leaq 0x18(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3304
incl 0xc(%rbx)
movl (%rbx), %eax
movq (%r15), %rcx
movl 0x18(%rcx), %ecx
testl %eax, %eax
je 0x2f68
movq %r14, %r15
cmpl %ecx, %eax
je 0x2f8b
movq 0x607b(%rip), %rdi      # 0x8fc8
leaq 0x20e4(%rip), %rsi      # 0x5038
callq 0x2230
movq %rax, %rdi
callq 0x2100
xorl %ebp, %ebp
jmp 0x304a
movl %ecx, (%rbx)
movq %r14, %r15
jmp 0x2f8b
movq 0x10(%r15), %rdi
movq 0x8(%rsp), %rsi
callq 0x2750
movq 0x8(%rsp), %rdi
movq 0x10(%r15), %rsi
callq 0x26d2
movq (%r15), %r15
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x330f
cmpq %rax, %r15
jne 0x2f6f
cmpl $0x0, 0x8(%rbx)
je 0x2fe9
movq 0x8(%rsp), %rdi
callq 0x27a2
movq 0x8(%rsp), %rax
movsd 0x68(%rax), %xmm0
ucomisd 0x10(%rbx), %xmm0
jbe 0x2fdc
movq 0x6002(%rip), %rdi      # 0x8fc8
leaq 0x20a1(%rip), %rsi      # 0x506e
callq 0x2230
movq 0x8(%rsp), %rdi
callq 0x295a
movq %rbx, %rdi
callq 0x307c
movb $0x1, %bpl
jmp 0x304a
movl 0x4(%rbx), %eax
incl %eax
movb $0x1, %bpl
cmpl %eax, 0xc(%rbx)
jne 0x304a
movq %rbx, %rdi
callq 0x30e6
movq %rbx, %rdi
movl %eax, %esi
callq 0x3156
movq %rbx, %rdi
callq 0x31d2
movsd %xmm0, 0x10(%rbx)
movq 0x8(%rsp), %rax
movsd 0x68(%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3042
movq 0x5f9c(%rip), %rdi      # 0x8fc8
leaq 0x203b(%rip), %rsi      # 0x506e
callq 0x2230
movq 0x8(%rsp), %rdi
callq 0x295a
movq %rbx, %rdi
callq 0x307c
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2b4c
jmp 0x3069
movq %rax, %rbx
pushq $0x70
popq %rsi
movq %r14, %rdi
callq 0x2250
movq %rbx, %rdi
callq 0x23f0