-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathVector.h
More file actions
170 lines (139 loc) · 3.43 KB
/
Vector.h
File metadata and controls
170 lines (139 loc) · 3.43 KB
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
/**
* Copyright (c) 2017 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#pragma once
#include "ArrayView.h"
#include "internal/Constants.h"
#include <type_traits>
#include <vector>
namespace flatdata
{
template < typename T >
class Vector
{
public:
using ValueType = typename T::MutatorType;
using ConstValueType = typename T::AccessorType;
using StreamType = typename T::MutatorType::StreamType;
using ConstStreamType = typename T::AccessorType::StreamType;
public:
explicit Vector( size_t size = 0 );
ConstValueType operator[]( size_t i ) const;
ValueType operator[]( size_t i );
ConstValueType front( ) const;
ValueType front( );
ConstValueType back( ) const;
ValueType back( );
size_t size_in_bytes( ) const;
size_t size( ) const;
ConstStreamType data( ) const;
StreamType data( );
void resize( size_t size );
void reserve( size_t size );
ValueType grow( );
void pop_back( );
operator ArrayView< ConstValueType >( ) const;
private:
std::vector< typename std::remove_pointer< StreamType >::type > m_data;
};
// -----------------------------------------------------------------------------
template < typename T >
Vector< T >::Vector( size_t size )
{
reserve( size );
resize( size );
}
template < typename T >
typename Vector< T >::ConstValueType
Vector< T >::operator[]( size_t i ) const
{
return ConstValueType{ m_data.data( ) + T::size_in_bytes( ) * i };
}
template < typename T >
typename Vector< T >::ValueType
Vector< T >::operator[]( size_t i )
{
return ValueType{ m_data.data( ) + T::size_in_bytes( ) * i };
}
template < typename T >
typename Vector< T >::ConstValueType
Vector< T >::front( ) const
{
return this->operator[]( 0 );
}
template < typename T >
typename Vector< T >::ValueType
Vector< T >::front( )
{
return this->operator[]( 0 );
}
template < typename T >
typename Vector< T >::ConstValueType
Vector< T >::back( ) const
{
return this->operator[]( size( ) - 1 );
}
template < typename T >
typename Vector< T >::ValueType
Vector< T >::back( )
{
return this->operator[]( size( ) - 1 );
}
template < typename T >
size_t
Vector< T >::size_in_bytes( ) const
{
return m_data.size( ) - PADDING_SIZE;
}
template < typename T >
typename Vector< T >::ConstStreamType
Vector< T >::data( ) const
{
return m_data.data( );
}
template < typename T >
typename Vector< T >::StreamType
Vector< T >::data( )
{
return m_data.data( );
}
template < typename T >
size_t
Vector< T >::size( ) const
{
return size_in_bytes( ) / T::size_in_bytes( );
}
template < typename T >
void
Vector< T >::resize( size_t size )
{
m_data.resize( size * T::size_in_bytes( ) + PADDING_SIZE );
}
template < typename T >
void
Vector< T >::reserve( size_t size )
{
m_data.reserve( size * T::size_in_bytes( ) + PADDING_SIZE );
}
template < typename T >
typename Vector< T >::ValueType
Vector< T >::grow( )
{
size_t old_size = m_data.size( );
m_data.resize( old_size + T::size_in_bytes( ) );
return back( );
}
template < typename T >
void
Vector< T >::pop_back( )
{
m_data.resize( m_data.size( ) - T::size_in_bytes( ) );
}
template < typename T >
Vector< T >::operator ArrayView< typename Vector< T >::ConstValueType >( ) const
{
return ArrayView< ConstValueType >( m_data.data( ),
m_data.data( ) + m_data.size( ) - PADDING_SIZE );
}
} // namespace flatdata