TPIE

v1.1rc1-6-g0c97303
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 :
3 // Copyright 2008, 2010, 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 
20 #ifndef __TPIE_QUEUE_H__
21 #define __TPIE_QUEUE_H__
22 
27 #include <tpie/portability.h>
28 #include <tpie/deprecated.h>
29 #include <tpie/file.h>
30 #include <tpie/err.h>
31 #include <tpie/tempname.h>
32 #include <tpie/file.h>
33 #include <limits>
34 #include <tpie/persist.h>
35 namespace tpie {
36 
41 template<class T>
42 class queue {
43 public:
47  queue(stream_size_type elements=std::numeric_limits<stream_size_type>::max(),
48  double block_factor=1.0): m_size(0), m_file(block_factor) {
49  m_file.open(m_temp, access_read_write, sizeof(stream_size_type) );
50  m_back.attach(m_file);
51  m_front.attach(m_file);
52  unused(elements);
53  }
54 
60  void resize(stream_size_type) {}
61 
66  queue(const std::string& basename, double block_factor=1.0);
67 
71  ~queue() {m_file.write_user_data(m_back.offset());}
72 
77  inline bool empty() {return m_size == 0;}
78  TPIE_DEPRECATED(bool is_empty());
79 
84  inline stream_size_type size() {return m_size;}
85 
90  inline void push(const T & t) {
91  m_back.write(t);
92  ++m_size;
93  }
94  TPIE_DEPRECATED(ami::err enqueue(const T &t));
95 
100  const T & pop() {
101  const T & el = m_front.read();
102  --m_size;
103  return el;
104  }
105  TPIE_DEPRECATED(ami::err dequeue(const T **t));
106 
111  const T & front() {
112  const T & el = m_front.read();
113  m_front.seek(-1, file_base::stream::current);
114  return el;
115  }
116  TPIE_DEPRECATED(ami::err peek(const T **t));
117 
121  TPIE_DEPRECATED(ami::err trim());
122 
126  static memory_size_type memory_usage(double blockFactor=1.0) {
127  return sizeof(queue<T>)
128  + file<T>::memory_usage() - sizeof(file<T>)
129  + 2*file<T>::stream::memory_usage(blockFactor) - 2*sizeof(typename file<T>::stream);
130  }
131 
136  TPIE_DEPRECATED(void persist(persistence p));
137 private:
138  temp_file m_temp;
139  stream_size_type m_size;
140  file<T> m_file;
141  typename file<T>::stream m_back;
142  typename file<T>::stream m_front;
143 };
144 
145 
146 template<class T>
147 void queue<T>::persist(persistence p) {
148  m_temp.set_persistent(p != PERSIST_DELETE);
149 }
150 
151 template<class T>
152 queue<T>::queue(const std::string& basename, double blockFactor):
153  m_temp(basename), m_size(0), m_file(blockFactor), m_back(m_file), m_front(m_file) {
154  m_temp.set_persistent(true);
155  m_file.open(basename, access_read_write, sizeof(stream_size_type) );
156  if (m_file.size() != 0) {
157  stream_size_type t;
158  m_file.read_user_data(t);
159  m_front.seek(t);
160  }
161  m_back.seek(0, file_base::stream::end);
162  m_size = m_back.offset() - m_front.offset();
163 }
164 
165 
167 
168 template<class T>
169 bool queue<T>::is_empty() {return empty();}
170 
172 
173 template<class T>
174 ami::err queue<T>::enqueue(const T &t) {
175  push(t);
176  return ami::NO_ERROR;
177 }
178 
180 
181 template<class T>
182 ami::err queue<T>::dequeue(const T **t) {
183  *t = &pop();
184  return ami::NO_ERROR;
185 }
186 
188 
189 template<class T>
190 ami::err queue<T>::peek(const T **t) {
191  *t = &front();
192  return ami::NO_ERROR;
193 }
194 
195 namespace ami {
196  TPIE_DEPRECATED_CLASS_A(
197  template <typename T>
198  class TPIE_DEPRECATED_CLASS_B queue: public tpie::queue<T> {
199  public:
200  queue() {}
201  queue(const std::string& basename): tpie::queue<T>(basename) {}
202  }
203  );
204 }
205 } // tpie namespace
206 #endif // __TPIE_QUEUE_H__
Central stream abstraction.
Definition: file.h:76
PERSIST_DELETE
Delete the stream from the disk when it is destructed.
Definition: persist.h:39
void unused(const T &x)
Declare that a variable is unused on purpose.
Definition: util.h:42
No error occurred.
Definition: err.h:47
Central file abstraction.
Definition: file.h:44
Macros for deprecating classes, methods and typedefs.
This file contains a few deprecated definitions for legacy code.
Streams that support substreams.
~queue()
Destructor, closes the underlying stream.
Definition: queue.h:71
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:168
queue(stream_size_type elements=std::numeric_limits< stream_size_type >::max(), double block_factor=1.0)
Constructor for Temporary Queue.
Definition: queue.h:47
stream_size_type size()
Returns the number of items currently on the queue.
Definition: queue.h:84
const T & pop()
Dequeues an item.
Definition: queue.h:100
bool empty()
Check if the queue is empty.
Definition: queue.h:77
static memory_size_type memory_usage(double blockFactor=1.0)
Compute the memory used by the queue.
Definition: queue.h:126
Class representing the existence of a temporary file.
Definition: tempname.h:152
void resize(stream_size_type)
Does nothing.
Definition: queue.h:60
Legacy AMI error types.
Persistence tags for deprecated TPIE AMI streams.
void push(const T &t)
Enqueue an item.
Definition: queue.h:90
Basic Implementation of I/O Efficient FIFO queue.
Definition: queue.h:42
err
Legacy TPIE error codes.
Definition: err.h:45
Temporary file names.
const T & front()
Returns at the frontmost item in the queue.
Definition: queue.h:111
Open a file for reading or writing.
Definition: access_type.h:35