Adobe Source Libraries 1.49.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
circular_queue.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifndef ADOBE_CIRCULAR_QUEUE_HPP
9#define ADOBE_CIRCULAR_QUEUE_HPP
10
11/**************************************************************************************************/
12
13#include <adobe/config.hpp>
14
16#include <adobe/iterator.hpp>
17
18#include <boost/operators.hpp>
19
20#include <cassert>
21#include <vector>
22
23/**************************************************************************************************/
24
25namespace adobe {
30
31
37
43
49
55
61
76
83
92
99
106
113
124
135
146
159
183
194
209
210
211/**************************************************************************************************/
212
213#if 1 // REVISIT (fbrereto) : Possible compiler optimization?
214#define ADOBE_NOTHROW throw()
215#else
216#define ADOBE_NOTHROW
217#endif
218
219/**************************************************************************************************/
220
221template <typename T>
222class circular_queue;
223
224template <typename T>
225bool operator==(const circular_queue<T>& x, const circular_queue<T>& y);
226
227template <typename T>
229
230/**************************************************************************************************/
231
249template <typename T>
250class circular_queue : boost::equality_comparable<circular_queue<T>> {
251public:
252 typedef T value_type;
253 typedef T* pointer;
254 typedef const T* const_pointer;
255 typedef T& reference;
256 typedef const T& const_reference;
257 typedef std::size_t size_type;
258
259 circular_queue(std::size_t capacity = 0);
260
261#if !defined(ADOBE_NO_DOCUMENTATION)
262 circular_queue(const circular_queue& rhs);
263
265#endif // !defined(ADOBE_NO_DOCUMENTATION)
266
268 size_type max_size() const ADOBE_NOTHROW { return container_m.size(); }
269 size_type capacity() const ADOBE_NOTHROW { return container_m.size(); }
270
271 bool empty() const ADOBE_NOTHROW { return is_empty_m; }
272 bool full() const ADOBE_NOTHROW { return !is_empty_m && begin_m == end_m; }
273
275 begin_m = end_m;
276 is_empty_m = true;
277 }
278
281
282 void push_back(T x);
283
285
286 void putback() ADOBE_NOTHROW;
287
288#if !defined(ADOBE_NO_DOCUMENTATION)
289private:
290 friend inline void swap(circular_queue& x, circular_queue& y) {
291 swap(x.container_m, y.container_m);
292 std::swap(x.begin_m, y.begin_m);
293 std::swap(x.end_m, y.end_m);
294 swap(x.is_empty_m, y.is_empty_m);
295 }
296
297 friend bool operator==<>(const circular_queue& x, const circular_queue& y);
298
299 typedef typename std::vector<value_type> container_t;
300 typedef typename container_t::iterator iterator;
301 typedef typename container_t::const_iterator const_iterator;
302
303 /* WARNING (sparent) : Order of members is important to initialization */
304 container_t container_m;
305 iterator begin_m;
306 iterator end_m;
307 bool is_empty_m;
308 /* WARNING (sparent) : Order of members is important to initialization */
309
310 // Note that these ranges assume non-empty.
311
312 typedef std::pair<const_iterator, const_iterator> const_range;
313
314 const_range first_range() const {
315 return const_range(begin_m,
316 begin_m < end_m ? const_iterator(end_m) : boost::end(container_m));
317 }
318
319 const_range second_range() const {
320 return const_range(begin_m < end_m ? const_iterator(end_m) : boost::begin(container_m),
321 end_m);
322 }
323
324#endif // !defined(ADOBE_NO_DOCUMENTATION)
325};
326
327/**************************************************************************************************/
328
329template <typename T>
331 : container_m(capacity), begin_m(boost::begin(container_m)), end_m(boost::begin(container_m)),
332 is_empty_m(true) {}
333
334#if !defined(ADOBE_NO_DOCUMENTATION)
335
336/**************************************************************************************************/
337
338template <typename T>
340 : container_m(rhs.capacity()), begin_m(boost::begin(container_m)),
341 end_m(boost::begin(container_m)), is_empty_m(rhs.is_empty_m) {
342 if (is_empty_m)
343 return;
344
345 end_m = copy(rhs.first_range(), end_m);
346 end_m = copy(rhs.second_range(), end_m);
347}
348
349/**************************************************************************************************/
350
351template <typename T>
353 swap(*this, rhs);
354 return *this;
355}
356
357#endif // !defined(ADOBE_NO_DOCUMENTATION)
358/**************************************************************************************************/
359
360template <typename T>
362 assert(!empty());
363 return *begin_m;
364}
365
366/**************************************************************************************************/
367
368template <typename T>
370 assert(!empty());
371 return *begin_m;
372}
373
374/**************************************************************************************************/
375
376template <typename T>
378 *end_m = std::move(x);
379
380 bool was_full(full());
381
382 if (++end_m == boost::end(container_m))
383 end_m = boost::begin(container_m);
384 if (was_full)
385 begin_m = end_m;
386
387 is_empty_m = false;
388}
389
390/**************************************************************************************************/
391
392template <typename T>
394 assert(!empty());
395 if (++begin_m == boost::end(container_m))
396 begin_m = boost::begin(container_m);
397 is_empty_m = begin_m == end_m;
398}
399
400/**************************************************************************************************/
401
402template <typename T>
404 assert(!full());
405 if (begin_m == boost::begin(container_m))
406 begin_m = boost::end(container_m);
407 --begin_m;
408 is_empty_m = false;
409}
410
411/**************************************************************************************************/
412
413template <typename T>
415 if (begin_m < end_m)
416 return std::distance(begin_m, end_m);
417
418 return is_empty_m ? 0 : capacity() - std::distance(end_m, begin_m);
419}
420
421/**************************************************************************************************/
422
423template <typename T>
425 /*
426 REVISIT (sparent) : I'm trying to move the code towards equality of containers to mean "the
427 size is the same and all the parts are the same." By making the segmented iterators part of
428 a public begin, end interface this would simply be a generic. "equal_containers()" function.
429 */
430 typedef typename circular_queue<T>::const_range const_range;
431
432 if (x.size() != y.size())
433 return false;
434
435 const_range sequence1[] = {x.first_range(), x.second_range()};
436 const_range sequence2[] = {y.first_range(), y.second_range()};
437
438 return equal(make_segmented_range(sequence1), make_segmented_iterator(sequence2));
439}
440
441/**************************************************************************************************/
442
443#undef ADOBE_NOTHROW
444
445/**************************************************************************************************/
446
447} // namespace adobe
448
449/**************************************************************************************************/
450
451#endif // ADOBE_CIRCULAR_QUEUE_HPP
452
453/**************************************************************************************************/
#define ADOBE_NOTHROW
A queue with a fixed capacity which supports putting back elements. Pushing more elements than there ...
circular_queue & operator=(circular_queue rhs)
void clear() ADOBE_NOTHROW
reference front() ADOBE_NOTHROW
size_type size() const ADOBE_NOTHROW
bool empty() const ADOBE_NOTHROW
circular_queue(std::size_t capacity=0)
void putback() ADOBE_NOTHROW
friend void swap(circular_queue &x, circular_queue &y)
void pop_front() ADOBE_NOTHROW
friend bool operator==(const circular_queue &x, const circular_queue &y)
size_type capacity() const ADOBE_NOTHROW
bool full() const ADOBE_NOTHROW
size_type max_size() const ADOBE_NOTHROW
bool empty(const any_regular_t &x)
segmented_iterator< typename boost::range_iterator< R >::type > make_segmented_iterator(R &r)
Definition iterator.hpp:220
boost::iterator_range< segmented_iterator< typename boost::range_iterator< R >::type > > make_segmented_range(R &r)
Definition iterator.hpp:211
OutputIterator copy(const InputRange &range, OutputIterator result)
copy implementation
Definition copy.hpp:42
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred)
Definition equal.hpp:36
void swap(any_regular_t &x, any_regular_t &y)
bool operator==(const any_regular_t &x, const any_regular_t &y)
void push_back(array_t &v, T x)
Definition array.hpp:28
void swap(adobe::extents_t::slice_t &x, adobe::extents_t::slice_t &y) BOOST_NOEXCEPT
Definition extents.hpp:120