Fixed definition clash
[SXSI/TextCollection.git] / BlockArray.h
1 #ifndef _BLOCK_ARRAY_H_
2 #define _BLOCK_ARRAY_H_
3 #include "Tools.h"
4 #include <iostream>
5 #include <stdexcept>
6
7 class BlockArray
8 {
9 private:
10     ulong* data;
11     ulong n;
12     ulong index;
13     ulong blockLength;
14 public:
15     BlockArray(ulong len, ulong blockLen) {
16        n = len;
17        blockLength = blockLen;
18        data = new ulong[n*blockLength/W +1];
19     }
20     ~BlockArray() {
21        delete [] data;
22     }
23
24     BlockArray& operator[](ulong i)  {
25        index = i;
26        return *this;
27     }  
28
29     void operator=(const ulong x) {
30        Tools::SetField(data,blockLength,index,x);
31     }  
32
33     BlockArray& operator=(const BlockArray& ba) {
34        if (this == &ba) return *this;
35        ulong value = Tools::GetField(ba.data, ba.blockLength, ba.index);
36        Tools::SetField(data,blockLength,index,value);
37        return *this;
38     }  
39         
40     operator ulong() {
41        return Tools::GetField(data,blockLength,index);
42     }
43     
44     ulong spaceInBits() {
45         return n*blockLength+W; // plus 4 ulong's
46     }
47
48     /**
49      * Saving data fields:
50      *     ulong n;
51      *     ulong blockLength;
52      *     ulong* data;
53      */
54     void Save(FILE *file) const
55     {
56         if (std::fwrite(&(this->n), sizeof(ulong), 1, file) != 1)
57             throw std::runtime_error("BlockArray::Save(): file write error (n).");
58         if (std::fwrite(&(this->blockLength), sizeof(ulong), 1, file) != 1)
59             throw std::runtime_error("BlockArray::Save(): file write error (blockLength).");
60     
61         for (ulong offset = 0; offset < n*blockLength/W+1; offset ++)
62         {
63             if (std::fwrite(this->data + offset, sizeof(ulong), 1, file) != 1)
64                 throw std::runtime_error("BlockArray::Save(): file write error (data).");
65         }
66     }
67
68     /**
69      * Load from file
70      */
71     BlockArray(FILE *file)
72     {
73         if (std::fread(&(this->n), sizeof(ulong), 1, file) != 1)
74             throw std::runtime_error("BlockArray::Load(): file read error (n).");
75         if (std::fread(&(this->blockLength), sizeof(ulong), 1, file) != 1)
76             throw std::runtime_error("BlockArray::Load(): file read error (blockLength).");
77
78         data = new ulong[n*blockLength/W+1];
79         for (ulong offset = 0; offset < n*blockLength/W+1; offset ++)
80         {
81             if (std::fread(this->data + offset, sizeof(ulong), 1, file) != 1)
82                 throw std::runtime_error("BlockArray::Load(): file read error (data).");
83         }
84     }
85 };
86
87 #endif
88