TPIE

2362a60
uncompressed_stream.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2009, 2011, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 #ifndef TPIE_UNCOMPRESSED_STREAM_H
20 #define TPIE_UNCOMPRESSED_STREAM_H
21 #include <tpie/tempname.h>
22 #include <tpie/file.h>
23 #include <tpie/memory.h>
24 #include <tpie/file_stream_base.h>
30 
31 namespace tpie {
32 
33 
43 template <typename T>
45 public:
47  typedef T item_type;
48 
54  uncompressed_stream(double blockFactor=1.0,
55  file_accessor::file_accessor * fileAccessor=NULL):
56  file_stream_base(sizeof(item_type), blockFactor, fileAccessor) {};
57 
58 
64  inline void write(const item_type & item) throw(stream_exception) {
65  assert(m_open);
66 #ifndef NDEBUG
67  if (!is_writable())
68  throw io_exception("Cannot write to read only stream");
69 #endif
70  if (m_index >= m_blockItems) update_block();
71  reinterpret_cast<item_type*>(m_block.data)[m_index++] = item;
72  write_update();
73  }
74 
80  template <typename IT>
81  inline void write(const IT & start, const IT & end) throw(stream_exception) {
82  assert(m_open);
83  write_array(*this, start, end);
84  }
85 
91  inline const item_type & read() throw(stream_exception) {
92  assert(m_open);
93  const item_type & x = peek();
94  ++m_index;
95  return x;
96  }
97 
103  template <typename IT>
104  inline void read(const IT & start, const IT & end) throw(stream_exception) {
105  assert(m_open);
106  read_array(*this, start, end);
107  }
108 
114  inline const item_type & read_back() throw(stream_exception) {
115  assert(m_open);
116  skip_back();
117  return peek();
118  }
119 
123  const item_type & peek() {
124  assert(m_open);
125  if (m_index >= m_block.size) {
126  update_block();
127  if (offset() >= size()) {
128  throw end_of_stream_exception();
129  }
130  }
131  return reinterpret_cast<item_type*>(m_block.data)[m_index];
132  }
133 
137  void skip() {
138  seek(1, current);
139  }
140 
144  void skip_back() {
145  seek(-1, current);
146  }
147 
156  inline static memory_size_type memory_usage(
157  float blockFactor=1.0,
158  bool includeDefaultFileAccessor=true) throw() {
159  // TODO
160  memory_size_type x = sizeof(uncompressed_stream);
161  x += block_memory_usage(blockFactor); // allocated in constructor
162  if (includeDefaultFileAccessor)
164  return x;
165  }
166 
167  void swap(uncompressed_stream<T> & other) {
168  file_stream_base::swap(other);
169  }
170 
171  friend struct stream_item_array_operations;
172 };
173 
174 } // namespace tpie
175 
176 namespace std {
177 
181 template <typename T>
183  a.swap(b);
184 }
185 
186 } // namespace std
187 
188 #endif // TPIE_UNCOMPRESSED_STREAM_H
static void write_array(Stream &stream, const IT &start, const IT &end)
Write several items to the stream.
Definition: stream_crtp.h:211
static memory_size_type block_memory_usage(double blockFactor)
Amount of memory used by a single block given the block factor.
Memory management subsystem.
static void read_array(Stream &stream, const IT &start, const IT &end)
Reads several items from the stream.
Definition: stream_crtp.h:165
const item_type & read_back()
Read an item from the stream.
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:90
T item_type
The type of the items stored in the stream.
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
Streams that support substreams.
bool is_writable() const
Check if we can write to the file.
void skip_back()
Advance the stream position to the previous item.
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:50
void write(const item_type &item)
Write an item to the stream.
const item_type & peek()
Get next item from stream without advancing the position.
static memory_size_type memory_usage(float blockFactor=1.0, bool includeDefaultFileAccessor=true)
Calculate the amount of memory used by a single uncompressed_stream.
Simple class acting both as file and a file::stream.
void write(const IT &start, const IT &end)
Write several items to the stream.
uncompressed_stream(double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
Construct a new uncompressed_stream.
void update_block()
Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.
Item type-agnostic file_stream operations.
Temporary file names.
const item_type & read()
Read an item from the stream.
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:132
static memory_size_type memory_usage()
Return memory usage of this file accessor.
void read(const IT &start, const IT &end)
Reads several items from the stream.
void skip()
Advance the stream position to the next item.