// File: /workspace/github_cpp_makefile_star2_O3/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 0x5dca
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 0x527f(%rip), %xmm0     # 0xb020
xorpd %xmm1, %xmm0
ucomisd %xmm1, %xmm0
jae 0x5dce
leaq 0x551c(%rip), %rdi      # 0xb2ce
leaq 0x535e(%rip), %rsi      # 0xb117
leaq 0x551f(%rip), %rcx      # 0xb2df
movl $0x29f, %edx            # imm = 0x29F
callq 0x3150
xorpd %xmm0, %xmm0
movl 0x4(%rbx), %edx
testq %rdx, %rdx
je 0x5e1c
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 0x5dec
cmpq %r14, %rcx
je 0x5e33
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 0x4e52
movq 0x110(%rbx), %rax
cmpq %rax, %r14
je 0x5e8e
movq 0xc8(%rbx), %rcx
movq 0xf8(%rbx), %rdx
movsd 0x5253(%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 0x5e96
movl %r8d, (%rdx,%rdi,4)
movq 0x8(%rsi), %r14
cmpq %rax, %r14
jne 0x5e65
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x545b(%rip), %rdi      # 0xb2f8
leaq 0x5273(%rip), %rsi      # 0xb117
leaq 0x5434(%rip), %rcx      # 0xb2df
movl $0x2bb, %edx            # imm = 0x2BB
callq 0x3150
nop