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
|
#pragma once
#include "reader.hpp"
#include "varint.hpp"
#include "../base/assert.hpp"
#include "../base/base.hpp"
#include "../base/exception.hpp"
#include "../base/src_point.hpp"
#include "../std/type_traits.hpp"
#include "../std/iterator_facade.hpp"
template <
typename T,
class TReader,
typename TSize = uint32_t,
typename TDifference = typename make_signed<TSize>::type
> class DDVector
{
public:
typedef T value_type;
typedef TSize size_type;
typedef TDifference difference_type;
typedef TReader ReaderType;
DECLARE_EXCEPTION(OpenException, RootException);
DDVector() : m_Size(0) {}
explicit DDVector(TReader const & reader) : m_reader(reader)
{
InitSize();
}
void Init(TReader const & reader)
{
m_reader = reader;
InitSize();
}
size_type size() const
{
return m_Size;
}
T const operator [] (size_type i) const
{
return ReadPrimitiveFromPos<T>(m_reader, static_cast<uint64_t>(i) * sizeof(T));
}
class const_iterator : public iterator_facade<
const_iterator,
value_type const,
random_access_traversal_tag>
{
public:
const_iterator() : m_pReader(NULL), m_I(0), m_bValueRead(false)
{
}
#ifdef DEBUG
const_iterator(ReaderType const * pReader, size_type i, size_type size)
: m_pReader(pReader), m_I(i), m_bValueRead(false), m_Size(size)
{
}
#else
const_iterator(ReaderType const * pReader, size_type i)
: m_pReader(pReader), m_I(i), m_bValueRead(false)
{
}
#endif
T const & dereference() const
{
ASSERT_LESS(m_I, m_Size, (m_bValueRead));
if (!m_bValueRead)
{
m_Value = ReadPrimitiveFromPos<T>(*m_pReader, static_cast<uint64_t>(m_I) * sizeof(T));
m_bValueRead = true;
}
return m_Value;
}
void advance(difference_type n)
{
ASSERT_LESS_OR_EQUAL(m_I, m_Size, (m_bValueRead));
m_I += n;
ASSERT_LESS_OR_EQUAL(m_I, m_Size, (m_bValueRead));
m_bValueRead = false;
}
difference_type distance_to(const_iterator const & it) const
{
ASSERT_LESS_OR_EQUAL(m_I, m_Size, (m_bValueRead));
ASSERT_LESS_OR_EQUAL(it.m_I, it.m_Size, (it.m_bValueRead));
ASSERT_EQUAL(m_Size, it.m_Size, (m_I, it.m_I, m_bValueRead, it.m_bValueRead));
ASSERT(m_pReader == it.m_pReader, (m_I, m_Size, it.m_I, it.m_Size));
return it.m_I - m_I;
}
void increment()
{
++m_I;
m_bValueRead = false;
ASSERT_LESS_OR_EQUAL(m_I, m_Size, (m_bValueRead));
}
void decrement()
{
--m_I;
m_bValueRead = false;
ASSERT_LESS_OR_EQUAL(m_I, m_Size, (m_bValueRead));
}
bool equal(const_iterator const & it) const
{
ASSERT_LESS_OR_EQUAL(m_I, m_Size, (m_bValueRead));
ASSERT_LESS_OR_EQUAL(it.m_I, it.m_Size, (it.m_bValueRead));
ASSERT_EQUAL(m_Size, it.m_Size, (m_I, it.m_I, m_bValueRead, it.m_bValueRead));
ASSERT(m_pReader == it.m_pReader, (m_I, m_Size, it.m_I, it.m_Size));
return m_I == it.m_I;
}
private:
ReaderType const * m_pReader;
size_type m_I;
mutable T m_Value;
mutable bool m_bValueRead;
#ifdef DEBUG
size_type m_Size;
#endif
};
const_iterator begin() const
{
#ifdef DEBUG
return const_iterator(&m_reader, 0, m_Size);
#else
return const_iterator(&m_reader, 0);
#endif
}
const_iterator end() const
{
#ifdef DEBUG
return const_iterator(&m_reader, m_Size, m_Size);
#else
return const_iterator(&m_reader, m_Size);
#endif
}
void Read(size_type i, T & result) const
{
ASSERT_LESS(i, m_Size, ());
ReadFromPos(m_reader, i * sizeof(T), &result, sizeof(T));
}
void Read(size_type i, T * result, size_t count)
{
ASSERT_LESS(i + count, m_Size, (i, count));
ReadFromPos(m_reader, i * sizeof(T), result, count * sizeof(T));
}
private:
void InitSize()
{
if ((m_reader.Size() % sizeof(T)) != 0)
MYTHROW(OpenException, (m_reader.Size(), sizeof(T)));
// TODO: Check that reader.Size() % sizeof(T) == 0
m_Size = m_reader.Size() / sizeof(T);
}
// TODO: Refactor me to use Reader by pointer.
ReaderType m_reader;
size_type m_Size;
};
|