4def5e84d9ff83f67ccf74eefc982e74811ccbc2
[SXSI/TextCollection.git] / Tools.h
1 /*
2  * Collection of basic tools and defines
3  */
4
5
6 #ifndef _TOOLS_H_
7 #define _TOOLS_H_
8
9 #include <iostream>
10 #include <fstream>
11 #include <cstdlib>
12 #include <ctime>
13 #include <climits>
14
15 // Generates an error if __WORDSIZE is not defined
16 #ifndef __WORDSIZE
17 #error Missing definition of __WORDSIZE; Please define __WORDSIZE in Tools.h!
18 #endif
19
20 // Check word length on GNU C/C++:
21 // __WORDSIZE should be defined in <bits/wordsize.h>, which is #included from <limits.h>
22 #if __WORDSIZE == 64
23 #   define W 64
24 #else
25 #   define W 32
26 #endif
27
28 #ifndef WW
29 #define WW (W*2)
30 #endif
31
32 #ifndef Wminusone
33 #define Wminusone (W-1)
34 #endif
35
36 #ifndef uchar
37 #define uchar unsigned char
38 #endif
39 #ifndef ulong
40 #define ulong unsigned long
41 #endif
42
43
44 class Tools
45 {
46 private:
47     static time_t startTime;
48 public:
49     static void StartTimer();
50     static double GetTime();
51     static uchar * GetRandomString(unsigned, unsigned, unsigned &);
52     static void PrintBitSequence(ulong *, ulong);
53     static uchar * GetFileContents(char *, ulong =0);
54     static ulong * bp2bitstream(uchar *);
55     static unsigned FloorLog2(ulong);
56     static unsigned CeilLog2(ulong);
57     static unsigned bits (ulong);
58
59     static inline void SetField(ulong *A, register unsigned len, register ulong index, register ulong x) 
60     {
61         ulong i = index * len / W, 
62                  j = index * len - i * W;
63         ulong mask = (j+len < W ? ~0lu << (j+len) : 0) 
64                         | (W-j < W ? ~0lu >> (W-j) : 0);
65         A[i] = (A[i] & mask) | x << j;
66         if (j + len > W) 
67         {
68             mask = ((~0lu) << (len + j - W));  
69             A[i+1] = (A[i+1] & mask)| x >> (W - j);
70         }     
71     }
72     
73     static inline ulong GetField(ulong *A, register unsigned len, register ulong index) 
74     {
75         register ulong i = index * len / W, 
76                        j = index * len - W * i, 
77                        result;
78         if (j + len <= W)
79             result = (A[i] << (W - j - len)) >> (W - len);
80         else 
81         {
82             result = A[i] >> j;
83             result = result | (A[i+1] << (WW - j - len)) >> (W - len);
84         }
85         return result;
86     }
87        
88     
89     static inline ulong GetVariableField(ulong *A, register unsigned len, register ulong index) 
90     {
91        register ulong i=index/W, j=index-W*i, result;
92        if (j+len <= W)
93        result = (A[i] << (W-j-len)) >> (W-len);
94        else {
95              result = A[i] >> j;
96              result = result | (A[i+1] << (WW-j-len)) >> (W-len);
97           }
98        return result;
99     }
100
101     static inline void SetVariableField(ulong *A, register unsigned len, register ulong index, register ulong x) {
102         ulong i=index/W, 
103                     j=index-i*W;
104         ulong mask = (j+len < W ? ~0lu << (j+len) : 0) 
105                         | (W-j < W ? ~0lu >> (W-j) : 0);
106         A[i] = (A[i] & mask) | x << j;
107         if (j+len>W) {
108             mask = ((~0lu) << (len+j-W));  
109             A[i+1] = (A[i+1] & mask)| x >> (W-j);
110         } 
111     }
112
113
114 };
115
116 #endif