-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefs.h
More file actions
executable file
·144 lines (121 loc) · 3.51 KB
/
defs.h
File metadata and controls
executable file
·144 lines (121 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <cstdio>
typedef uint8_t byte;
#define slotsof(a) (sizeof (a) / sizeof (a[0]))
// System Configuration
#define PAGE_SIZE 10 // 10 records
#define MEMORY_SIZE 5 //5 pages
#define FAN_IN (MEMORY_SIZE-1) //4 pages
#define CACHE_SIZE 3 //3 records
// User Input Default Values
static int ROW_SIZE = 10;
static int INPUT_SIZE = 200;
#define yesno(b) ((b) ? "yes" : "no")
// call-through to assert() from <assert.h>
//
void Assert (bool const predicate,
char const * const file, int const line);
//
#if defined ( _DEBUG ) || defined (DEBUG)
#define DebugAssert(b) Assert ((b), __FILE__, __LINE__)
#else // _DEBUG DEBUG
#define DebugAssert(b) (void) (0)
#endif // _DEBUG DEBUG
//
#define FinalAssert(b) Assert ((b), __FILE__, __LINE__)
// #define FinalAssert(b) (void) (0)
//
#define ParamAssert(b) FinalAssert (b)
#define traceprintf printf ("%s:%d:%s ", \
__FILE__, __LINE__, __FUNCTION__), \
printf
// -----------------------------------------------------------------
class Trace
{
public :
Trace (bool const trace, char const * const function,
char const * const file, int const line);
~Trace ();
private :
void _trace (char const lead []);
bool const _output;
char const * const _function;
char const * const _file;
int const _line;
}; // class Trace
#define TRACE(trace) Trace __trace (!trace, __FUNCTION__, __FILE__, __LINE__)
// -----------------------------------------------------------------
template <class Value> inline bool odd (
Value const value, int bits_from_lsb = 0)
{
return ((value >> bits_from_lsb) & Value (1)) != 0;
}
//
template <class Value> inline bool even (
Value const value, int bits_from_lsb = 0)
{
return ((Value (1) << bits_from_lsb) & value) == 0;
}
// templates for generic min and max operations
//
template <class Val> inline Val min (Val const a, Val const b)
{
return a < b ? a : b;
}
//
template <class Val> inline Val max (Val const a, Val const b)
{
return a > b ? a : b;
}
//
template <class Val> inline Val between (Val const value, Val const low, Val const high)
// 'value' but no less than 'low' and no more than 'high'
{
return (value < low ? low : value > high ? high : value);
}
//
template <class Val> inline void extremes (Val const value, Val & low, Val & high)
// retain 'value' in 'low' or 'high' keeping track of extremes
{
if (value < low) low = value;
if (value > high) high = value;
}
// templates for generic division
// should work for any integer type
//
template <class Val> inline Val divide (Val const a, Val const b)
{
return 1 + (a - 1) / b;
} // divide
//
template <class Val> inline Val roundup (Val const a, Val const b)
{
return b * divide (a, b);
} // roundup
template <class Val> inline void exchange (Val & a, Val & b)
{
Val const c = a;
a = b;
b = c;
} // exchange
// -----------------------------------------------------------------
template <class Val> inline Val mask (int const from, int const to)
{
// from: index of lsb set to 1, range 0..31
// to: index of msb set to 1, range from..31
// from==to ==> single bit set
//
return (((Val) 2) << to) - (((Val) 1) << from);
} // Value mask (int from, int to)
size_t Random (size_t const range);
size_t Random (size_t const low_incl, size_t const high_incl);
size_t RoundDown (size_t const x, size_t const y);
size_t RoundUp (size_t const x, size_t const y);
bool IsPowerOf2 (size_t const x);
size_t lsb (size_t const x);
size_t msb (size_t const x);
int msbi (size_t const x);
char const * YesNo (bool const b);
char const * OkBad (bool const b);