forked from petegoodliffe/skip_list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskip_list_detail.h
203 lines (155 loc) · 5.37 KB
/
skip_list_detail.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//==============================================================================
// skip_list_detail.h
// Copyright (c) 2011 Pete Goodliffe. All rights reserved.
//==============================================================================
#pragma once
#include <cmath> // for std::log
#include <cstdlib> // for std::rand
//==============================================================================
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning (disable : 4068 ) /* disable unknown pragma warnings */
#endif
//==============================================================================
#pragma mark - internal forward declarations
namespace goodliffe {
/// @internal
/// Internal namespace for impementation of skip list data structure
namespace detail
{
template <unsigned NumLevels> class bit_based_skip_list_level_generator;
template <unsigned NumLevels> class skip_list_level_generator;
}
}
//==============================================================================
#pragma mark - diagnostics
//==============================================================================
//#define SKIP_LIST_IMPL_DIAGNOSTICS 1
#if defined(DEBUG) || defined(_DEBUG) || defined(SKIP_LIST_IMPL_DIAGNOSTICS)
#define SKIP_LIST_DIAGNOSTICS 1
#endif
#ifdef SKIP_LIST_DIAGNOSTICS
#include <cstdio>
void pg_assertion_break();
inline
void pg_assertion_break() { fprintf(stderr, "**** place a breakpoint at pg_assertion_break to debug\n"); }
#include <cassert>
#include <stdio.h>
#define pg_fail(a) {fprintf(stderr,"%s:%d: \"%s\"\n", __FILE__, __LINE__, a); /*assert(false);*/ pg_assertion_break(); }
#define assert_that(a) {if (!(a)) pg_fail(#a);}
#define pg_not_implemented_yet() pg_fail("not implemented yet")
#else
#define pg_fail(a)
#define assert_that(a)
#define pg_not_implemented_yet()
#endif
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
#include <iostream>
#define impl_assert_that(a) assert_that(a)
#else
#define impl_assert_that(a)
#endif
namespace goodliffe {
namespace detail {
template<bool> struct static_assert_that_impl;
template<> struct static_assert_that_impl<true> {};
#define static_assert_that(a) \
{::goodliffe::detail::static_assert_that_impl<a> foo;(void)foo;}
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
enum
{
MAGIC_GOOD = 0x01020304,
MAGIC_BAD = 0xfefefefe
};
#endif
} // namespace detail
} // namespace goodliffe
//==============================================================================
#pragma mark - skip_list level generators
//==============================================================================
namespace goodliffe {
namespace detail {
/// Generate a stream of levels, probabilstically chosen.
/// - With a probability of 1/2, return 0.
/// - With 1/4 probability, return 1.
/// - With 1/8 probability, return 2.
/// - And so forth.
template <unsigned NumLevels>
class skip_list_level_generator
{
public:
static const unsigned num_levels = NumLevels;
unsigned new_level();
};
template <unsigned NumLevels>
class bit_based_skip_list_level_generator
{
public:
static const unsigned num_levels = NumLevels;
unsigned new_level();
};
} // namespace detail
} // namespace goodliffe
//==============================================================================
#pragma mark - value equivalence based on "less"
//==============================================================================
namespace goodliffe {
namespace detail {
#if 1
template <typename Compare, typename T>
inline
bool equivalent(const T &lhs, const T &rhs, const Compare &less)
{ return !less(lhs, rhs) && !less(rhs, lhs); }
template <typename Compare, typename T>
inline
bool less_or_equal(const T &lhs, const T &rhs, const Compare &less)
{ return !less(rhs, lhs); }
#else
// These "simple" versions are left here for efficiency comparison with
// the versions above.
// There should be no appriciable difference in performance (at least, for
// the built-in types).
template <typename Compare, typename T>
inline
bool equivalent(const T &lhs, const T &rhs, Compare &less)
{ return lhs == rhs; }
template <typename Compare, typename T>
inline
bool less_or_equal(const T &lhs, const T &rhs, Compare &less)
{ return lhs <= rhs; }
#endif
} // namespace detail
} // namespace goodliffe
//==============================================================================
#pragma mark - skip_list_level_generator
//==============================================================================
namespace goodliffe {
namespace detail {
template <unsigned ML>
inline
unsigned bit_based_skip_list_level_generator<ML>::new_level()
{
// The number of 1-bits before we encounter the first 0-bit is the level of
/// the node. Since R is 32-bit, the level can be at most 32.
assert_that(num_levels < 33);
unsigned level = 0;
for (unsigned number = unsigned(rand()); (number & 1) == 1; number >>= 1)
{
level++;
}
return level;
}
template <unsigned ML>
inline
unsigned skip_list_level_generator<ML>::new_level()
{
float f = float(std::rand())/float(RAND_MAX);
unsigned level = unsigned(std::log(f)/std::log(0.5));
return level < num_levels ? level : num_levels;
}
} // namespace detail
} // namespace goodliffe
//==============================================================================
#ifdef _MSC_VER
#pragma warning( pop )
#endif