// File: hanhwi[P]SimPoint/analysiscode/Simpoint.cpp
// Function: Simpoint::sampleDataset()
void Simpoint::sampleDataset() {
    Utilities::check(sampledDataset == NULL, 
            "Simpoint::sampleDataset() sampledDataset is not NULL");

    if ((options.sampleSize > 0) && 
            ((unsigned int)options.sampleSize < wholeDataset->numRows())) {
        // choose enough samples to satisfy the number of desired samples
        Random rand(options.randSeedSample);

        vector<pair<double, int> > sortedWeights(wholeDataset->numRows());
        for (unsigned int i = 0; i < wholeDataset->numRows(); i++) {
            sortedWeights[i] = pair<double, int>(wholeDataset->getWeight(i), i);
        }
        sort(sortedWeights.begin(), sortedWeights.end(), greater<pair<double, int> >());

        for (unsigned int i = 1; i < wholeDataset->numRows(); i++) {
            sortedWeights[i].first += sortedWeights[i-1].first;
        }
        sortedWeights[wholeDataset->numRows() - 1].first = 1.0; // just to be sure

        set<int> samples;
        double sampledPct = 0.0;
        while (samples.size() < (unsigned int)options.sampleSize) {
            double r = (double)rand.randFloat();

            // binary search for the sample corresponding to the generated
            // random number
            unsigned int lower = 0, upper = sortedWeights.size();
            unsigned int sample = (upper + lower) / 2;
            while (true) {
                bool below = (sample > 0) ? (sortedWeights[sample - 1].first <= r) : true;
                bool above = (sample < sortedWeights.size()) ? 
                             (sortedWeights[sample].first >= r) : true;
                if (above && below) { break; }
                if (below) { lower = sample; }
                if (above) { upper = sample; }
                sample = (upper + lower) / 2;
            }

            if (samples.find(sortedWeights[sample].second) == samples.end()) {
                sampledPct += wholeDataset->getWeight(sortedWeights[sample].second);
                samples.insert(sortedWeights[sample].second);
            }
        }

        unsigned int sampleSize = samples.size();
        Logger::log() << "  Creating a random sample of size " << sampleSize 
            << " vectors for clustering\n";
        Logger::log() << "    which represents " << (sampledPct * 100) 
            << "% of the weights\n";
        sampledDataset = new Dataset(sampleSize, wholeDataset->numCols());

        // copy the sampled data into the working data structure and reweight it 
        int j = 0;
        for (set<int>::iterator i = samples.begin(); i != samples.end(); i++) {
            for (unsigned int col = 0; col < wholeDataset->numCols(); col++) {
                (*sampledDataset)[j][col] = (*wholeDataset)[*i][col];
                sampledDataset->setWeight(j, wholeDataset->getWeight(*i) / sampledPct);
            }
            j++;
        }
    } else {
        sampledDataset = wholeDataset;
    }
}

// Assembly:
subq $0x368, %rsp            # imm = 0x368
movq %rdi, 0x360(%rsp)
movq 0x360(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x8(%rax), %rax
testq %rax, %rax
sete %al
movb %al, 0x11f(%rsp)
leaq 0x33f(%rsp), %rdi
movq %rdi, 0x120(%rsp)
callq 0x56a0
movq 0x120(%rsp), %rdx
leaq 0x1ea5c(%rip), %rsi     # 0x338b0
leaq 0x340(%rsp), %rdi
callq 0x74e0
jmp 0x14e63
movb 0x11f(%rsp), %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x340(%rsp), %rsi
callq 0x7480
jmp 0x14e7f
leaq 0x340(%rsp), %rdi
callq 0x5788
leaq 0x33f(%rsp), %rdi
callq 0x53e0
movq 0x110(%rsp), %rax
cmpl $0x0, 0xbc(%rax)
jle 0x15848
movq 0x110(%rsp), %rax
movl 0xbc(%rax), %ecx
movl %ecx, 0x10c(%rsp)
movq (%rax), %rdi
callq 0xc0c0
movl %eax, %ecx
movl 0x10c(%rsp), %eax
cmpl %ecx, %eax
jae 0x15848
movq 0x110(%rsp), %rax
movslq 0xb0(%rax), %rsi
leaq 0x210(%rsp), %rdi
callq 0x10cb0
movq 0x110(%rsp), %rax
movq (%rax), %rdi
callq 0xc0c0
movl %eax, %eax
movq %rax, 0xf8(%rsp)
leaq 0x1f7(%rsp), %rdi
movq %rdi, 0x100(%rsp)
callq 0x1a340
movq 0xf8(%rsp), %rsi
movq 0x100(%rsp), %rdx
leaq 0x1f8(%rsp), %rdi
callq 0x1a350
jmp 0x14f46
leaq 0x1f7(%rsp), %rdi
callq 0x1a3d0
movl $0x0, 0x1f0(%rsp)
movq 0x110(%rsp), %rax
movl 0x1f0(%rsp), %ecx
movl %ecx, 0xf0(%rsp)
movq (%rax), %rdi
callq 0xc0c0
movl %eax, 0xf4(%rsp)
jmp 0x14f85
movl 0xf0(%rsp), %eax
movl 0xf4(%rsp), %ecx
cmpl %ecx, %eax
jae 0x150b2
movq 0x110(%rsp), %rax
movq (%rax), %rdi
movl 0x1f0(%rsp), %esi
callq 0x10d30
movsd %xmm0, 0xe8(%rsp)
jmp 0x14fbd
movsd 0xe8(%rsp), %xmm0
movsd %xmm0, 0x1d8(%rsp)
leaq 0x1e0(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
leaq 0x1f0(%rsp), %rdx
callq 0x1a3e0
jmp 0x14fee
movl 0x1f0(%rsp), %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movq %rax, %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x1a430
movl 0x1f0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1f0(%rsp)
jmp 0x14f5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0x15061
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
leaq 0x340(%rsp), %rdi
callq 0x5788
leaq 0x33f(%rsp), %rdi
callq 0x53e0
jmp 0x1585f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
leaq 0x1f7(%rsp), %rdi
callq 0x1a3d0
jmp 0x1585f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0x15839
leaq 0x1f8(%rsp), %rdi
movq %rdi, 0xe0(%rsp)
callq 0x1a4a0
movq 0xe0(%rsp), %rdi
movq %rax, 0x1d0(%rsp)
callq 0x1a4d0
movq %rax, 0x1c8(%rsp)
movq 0x1d0(%rsp), %rdi
movq 0x1c8(%rsp), %rsi
callq 0x1a460
jmp 0x150fb
movl $0x1, 0x1c0(%rsp)
movq 0x110(%rsp), %rax
movl 0x1c0(%rsp), %ecx
movl %ecx, 0xd8(%rsp)
movq (%rax), %rdi
callq 0xc0c0
movl %eax, 0xdc(%rsp)
jmp 0x1512d
movl 0xd8(%rsp), %eax
movl 0xdc(%rsp), %ecx
cmpl %ecx, %eax
jae 0x151a4
movl 0x1c0(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movsd (%rax), %xmm0
movsd %xmm0, 0xd0(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movsd 0xd0(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
jmp 0x15106
movq 0x110(%rsp), %rax
movq (%rax), %rdi
callq 0xc0c0
movl %eax, 0xcc(%rsp)
jmp 0x151bd
movl 0xcc(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movsd 0x1e070(%rip), %xmm0    # 0x33250
movsd %xmm0, (%rax)
leaq 0x190(%rsp), %rdi
callq 0x1a500
xorps %xmm0, %xmm0
movsd %xmm0, 0x188(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x1a510
movq 0x110(%rsp), %rcx
movl 0xbc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1550d
leaq 0x210(%rsp), %rdi
callq 0x304a0
movss %xmm0, 0xc8(%rsp)
jmp 0x15239
movss 0xc8(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x180(%rsp)
movl $0x0, 0x17c(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x1a520
movl %eax, 0x178(%rsp)
movl 0x178(%rsp), %eax
addl 0x17c(%rsp), %eax
shrl %eax
movl %eax, 0x174(%rsp)
jmp 0x15287
cmpl $0x0, 0x174(%rsp)
jbe 0x152c9
movl 0x174(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movsd (%rax), %xmm1
movsd 0x180(%rsp), %xmm0
ucomisd %xmm1, %xmm0
setae %al
movb %al, 0xc7(%rsp)
jmp 0x152d4
movb $0x1, %al
movb %al, 0xc7(%rsp)
jmp 0x152d4
movb 0xc7(%rsp), %al
andb $0x1, %al
movb %al, 0x173(%rsp)
movl 0x174(%rsp), %eax
movq %rax, 0xb8(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x1a520
movq %rax, %rcx
movq 0xb8(%rsp), %rax
cmpq %rcx, %rax
jae 0x1533f
movl 0x174(%rsp), %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movsd (%rax), %xmm0
ucomisd 0x180(%rsp), %xmm0
setae %al
movb %al, 0xb7(%rsp)
jmp 0x1534a
movb $0x1, %al
movb %al, 0xb7(%rsp)
jmp 0x1534a
movb 0xb7(%rsp), %al
andb $0x1, %al
movb %al, 0x172(%rsp)
testb $0x1, 0x172(%rsp)
je 0x15389
testb $0x1, 0x173(%rsp)
je 0x15389
jmp 0x153d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0x1582c
testb $0x1, 0x173(%rsp)
je 0x153a1
movl 0x174(%rsp), %eax
movl %eax, 0x17c(%rsp)
testb $0x1, 0x172(%rsp)
je 0x153b9
movl 0x174(%rsp), %eax
movl %eax, 0x178(%rsp)
movl 0x178(%rsp), %eax
addl 0x17c(%rsp), %eax
shrl %eax
movl %eax, 0x174(%rsp)
jmp 0x15285
movl 0x174(%rsp), %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movq %rax, %rsi
addq $0x8, %rsi
leaq 0x190(%rsp), %rdi
callq 0x1a560
movq %rax, 0xa8(%rsp)
jmp 0x15409
movq 0xa8(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x1a5a0
movq %rax, 0x160(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x1a540
testb $0x1, %al
jne 0x1544c
jmp 0x15508
movq 0x110(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movl 0x174(%rsp), %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movq 0x98(%rsp), %rdi
movl 0x8(%rax), %esi
callq 0x10d30
movsd %xmm0, 0xa0(%rsp)
jmp 0x15490
movsd 0xa0(%rsp), %xmm1
movsd 0x188(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x188(%rsp)
movl 0x174(%rsp), %eax
movl %eax, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x1a410
movq %rax, %rsi
addq $0x8, %rsi
leaq 0x190(%rsp), %rdi
callq 0x1a5d0
movb %dl, 0x8f(%rsp)
movq %rax, 0x90(%rsp)
jmp 0x154ea
movb 0x8f(%rsp), %al
movq 0x90(%rsp), %rcx
movq %rcx, 0x150(%rsp)
movb %al, 0x158(%rsp)
jmp 0x151fd
leaq 0x190(%rsp), %rdi
callq 0x1a510
movl %eax, 0x14c(%rsp)
xorl %edi, %edi
callq 0x7ba0
movq %rax, 0x80(%rsp)
jmp 0x15532
movq 0x80(%rsp), %rdi
leaq 0x1e3a4(%rip), %rsi     # 0x338e5
callq 0x5360
movq %rax, 0x78(%rsp)
jmp 0x1554d
movq 0x78(%rsp), %rdi
movl 0x14c(%rsp), %esi
callq 0x5520
movq %rax, 0x70(%rsp)
jmp 0x15565
movq 0x70(%rsp), %rdi
leaq 0x1e398(%rip), %rsi     # 0x33909
callq 0x5360
jmp 0x15578
xorl %edi, %edi
callq 0x7ba0
movq %rax, 0x68(%rsp)
jmp 0x15586
movq 0x68(%rsp), %rdi
leaq 0x1e390(%rip), %rsi     # 0x33922
callq 0x5360
movq %rax, 0x60(%rsp)
jmp 0x1559e
movq 0x60(%rsp), %rdi
movsd 0x188(%rsp), %xmm0
movsd 0x1dde4(%rip), %xmm1    # 0x33398
mulsd %xmm1, %xmm0
callq 0x5700
movq %rax, 0x58(%rsp)
jmp 0x155c4
movq 0x58(%rsp), %rdi
leaq 0x1e368(%rip), %rsi     # 0x33938
callq 0x5360
jmp 0x155d7
movl $0x30, %edi
callq 0x5380
movq %rax, 0x50(%rsp)
jmp 0x155e8
movq 0x110(%rsp), %rax
movl 0x14c(%rsp), %ecx
movl %ecx, 0x48(%rsp)
movq (%rax), %rdi
callq 0xc0d0
movl %eax, 0x4c(%rsp)
jmp 0x15609
movl 0x4c(%rsp), %edx
movl 0x48(%rsp), %esi
movq 0x50(%rsp), %rdi
callq 0xaf10
jmp 0x1561d
movq 0x110(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, 0x148(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x1a630
movq %rax, 0x140(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x1a5a0
movq %rax, 0x138(%rsp)
leaq 0x140(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x1a660
testb $0x1, %al
jne 0x15681
jmp 0x15810
movl $0x0, 0x134(%rsp)
movq 0x110(%rsp), %rax
movl 0x134(%rsp), %ecx
movl %ecx, 0x40(%rsp)
movq (%rax), %rdi
callq 0xc0d0
movl %eax, 0x44(%rsp)
jmp 0x156ad
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %ecx
cmpl %ecx, %eax
jae 0x157e3
movq 0x110(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x140(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x1a680
movq 0x8(%rsp), %rdi
movslq (%rax), %rsi
callq 0xc130
movq %rax, %rdi
movl 0x134(%rsp), %eax
movl %eax, %esi
callq 0x9ba0
movq %rax, %rcx
movq 0x110(%rsp), %rax
movsd (%rcx), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x8(%rax), %rdi
movslq 0x148(%rsp), %rsi
callq 0xc130
movq %rax, %rdi
movl 0x134(%rsp), %eax
movl %eax, %esi
callq 0x9ba0
movsd 0x10(%rsp), %xmm0
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movq 0x110(%rsp), %rax
movsd %xmm0, (%rcx)
movq (%rax), %rcx
movq %rcx, 0x30(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x148(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq 0x1a680
movq 0x30(%rsp), %rdi
movl (%rax), %esi
callq 0x10d30
movsd %xmm0, 0x38(%rsp)
jmp 0x15783
movl 0x2c(%rsp), %esi
movq 0x20(%rsp), %rdi
movsd 0x38(%rsp), %xmm0
movsd 0x188(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0xc150
jmp 0x157a6
jmp 0x157a8
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0x1568c
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
movl $0x30, %esi
callq 0x5390
jmp 0x1582c
movl 0x148(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x148(%rsp)
leaq 0x140(%rsp), %rdi
xorl %esi, %esi
callq 0x1a6a0
movq %rax, 0x128(%rsp)
jmp 0x1564e
leaq 0x190(%rsp), %rdi
callq 0x1a6e0
leaq 0x1f8(%rsp), %rdi
callq 0x1a6f0
jmp 0x15857
leaq 0x190(%rsp), %rdi
callq 0x1a6e0
leaq 0x1f8(%rsp), %rdi
callq 0x1a6f0
jmp 0x1585f
movq 0x110(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x8(%rax)
addq $0x368, %rsp            # imm = 0x368
retq
movq 0x330(%rsp), %rdi
callq 0x5690
nopl (%rax)