TPIE

2362a60
stack.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 2008, 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 
23 
24 #ifndef _TPIE_AMI_STACK_H
25 #define _TPIE_AMI_STACK_H
26 
27 #include <tpie/array.h>
28 #include <tpie/portability.h>
29 #include <tpie/deprecated.h>
30 #include <tpie/stream.h>
31 #include <tpie/compressed/stream.h>
32 #include <tpie/tpie_assert.h>
33 
34 namespace tpie {
35 
39 template <typename T>
40 class stack {
41 public:
42 
46  stack(double blockFactor=1.0)
47  : m_stream(blockFactor)
48  , m_buffer(buffer_size(blockFactor))
49  , m_bufferItems(0)
50  {
51  m_stream.open(static_cast<memory_size_type>(0), access_normal,
53  }
54 
61  stack(const std::string & path, double blockFactor=1.0)
62  : m_stream(blockFactor)
63  , m_buffer(buffer_size(blockFactor))
64  , m_bufferItems(0)
65  {
66  m_stream.open(path, access_read_write,
67  static_cast<memory_size_type>(0), access_normal,
69  m_stream.seek(0, file_stream_base::end);
70  }
71 
78  stack(temp_file & tempFile, double blockFactor=1.0)
79  : m_stream(blockFactor)
80  , m_buffer(buffer_size(blockFactor))
81  , m_bufferItems(0)
82  {
83  m_stream.open(tempFile, access_read_write,
84  static_cast<memory_size_type>(0), access_normal,
86  m_stream.seek(0, file_stream_base::end);
87  }
88 
89 
94  ~stack() {
95  empty_buffer();
96  m_stream.truncate(m_stream.get_position());
97  }
98 
105  inline void push(const T & t) throw(stream_exception) {
106  if (m_buffer.size() == m_bufferItems) empty_buffer();
107  m_buffer[m_bufferItems++] = t;
108  }
109 
113  inline const T & pop() throw(stream_exception) {
114  if (m_bufferItems) return m_buffer[--m_bufferItems];
115  const T & item = m_stream.read_back();
116  return item;
117  }
118 
122  inline const T & top() throw(stream_exception) {
123  if (m_bufferItems) return m_buffer[m_bufferItems-1];
124  m_buffer[0] = m_stream.read_back();
125  m_stream.read();
126  return m_buffer[0];
127  }
128 
132  stream_size_type size() const {
133  return m_stream.offset()+m_bufferItems;
134  }
135 
139  inline bool empty() const {
140  return size() == 0;
141  }
142 
146  inline static memory_size_type memory_usage(float blockFactor=1.0) {
147  return sizeof(stack<T>)
148  + file_stream<T>::memory_usage(blockFactor)
149  + array<T>::memory_usage(buffer_size(blockFactor));
150  }
151 
152 protected:
153 
156 
157 private:
158  array<T> m_buffer;
159  size_t m_bufferItems;
160 
161  inline void empty_buffer() {
162  if (m_bufferItems == 0) return;
163  m_stream.truncate(m_stream.get_position());
164  m_stream.write(m_buffer.begin(), m_buffer.begin()+m_bufferItems);
165  m_bufferItems = 0;
166  }
167 
168  static memory_size_type buffer_size(double blockFactor) {
169  return file_stream<T>::block_size(blockFactor)/sizeof(T);
170  }
171 
172 };
173 
174 namespace ami {
175 
179 template<class T>
180 class stack {
181 public:
182 
186  stack() :
187  m_ulate(m_tempFile) {
188  // Empty ctor.
189  }
190 
198  stack(const std::string& path,
199  stream_type type = READ_WRITE_STREAM)
200  : m_tempFile(path, true)
201  , m_ulate(m_tempFile)
202  {
203  unused(type);
204  }
205 
212  err push(const T &t);
213 
222  err pop(const T **t);
223 
232  err peek(const T **t);
233 
237  TPIE_OS_OFFSET size() const {
238  return m_ulate.size();
239  }
240 
244  bool is_empty() const {
245  return m_ulate.empty();
246  }
247 
253  void persist(persistence p) {
254  m_tempFile.set_persistent(p == PERSIST_PERSISTENT);
255  }
256 
261  persistence persist() const {
262  return m_tempFile.is_persistent() ? PERSIST_PERSISTENT : PERSIST_DELETE;
263  }
264 
270  err trim() {
271  return NO_ERROR;
272  }
273 
281  err main_memory_usage(TPIE_OS_SIZE_T *usage,
282  stream_usage usage_type) const;
283 
287  TPIE_OS_OFFSET stream_len() const {
288  TP_LOG_WARNING_ID("Using AMI_stack<T>::stream_len() is deprecated.");
289  return size();
290  }
291 
292  tpie::stack<T> & underlying_stack() {
293  return m_ulate;
294  }
295 private:
296 
297  temp_file m_tempFile;
298 
299  // em-ulate. get it?
300  tpie::stack<T> m_ulate;
301 };
302 
304 
305 template<class T>
306 err stack<T>::push(const T &t) {
307 
308  err retval = NO_ERROR;
309 
310  try {
311  m_ulate.push(t);
312  } catch (end_of_stream_exception &) {
313  retval = END_OF_STREAM;
314  } catch (stream_exception &) {
315  retval = IO_ERROR;
316  }
317 
318  return retval;
319 
320 }
321 
323 
324 template<class T>
325 err stack<T>::pop(const T **t) {
326  if(m_ulate.empty())
327  return END_OF_STREAM;
328 
329  err retval = NO_ERROR;
330 
331  try {
332 
333  const T & res = m_ulate.pop();
334  *t = &res;
335 
336  } catch (end_of_stream_exception &) {
337  retval = END_OF_STREAM;
338  } catch (stream_exception &) {
339  retval = IO_ERROR;
340  }
341 
342  return retval;
343 
344 }
345 
347 
348 template<class T>
349 err stack<T>::peek(const T **t) {
350  if(m_ulate.empty())
351  return END_OF_STREAM;
352 
353  err retval = NO_ERROR;
354 
355  try {
356 
357  const T & res = m_ulate.top();
358  *t = &res;
359 
360  } catch (end_of_stream_exception &) {
361  retval = END_OF_STREAM;
362  } catch (stream_exception &) {
363  retval = IO_ERROR;
364  }
365 
366  return retval;
367 
368 }
369 
371 
372 template<class T>
373 err stack<T>::main_memory_usage(TPIE_OS_SIZE_T *usage,
374  stream_usage usage_type) const {
375 
376  switch (usage_type) {
377 
378  // All these types are o.k.
383  case STREAM_USAGE_BUFFER:
384  *usage = tpie::stack<T>::memory_usage();
385  *usage += sizeof(*this);
386  break;
387 
388  default:
389  tp_assert(0, "Unknown mem::stream_usage type added.");
390  }
391 
392  return NO_ERROR;
393 }
394 
396 
397 } // ami namespace
398 
399 } // tpie namespace
400 
401 #endif // _TPIE_AMI_STACK_H
Defines the tp_assert macro.
stack()
Initializes the stack.
Definition: stack.h:186
Amount currently in use.
Definition: stream_usage.h:34
Maximum additional amount used by each substream created.
Definition: stream_usage.h:38
PERSIST_DELETE
Delete the stream from the disk when it is destructed.
Definition: persist.h:39
Max amount that will ever be used.
Definition: stream_usage.h:36
static memory_size_type memory_usage(memory_size_type size)
Return the number of bytes required to create a data structure supporting a given number of elements...
Definition: util.h:81
stack(const std::string &path, double blockFactor=1.0)
Initialize named, nontemporary stack.
Definition: stack.h:61
void unused(const T &x)
Declare that a variable is unused on purpose.
Definition: util.h:42
TPIE_OS_OFFSET stream_len() const
Definition: stack.h:287
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155
No error occurred.
Definition: err.h:47
stack(temp_file &tempFile, double blockFactor=1.0)
Initialize temporary stack.
Definition: stack.h:78
~stack()
Closes the underlying stream and truncates it to the logical end of the stack.
Definition: stack.h:94
const T & pop()
Pops one item from the stack.
Definition: stack.h:113
Macros for deprecating classes, methods and typedefs.
err push(const T &t)
Pushes one item onto the stack.
Definition: stack.h:306
An implementation of an external-memory stack.
Definition: stack.h:40
This file contains a few deprecated definitions for legacy code.
Overhead of the object without the buffer.
Definition: stream_usage.h:30
Compressed stream public API.
TPIE_OS_OFFSET size() const
Returns the number of items currently on the stack.
Definition: stack.h:237
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:230
stack(const std::string &path, stream_type type=READ_WRITE_STREAM)
Initializes the stack by (re-)opening the file given.
Definition: stack.h:198
A low level I/O error occurred.
Definition: err.h:49
Generic internal array with known memory requirements.
persistence persist() const
Returns the persistence status of the (stream underlying the) stack.
Definition: stack.h:261
stream_size_type size() const
Returns the number of items currently on the stack.
Definition: stack.h:132
void persist(persistence p)
Set the persistence status of the (stream underlying the) stack.
Definition: stack.h:253
Class representing a reference to a temporary file.
Definition: tempname.h:202
err trim()
Truncates the underlying stream to the exact size (rounded up to the next block) of items...
Definition: stack.h:270
An implementation of an external-memory stack compatible with the old AMI interface.
Definition: stack.h:180
stream_type
AMI stream types passed to constructors.
Definition: stream_old.h:53
PERSIST_PERSISTENT
Do not delete the stream from the disk when it is destructed.
Definition: persist.h:41
bool is_empty() const
Returns whether the stack is empty or not.
Definition: stack.h:244
bool is_persistent() const
Definition: tempname.h:222
err pop(const T **t)
Pops one item from the stack.
Definition: stack.h:325
bool empty() const
Returns whether the stack is empty or not.
Definition: stack.h:139
Compressed stream.
Definition: predeclare.h:46
err
Legacy TPIE error codes.
Definition: err.h:45
Compress some blocks according to available resources (time, memory).
Definition: scheme.h:40
void push(const T &t)
Pushes one item onto the stack.
Definition: stack.h:105
static memory_size_type memory_usage(float blockFactor=1.0)
Compute the memory used by a stack.
Definition: stack.h:146
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:307
stack(double blockFactor=1.0)
Initialize anonymous stack.
Definition: stack.h:46
size_type size() const
Return the size of the array.
Definition: array.h:526
const T & top()
Peeks at the topmost item on the stack.
Definition: stack.h:122
#define tp_assert(condition, message)
Definition: tpie_assert.h:48
stream_usage
Definition: stream_usage.h:28
Neither sequential access nor random access is intended.
Definition: cache_hint.h:31
An attempt was made to read past the end of a stream or write past the end of a substream.
Definition: err.h:52
err main_memory_usage(TPIE_OS_SIZE_T *usage, stream_usage usage_type) const
Compute the memory used by the stack and the aggregated stream.
Definition: stack.h:373
Max amount ever used by a buffer.
Definition: stream_usage.h:32
err peek(const T **t)
Peeks at the topmost item on the stack.
Definition: stack.h:349
Open a file for reading or writing.
Definition: access_type.h:35