// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
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 0x5de0
movl 0x10(%rdx), %edx
movslq %edx, %rsi
movq 0x38(%rbx), %rdi
movq 0xc8(%rbx), %r8
leaq (%rsi,%rsi,2), %rsi
movq (%r8,%rsi,8), %rsi
movsd (%rsi,%rax,8), %xmm1
addsd (%rdi,%rax,8), %xmm1
movq 0xf8(%rbx), %rsi
movl (%rsi,%rdx,4), %edx
subsd (%rdi,%rdx,8), %xmm1
movapd 0x5269(%rip), %xmm0     # 0xb020
xorpd %xmm1, %xmm0
ucomisd %xmm1, %xmm0
jae 0x5de4
leaq 0x54e6(%rip), %rdi      # 0xb2ae
leaq 0x5328(%rip), %rsi      # 0xb0f7
leaq 0x54e9(%rip), %rcx      # 0xb2bf
movl $0x29f, %edx            # imm = 0x29F
callq 0x3150
xorpd %xmm0, %xmm0
movl 0x4(%rbx), %edx
testq %rdx, %rdx
je 0x5e32
movq 0x38(%rbx), %rsi
movq 0x8(%rbx), %rdi
movq 0x20(%rbx), %r8
movq 0xf8(%rbx), %r9
xorl %r10d, %r10d
movsd (%r8,%r10,8), %xmm1
subsd (%rsi,%r10,8), %xmm1
movsd %xmm1, (%r8,%r10,8)
movl (%r9,%r10,4), %r11d
movsd (%rsi,%r11,8), %xmm1
addsd (%rdi,%r10,8), %xmm1
movsd %xmm1, (%rdi,%r10,8)
incq %r10
cmpq %r10, %rdx
jne 0x5e02
cmpq %r14, %rcx
je 0x5e49
movq 0x20(%rbx), %rcx
movsd (%rcx,%rax,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%rax,8)
leaq 0x20(%rbx), %rdx
leaq 0x8(%rbx), %rsi
movq %rbx, %rdi
callq 0x4e34
movq 0x110(%rbx), %rax
cmpq %rax, %r14
je 0x5ea4
movq 0xc8(%rbx), %rcx
movq 0xf8(%rbx), %rdx
movsd 0x523d(%rip), %xmm0     # 0xb0b8
movq 0x8(%r14), %rsi
movslq 0x10(%rsi), %rdi
movslq 0x14(%rsi), %r8
leaq (%rdi,%rdi,2), %r9
movq (%rcx,%r9,8), %r9
ucomisd (%r9,%r8,8), %xmm0
jbe 0x5eac
movl %r8d, (%rdx,%rdi,4)
movq 0x8(%rsi), %r14
cmpq %rax, %r14
jne 0x5e7b
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x5425(%rip), %rdi      # 0xb2d8
leaq 0x523d(%rip), %rsi      # 0xb0f7
leaq 0x53fe(%rip), %rcx      # 0xb2bf
movl $0x2bb, %edx            # imm = 0x2BB
callq 0x3150
nop