TPIE

2362a60
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;
75  template <typename T>
76  friend class file_stream;
77 
78  uint64_t m_readOffset;
79  uint64_t m_offset;
80 
81 public:
86  return stream_position(0, 0);
87  }
88 
92  static stream_position end() {
93  return stream_position(
94  std::numeric_limits<uint64_t>::max() - 1,
95  std::numeric_limits<uint64_t>::max() - 1);
96  }
97 
102  : m_readOffset(std::numeric_limits<uint64_t>::max())
103  , m_offset(std::numeric_limits<uint64_t>::max())
104  {
105  }
106 
107 private:
108  stream_position(stream_size_type readOffset,
109  stream_size_type offset)
110  : m_readOffset(readOffset)
111  , m_offset(offset)
112  {
113  }
114 
115  stream_size_type read_offset() const {
116  return m_readOffset;
117  }
118 
119 public:
123  stream_size_type offset() const {
124  return m_offset;
125  }
126 
127 private:
128  void advance_items(memory_size_type offset) {
129  m_offset += offset;
130  }
131 
132  void advance_item() {
133  advance_items(1);
134  }
135 
136 public:
137  bool operator==(const stream_position & other) const {
138  return m_readOffset == other.m_readOffset
139  && m_offset == other.m_offset;
140  }
141 
142  bool operator!=(const stream_position & other) const {
143  return !(*this == other);
144  }
145 
151  bool operator<(const stream_position & other) const {
152  return (m_offset != other.m_offset)
153  ? (m_offset < other.m_offset)
154  : (m_readOffset < other.m_readOffset);
155  }
156 
157  friend std::ostream & operator<<(std::ostream & s, const stream_position & p) {
158  return s << "(" << p.read_offset() << "," << p.offset() << ")";
159  }
160 };
161 
162 } // namespace tpie
163 
164 #endif // TPIE_COMPRESSED_STREAM_POSITION_H
bool operator<(const stream_position &other) const
Total ordering of stream_position objects.
static stream_position beginning()
Convenience constructor returning a pointer to the beginning.
Base class containing the implementation details that are independent of the item type...
Definition: stream.h:125
POD object indicating the position of an item in a stream.
Useful compressed stream predeclarations.
stream_size_type offset() const
The stream offset of the item pointed to.
static stream_position end()
Special-value constructor returning a pointer to the end.
Compressed stream.
Definition: predeclare.h:46
stream_position()
Default constructor resulting in a not-a-stream_position.