TPIE

2362a60
tpie::stack< T > Class Template Reference

An implementation of an external-memory stack. More...

#include <tpie/stack.h>

Public Member Functions

 stack (double blockFactor=1.0)
 Initialize anonymous stack. More...
 
 stack (const std::string &path, double blockFactor=1.0)
 Initialize named, nontemporary stack. More...
 
 stack (temp_file &tempFile, double blockFactor=1.0)
 Initialize temporary stack. More...
 
 ~stack ()
 Closes the underlying stream and truncates it to the logical end of the stack. More...
 
void push (const T &t) throw (stream_exception)
 Pushes one item onto the stack. More...
 
const T & pop () throw (stream_exception)
 Pops one item from the stack. More...
 
const T & top () throw (stream_exception)
 Peeks at the topmost item on the stack. More...
 
stream_size_type size () const
 Returns the number of items currently on the stack. More...
 
bool empty () const
 Returns whether the stack is empty or not. More...
 

Static Public Member Functions

static memory_size_type memory_usage (float blockFactor=1.0)
 Compute the memory used by a stack. More...
 

Protected Attributes

file_stream< T > m_stream
 The stream used to store the items. More...
 

Detailed Description

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

An implementation of an external-memory stack.

Definition at line 40 of file stack.h.

Constructor & Destructor Documentation

template<typename T>
tpie::stack< T >::stack ( double  blockFactor = 1.0)
inline

Initialize anonymous stack.

Definition at line 46 of file stack.h.

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  }
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155
Compress some blocks according to available resources (time, memory).
Definition: scheme.h:40
Neither sequential access nor random access is intended.
Definition: cache_hint.h:31
template<typename T>
tpie::stack< T >::stack ( const std::string &  path,
double  blockFactor = 1.0 
)
inline

Initialize named, nontemporary stack.

Parameters
pathThe path to a file used for storing the items.
blockFactorThe block factor to use

Definition at line 61 of file stack.h.

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  }
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155
Compress some blocks according to available resources (time, memory).
Definition: scheme.h:40
Neither sequential access nor random access is intended.
Definition: cache_hint.h:31
Open a file for reading or writing.
Definition: access_type.h:35
template<typename T>
tpie::stack< T >::stack ( temp_file tempFile,
double  blockFactor = 1.0 
)
inline

Initialize temporary stack.

Parameters
tempFileThe temporary file containing the stack
blockFactorThe block factor to use

Definition at line 78 of file stack.h.

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  }
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155
Compress some blocks according to available resources (time, memory).
Definition: scheme.h:40
Neither sequential access nor random access is intended.
Definition: cache_hint.h:31
Open a file for reading or writing.
Definition: access_type.h:35
template<typename T>
tpie::stack< T >::~stack ( )
inline

Closes the underlying stream and truncates it to the logical end of the stack.

Definition at line 94 of file stack.h.

94  {
95  empty_buffer();
96  m_stream.truncate(m_stream.get_position());
97  }
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155

Member Function Documentation

template<typename T>
bool tpie::stack< T >::empty ( ) const
inline

Returns whether the stack is empty or not.

m_file_stream.can_read_back();

Definition at line 139 of file stack.h.

Referenced by tpie::pipelining::bits::reverser_pull_output_t< T >::can_pull(), and tpie::pipelining::bits::reverser_output_t< dest_t >::go().

139  {
140  return size() == 0;
141  }
stream_size_type size() const
Returns the number of items currently on the stack.
Definition: stack.h:132
template<typename T>
static memory_size_type tpie::stack< T >::memory_usage ( float  blockFactor = 1.0)
inlinestatic

Compute the memory used by a stack.

Definition at line 146 of file stack.h.

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

146  {
147  return sizeof(stack<T>)
148  + file_stream<T>::memory_usage(blockFactor)
149  + array<T>::memory_usage(buffer_size(blockFactor));
150  }
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
template<typename T>
const T& tpie::stack< T >::pop ( )
throw (stream_exception
)
inline

Pops one item from the stack.

Definition at line 113 of file stack.h.

Referenced by tpie::pipelining::bits::reverser_output_t< dest_t >::go(), and tpie::pipelining::bits::reverser_pull_output_t< T >::pull().

113  {
114  if (m_bufferItems) return m_buffer[--m_bufferItems];
115  const T & item = m_stream.read_back();
116  return item;
117  }
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155
template<typename T>
void tpie::stack< T >::push ( const T &  t)
throw (stream_exception
)
inline

Pushes one item onto the stack.

Returns ERROR_* as given by the underlying stream.

Parameters
tThe item to push onto the stack.

Definition at line 105 of file stack.h.

105  {
106  if (m_buffer.size() == m_bufferItems) empty_buffer();
107  m_buffer[m_bufferItems++] = t;
108  }
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T>
stream_size_type tpie::stack< T >::size ( ) const
inline

Returns the number of items currently on the stack.

Definition at line 132 of file stack.h.

Referenced by tpie::stack< item_type >::empty().

132  {
133  return m_stream.offset()+m_bufferItems;
134  }
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155
template<typename T>
const T& tpie::stack< T >::top ( )
throw (stream_exception
)
inline

Peeks at the topmost item on the stack.

Definition at line 122 of file stack.h.

122  {
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  }
file_stream< T > m_stream
The stream used to store the items.
Definition: stack.h:155

Member Data Documentation

template<typename T>
file_stream<T> tpie::stack< T >::m_stream
protected

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