TPIE

2362a60
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
 
tpie::stack< T > & underlying_stack ()
 

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 180 of file stack.h.

Constructor & Destructor Documentation

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

Initializes the stack.

Definition at line 186 of file stack.h.

186  :
187  m_ulate(m_tempFile) {
188  // Empty ctor.
189  }
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 198 of file stack.h.

200  : m_tempFile(path, true)
201  , m_ulate(m_tempFile)
202  {
203  unused(type);
204  }
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 244 of file stack.h.

244  {
245  return m_ulate.empty();
246  }
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 373 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.

374  {
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 }
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:146
#define tp_assert(condition, message)
Definition: tpie_assert.h:48
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 349 of file stack.h.

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

349  {
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 }
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 253 of file stack.h.

253  {
254  m_tempFile.set_persistent(p == PERSIST_PERSISTENT);
255  }
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:230
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 261 of file stack.h.

261  {
262  return m_tempFile.is_persistent() ? PERSIST_PERSISTENT : PERSIST_DELETE;
263  }
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:222
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 325 of file stack.h.

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

Referenced by tpie::pipelining::bits::ami_input_stack_t< dest_t >::go().

325  {
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 }
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 306 of file stack.h.

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

306  {
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 }
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 237 of file stack.h.

Referenced by tpie::pipelining::bits::ami_input_stack_t< dest_t >::propagate(), tpie::pipelining::bits::ami_pull_input_stack_t< T >::propagate(), and tpie::ami::stack< item_type >::stream_len().

237  {
238  return m_ulate.size();
239  }
template<class T>
TPIE_OS_OFFSET tpie::ami::stack< T >::stream_len ( ) const
inline

Definition at line 287 of file stack.h.

287  {
288  TP_LOG_WARNING_ID("Using AMI_stack<T>::stream_len() is deprecated.");
289  return size();
290  }
TPIE_OS_OFFSET size() const
Returns the number of items currently on the stack.
Definition: stack.h:237
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 270 of file stack.h.

270  {
271  return NO_ERROR;
272  }
No error occurred.
Definition: err.h:47

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