Guest User

Untitled

a guest
Mar 28th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.40 KB | None | 0 0
  1. // BTree.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include <iostream>
  7. #include <vector>
  8. #include <vld.h>
  9. using namespace std;
  10.  
  11. class ListAllocator {
  12.     class node {
  13.     public:
  14.         void* mem;
  15.         node *next;
  16.  
  17.         node (void *data = NULL, node *nxt = NULL)
  18.             : mem(data), next(nxt) {}
  19.  
  20.         ~node () {
  21.             char *_mem = reinterpret_cast<char *>(mem);
  22.             delete [] _mem;
  23.  
  24.             delete next;
  25.         }
  26.     };
  27.  
  28. public:
  29.  
  30.     ListAllocator () { head = NULL, length = 0; }
  31.  
  32.     ~ListAllocator () {
  33.         delete head;
  34.     }
  35.  
  36.     void* alloc () {                // memory leak - why????
  37.         void *mem  = head->mem;
  38.         head       = head->next;
  39.        
  40.         length--;
  41.  
  42.         return mem;
  43.     }
  44.  
  45.     void recycle (void *mem) {
  46.         node *tmp = new node(mem, head);
  47.  
  48.         head      = tmp;
  49.  
  50.         length++;
  51.     }
  52.  
  53.     int size () const { return length; }
  54.  
  55. private:
  56.  
  57.     node *head;
  58.     int length;
  59. } alc;
  60.  
  61. class BTree;
  62.  
  63. class NODE {
  64. public:
  65.  
  66.     NODE () {
  67.         n = offset = 0; load = false;
  68.  
  69.         for (int i = 0; i < 2*t + 1; i++) {
  70.             lpc[i] = NULL;
  71.         }
  72.     }
  73.  
  74.     ~NODE () {
  75.         for (int i = 0; i < n + 1; i++) {  
  76.             delete lpc[i];
  77.             lpc[i] = NULL;
  78.         }
  79.  
  80.         n = offset = 0; load = false;
  81.     }
  82.  
  83.     void * operator new (size_t size) {
  84.         if (size != sizeof(NODE)) {
  85.             return NULL;
  86.         }
  87.  
  88.         if (!alc.size())    return new char[size];
  89.         else                return alc.alloc();
  90.     }
  91.  
  92.     void operator delete (void * mem) {
  93.         if (mem) {
  94.             alc.recycle(mem);
  95.         }
  96.     }
  97.  
  98.     bool full () const { return n == 2*t - 1; }
  99.  
  100.     static const int size = 84; //2048;
  101.     static const int t = 3;     //84;
  102.  
  103.     int n;
  104.     int leaf;
  105.     int key[2*t];
  106.     int d[2*t];    // offset of i'th data record in data file with key equals to key_i
  107.     int c[2*t+1];  // offset of i'th child of node in BTree file
  108.    
  109.     NODE *lpc[2*t + 1];
  110.  
  111.     int offset;
  112.  
  113.     bool load;
  114.  
  115. private:
  116.  
  117.     static void node_to_byte (NODE *x, int *&arr, int &sz) {
  118.         sz = size / 4; //(int)pow(2, ceil( log(size) / log(2.0) )) / 4;
  119.         arr = new int[sz];
  120.    
  121.         arr[0] = x->n;
  122.         arr[1] = x->leaf;
  123.         int j = 2;
  124.         for (int i = 0; i < x->n; i++, j++) {
  125.             arr[j]          = x->key[i];
  126.             arr[j + x->n]   = x->d[i];
  127.         }
  128.  
  129.         if (!x->leaf) {
  130.             j = 2 * x->n + 2;
  131.             for (int i = 0; i <= x->n; i++, j++)
  132.                 arr[j] = x->c[i];
  133.         }
  134.     }
  135.    
  136.     static void byte_to_node (int *arr, NODE *&x) {
  137.         x->n = arr[0];
  138.         x->leaf = arr[1];
  139.        
  140.         int j = 2;
  141.         for (int i = 0; i < x->n; i++, j++) {
  142.             x->key[i]   = arr[j];
  143.             x->d[i]     = arr[x->n + j];
  144.         }
  145.  
  146.         if (!x->leaf) {
  147.             j = 2 * x->n + 2;
  148.             for (int i = 0; i <= x->n; i++, j++)
  149.                 x->c[i] = arr[j];
  150.         }
  151.     }
  152.  
  153.     friend class BTree;
  154. };
  155. typedef NODE *LPNODE;
  156. #define NIL reinterpret_cast<LPNODE>(NULL)
  157.  
  158. class BTree {
  159. public:
  160.    
  161.     BTree () {
  162.         fName = L"tmp";
  163.         Init_Disk (fName);
  164.  
  165.         root        = new NODE;
  166.         root->n     = 0;
  167.         root->leaf  = true;
  168.         root->offset= 8;
  169.         root->load  = true;
  170.        
  171.         Write_Disk (root);
  172.         offset = NODE::size + root->offset;
  173.     }
  174.  
  175.     BTree (LPWSTR fName) : fName(fName) {
  176.         Init_Disk(fName, FALSE);
  177.  
  178.         DWORD read, root_offset;
  179.         ReadFile(hFile, &offset, 4, &read, NULL);
  180.         ReadFile(hFile, &root_offset, 4, &read, NULL);
  181.  
  182.         root = NULL;
  183.         Read_Disk(root, root_offset);
  184.     }
  185.  
  186.     ~BTree () {
  187.         SetFilePointer (hFile, 0, NULL, FILE_BEGIN);
  188.  
  189.         DWORD written;
  190.         WriteFile(hFile, &offset, 4, &written, NULL);
  191.         WriteFile(hFile, &root->offset, 4, &written, NULL);
  192.  
  193.         CloseHandle (hFile);
  194.  
  195.         delete root;
  196.         root = NULL;
  197.     }
  198.    
  199.     void SetFileName (LPWSTR str) { fName = str; }
  200.     LPWSTR GetFileName () const { return fName; }
  201.  
  202.     pair<LPNODE, int> BTree_Search (LPNODE x, int k) {
  203.         int i = 0;
  204.         while (i < x->n && k > x->key[i]) i++;
  205.  
  206.         if (i < x->n && k == x->key[i]) return make_pair(x, i);
  207.         if (x->leaf)                    return make_pair(NIL, -1);
  208.         else {
  209.             Read_Disk(x->lpc[i], x->c[i]);
  210.             return BTree_Search (x->lpc[i], k);
  211.         }
  212.     }
  213.  
  214.     void BTree_Insert (int k, int fd) {
  215.         if (root->full()) {
  216.             LPNODE r = root;
  217.  
  218.             LPNODE s = new NODE;
  219.             root = s;
  220.             s->leaf = FALSE;
  221.             s->n = 0;
  222.             s->c[0] = r->offset;
  223.             s->lpc[0] = r;
  224.  
  225.             s->offset = offset;
  226.             offset += NODE::size;
  227.  
  228.             s->load = true;
  229.  
  230.             BTree_Split (s, 0, r);
  231.  
  232.             BTree_Insert_Nonfull(s, k, fd);
  233.         } else {
  234.             BTree_Insert_Nonfull(root, k, fd);
  235.         }
  236.     }
  237.  
  238.     LPNODE root;
  239.  
  240. private:
  241.  
  242.     void BTree_Insert_Nonfull (LPNODE &x, int k, int fd) {
  243.         int i = x->n - 1;
  244.  
  245.         if (x->leaf) {
  246.             while (i >= 0 && k < x->key[i]) {
  247.                 x->key[i+1] = x->key[i];
  248.                 x->d[i+1]   = x->d[i];
  249.                 i--;
  250.             }
  251.  
  252.             x->key[i+1] = k;
  253.             x->d[i+1]   = fd;
  254.             x->n        = x->n + 1;
  255.  
  256.             Write_Disk(x);
  257.         }
  258.         else {
  259.             while (i >= 0 && k < x->key[i]) i--;
  260.  
  261.             i++;
  262.             Read_Disk(x->lpc[i], x->c[i]);
  263.            
  264.             if (x->lpc[i]->full()) {
  265.                 BTree_Split(x, i, x->lpc[i]);
  266.  
  267.                 if (k > x->key[i]) i++;
  268.             }
  269.            
  270.             BTree_Insert_Nonfull(x->lpc[i], k, fd);
  271.         }
  272.     }
  273.  
  274.     void BTree_Split (LPNODE &x, int i, LPNODE &y) {
  275.         LPNODE z = new NODE;
  276.         z->leaf  = y->leaf;
  277.         z->n     = NODE::t - 1;
  278.         z->offset= offset;
  279.         z->load  = true;
  280.  
  281.         for (int j = 0; j < NODE::t - 1; j++) {
  282.             z->key[j]   = y->key[NODE::t + j];
  283.             z->d[j]     = y->d[NODE::t + j];
  284.         }
  285.  
  286.         if (!y->leaf) {
  287.             for (int j = 0; j < NODE::t; j++) {
  288.                 z->c[j]     = y->c[NODE::t + j];
  289.                 z->lpc[j]   = y->lpc[NODE::t + j];
  290.             }
  291.         }
  292.  
  293.         y->n = NODE::t - 1;
  294.  
  295.         for (int j = x->n; j > i; j--) {
  296.             x->c[j+1]   = x->c[j];
  297.             x->lpc[j+1] = x->lpc[j];
  298.         }
  299.  
  300.         x->c[i+1]   = z->offset;
  301.         x->lpc[i+1] = z;
  302.  
  303.         for (int j = x->n - 1; j >= i; j--) {
  304.             x->key[j+1] = x->key[j];
  305.             x->d[j+1]   = x->d[j];
  306.         }
  307.  
  308.         x->key[i] = y->key[NODE::t - 1];
  309.         x->d[i]   = y->d[NODE::t - 1];
  310.         x->n      = x->n + 1;
  311.  
  312.         Write_Disk(x);
  313.         Write_Disk(y);
  314.         Write_Disk(z);
  315.  
  316.         offset += NODE::size;
  317.     }
  318.  
  319.     void Init_Disk (LPCWSTR fName, BOOL create = TRUE) {
  320.         hFile = CreateFile(fName,
  321.                           GENERIC_ALL,
  322.                           FILE_SHARE_READ,
  323.                           NULL,
  324.                           create ? CREATE_ALWAYS : OPEN_EXISTING,
  325.                           FILE_ATTRIBUTE_HIDDEN,
  326.                           NULL );
  327.                          
  328.         if( hFile == INVALID_HANDLE_VALUE ) {
  329.             throw INVALID_HANDLE_VALUE;
  330.         }
  331.     }
  332.    
  333.     void Write_Disk (LPNODE x) {
  334.         SetFilePointer (hFile, x->offset, NULL, FILE_BEGIN);
  335.    
  336.         int *arr, sz;
  337.         DWORD written;
  338.         NODE::node_to_byte (x, arr, sz);
  339.        
  340.         WriteFile (hFile, arr, sz*4, &written, NULL);
  341.  
  342.         delete [] arr;
  343.     }
  344.    
  345.     void Read_Disk (LPNODE &x, int offset = 0) {
  346.         if (x == NULL) {
  347.             x = new NODE;
  348.         }
  349.  
  350.         if (x->load) return;
  351.  
  352.         SetFilePointer (hFile, offset, NULL, FILE_BEGIN);
  353.        
  354.         int sz = NODE::size / 4, read; //(int)pow(2, ceil( log(NODE::size) / log(2.0) )) / 4;
  355.         int *arr = new int[sz];
  356.        
  357.         ReadFile(hFile, arr, sz*4, (LPDWORD)(&read), NULL);
  358.        
  359.         NODE::byte_to_node (arr, x);
  360.         x->offset   = offset;
  361.         x->load     = true;
  362.  
  363.         delete [] arr;
  364.     }
  365.    
  366.     HANDLE hFile;
  367.     DWORD offset;  
  368.  
  369.     LPWSTR fName;
  370. };
  371.  
  372. void CreateTestCase (LPCWSTR fName, int size) {
  373.     HANDLE hFile = CreateFile(fName,           // name of the write
  374.                        GENERIC_WRITE,          // open for writing
  375.                        0,                      // do not share
  376.                        NULL,                   // default security
  377.                        CREATE_ALWAYS,          // create new file (or open existing)
  378.                        FILE_ATTRIBUTE_NORMAL,  // normal file
  379.                        NULL);                  // no attr. template
  380.  
  381.  
  382.     if (hFile == INVALID_HANDLE_VALUE) {
  383.         printf("error while creating file.\n");
  384.         return;
  385.     }
  386.  
  387.     DWORD dwBytesWritten = 0, arr_sz = 128;
  388.  
  389.     WriteFile(hFile, (LPCVOID)(&size), sizeof(int),
  390.         &dwBytesWritten, NULL);
  391.  
  392.     int *arr = new int[arr_sz];
  393.    
  394.     while (size > 0) {
  395.         for (int i = 0; i < min(arr_sz, size); i++) {
  396.             arr[i] = rand() % 1000;
  397.             //printf("%d ", arr[i]);
  398.         }
  399.         //printf("\n");
  400.  
  401.         BOOL flag = WriteFile(hFile, (LPCVOID)arr, 4 * min(arr_sz, size),
  402.             &dwBytesWritten, NULL);
  403.  
  404.         if (dwBytesWritten == 0) return;
  405.  
  406.         if (FALSE == flag) {
  407.             printf("error while creating test file.\n");
  408.             return;
  409.         }
  410.  
  411.         size -= arr_sz;
  412.     }
  413.  
  414.     CloseHandle(hFile);
  415.  
  416.     delete [] arr;
  417. }
  418.  
  419. BOOL RunTestCase (LPCWSTR fName, BTree *&bt, vector<int> &num) {
  420.     vector< bool > in(num.size(), false);
  421.  
  422.     HANDLE hFile = CreateFile(fName,
  423.         GENERIC_READ, NULL, NULL,
  424.         OPEN_EXISTING,
  425.         NULL, NULL);
  426.  
  427.     DWORD bytesRead;
  428.     int size, arr_sz = 128;
  429.     ReadFile(hFile, &size, 4, &bytesRead, NULL);
  430.  
  431.     int *arr = new int [arr_sz], cnt = 0;
  432.     while (size > 0) {
  433.  
  434.         ReadFile(hFile, arr, min(size, arr_sz) * 4, &bytesRead, NULL);
  435.  
  436.         for (int i = 0; i < min(size, arr_sz); i++) {
  437.            
  438.             bt->BTree_Insert(arr[i], cnt * arr_sz + i);
  439.            
  440.             if ( (cnt * arr_sz + i) % 100 == 0 && i > 0) {
  441.                 // flush B-Tree
  442.                 LPWSTR file = bt->GetFileName();
  443.                 delete bt;
  444.            
  445.                 bt = new BTree(file);
  446.                 //break;
  447.             }
  448.  
  449.             for (int j = 0; j < num.size(); j++) {
  450.                 if (num[j] == arr[i]) in[j] = true;
  451.             }
  452.         }
  453.  
  454.         size -= arr_sz;
  455.         cnt++;
  456.     }
  457.  
  458.     CloseHandle(hFile);
  459.     delete [] arr;
  460.  
  461.     bool result = true;
  462.     for (int i = 0; i < num.size(); i++) {
  463.         if (in[i]) {
  464.             pair<LPNODE, int> tmp = bt->BTree_Search(bt->root, num[i]);
  465.             result &= tmp.first != NIL ;
  466.         }
  467.     }
  468.  
  469.     return result;
  470. }
  471.  
  472. int _tmain(int argc, _TCHAR* argv[])
  473. {
  474.     LPCWSTR fName = L"test_small"; 
  475.     BTree *bt = new BTree;
  476.     //BTree bt(L"tmp");
  477.     vector<int> num;
  478.     //CreateTestCase(fName, 128);
  479.    
  480.     for (int i = 0; i < 150; i++) num.push_back(i);
  481.  
  482.     cout << RunTestCase (fName, bt, num);
  483.  
  484.     delete bt;
  485.  
  486.     return 0;
  487. }
Advertisement
Add Comment
Please, Sign In to add comment