f62840061d50d19369337de09608002fe566ac92
[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 // #define MULTITHREAD_SUPPORT   // Try to parallelize things using OpenMP.
9 // #define MASSIVE_DATA_RLCSA    // usint and sint become 64-bit in a 64-bit environment.
10
11
12 namespace CSA
13 {
14
15
16 #ifdef MASSIVE_DATA_RLCSA
17 typedef unsigned long usint;
18 typedef signed long   sint;
19 #else
20 typedef unsigned int  usint;
21 typedef signed int    sint;
22 #endif
23
24 #ifndef uint
25 typedef unsigned int uint;
26 #endif
27
28 #ifndef uchar
29 typedef unsigned char uchar;
30 #endif
31
32 typedef std::pair<usint, usint> pair_type;
33
34
35 inline usint length(usint n)
36 {
37   usint b = 0;
38   while(n > 0) { b++; n >>= 1; }
39   return b;
40 }
41
42 inline bool isEmpty(const pair_type& data)
43 {
44   return (data.first > data.second);
45 }
46
47 inline usint length(const pair_type& data)
48 {
49   return data.second + 1 - data.first;
50 }
51
52 inline usint nextMultipleOf(usint multiplier, usint value)
53 {
54   return multiplier * ((value / multiplier) + 1);
55 }
56
57
58 const usint CHARS = ((usint)1 << CHAR_BIT);
59 const usint MEGABYTE = 1048576;
60 const usint MILLION  = 1000000;
61 const usint WORD_BITS = CHAR_BIT * sizeof(usint);
62 const usint WORD_MAX = ~((usint)0);
63
64 const pair_type EMPTY_PAIR = pair_type(1, 0);
65
66
67 // Previous GET was broken when BITS == WORD_BITS
68 // Current version works for usints and less
69 //#define GET(FIELD, BITS) ((FIELD) & ((1 << (BITS)) - 1))
70 #define GET(FIELD, BITS) ((FIELD) & (WORD_MAX >> (WORD_BITS - (BITS))))
71 #define LOWER(FIELD, N)  ((FIELD) >> (N))
72 #define HIGHER(FIELD, N) ((FIELD) << (N))
73
74 #define BITS_TO_BYTES(BITS) (((BITS) + CHAR_BIT - 1) / CHAR_BIT)
75 #define BYTES_TO_WORDS(BYTES) (((BYTES) + sizeof(usint) - 1) / sizeof(usint))
76 #define BITS_TO_WORDS(BITS) (((BITS) + WORD_BITS - 1) / WORD_BITS)
77
78
79 } // namespace CSA
80
81
82 #endif