TPIE

v1.1rc1-6-g0c97303
tpie::ami::stack< T > Class Template Reference

An implementation of an external-memory stack compatible with the old AMI interface. More...

#include <tpie/stack.h>

Public Member Functions

 stack ()
 Initializes the stack. More...
 
 stack (const std::string &path, stream_type type=READ_WRITE_STREAM)
 Initializes the stack by (re-)opening the file given. More...
 
err push (const T &t)
 Pushes one item onto the stack. More...
 
err pop (const T **t)
 Pops one item from the stack. More...
 
err peek (const T **t)
 Peeks at the topmost item on the stack. More...
 
TPIE_OS_OFFSET size () const
 Returns the number of items currently on the stack. More...
 
bool is_empty () const
 Returns whether the stack is empty or not. More...
 
void persist (persistence p)
 Set the persistence status of the (stream underlying the) stack. More...
 
persistence persist () const
 Returns the persistence status of the (stream underlying the) stack. More...
 
err trim ()
 Truncates the underlying stream to the exact size (rounded up to the next block) of items. More...
 
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. More...
 
TPIE_OS_OFFSET stream_len () const
 

Detailed Description

template<class T>
class tpie::ami::stack< T >

An implementation of an external-memory stack compatible with the old AMI interface.

Definition at line 176 of file stack.h.

Constructor & Destructor Documentation

template<class T >
tpie::ami::stack< T >::stack ( )
inline

Initializes the stack.

Definition at line 182 of file stack.h.

182  :
183  m_ulate(m_tempFile) {
184  // Empty ctor.
185  }
template<class T >
tpie::ami::stack< T >::stack ( const std::string &  path,
stream_type  type = READ_WRITE_STREAM 
)
inline

Initializes the stack by (re-)opening the file given.

Parameters
pathThe path to a file used for storing the items.
typeAn stream_type that indicates the read/write mode of the file.

Definition at line 194 of file stack.h.

References tpie::unused().

196  : m_tempFile(path, true)
197  , m_ulate(m_tempFile)
198  {
199  unused(type);
200  }
void unused(const T &x)
Declare that a variable is unused on purpose.
Definition: util.h:42

Member Function Documentation

template<class T >
bool tpie::ami::stack< T >::is_empty ( ) const
inline

Returns whether the stack is empty or not.

Definition at line 240 of file stack.h.

240  {
241  return m_ulate.empty();
242  }
template<class T >
err tpie::ami::stack< T >::main_memory_usage ( TPIE_OS_SIZE_T *  usage,
stream_usage  usage_type 
) const

Compute the memory used by the stack and the aggregated stream.

Parameters
usageWhere the usage will be stored.
usage_typeThe type of usage_type inquired from the stream.

Definition at line 362 of file stack.h.

References tpie::stack< T >::memory_usage(), tpie::ami::NO_ERROR, tpie::STREAM_USAGE_BUFFER, tpie::STREAM_USAGE_CURRENT, tpie::STREAM_USAGE_MAXIMUM, tpie::STREAM_USAGE_OVERHEAD, tpie::STREAM_USAGE_SUBSTREAM, and tp_assert.

363  {
364 
365  switch (usage_type) {
366 
367  // All these types are o.k.
372  case STREAM_USAGE_BUFFER:
373  *usage = tpie::stack<T>::memory_usage();
374  *usage += sizeof(*this);
375  break;
376 
377  default:
378  tp_assert(0, "Unknown mem::stream_usage type added.");
379  }
380 
381  return NO_ERROR;
382 }
Amount currently in use.
Definition: stream_usage.h:34
Maximum additional amount used by each substream created.
Definition: stream_usage.h:38
Max amount that will ever be used.
Definition: stream_usage.h:36
No error occurred.
Definition: err.h:47
Overhead of the object without the buffer.
Definition: stream_usage.h:30
static memory_size_type memory_usage(float blockFactor=1.0)
Compute the memory used by a stack.
Definition: stack.h:143
#define tp_assert(condition, message)
Definition: tpie_assert.h:47
Max amount ever used by a buffer.
Definition: stream_usage.h:32
template<class T >
err tpie::ami::stack< T >::peek ( const T **  t)

Peeks at the topmost item on the stack.

Returns ERROR_* as given by the underlying stream or END_OF_STREAM if the stack is empty.

Parameters
tA pointer to a pointer that will point to the topmost item.

Definition at line 340 of file stack.h.

References tpie::ami::END_OF_STREAM, tpie::ami::IO_ERROR, and tpie::ami::NO_ERROR.

340  {
341 
342  err retval = NO_ERROR;
343 
344  try {
345 
346  const T & res = m_ulate.top();
347  *t = &res;
348 
349  } catch (end_of_stream_exception &) {
350  retval = END_OF_STREAM;
351  } catch (stream_exception &) {
352  retval = IO_ERROR;
353  }
354 
355  return retval;
356 
357 }
No error occurred.
Definition: err.h:47
A low level I/O error occurred.
Definition: err.h:49
err
Legacy TPIE error codes.
Definition: err.h:45
An attempt was made to read past the end of a stream or write past the end of a substream.
Definition: err.h:52
template<class T >
void tpie::ami::stack< T >::persist ( persistence  p)
inline

Set the persistence status of the (stream underlying the) stack.

Parameters
pA persistence status.

Definition at line 249 of file stack.h.

References PERSIST_PERSISTENT, and tpie::temp_file::set_persistent().

249  {
250  m_tempFile.set_persistent(p == PERSIST_PERSISTENT);
251  }
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:168
PERSIST_PERSISTENT
Do not delete the stream from the disk when it is destructed.
Definition: persist.h:41
template<class T >
persistence tpie::ami::stack< T >::persist ( ) const
inline

Returns the persistence status of the (stream underlying the) stack.

Definition at line 257 of file stack.h.

References tpie::temp_file::is_persistent(), PERSIST_DELETE, and PERSIST_PERSISTENT.

257  {
258  return m_tempFile.is_persistent() ? PERSIST_PERSISTENT : PERSIST_DELETE;
259  }
PERSIST_DELETE
Delete the stream from the disk when it is destructed.
Definition: persist.h:39
PERSIST_PERSISTENT
Do not delete the stream from the disk when it is destructed.
Definition: persist.h:41
bool is_persistent() const
Definition: tempname.h:162
template<class T >
err tpie::ami::stack< T >::pop ( const T **  t)

Pops one item from the stack.

Returns ERROR_* as given by the underlying stream or END_OF_STREAM if the stack is empty.

Parameters
tA pointer to a pointer that will point to the topmost item.

Definition at line 318 of file stack.h.

References tpie::ami::END_OF_STREAM, tpie::ami::IO_ERROR, and tpie::ami::NO_ERROR.

318  {
319 
320  err retval = NO_ERROR;
321 
322  try {
323 
324  const T & res = m_ulate.pop();
325  *t = &res;
326 
327  } catch (end_of_stream_exception &) {
328  retval = END_OF_STREAM;
329  } catch (stream_exception &) {
330  retval = IO_ERROR;
331  }
332 
333  return retval;
334 
335 }
No error occurred.
Definition: err.h:47
A low level I/O error occurred.
Definition: err.h:49
err
Legacy TPIE error codes.
Definition: err.h:45
An attempt was made to read past the end of a stream or write past the end of a substream.
Definition: err.h:52
template<class T >
err tpie::ami::stack< T >::push ( const T &  t)

Pushes one item onto the stack.

Returns ERROR_* as given by the underlying stream.

Parameters
tThe item to be pushed onto the stack.

Definition at line 299 of file stack.h.

References tpie::ami::END_OF_STREAM, tpie::ami::IO_ERROR, and tpie::ami::NO_ERROR.

299  {
300 
301  err retval = NO_ERROR;
302 
303  try {
304  m_ulate.push(t);
305  } catch (end_of_stream_exception &) {
306  retval = END_OF_STREAM;
307  } catch (stream_exception &) {
308  retval = IO_ERROR;
309  }
310 
311  return retval;
312 
313 }
No error occurred.
Definition: err.h:47
A low level I/O error occurred.
Definition: err.h:49
err
Legacy TPIE error codes.
Definition: err.h:45
An attempt was made to read past the end of a stream or write past the end of a substream.
Definition: err.h:52
template<class T >
TPIE_OS_OFFSET tpie::ami::stack< T >::size ( ) const
inline

Returns the number of items currently on the stack.

Definition at line 233 of file stack.h.

Referenced by tpie::ami::stack< T >::stream_len().

233  {
234  return m_ulate.size();
235  }
template<class T >
TPIE_OS_OFFSET tpie::ami::stack< T >::stream_len ( ) const
inline

Definition at line 283 of file stack.h.

References tpie::ami::stack< T >::size().

283  {
284  TP_LOG_WARNING_ID("Using AMI_stack<T>::stream_len() is deprecated.");
285  return size();
286  }
TPIE_OS_OFFSET size() const
Returns the number of items currently on the stack.
Definition: stack.h:233
template<class T >
err tpie::ami::stack< T >::trim ( )
inline

Truncates the underlying stream to the exact size (rounded up to the next block) of items.

In the current implementation, this does nothing.

Definition at line 266 of file stack.h.

References tpie::ami::NO_ERROR.

266  {
267  return NO_ERROR;
268  }
No error occurred.
Definition: err.h:47

The documentation for this class was generated from the following file: