TPIE

v1.1rc1-6-g0c97303
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 block_factor=1.0)
 Initialize named, nontemporary stack. More...
 
 stack (temp_file &tempFile, double block_factor=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_file_stream
 The file_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 39 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 45 of file stack.h.

46  : m_file_stream(blockFactor)
47  , m_buffer(buffer_size(blockFactor))
48  , m_bufferItems(0)
49  {
50  m_file_stream.open(static_cast<memory_size_type>(0), access_normal);
51  }
file_stream< T > m_file_stream
The file_stream used to store the items.
Definition: stack.h:152
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  block_factor = 1.0 
)
inline

Initialize named, nontemporary stack.

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

Definition at line 59 of file stack.h.

60  : m_file_stream(block_factor)
61  , m_buffer(buffer_size(block_factor))
62  , m_bufferItems(0)
63  {
64  m_file_stream.open(path, access_read_write, static_cast<memory_size_type>(0), access_normal);
65 
66  m_file_stream.seek(0, file_stream_base::end);
67  }
file_stream< T > m_file_stream
The file_stream used to store the items.
Definition: stack.h:152
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  block_factor = 1.0 
)
inline

Initialize temporary stack.

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

Definition at line 75 of file stack.h.

76  : m_file_stream(block_factor)
77  , m_buffer(buffer_size(block_factor))
78  , m_bufferItems(0)
79  {
80  m_file_stream.open(tempFile, access_read_write,
81  static_cast<memory_size_type>(0), access_normal);
82 
83  m_file_stream.seek(0, file_stream_base::end);
84  }
file_stream< T > m_file_stream
The file_stream used to store the items.
Definition: stack.h:152
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 91 of file stack.h.

91  {
92  empty_buffer();
93  m_file_stream.truncate(this->size());
94  }
file_stream< T > m_file_stream
The file_stream used to store the items.
Definition: stack.h:152
stream_size_type size() const
Returns the number of items currently on the stack.
Definition: stack.h:129

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

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

136  {
137  return size() == 0;
138  }
stream_size_type size() const
Returns the number of items currently on the stack.
Definition: stack.h:129
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 143 of file stack.h.

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

143  {
144  return sizeof(stack<T>)
145  + file_stream<T>::memory_usage(blockFactor)
146  + array<T>::memory_usage(buffer_size(blockFactor));
147  }
static stream_size_type memory_usage(stream_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 110 of file stack.h.

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

110  {
111  if (m_bufferItems) return m_buffer[--m_bufferItems];
112  const T & item = m_file_stream.read_back();
113  return item;
114  }
file_stream< T > m_file_stream
The file_stream used to store the items.
Definition: stack.h:152
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 102 of file stack.h.

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

Returns the number of items currently on the stack.

Definition at line 129 of file stack.h.

Referenced by tpie::stack< item_type >::empty(), tpie::pipelining::bits::reverser_output_t< dest_t >::propagate(), and tpie::stack< item_type >::~stack().

129  {
130  return m_file_stream.offset()+m_bufferItems;
131  }
file_stream< T > m_file_stream
The file_stream used to store the items.
Definition: stack.h:152
template<typename T>
const T& tpie::stack< T >::top ( )
throw (stream_exception
)
inline

Peeks at the topmost item on the stack.

Definition at line 119 of file stack.h.

119  {
120  if (m_bufferItems) return m_buffer[m_bufferItems-1];
121  m_buffer[0] = m_file_stream.read_back();
122  m_file_stream.read();
123  return m_buffer[0];
124  }
file_stream< T > m_file_stream
The file_stream used to store the items.
Definition: stack.h:152

Member Data Documentation

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

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