TPIE

11a2c2d
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

◆ stack() [1/2]

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  }

◆ stack() [2/2]

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  }

Member Function Documentation

◆ is_empty()

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  }

◆ main_memory_usage()

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.

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 }

◆ peek()

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.

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 }

◆ persist() [1/2]

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() [2/2]

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  }

◆ pop()

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.

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 }

◆ push()

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.

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 }

◆ size()

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.

237  {
238  return m_ulate.size();
239  }

◆ stream_len()

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  }

◆ trim()

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  }

The documentation for this class was generated from the following file:
tpie::stack::memory_usage
static memory_size_type memory_usage(float blockFactor=1.0)
Compute the memory used by a stack.
Definition: stack.h:146
tpie::ami::END_OF_STREAM
@ END_OF_STREAM
An attempt was made to read past the end of a stream or write past the end of a substream.
Definition: err.h:52
tpie::STREAM_USAGE_CURRENT
@ STREAM_USAGE_CURRENT
Amount currently in use.
Definition: stream_usage.h:34
PERSIST_DELETE
PERSIST_DELETE
Delete the stream from the disk when it is destructed.
Definition: persist.h:39
tpie::ami::stack::size
TPIE_OS_OFFSET size() const
Returns the number of items currently on the stack.
Definition: stack.h:237
tpie::ami::NO_ERROR
@ NO_ERROR
No error occurred.
Definition: err.h:47
tpie::STREAM_USAGE_BUFFER
@ STREAM_USAGE_BUFFER
Max amount ever used by a buffer.
Definition: stream_usage.h:32
tpie::temp_file::set_persistent
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:229
tp_assert
#define tp_assert(condition, message)
Definition: tpie_assert.h:65
tpie::unused
void unused(const T &x)
Declare that a variable is unused on purpose.
Definition: util.h:42
tpie::ami::IO_ERROR
@ IO_ERROR
A low level I/O error occurred.
Definition: err.h:49
tpie::temp_file::is_persistent
bool is_persistent() const
Definition: tempname.h:221
PERSIST_PERSISTENT
PERSIST_PERSISTENT
Do not delete the stream from the disk when it is destructed.
Definition: persist.h:41
tpie::STREAM_USAGE_MAXIMUM
@ STREAM_USAGE_MAXIMUM
Max amount that will ever be used.
Definition: stream_usage.h:36
tpie::STREAM_USAGE_SUBSTREAM
@ STREAM_USAGE_SUBSTREAM
Maximum additional amount used by each substream created.
Definition: stream_usage.h:38
tpie::STREAM_USAGE_OVERHEAD
@ STREAM_USAGE_OVERHEAD
Overhead of the object without the buffer.
Definition: stream_usage.h:30
tpie::ami::err
err
Legacy TPIE error codes.
Definition: err.h:45