// File: /workspace/github_cpp_makefile_star2_O2/iu-vail[P]iprimal/iPrimal.cpp
// Function: Primal::SwapTasks()
void
Primal::SwapTasks(void){

  double augment_delta = 0;
  cell c_first = swap_loop.back();
  if(!swap_loop.empty()){
    // the first cell in the loop is special
    // only the column corresponding the first cell need to be adjusted

    // get the first cell, which was stored reversely
    augment_delta = - (oprt_matrix[c_first.row_idx][c_first.col_idx] + deltas[c_first.col_idx] - deltas[GetAssignedCol(c_first.row_idx)] );
    assert(augment_delta>=0);
    _cout("Augment delta: "<<augment_delta<<endl);
  }

  _cout("Before swaps:"<<endl);
  DisplayMatrix(oprt_matrix);

  // update dual variables
  for(uint i=0; i<col_size; i++){
    dual_col_vars[i] -= deltas[i];
    dual_row_vars[i] += deltas[GetAssignedCol(i)];
  }
  if(!swap_loop.empty()){
    // also specially process the starting col, to augment the sum
    dual_col_vars[c_first.col_idx] -= augment_delta;
  }

  _cout("Update dual cols: ");
  DisplayVec<double>(dual_col_vars);
  _cout("Update dual rows: ");
  DisplayVec<double>(dual_row_vars);

  UpdateReducedCostMatrix(dual_row_vars, dual_col_vars);

  // swap the tasks in the assignment vector
  // only need the odd-numbered cell from the loop
  for(list<cell>::reverse_iterator itr=swap_loop.rbegin(); itr!=swap_loop.rend(); itr++){
    cell c_odd = *itr++;
    assert(oprt_matrix[c_odd.row_idx][c_odd.col_idx] < DOUBLE_EPSILON);
    assignment[c_odd.row_idx] = c_odd.col_idx;
  } 

  _cout("After swaps:"<<endl);
  DisplayMatrix(oprt_matrix);
  _cout(endl);

}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x110(%rdi), %r14
movq 0x110(%rdi), %rcx
movq 0x118(%rdi), %rdx
movslq 0x14(%rdx), %rax
cmpq %r14, %rcx
je 0x4661
movl 0x10(%rdx), %edx
movslq %edx, %rsi
movq 0x38(%rbx), %rdi
movq 0xc8(%rbx), %r8
imulq $0x18, %rsi, %rsi
movq (%r8,%rsi), %rsi
movsd (%rsi,%rax,8), %xmm1
addsd (%rdi,%rax,8), %xmm1
movq 0xf8(%rbx), %r8
movl (%r8,%rdx,4), %edx
subsd (%rdi,%rdx,8), %xmm1
movapd 0x49e8(%rip), %xmm0     # 0x9020
xorpd %xmm1, %xmm0
ucomisd %xmm1, %xmm0
jae 0x4670
leaq 0x4c65(%rip), %rdi      # 0x92ae
leaq 0x4aa7(%rip), %rsi      # 0x90f7
leaq 0x4c68(%rip), %rcx      # 0x92bf
movl $0x29f, %edx            # imm = 0x29F
callq 0x2150
movq 0x38(%rbx), %rdi
movq 0xf8(%rbx), %r8
xorpd %xmm0, %xmm0
movl 0x4(%rbx), %r10d
leaq 0x20(%rbx), %rdx
movq 0x20(%rbx), %r9
leaq 0x8(%rbx), %rsi
movq 0x8(%rbx), %r11
xorl %r15d, %r15d
cmpq %r10, %r15
jae 0x46b9
movsd (%r9,%r15,8), %xmm1
subsd (%rdi,%r15,8), %xmm1
movsd %xmm1, (%r9,%r15,8)
movl (%r8,%r15,4), %r12d
movsd (%rdi,%r12,8), %xmm1
addsd (%r11,%r15,8), %xmm1
movsd %xmm1, (%r11,%r15,8)
incq %r15
jmp 0x4687
cmpq %r14, %rcx
je 0x46ce
movsd (%r9,%rax,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%r9,%rax,8)
movq %rbx, %rdi
callq 0x3b06
movq 0x110(%rbx), %rax
movq 0xc8(%rbx), %rcx
movq 0xf8(%rbx), %rdx
movsd 0x49c5(%rip), %xmm0     # 0x90b8
cmpq %rax, %r14
je 0x471e
movq 0x8(%r14), %rsi
movslq 0x10(%rsi), %rdi
movslq 0x14(%rsi), %r8
imulq $0x18, %rdi, %r9
movq (%rcx,%r9), %r9
ucomisd (%r9,%r8,8), %xmm0
jbe 0x472a
movl %r8d, (%rdx,%rdi,4)
movq 0x8(%rsi), %r14
jmp 0x46f3
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x4ba7(%rip), %rdi      # 0x92d8
leaq 0x49bf(%rip), %rsi      # 0x90f7
leaq 0x4b80(%rip), %rcx      # 0x92bf
movl $0x2bb, %edx            # imm = 0x2BB
callq 0x2150
nop