// File: ashlall[P]StreamStats/ChiSquareExperiments/./../SourceCode/ChiSquare/../Quantiles/Q-Digest.cpp
// Function: QDigest::rebuildToCapacity(long)
void QDigest::rebuildToCapacity(long newCapacity) 
{
  std::unordered_map<long, long> newNode2count;
  long scaleR = newCapacity / capacity - 1;  
  long scaleL = 1;
  std::vector<long> keys;
  keys.reserve(node2count.size());
	
  for (std::unordered_map<long, long>::const_iterator i = node2count.begin(); i != node2count.end(); i++)
    keys.push_back(i->first);
	
  std::sort(keys.begin(), keys.end());
    
  // adjusts and adds the values from the old tree into the new tree
  for (std::vector<long>::const_iterator i = keys.begin(); i != keys.end(); i++)
  {
    while (scaleL <= *i / 2)
      scaleL <<= 1;
    newNode2count.insert(std::make_pair (*i + scaleL * scaleR, get(*i))); 
   }
  node2count = newNode2count;
  capacity = newCapacity;
  compressFully();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x8a00
movq -0x110(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x48(%rcx), %rcx
cqto
idivq %rcx
decq %rax
movq %rax, -0x50(%rbp)
movq $0x1, -0x58(%rbp)
leaq -0x70(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x8d40
movq -0x110(%rbp), %rdi
addq $0x8, %rdi
callq 0x8d20
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x8d60
jmp 0x3fcb
movq -0x110(%rbp), %rdi
addq $0x8, %rdi
callq 0x8e60
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x8e90
movq -0x110(%rbp), %rdi
addq $0x8, %rdi
callq 0x8c50
movq %rax, -0x98(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x8ec0
testb $0x1, %al
jne 0x4025
jmp 0x407b
leaq -0x88(%rbp), %rdi
callq 0x8f60
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x8ef0
jmp 0x403f
jmp 0x4041
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0x8f90
movq %rax, -0xa0(%rbp)
jmp 0x3ff5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x91a0
leaq -0x48(%rbp), %rdi
callq 0x8a20
jmp 0x4282
leaq -0x70(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x9000
movq -0x118(%rbp), %rdi
movq %rax, -0xa8(%rbp)
callq 0x9030
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x8fc0
jmp 0x40ba
leaq -0x70(%rbp), %rdi
callq 0x9000
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x9060
leaq -0x70(%rbp), %rdi
callq 0x9030
movq %rax, -0xc8(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x90a0
testb $0x1, %al
jne 0x4109
jmp 0x423b
jmp 0x410b
movq -0x58(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x90e0
movq (%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x120(%rbp), %rax
cmpq %rcx, %rax
jg 0x414b
movq -0x58(%rbp), %rax
shlq %rax
movq %rax, -0x58(%rbp)
jmp 0x410b
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x90e0
movq -0x130(%rbp), %rdi
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq -0x50(%rbp), %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
callq 0x90e0
movq -0x110(%rbp), %rdi
movq (%rax), %rsi
callq 0x42e0
movq %rax, -0x128(%rbp)
jmp 0x419b
movq -0x128(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x9100
movq %rdx, -0x140(%rbp)
movq %rax, -0x138(%rbp)
jmp 0x41cc
movq -0x140(%rbp), %rax
movq -0x138(%rbp), %rcx
movq %rcx, -0xd8(%rbp)
movq %rax, -0xd0(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x8c80
movb %dl, -0x149(%rbp)
movq %rax, -0x148(%rbp)
jmp 0x4207
movb -0x149(%rbp), %al
movq -0x148(%rbp), %rcx
movq %rcx, -0xf8(%rbp)
movb %al, -0xf0(%rbp)
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
callq 0x9130
movq %rax, -0x100(%rbp)
jmp 0x40dd
movq -0x110(%rbp), %rdi
addq $0x8, %rdi
leaq -0x48(%rbp), %rsi
callq 0x9170
jmp 0x4251
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, 0x48(%rdi)
callq 0x4590
jmp 0x4267
leaq -0x70(%rbp), %rdi
callq 0x91a0
leaq -0x48(%rbp), %rdi
callq 0x8a20
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x2320
nopl (%rax,%rax)