TPIE

11a2c2d
internal_queue.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet cino+=(0 :
3 // Copyright 2010, 2011, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 #ifndef __TPIE_INTERNAL_QUEUE_H__
20 #define __TPIE_INTERNAL_QUEUE_H__
21 
26 #include <tpie/array.h>
27 #include <tpie/util.h>
28 #include <tpie/tpie_assert.h>
29 
30 namespace tpie {
31 
41 template <typename T>
42 class internal_queue: public linear_memory_base<internal_queue<T> > {
43  array<T> m_elements;
44  size_t m_first, m_last;
45 public:
51 
56  static double memory_overhead() {return array<T>::memory_overhead() - sizeof(array<T>) + sizeof(internal_queue);}
57 
64  internal_queue(size_t size=0): m_first(0), m_last(0) {m_elements.resize(size);}
65 
71  void resize(size_t size=0) {m_elements.resize(size); m_first = m_last = 0;}
72 
76  const T & front() const {tp_assert(!empty(), "front() on empty queue"); return m_elements[m_first % m_elements.size()];}
77 
81  const T & back() const {return m_elements[(m_last-1) % m_elements.size()];}
82 
88  inline void push(T val){m_elements[m_last++ % m_elements.size()] = val;}
89 
93  inline void pop(){++m_first;}
94 
99  inline bool empty() const {return m_first == m_last;}
100 
105  inline bool full() const {return m_last - m_first == m_elements.size();}
106 
111  inline size_t size() const { return m_last - m_first;}
112 
118  inline void clear(){m_first = m_last =0;}
119 }; // class internal_queue
120 
121 } // namespace tpie
122 #endif //__TPIE_INTERNAL_QUEUE_H__
123 
tpie::linear_memory_base
Base class of data structures with linear memory usage.
Definition: util.h:98
tpie_assert.h
tpie::internal_queue::front
const T & front() const
Return the item that has been in the queue for the longest time.
Definition: internal_queue.h:76
tpie::internal_queue::back
const T & back() const
Return the last item pushed to the queue.
Definition: internal_queue.h:81
tpie::internal_queue::full
bool full() const
Check if the queue is empty.
Definition: internal_queue.h:105
tpie::internal_queue
A generic internal circular queue.
Definition: internal_queue.h:42
tpie::array< T >
tpie::internal_queue::size
size_t size() const
Return the number of elements in the queue.
Definition: internal_queue.h:111
tpie::internal_queue::push
void push(T val)
Add an element to the front of the queue.
Definition: internal_queue.h:88
tpie::array::memory_overhead
static constexpr double memory_overhead() noexcept
Return the memory overhead of the structure.
Definition: array.h:405
tpie::internal_queue::memory_overhead
static double memory_overhead()
Return the memory overhead of the structure.
Definition: internal_queue.h:56
array.h
tpie::internal_queue::pop
void pop()
Remove an element from the back of the queue.
Definition: internal_queue.h:93
tpie::internal_queue::clear
void clear()
Clear the queue of all elements.
Definition: internal_queue.h:118
tp_assert
#define tp_assert(condition, message)
Definition: tpie_assert.h:65
tpie::internal_queue::empty
bool empty() const
Check if the queue is empty.
Definition: internal_queue.h:99
tpie::array::memory_coefficient
static constexpr double memory_coefficient() noexcept
Return the memory coefficient of the structure.
Definition: array.h:398
tpie::array::resize
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:490
tpie::internal_queue::internal_queue
internal_queue(size_t size=0)
Construct a queue.
Definition: internal_queue.h:64
tpie::internal_queue::resize
void resize(size_t size=0)
Resize the queue; all data is lost.
Definition: internal_queue.h:71
util.h
tpie::internal_queue::memory_coefficient
static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: internal_queue.h:50
tpie
Definition: access_type.h:26
tpie::array::size
size_type size() const
Return the size of the array.
Definition: array.h:531