Guest User

Untitled

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