-
Notifications
You must be signed in to change notification settings - Fork 2
/
distinct.cpp
62 lines (54 loc) · 1.66 KB
/
distinct.cpp
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
52
53
54
55
56
57
58
59
60
61
62
#include "index.cpp"
#pragma once
const addr_t distinctTableStart = 500;
/**
* @brief 去重操作
*
* @param table 待去重的表
* @param resStartAddr 去重结果存放的起始位置,默认值为distinctTableStart
*/
void tableDistinct(table_t table, addr_t resStartAddr = distinctTableStart) {
int numOfUsedBlock = numOfBufBlock - 1;
int numOfRows = numOfRowInBlk * numOfUsedBlock;
addr_t resAddr = resStartAddr;
// 想去重,先聚簇
addr_t readAddr = useCluster(table);
block_t readBlk[numOfUsedBlock], resBlk;
resBlk.writeInit(resAddr);
addr_t nextStart = readAddr;
for (int i = 0; i < numOfUsedBlock; ++i) {
readBlk[i].loadFromDisk(nextStart);
nextStart = readBlk[i].readNextAddr();
if (nextStart == END_OF_FILE) {
numOfUsedBlock = i + 1;
break;
}
}
row_t t[numOfRows], prior;
int readRows;
while(1) {
readRows = read_N_Rows_From_M_Block(readBlk, t, numOfRows, numOfUsedBlock);
insertSort<row_t>(t, readRows);
// printRows(t, readRows, val);
for (int i = 0; i < readRows; ++i) {
if (t[i] == prior) {
// 聚簇后,与上一个出现过的记录相同则重复
continue;
}
resBlk.writeRow(t[i]);
prior = t[i];
}
if (readRows < numOfUsedBlock) {
resBlk.writeLastBlock();
break;
}
}
}
/**************************** main ****************************/
// int main() {
// bufferInit();
// tableDistinct(table_S);
// showResult(distinctTableStart);
// system("pause");
// return 0;
// }