Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 390 lines (317 sloc) 12.79 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
// vi:set ts=4 sts=4 sw=4 noet cino+=(0 :
// Copyright 2008, 2011, The TPIE development team
//
// This file is part of TPIE.
//
// TPIE is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with TPIE. If not, see <http://www.gnu.org/licenses/>

///////////////////////////////////////////////////////////////////////////////
/// \file stack.h External memory stack
///////////////////////////////////////////////////////////////////////////////

#ifndef _TPIE_AMI_STACK_H
#define _TPIE_AMI_STACK_H

#include <tpie/array.h>
#include <tpie/portability.h>
#include <tpie/deprecated.h>
#include <tpie/stream.h>
#include <tpie/tpie_assert.h>

namespace tpie {

///////////////////////////////////////////////////////////////////
/// \brief An implementation of an external-memory stack.
///////////////////////////////////////////////////////////////////
template <typename T>
class stack {
public:

    ////////////////////////////////////////////////////////////////////
    /// \brief Initialize anonymous stack.
    ////////////////////////////////////////////////////////////////////
inline stack(double blockFactor = 1.0)
: m_file_stream(blockFactor)
, m_buffer(buffer_size(blockFactor))
, m_bufferItems(0)
{
m_file_stream.open(static_cast<memory_size_type>(0), access_normal);
}

    ////////////////////////////////////////////////////////////////////
    /// \brief Initialize named, nontemporary stack.
    ///
    /// \param path The path to a file used for storing the items.
    /// \param block_factor The block factor to use
    ////////////////////////////////////////////////////////////////////
inline stack(const std::string& path, double block_factor = 1.0)
: m_file_stream(block_factor)
, m_buffer(buffer_size(block_factor))
, m_bufferItems(0)
{
m_file_stream.open(path, access_read_write, static_cast<memory_size_type>(0), access_normal);

m_file_stream.seek(0, file_stream_base::end);
}

    ////////////////////////////////////////////////////////////////////
    /// \brief Initialize temporary stack.
    ///
    /// \param tempFile The temporary file containing the stack
    /// \param block_factor The block factor to use
    ////////////////////////////////////////////////////////////////////
inline stack(temp_file & tempFile, double block_factor = 1.0)
: m_file_stream(block_factor)
, m_buffer(buffer_size(block_factor))
, m_bufferItems(0)
{
m_file_stream.open(tempFile, access_read_write,
static_cast<memory_size_type>(0), access_normal);

m_file_stream.seek(0, file_stream_base::end);
}


    ////////////////////////////////////////////////////////////////////
    /// \brief Closes the underlying stream and truncates it to the logical
    /// end of the stack.
    ////////////////////////////////////////////////////////////////////
~stack() {
empty_buffer();
m_file_stream.truncate(this->size());
}

    ////////////////////////////////////////////////////////////////////
    /// \brief Pushes one item onto the stack. Returns ERROR_* as
    /// given by the underlying stream.
    ///
    /// \param t The item to push onto the stack.
    ////////////////////////////////////////////////////////////////////
inline void push(const T & t) throw(stream_exception) {
if (m_buffer.size() == m_bufferItems) empty_buffer();
m_buffer[m_bufferItems++] = t;
}

    ////////////////////////////////////////////////////////////////////
    /// \brief Pops one item from the stack.
    ////////////////////////////////////////////////////////////////////
inline const T & pop() throw(stream_exception) {
if (m_bufferItems) return m_buffer[--m_bufferItems];
const T & item = m_file_stream.read_back();
return item;
}

    ////////////////////////////////////////////////////////////////////
    /// \brief Peeks at the topmost item on the stack.
    ////////////////////////////////////////////////////////////////////
inline const T & top() throw(stream_exception) {
if (m_bufferItems) return m_buffer[m_bufferItems-1];
m_buffer[0] = m_file_stream.read_back();
m_file_stream.read();
return m_buffer[0];
}

    ////////////////////////////////////////////////////////////////////
    /// \brief Returns the number of items currently on the stack.
    ////////////////////////////////////////////////////////////////////
    inline stream_size_type size() const {
return m_file_stream.offset()+m_bufferItems;
    }

    ////////////////////////////////////////////////////////////////////
    /// \brief Returns whether the stack is empty or not.
    ////////////////////////////////////////////////////////////////////
    inline bool empty() const {
return size() == 0;//!m_file_stream.can_read_back();
    }

    ////////////////////////////////////////////////////////////////////
    /// \brief Compute the memory used by a stack.
    ////////////////////////////////////////////////////////////////////
inline static memory_size_type memory_usage(float blockFactor=1.0) {
return sizeof(stack<T>)
+ file_stream<T>::memory_usage(blockFactor)
+ array<T>::memory_usage(buffer_size(blockFactor));
}

protected:

/** The file_stream used to store the items. */
file_stream<T> m_file_stream;

private:
array<T> m_buffer;
size_t m_bufferItems;

inline void empty_buffer() {
if (m_bufferItems == 0) return;
m_file_stream.write(m_buffer.begin(), m_buffer.begin()+m_bufferItems);
m_bufferItems = 0;
}

inline static memory_size_type buffer_size(double blockFactor) {
return file<T>::block_size(blockFactor)/sizeof(T);
}

};

namespace ami {

///////////////////////////////////////////////////////////////////
/// An implementation of an external-memory stack compatible with the old AMI interface.
///////////////////////////////////////////////////////////////////
template<class T>
class stack {
public:
   
    ////////////////////////////////////////////////////////////////////
    /// Initializes the stack.
    ////////////////////////////////////////////////////////////////////
    stack() :
m_ulate(m_tempFile) {
// Empty ctor.
}

    ////////////////////////////////////////////////////////////////////
    /// Initializes the stack by (re-)opening the file given.
    ///
    /// \param path The path to a file used for storing the items.
    /// \param type An stream_type that indicates the
    /// read/write mode of the file.
    ////////////////////////////////////////////////////////////////////
    stack(const std::string& path,
stream_type type = READ_WRITE_STREAM)
: m_tempFile(path, true)
, m_ulate(m_tempFile)
{
unused(type);
}

    ////////////////////////////////////////////////////////////////////
    /// Pushes one item onto the stack. Returns ERROR_* as
    /// given by the underlying stream.
    ///
    /// \param t The item to be pushed onto the stack.
    ////////////////////////////////////////////////////////////////////
    err push(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.
    ///
    /// \param t A pointer to a pointer that will point to the
    /// topmost item.
    ////////////////////////////////////////////////////////////////////
    err pop(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.
    ///
    /// \param t A pointer to a pointer that will point to the
    /// topmost item.
    ////////////////////////////////////////////////////////////////////
    err peek(const T **t);

    ////////////////////////////////////////////////////////////////////
    /// Returns the number of items currently on the stack.
    ////////////////////////////////////////////////////////////////////
    TPIE_OS_OFFSET size() const {
return m_ulate.size();
    }

    ////////////////////////////////////////////////////////////////////
    /// Returns whether the stack is empty or not.
    ////////////////////////////////////////////////////////////////////
    bool is_empty() const {
return m_ulate.empty();
    }

    ////////////////////////////////////////////////////////////////////
    /// Set the persistence status of the (stream underlying the) stack.
    ///
    /// \param p A persistence status.
    ////////////////////////////////////////////////////////////////////
void persist(persistence p) {
m_tempFile.set_persistent(p == PERSIST_PERSISTENT);
    }

    ////////////////////////////////////////////////////////////////////
    /// Returns the persistence status of the (stream underlying the)
    /// stack.
    ////////////////////////////////////////////////////////////////////
persistence persist() const {
return m_tempFile.is_persistent() ? PERSIST_PERSISTENT : PERSIST_DELETE;
    }

    ////////////////////////////////////////////////////////////////////
    /// Truncates the underlying stream to the exact size (rounded up
    /// to the next block) of items. In the current implementation,
/// this does nothing.
    ////////////////////////////////////////////////////////////////////
    err trim() {
return NO_ERROR;
    }

    ////////////////////////////////////////////////////////////////////
    /// Compute the memory used by the stack and the aggregated stream.
    ///
    /// \param usage Where the usage will be stored.
    /// \param usage_type The type of usage_type inquired from
    /// the stream.
    ////////////////////////////////////////////////////////////////////
err main_memory_usage(TPIE_OS_SIZE_T *usage,
stream_usage usage_type) const;

    ////////////////////////////////////////////////////////////////////
    /// \deprecated This should go as soon as all old code has been migrated.
    ////////////////////////////////////////////////////////////////////
    TPIE_OS_OFFSET stream_len() const {
TP_LOG_WARNING_ID("Using AMI_stack<T>::stream_len() is deprecated.");
return size();
    }

private:

temp_file m_tempFile;

// em-ulate. get it?
tpie::stack<T> m_ulate;
};

/////////////////////////////////////////////////////////////////////////

template<class T>
err stack<T>::push(const T &t) {

    err retval = NO_ERROR;

try {
m_ulate.push(t);
} catch (end_of_stream_exception &) {
retval = END_OF_STREAM;
} catch (stream_exception &) {
retval = IO_ERROR;
}

return retval;

}

/////////////////////////////////////////////////////////////////////////

template<class T>
err stack<T>::pop(const T **t) {

    err retval = NO_ERROR;

try {

const T & res = m_ulate.pop();
*t = &res;

} catch (end_of_stream_exception &) {
retval = END_OF_STREAM;
} catch (stream_exception &) {
retval = IO_ERROR;
}

return retval;

}

/////////////////////////////////////////////////////////////////////////

template<class T>
err stack<T>::peek(const T **t) {

    err retval = NO_ERROR;

try {

const T & res = m_ulate.top();
*t = &res;

} catch (end_of_stream_exception &) {
retval = END_OF_STREAM;
} catch (stream_exception &) {
retval = IO_ERROR;
}

return retval;

}

/////////////////////////////////////////////////////////////////////////

template<class T>
err stack<T>::main_memory_usage(TPIE_OS_SIZE_T *usage,
stream_usage usage_type) const {
    
    switch (usage_type) {

// All these types are o.k.
    case STREAM_USAGE_OVERHEAD:
    case STREAM_USAGE_CURRENT:
    case STREAM_USAGE_MAXIMUM:
    case STREAM_USAGE_SUBSTREAM:
    case STREAM_USAGE_BUFFER:
*usage = tpie::stack<T>::memory_usage();
*usage += sizeof(*this);
break;

    default:
tp_assert(0, "Unknown mem::stream_usage type added.");
    }
    
    return NO_ERROR;
}

/////////////////////////////////////////////////////////////////////////

} // ami namespace

} // tpie namespace

#endif // _TPIE_AMI_STACK_H
Something went wrong with that request. Please try again.