// File: zvrba[P]nonogram/Solver.cpp
// Function: nonogram::ColumnAgent::setCell(unsigned long)
bool ColumnAgent::setCell(size_t pos)
{
  using std::get;
  
  if (pos >= _lineSize)
    throw std::logic_error("ColumnAgent::setCell: invalid pos");
  if (_blocks.size() > _description.size())
    throw std::logic_error("Agent::setCell: too many blocks");

  // Determine whether to extend existing block or create a new one.
  if (!_blocks.empty())
  {
    auto& lastBlock = _blocks.back();

    const auto lastBlockIndex = _blocks.size()-1;
    const auto blockBegin = get<0>(lastBlock);
    const auto blockEnd = get<1>(lastBlock);

    if (blockEnd - blockBegin > _description[lastBlockIndex])
      throw std::logic_error("Agent::setCell: too long block");
    if (pos < blockEnd-1)
      throw std::logic_error("Agent::setCell: going backwards");

    // Extending last block if possible.
    if (pos == blockEnd) {
      if (blockEnd - blockBegin == _description[lastBlockIndex])
        return false;
      ++get<1>(lastBlock);
      return true;
    }
    // Not extending last block, proceed to create new.
  }
  
  // New block.
  if (_blocks.size() == _description.size())
    return false;
  _blocks.push_back(Block(pos, pos+1));
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x68(%rbp)
movq -0x18(%rbp), %rax
cmpq 0x10(%rcx), %rax
jb 0x31308
movl $0x10, %edi
callq 0x30e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
leaq 0x7134(%rip), %rsi      # 0x38404
callq 0x3260
jmp 0x312d7
movq -0x70(%rbp), %rdi
movq 0x27cee(%rip), %rsi     # 0x58fd0
movq 0x27ccf(%rip), %rdx     # 0x58fb8
callq 0x32e0
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x3160
jmp 0x315c6
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x2fe90
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x78(%rbp)
movq 0x8(%rax), %rdi
callq 0x8970
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jbe 0x31388
movl $0x10, %edi
callq 0x30e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
leaq 0x70d6(%rip), %rsi      # 0x38426
callq 0x3260
jmp 0x31357
movq -0x80(%rbp), %rdi
movq 0x27c6e(%rip), %rsi     # 0x58fd0
movq 0x27c4f(%rip), %rdx     # 0x58fb8
callq 0x32e0
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x3160
jmp 0x315c6
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x31eb0
testb $0x1, %al
jne 0x3153f
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x31ef0
movq -0x68(%rbp), %rdi
movq %rax, -0x30(%rbp)
addq $0x18, %rdi
callq 0x2fe90
subq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x31f30
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
callq 0x31f50
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, -0x48(%rbp)
movq -0x48(%rbp), %rcx
subq -0x40(%rbp), %rcx
movq %rcx, -0x88(%rbp)
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x8a40
movq %rax, %rcx
movq -0x88(%rbp), %rax
movl (%rcx), %ecx
cmpq %rcx, %rax
jbe 0x31473
movl $0x10, %edi
callq 0x30e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
leaq 0x7011(%rip), %rsi      # 0x38446
callq 0x3260
jmp 0x3143c
movq -0x90(%rbp), %rdi
movq 0x27b86(%rip), %rsi     # 0x58fd0
movq 0x27b67(%rip), %rdx     # 0x58fb8
callq 0x32e0
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x3160
jmp 0x315c6
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x314e0
movl $0x10, %edi
callq 0x30e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
leaq 0x6fc3(%rip), %rsi      # 0x38465
callq 0x3260
jmp 0x314a9
movq -0x98(%rbp), %rdi
movq 0x27b19(%rip), %rsi     # 0x58fd0
movq 0x27afa(%rip), %rdx     # 0x58fb8
callq 0x32e0
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x3160
jmp 0x315c6
movq -0x18(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x3153d
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
subq -0x40(%rbp), %rcx
movq %rcx, -0xa0(%rbp)
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x8a40
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movl (%rcx), %ecx
cmpq %rcx, %rax
jne 0x31524
movb $0x0, -0x1(%rbp)
jmp 0x315b8
movq -0x30(%rbp), %rdi
callq 0x31f50
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x315b8
jmp 0x3153f
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x2fe90
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0xa8(%rbp)
movq 0x8(%rax), %rdi
callq 0x8970
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
jne 0x31578
movb $0x0, -0x1(%rbp)
jmp 0x315b8
movq -0x68(%rbp), %rax
addq $0x18, %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x18(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x31fa0
movq -0xb0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x31f70
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x32f0
nop