-
Notifications
You must be signed in to change notification settings - Fork 115
/
circular_int.hh
146 lines (134 loc) · 4.02 KB
/
circular_int.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2014 President and Fellows of Harvard College
* Copyright (c) 2012-2014 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef KVDB_CIRCULAR_INT_HH
#define KVDB_CIRCULAR_INT_HH 1
#include "compiler.hh"
template <typename T>
class circular_int {
public:
typedef typename mass::make_unsigned<T>::type value_type;
typedef typename mass::make_signed<T>::type difference_type;
circular_int()
: v_() {
}
circular_int(T x)
: v_(x) {
}
value_type value() const {
return v_;
}
circular_int<T> &operator++() {
++v_;
return *this;
}
circular_int<T> operator++(int) {
++v_;
return circular_int<T>(v_ - 1);
}
circular_int<T> &operator--() {
--v_;
return *this;
}
circular_int<T> operator--(int) {
--v_;
return circular_int<T>(v_ + 1);
}
circular_int<T> &operator+=(unsigned x) {
v_ += x;
return *this;
}
circular_int<T> &operator+=(int x) {
v_ += x;
return *this;
}
circular_int<T> &operator-=(unsigned x) {
v_ -= x;
return *this;
}
circular_int<T> &operator-=(int x) {
v_ -= x;
return *this;
}
circular_int<T> cmpxchg(circular_int<T> expected, circular_int<T> desired) {
return ::cmpxchg(&v_, expected.v_, desired.v_);
}
circular_int<T> cmpxchg(T expected, T desired) {
return ::cmpxchg(&v_, expected, desired);
}
typedef value_type (circular_int<T>::*unspecified_bool_type)() const;
operator unspecified_bool_type() const {
return v_ != 0 ? &circular_int<T>::value : 0;
}
bool operator!() const {
return v_ == 0;
}
circular_int<T> operator+(unsigned x) const {
return circular_int<T>(v_ + x);
}
circular_int<T> operator+(int x) const {
return circular_int<T>(v_ + x);
}
circular_int<T> next_nonzero() const {
value_type v = v_ + 1;
return circular_int<T>(v + !v);
}
static value_type next_nonzero(value_type x) {
++x;
return x + !x;
}
circular_int<T> operator-(unsigned x) const {
return circular_int<T>(v_ - x);
}
circular_int<T> operator-(int x) const {
return circular_int<T>(v_ - x);
}
difference_type operator-(circular_int<T> x) const {
return v_ - x.v_;
}
bool operator==(circular_int<T> x) const {
return v_ == x.v_;
}
bool operator!=(circular_int<T> x) const {
return !(*this == x);
}
static bool less(value_type a, value_type b) {
return difference_type(a - b) < 0;
}
static bool less_equal(value_type a, value_type b) {
return difference_type(a - b) <= 0;
}
bool operator<(circular_int<T> x) const {
return less(v_, x.v_);
}
bool operator<=(circular_int<T> x) const {
return !less(x.v_, v_);
}
bool operator>=(circular_int<T> x) const {
return !less(v_, x.v_);
}
bool operator>(circular_int<T> x) const {
return less(x.v_, v_);
}
private:
value_type v_;
};
typedef circular_int<uint64_t> kvepoch_t;
template <typename T>
inline circular_int<T> cmpxchg(circular_int<T> *object, circular_int<T> expected,
circular_int<T> desired) {
return object->cmpxchg(expected, desired);
}
#endif