-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairwiseIndexer.cpp
More file actions
51 lines (44 loc) · 1.08 KB
/
PairwiseIndexer.cpp
File metadata and controls
51 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Created by tzhang on 7/19/24.
//
#include <iostream>
#include "PairwiseIndexer.h"
Indexer::Indexer(unsigned long nItems)
{
this->nItems = nItems;
this->itemsToRow = new unsigned long[nItems];
this->itemsToRow[0] = 0;
// compute LUT
unsigned int thisRow;
for (unsigned long i = 1; i < nItems; i++)
{
thisRow = nItems - i;
this->itemsToRow[i] = this->itemsToRow[i - 1] + thisRow;
}
}
Indexer::~Indexer()
{
delete [] this->itemsToRow;
}
unsigned long Indexer::RowIndex(unsigned long long condensed) const
{
if (condensed > (nItems * (nItems - 1) / 2))
return nItems; // failure mode for when indexing beyond max
long center;
long left = 0;
long right = nItems - 2;
// binary search for row
while (left < right)
{
center = (left + right) / 2; // TODO: can change this int division to float division for more speed
if (itemsToRow[center + 1] <= condensed)
left = center + 1;
else
right = center;
}
return left;
}
unsigned long Indexer::ColIndex(unsigned long long condensed, unsigned int row) const
{
return (row + 1) + (condensed - this->itemsToRow[row]);
}