blob: e0250137f28e36e9bc006888c7f93fa1df64427d (
plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
// Copyright (C) 2008-2018 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
//[mitchell02_simple_queue
#include <boost/contract.hpp>
#include <boost/optional.hpp>
#include <vector>
#include <cassert>
template<typename T>
class simple_queue
#define BASES private boost::contract::constructor_precondition< \
simple_queue<T> >
: BASES
{
friend class boost::contract::access;
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
void invariant() const {
BOOST_CONTRACT_ASSERT(count() >= 0); // Non-negative count.
}
public:
/* Creation */
// Create empty queue.
explicit simple_queue(int a_capacity) :
boost::contract::constructor_precondition<simple_queue>([&] {
BOOST_CONTRACT_ASSERT(a_capacity > 0); // Positive capacity.
})
{
boost::contract::check c = boost::contract::constructor(this)
.postcondition([&] {
// Capacity set.
BOOST_CONTRACT_ASSERT(capacity() == a_capacity);
BOOST_CONTRACT_ASSERT(is_empty()); // Empty.
})
;
items_.reserve(a_capacity);
}
// Destroy queue.
virtual ~simple_queue() {
// Check invariants.
boost::contract::check c = boost::contract::destructor(this);
}
/* Basic Queries */
// Items in queue (in their order).
// (Somewhat exposes implementation but allows to check more contracts.)
std::vector<T> const& items() const {
// Check invariants.
boost::contract::check c = boost::contract::public_function(this);
return items_;
}
// Max number of items queue can hold.
int capacity() const {
// Check invariants.
boost::contract::check c = boost::contract::public_function(this);
return items_.capacity();
}
/* Derived Queries */
// Number of items.
int count() const {
int result;
boost::contract::check c = boost::contract::public_function(this)
.postcondition([&] {
// Return items count.
BOOST_CONTRACT_ASSERT(result == int(items().size()));
})
;
return result = items_.size();
}
// Item at head.
T const& head() const {
boost::optional<T const&> result;
boost::contract::check c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(!is_empty()); // Not empty.
})
.postcondition([&] {
// Return item on top.
BOOST_CONTRACT_ASSERT(*result == items().at(0));
})
;
return *(result = items_.at(0));
}
// If queue contains no item.
bool is_empty() const {
bool result;
boost::contract::check c = boost::contract::public_function(this)
.postcondition([&] {
// Consistent with count.
BOOST_CONTRACT_ASSERT(result == (count() == 0));
})
;
return result = (items_.size() == 0);
}
// If queue has no room for another item.
bool is_full() const {
bool result;
boost::contract::check c = boost::contract::public_function(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT( // Consistent with size and capacity.
result == (capacity() == int(items().size())));
})
;
return result = (items_.size() == items_.capacity());
}
/* Commands */
// Remove head itme and shift all other items.
void remove() {
// Expensive all_equal postcond. and old_items copy might be skipped.
boost::contract::old_ptr<std::vector<T> > old_items;
#ifdef BOOST_CONTRACT_AUDIITS
= BOOST_CONTRACT_OLDOF(items())
#endif // Else, leave old pointer null...
;
boost::contract::old_ptr<int> old_count = BOOST_CONTRACT_OLDOF(count());
boost::contract::check c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(!is_empty()); // Not empty.
})
.postcondition([&] {
BOOST_CONTRACT_ASSERT(count() == *old_count - 1); // Count dec.
// ...following skipped #ifndef AUDITS.
if(old_items) all_equal(items(), *old_items, /* shifted = */ 1);
})
;
items_.erase(items_.begin());
}
// Add item to tail.
void put(T const& item) {
// Expensive all_equal postcond. and old_items copy might be skipped.
boost::contract::old_ptr<std::vector<T> > old_items;
#ifdef BOOST_CONTRACT_AUDITS
= BOOST_CONTRACT_OLDOF(items())
#endif // Else, leave old pointer null...
;
boost::contract::old_ptr<int> old_count = BOOST_CONTRACT_OLDOF(count());
boost::contract::check c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(count() < capacity()); // Room for add.
})
.postcondition([&] {
BOOST_CONTRACT_ASSERT(count() == *old_count + 1); // Count inc.
// Second to last item.
BOOST_CONTRACT_ASSERT(items().at(count() - 1) == item);
// ...following skipped #ifndef AUDITS.
if(old_items) all_equal(items(), *old_items);
})
;
items_.push_back(item);
}
private:
// Contract helper.
static bool all_equal(std::vector<T> const& left,
std::vector<T> const& right, unsigned offset = 0) {
boost::contract::check c = boost::contract::function()
.precondition([&] {
// Correct offset.
BOOST_CONTRACT_ASSERT(right.size() == left.size() + offset);
})
;
for(unsigned i = offset; i < right.size(); ++i) {
if(left.at(i - offset) != right.at(i)) return false;
}
return true;
}
std::vector<T> items_;
};
int main() {
simple_queue<int> q(10);
q.put(123);
q.put(456);
assert(q.capacity() == 10);
assert(q.head() == 123);
assert(!q.is_empty());
assert(!q.is_full());
std::vector<int> const& items = q.items();
assert(items.at(0) == 123);
assert(items.at(1) == 456);
q.remove();
assert(q.count() == 1);
return 0;
}
//]
|