768c09978db25fd7eba11406f80823b49f094ae1
[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 #ifndef uchar
22 typedef unsigned char           uchar;
23 #endif
24 typedef std::pair<usint, usint> pair_type;
25
26
27 inline usint length(usint n)
28 {
29   usint b = 0;
30   while(n > 0) { b++; n >>= 1; }
31   return b;
32 }
33
34 inline bool isEmpty(const pair_type& data)
35 {
36   return (data.first > data.second);
37 }
38
39 inline usint length(const pair_type& data)
40 {
41   return data.second + 1 - data.first;
42 }
43
44
45 const usint CHARS = ((usint)1 << CHAR_BIT);
46 const usint MEGABYTE = 1048576;
47 const usint MILLION  = 1000000;
48 const usint WORD_BITS = CHAR_BIT * sizeof(usint);
49 const usint WORD_MAX = ~((usint)0);
50
51 const pair_type EMPTY_PAIR = pair_type(1, 0);
52
53
54 // Previous GET was broken when BITS == WORD_BITS
55 // Current version works for usints and less
56 //#define GET(FIELD, BITS) ((FIELD) & ((1 << (BITS)) - 1))
57 #define GET(FIELD, BITS) ((FIELD) & (WORD_MAX >> (WORD_BITS - (BITS))))
58 #define LOWER(FIELD, N)  ((FIELD) >> (N))
59 #define HIGHER(FIELD, N) ((FIELD) << (N))
60
61 #define BITS_TO_BYTES(BITS) (((BITS) + CHAR_BIT - 1) / CHAR_BIT)
62 #define BYTES_TO_WORDS(BYTES) (((BYTES) + sizeof(usint) - 1) / sizeof(usint))
63 #define BITS_TO_WORDS(BITS) (((BITS) + WORD_BITS - 1) / WORD_BITS)
64
65
66 } // namespace CSA
67
68
69 #endif