Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BTree.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <Windows.h>
- #include <iostream>
- #include <vector>
- #include <vld.h>
- using namespace std;
- class ListAllocator {
- class node {
- public:
- void* mem;
- node *next;
- node (void *data = NULL, node *nxt = NULL)
- : mem(data), next(nxt) {}
- ~node () {
- char *_mem = reinterpret_cast<char *>(mem);
- delete [] _mem;
- delete next;
- }
- };
- public:
- ListAllocator () { head = NULL, length = 0; }
- ~ListAllocator () {
- delete head;
- }
- void* alloc () { // memory leak - why????
- void *mem = head->mem;
- head = head->next;
- length--;
- return mem;
- }
- void recycle (void *mem) {
- node *tmp = new node(mem, head);
- head = tmp;
- length++;
- }
- int size () const { return length; }
- private:
- node *head;
- int length;
- } alc;
- class BTree;
- class NODE {
- public:
- NODE () {
- n = offset = 0; load = false;
- for (int i = 0; i < 2*t + 1; i++) {
- lpc[i] = NULL;
- }
- }
- ~NODE () {
- for (int i = 0; i < n + 1; i++) {
- delete lpc[i];
- lpc[i] = NULL;
- }
- n = offset = 0; load = false;
- }
- void * operator new (size_t size) {
- if (size != sizeof(NODE)) {
- return NULL;
- }
- if (!alc.size()) return new char[size];
- else return alc.alloc();
- }
- void operator delete (void * mem) {
- if (mem) {
- alc.recycle(mem);
- }
- }
- bool full () const { return n == 2*t - 1; }
- static const int size = 84; //2048;
- static const int t = 3; //84;
- int n;
- int leaf;
- int key[2*t];
- int d[2*t]; // offset of i'th data record in data file with key equals to key_i
- int c[2*t+1]; // offset of i'th child of node in BTree file
- NODE *lpc[2*t + 1];
- int offset;
- bool load;
- private:
- static void node_to_byte (NODE *x, int *&arr, int &sz) {
- sz = size / 4; //(int)pow(2, ceil( log(size) / log(2.0) )) / 4;
- arr = new int[sz];
- arr[0] = x->n;
- arr[1] = x->leaf;
- int j = 2;
- for (int i = 0; i < x->n; i++, j++) {
- arr[j] = x->key[i];
- arr[j + x->n] = x->d[i];
- }
- if (!x->leaf) {
- j = 2 * x->n + 2;
- for (int i = 0; i <= x->n; i++, j++)
- arr[j] = x->c[i];
- }
- }
- static void byte_to_node (int *arr, NODE *&x) {
- x->n = arr[0];
- x->leaf = arr[1];
- int j = 2;
- for (int i = 0; i < x->n; i++, j++) {
- x->key[i] = arr[j];
- x->d[i] = arr[x->n + j];
- }
- if (!x->leaf) {
- j = 2 * x->n + 2;
- for (int i = 0; i <= x->n; i++, j++)
- x->c[i] = arr[j];
- }
- }
- friend class BTree;
- };
- typedef NODE *LPNODE;
- #define NIL reinterpret_cast<LPNODE>(NULL)
- class BTree {
- public:
- BTree () {
- fName = L"tmp";
- Init_Disk (fName);
- root = new NODE;
- root->n = 0;
- root->leaf = true;
- root->offset= 8;
- root->load = true;
- Write_Disk (root);
- offset = NODE::size + root->offset;
- }
- BTree (LPWSTR fName) : fName(fName) {
- Init_Disk(fName, FALSE);
- DWORD read, root_offset;
- ReadFile(hFile, &offset, 4, &read, NULL);
- ReadFile(hFile, &root_offset, 4, &read, NULL);
- root = NULL;
- Read_Disk(root, root_offset);
- }
- ~BTree () {
- SetFilePointer (hFile, 0, NULL, FILE_BEGIN);
- DWORD written;
- WriteFile(hFile, &offset, 4, &written, NULL);
- WriteFile(hFile, &root->offset, 4, &written, NULL);
- CloseHandle (hFile);
- delete root;
- root = NULL;
- }
- void SetFileName (LPWSTR str) { fName = str; }
- LPWSTR GetFileName () const { return fName; }
- pair<LPNODE, int> BTree_Search (LPNODE x, int k) {
- int i = 0;
- while (i < x->n && k > x->key[i]) i++;
- if (i < x->n && k == x->key[i]) return make_pair(x, i);
- if (x->leaf) return make_pair(NIL, -1);
- else {
- Read_Disk(x->lpc[i], x->c[i]);
- return BTree_Search (x->lpc[i], k);
- }
- }
- void BTree_Insert (int k, int fd) {
- if (root->full()) {
- LPNODE r = root;
- LPNODE s = new NODE;
- root = s;
- s->leaf = FALSE;
- s->n = 0;
- s->c[0] = r->offset;
- s->lpc[0] = r;
- s->offset = offset;
- offset += NODE::size;
- s->load = true;
- BTree_Split (s, 0, r);
- BTree_Insert_Nonfull(s, k, fd);
- } else {
- BTree_Insert_Nonfull(root, k, fd);
- }
- }
- LPNODE root;
- private:
- void BTree_Insert_Nonfull (LPNODE &x, int k, int fd) {
- int i = x->n - 1;
- if (x->leaf) {
- while (i >= 0 && k < x->key[i]) {
- x->key[i+1] = x->key[i];
- x->d[i+1] = x->d[i];
- i--;
- }
- x->key[i+1] = k;
- x->d[i+1] = fd;
- x->n = x->n + 1;
- Write_Disk(x);
- }
- else {
- while (i >= 0 && k < x->key[i]) i--;
- i++;
- Read_Disk(x->lpc[i], x->c[i]);
- if (x->lpc[i]->full()) {
- BTree_Split(x, i, x->lpc[i]);
- if (k > x->key[i]) i++;
- }
- BTree_Insert_Nonfull(x->lpc[i], k, fd);
- }
- }
- void BTree_Split (LPNODE &x, int i, LPNODE &y) {
- LPNODE z = new NODE;
- z->leaf = y->leaf;
- z->n = NODE::t - 1;
- z->offset= offset;
- z->load = true;
- for (int j = 0; j < NODE::t - 1; j++) {
- z->key[j] = y->key[NODE::t + j];
- z->d[j] = y->d[NODE::t + j];
- }
- if (!y->leaf) {
- for (int j = 0; j < NODE::t; j++) {
- z->c[j] = y->c[NODE::t + j];
- z->lpc[j] = y->lpc[NODE::t + j];
- }
- }
- y->n = NODE::t - 1;
- for (int j = x->n; j > i; j--) {
- x->c[j+1] = x->c[j];
- x->lpc[j+1] = x->lpc[j];
- }
- x->c[i+1] = z->offset;
- x->lpc[i+1] = z;
- for (int j = x->n - 1; j >= i; j--) {
- x->key[j+1] = x->key[j];
- x->d[j+1] = x->d[j];
- }
- x->key[i] = y->key[NODE::t - 1];
- x->d[i] = y->d[NODE::t - 1];
- x->n = x->n + 1;
- Write_Disk(x);
- Write_Disk(y);
- Write_Disk(z);
- offset += NODE::size;
- }
- void Init_Disk (LPCWSTR fName, BOOL create = TRUE) {
- hFile = CreateFile(fName,
- GENERIC_ALL,
- FILE_SHARE_READ,
- NULL,
- create ? CREATE_ALWAYS : OPEN_EXISTING,
- FILE_ATTRIBUTE_HIDDEN,
- NULL );
- if( hFile == INVALID_HANDLE_VALUE ) {
- throw INVALID_HANDLE_VALUE;
- }
- }
- void Write_Disk (LPNODE x) {
- SetFilePointer (hFile, x->offset, NULL, FILE_BEGIN);
- int *arr, sz;
- DWORD written;
- NODE::node_to_byte (x, arr, sz);
- WriteFile (hFile, arr, sz*4, &written, NULL);
- delete [] arr;
- }
- void Read_Disk (LPNODE &x, int offset = 0) {
- if (x == NULL) {
- x = new NODE;
- }
- if (x->load) return;
- SetFilePointer (hFile, offset, NULL, FILE_BEGIN);
- int sz = NODE::size / 4, read; //(int)pow(2, ceil( log(NODE::size) / log(2.0) )) / 4;
- int *arr = new int[sz];
- ReadFile(hFile, arr, sz*4, (LPDWORD)(&read), NULL);
- NODE::byte_to_node (arr, x);
- x->offset = offset;
- x->load = true;
- delete [] arr;
- }
- HANDLE hFile;
- DWORD offset;
- LPWSTR fName;
- };
- void CreateTestCase (LPCWSTR fName, int size) {
- HANDLE hFile = CreateFile(fName, // name of the write
- GENERIC_WRITE, // open for writing
- 0, // do not share
- NULL, // default security
- CREATE_ALWAYS, // create new file (or open existing)
- FILE_ATTRIBUTE_NORMAL, // normal file
- NULL); // no attr. template
- if (hFile == INVALID_HANDLE_VALUE) {
- printf("error while creating file.\n");
- return;
- }
- DWORD dwBytesWritten = 0, arr_sz = 128;
- WriteFile(hFile, (LPCVOID)(&size), sizeof(int),
- &dwBytesWritten, NULL);
- int *arr = new int[arr_sz];
- while (size > 0) {
- for (int i = 0; i < min(arr_sz, size); i++) {
- arr[i] = rand() % 1000;
- //printf("%d ", arr[i]);
- }
- //printf("\n");
- BOOL flag = WriteFile(hFile, (LPCVOID)arr, 4 * min(arr_sz, size),
- &dwBytesWritten, NULL);
- if (dwBytesWritten == 0) return;
- if (FALSE == flag) {
- printf("error while creating test file.\n");
- return;
- }
- size -= arr_sz;
- }
- CloseHandle(hFile);
- delete [] arr;
- }
- BOOL RunTestCase (LPCWSTR fName, BTree *&bt, vector<int> &num) {
- vector< bool > in(num.size(), false);
- HANDLE hFile = CreateFile(fName,
- GENERIC_READ, NULL, NULL,
- OPEN_EXISTING,
- NULL, NULL);
- DWORD bytesRead;
- int size, arr_sz = 128;
- ReadFile(hFile, &size, 4, &bytesRead, NULL);
- int *arr = new int [arr_sz], cnt = 0;
- while (size > 0) {
- ReadFile(hFile, arr, min(size, arr_sz) * 4, &bytesRead, NULL);
- for (int i = 0; i < min(size, arr_sz); i++) {
- bt->BTree_Insert(arr[i], cnt * arr_sz + i);
- if ( (cnt * arr_sz + i) % 100 == 0 && i > 0) {
- // flush B-Tree
- LPWSTR file = bt->GetFileName();
- delete bt;
- bt = new BTree(file);
- //break;
- }
- for (int j = 0; j < num.size(); j++) {
- if (num[j] == arr[i]) in[j] = true;
- }
- }
- size -= arr_sz;
- cnt++;
- }
- CloseHandle(hFile);
- delete [] arr;
- bool result = true;
- for (int i = 0; i < num.size(); i++) {
- if (in[i]) {
- pair<LPNODE, int> tmp = bt->BTree_Search(bt->root, num[i]);
- result &= tmp.first != NIL ;
- }
- }
- return result;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- LPCWSTR fName = L"test_small";
- BTree *bt = new BTree;
- //BTree bt(L"tmp");
- vector<int> num;
- //CreateTestCase(fName, 128);
- for (int i = 0; i < 150; i++) num.push_back(i);
- cout << RunTestCase (fName, bt, num);
- delete bt;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment