TPIE

2362a60
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 
internal_queue(size_t size=0)
Construct a queue.
Defines the tp_assert macro.
bool full() const
Check if the queue is empty.
Base class of data structures with linear memory usage.
Definition: util.h:73
const T & back() const
Return the last item pushed to the queue.
const T & front() const
Return the item that has been in the queue for the longest time.
A generic internal circular queue.
size_t size() const
Return the number of elements in the queue.
static double memory_overhead()
Return the memory overhead of the structure.
Generic internal array with known memory requirements.
void clear()
Clear the queue of all elements.
void push(T val)
Add an element to the front of the queue.
void pop()
Remove an element from the back of the queue.
Miscellaneous utility functions.
static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: array.h:393
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
bool empty() const
Check if the queue is empty.
static double memory_overhead()
Return the memory overhead of the structure.
Definition: array.h:400
size_type size() const
Return the size of the array.
Definition: array.h:526
#define tp_assert(condition, message)
Definition: tpie_assert.h:48
static double memory_coefficient()
Return the memory coefficient of the structure.
void resize(size_t size=0)
Resize the queue; all data is lost.