TPIE

11a2c2d
stream_position.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 2013, 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 
20 #ifndef TPIE_COMPRESSED_STREAM_POSITION_H
21 #define TPIE_COMPRESSED_STREAM_POSITION_H
22 
26 
28 
29 namespace tpie {
30 
60 // IMPLEMENTATION NOTES
61 //
62 // A stream position is the tuple `(read_offset, offset)`.
63 //
64 // For compressed streams, the stream block begins with a header at byte
65 // position `read_offset`. After the block header, the items follow.
66 //
67 // For uncompressed streams, `read_offset` is zero and the item position
68 // in the stream is determined solely by `offset`.
69 //
70 // Thus, the first item in the stream has the position tuple `(0, 0)`.
73 private:
74  friend class compressed_stream_base_p;
75  friend class compressed_stream_base;
76 
77  template <typename T>
78  friend class file_stream;
79 
80  uint64_t m_readOffset;
81  uint64_t m_offset;
82 
83 public:
88  return stream_position(0, 0);
89  }
90 
94  static stream_position end() {
95  return stream_position(
96  std::numeric_limits<uint64_t>::max() - 1,
97  std::numeric_limits<uint64_t>::max() - 1);
98  }
99 
104  : m_readOffset(std::numeric_limits<uint64_t>::max())
105  , m_offset(std::numeric_limits<uint64_t>::max())
106  {
107  }
108 
109 private:
110  stream_position(stream_size_type readOffset,
111  stream_size_type offset)
112  : m_readOffset(readOffset)
113  , m_offset(offset)
114  {
115  }
116 
117  stream_size_type read_offset() const {
118  return m_readOffset;
119  }
120 
121 public:
125  stream_size_type offset() const {
126  return m_offset;
127  }
128 
129 private:
130  void advance_items(memory_size_type offset) {
131  m_offset += offset;
132  }
133 
134  void advance_item() {
135  advance_items(1);
136  }
137 
138 public:
139  bool operator==(const stream_position & other) const {
140  return m_readOffset == other.m_readOffset
141  && m_offset == other.m_offset;
142  }
143 
144  bool operator!=(const stream_position & other) const {
145  return !(*this == other);
146  }
147 
153  bool operator<(const stream_position & other) const {
154  return (m_offset != other.m_offset)
155  ? (m_offset < other.m_offset)
156  : (m_readOffset < other.m_readOffset);
157  }
158 
159  friend std::ostream & operator<<(std::ostream & s, const stream_position & p) {
160  return s << "(" << p.read_offset() << "," << p.offset() << ")";
161  }
162 };
163 
164 } // namespace tpie
165 
166 #endif // TPIE_COMPRESSED_STREAM_POSITION_H
tpie::file_stream
Compressed stream.
Definition: predeclare.h:49
tpie::stream_position
POD object indicating the position of an item in a stream.
Definition: stream_position.h:72
tpie::stream_position::end
static stream_position end()
Special-value constructor returning a pointer to the end.
Definition: stream_position.h:94
tpie::stream_position::offset
stream_size_type offset() const
The stream offset of the item pointed to.
Definition: stream_position.h:125
tpie::compressed_stream_base
Base class containing the implementation details that are independent of the item type.
Definition: stream.h:79
tpie::stream_position::beginning
static stream_position beginning()
Convenience constructor returning a pointer to the beginning.
Definition: stream_position.h:87
tpie::stream_position::operator<
bool operator<(const stream_position &other) const
Total ordering of stream_position objects.
Definition: stream_position.h:153
predeclare.h
tpie::stream_position::stream_position
stream_position()
Default constructor resulting in a not-a-stream_position.
Definition: stream_position.h:103
tpie
Definition: access_type.h:26