TPIE

11a2c2d
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) {
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) {
82  assert(m_open);
83  write_array(*this, start, end);
84  }
85 
91  inline const item_type & read() {
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) {
105  assert(m_open);
106  read_array(*this, start, end);
107  }
108 
114  inline const item_type & read_back() {
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  static constexpr memory_size_type memory_usage(
157  float blockFactor=1.0,
158  bool includeDefaultFileAccessor=true) noexcept {
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
file_stream_base.h
tpie::stream_crtp< file_stream_base >::read_array
static void read_array(Stream &stream, const IT &start, const IT &end)
Reads several items from the stream.
Definition: stream_crtp.h:166
tpie::uncompressed_stream::uncompressed_stream
uncompressed_stream(double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
Construct a new uncompressed_stream.
Definition: uncompressed_stream.h:54
tpie::uncompressed_stream::read
const item_type & read()
Read an item from the stream.
Definition: uncompressed_stream.h:91
tpie::stream_crtp< file_stream_base >::seek
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:51
tpie::stream_crtp< file_stream_base >::m_index
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:245
tpie::uncompressed_stream::skip
void skip()
Advance the stream position to the next item.
Definition: uncompressed_stream.h:137
tempname.h
tpie::file_base_crtp::is_writable
bool is_writable() const
Check if we can write to the file.
Definition: file_base_crtp.h:66
tpie::end_of_stream_exception
Definition: exception.h:62
tpie::uncompressed_stream::peek
const item_type & peek()
Get next item from stream without advancing the position.
Definition: uncompressed_stream.h:123
tpie::stream_crtp< file_stream_base >::size
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:133
tpie::uncompressed_stream::memory_usage
static constexpr memory_size_type memory_usage(float blockFactor=1.0, bool includeDefaultFileAccessor=true) noexcept
Calculate the amount of memory used by a single uncompressed_stream.
Definition: uncompressed_stream.h:156
tpie::uncompressed_stream::item_type
T item_type
The type of the items stored in the stream.
Definition: uncompressed_stream.h:47
tpie::stream_crtp< file_stream_base >::update_block
void update_block()
Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.
tpie::stream_crtp< file_stream_base >::write_array
static void write_array(Stream &stream, const IT &start, const IT &end)
Write several items to the stream.
Definition: stream_crtp.h:212
tpie::uncompressed_stream::write
void write(const IT &start, const IT &end)
Write several items to the stream.
Definition: uncompressed_stream.h:81
std::swap
void swap(tpie::array< T > &a, tpie::array< T > &b)
Enable std::swapping two tpie::arrays.
Definition: array.h:709
tpie::uncompressed_stream::read_back
const item_type & read_back()
Read an item from the stream.
Definition: uncompressed_stream.h:114
tpie::uncompressed_stream::read
void read(const IT &start, const IT &end)
Reads several items from the stream.
Definition: uncompressed_stream.h:104
tpie::file_accessor::stream_accessor_base::memory_usage
static memory_size_type memory_usage()
Return memory usage of this file accessor.
Definition: stream_accessor_base.h:177
tpie::file_accessor::stream_accessor_base
Definition: stream_accessor_base.h:34
tpie::uncompressed_stream::write
void write(const item_type &item)
Write an item to the stream.
Definition: uncompressed_stream.h:64
tpie::file_base_crtp::block_memory_usage
static memory_size_type block_memory_usage(double blockFactor)
Amount of memory used by a single block given the block factor.
Definition: file_base_crtp.h:99
file.h
tpie::uncompressed_stream
Simple class acting both as file and a file::stream.
Definition: uncompressed_stream.h:44
tpie::io_exception
Definition: exception.h:50
tpie::uncompressed_stream::skip_back
void skip_back()
Advance the stream position to the previous item.
Definition: uncompressed_stream.h:144
tpie::file_stream_base
Definition: file_stream_base.h:33
memory.h
tpie
Definition: access_type.h:26
tpie::stream_crtp< file_stream_base >::offset
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:91