Jouni's Incremental BWT integrated into TextCollection
[SXSI/TextCollection.git] / incbwt / misc / definitions.h
1 #ifndef DEFINITIONS_H
2 #define DEFINITIONS_H
3
4 #include <algorithm>
5 #include <climits>
6
7
8 namespace CSA
9 {
10
11
12 #ifdef MASSIVE_DATA_RLCSA
13 typedef unsigned long usint;
14 typedef signed long   sint;
15 #else
16 typedef unsigned int  usint;
17 typedef signed int    sint;
18 #endif
19
20
21 typedef unsigned char           uchar;
22 typedef std::pair<usint, usint> pair_type;
23
24
25 inline usint length(usint n)
26 {
27   usint b = 0;
28   while(n > 0) { b++; n >>= 1; }
29   return b;
30 }
31
32 inline bool isEmpty(const pair_type& data)
33 {
34   return (data.first > data.second);
35 }
36
37 inline usint length(const pair_type& data)
38 {
39   return data.second + 1 - data.first;
40 }
41
42
43 const usint CHARS = ((usint)1 << CHAR_BIT);
44 const usint MEGABYTE = 1048576;
45 const usint MILLION  = 1000000;
46 const usint WORD_BITS = CHAR_BIT * sizeof(usint);
47 const usint WORD_MAX = ~((usint)0);
48
49 const pair_type EMPTY_PAIR = pair_type(1, 0);
50
51
52 // Previous GET was broken when BITS == WORD_BITS
53 // Current version works for usints and less
54 //#define GET(FIELD, BITS) ((FIELD) & ((1 << (BITS)) - 1))
55 #define GET(FIELD, BITS) ((FIELD) & (WORD_MAX >> (WORD_BITS - (BITS))))
56 #define LOWER(FIELD, N)  ((FIELD) >> (N))
57 #define HIGHER(FIELD, N) ((FIELD) << (N))
58
59 #define BITS_TO_BYTES(BITS) (((BITS) + CHAR_BIT - 1) / CHAR_BIT)
60 #define BYTES_TO_WORDS(BYTES) (((BYTES) + sizeof(usint) - 1) / sizeof(usint))
61 #define BITS_TO_WORDS(BITS) (((BITS) + WORD_BITS - 1) / WORD_BITS)
62
63
64 } // namespace CSA
65
66
67 #endif