-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice.hpp
171 lines (139 loc) · 3.34 KB
/
slice.hpp
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
#pragma once
#include <cstddef>
#include <initializer_list>
#include <type_traits>
#include <stdexcept>
namespace cu
{
/// A view into an array of contiguous elements.
///
/// It contains a pointer and a size as information and supports reference
/// semantics to the underlying storage. It is similiar to std::string_view
/// or std::array_view.
template <typename T>
struct Slice
{
Slice() noexcept = default;
Slice( T * ptr, std::size_t size ) noexcept
: ptr_ (ptr )
, size_(size)
{}
Slice( T * first, T * last ) noexcept
: Slice( first, last-first )
{}
Slice( std::initializer_list<T> list )
: Slice( list.begin(), list.size() )
{}
template <typename Container,
typename std::enable_if<
std::is_same<T*,typename Container::pointer>::value,int>::type = 0>
Slice( Container & container ) noexcept
: Slice( container.data(), container.size() )
{}
template <typename Container,
typename std::enable_if<
std::is_same<T*,typename Container::const_pointer>::value,int>::type = 0>
Slice( const Container & container ) noexcept
: Slice( container.data(), container.size() )
{}
template <std::size_t N>
Slice( T(&arr)[N] ) noexcept
: Slice( arr, N )
{}
T * begin() const noexcept
{
return ptr_;
}
T * end() const noexcept
{
return ptr_ + size_;
}
T * data() const noexcept
{
return ptr_;
}
std::size_t size() const noexcept
{
return size_;
}
bool empty() const noexcept
{
return size_ == 0;
}
void swap( Slice & other ) noexcept
{
Slice tmp = other;
other = *this;
*this = tmp;
}
T & operator[]( std::size_t index ) const noexcept
{
return ptr_[index];
}
T & at( std::size_t index ) const
{
if ( index >= size_ )
throw std::out_of_range("cu::Slice access out of range.");
}
T & front() const noexcept
{
return *ptr_;
}
T & back() const noexcept
{
return ptr_[size_-1];
}
Slice withoutHead( std::size_t nElems = 1 ) const noexcept
{
assert( nElems <= size_ );
return { ptr_+nElems, size_-nElems };
}
Slice withoutTail( std::size_t nElems = 1 ) const noexcept
{
assert( nElems <= size_ );
return { ptr_, size_-nElems };
}
Slice truncated( std::size_t index ) const noexcept
{
assert( index <= size_ );
return { ptr_, index };
}
Slice<const T> toConst() const noexcept
{
return { ptr_, size_ };
}
private:
T * ptr_ = nullptr;
std::size_t size_ = 0;
};
template <typename T>
Slice<T> makeSlice( T * ptr, std::size_t size )
{
return { ptr, size };
};
template <typename T>
Slice<T> makeSlice( T * first, T * last )
{
return { first, last };
};
template <typename Container>
Slice<typename Container::value_type> makeSlice( Container & container )
{
return { container };
}
template <typename Container>
Slice<const typename Container::value_type> makeSlice( const Container & container )
{
return { container };
}
template <typename T, std::size_t N>
Slice<T> makeSlice( T(&arr)[N] )
{
return { arr };
}
template <typename T>
void swap( Slice<T> & lhs, Slice<T> rhs )
{
lhs.swap( rhs );
}
} // namespace cu