RavEngine
Loading...
Searching...
No Matches
span.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_BASE_SPAN_H_
29#define OZZ_OZZ_BASE_SPAN_H_
30
31#include "ozz/base/platform.h"
32
33namespace ozz {
34
35// Defines a range [begin,end[ of objects ot type _Ty.
36template <typename _Ty>
37struct span {
38 // Constants and types
39 using element_type = _Ty;
40 using value_type = _Ty;
41 using index_type = size_t;
42 using difference_type = ptrdiff_t;
43 using pointer = _Ty*;
44 using const_pointer = const _Ty*;
45 using reference = _Ty&;
46 using const_reference = const _Ty&;
47 // Iterators
48 using iterator = pointer;
49 using const_iterator = const_pointer;
50
51 // Default constructor initializes range to empty.
52 span() : data_(nullptr), size_(0) {}
53
54 // Constructs a range from its extreme values.
55 span(_Ty* _begin, _Ty* _end)
56 : data_(_begin), size_(static_cast<size_t>(_end - _begin)) {
57 assert(_begin <= _end && "Invalid range.");
58 }
59
60 // Construct a range from a pointer to a buffer and its size, ie its number of
61 // elements.
62 span(_Ty* _begin, size_t _size) : data_(_begin), size_(_size) {}
63
64 // Copy operator.
65 void operator=(const span& _other) {
66 data_ = _other.data_;
67 size_ = _other.size_;
68 }
69
70 // Construct a range from a single element.
71 explicit span(_Ty& _element) : data_(&_element), size_(1) {}
72
73 // Construct a range from an array, its size is automatically deduced.
74 // It isn't declared explicit as conversion is free and safe.
75 template <size_t _size>
76 span(_Ty (&_array)[_size]) : data_(_array), size_(_size) {}
77
78 // Reinitialized from an array, its size is automatically deduced.
79 template <size_t _size>
80 void operator=(_Ty (&_array)[_size]) {
81 data_ = _array;
82 size_ = _size;
83 }
84
85 // Implement cast operator to allow conversions to span<const _Ty>.
86 operator span<const _Ty>() const { return span<const _Ty>(data_, size_); }
87
88 // Subspan
89
90 span<element_type> first(index_type _count) const {
91 assert(_count <= size_ && "Count out of range");
92 return {data(), _count};
93 }
94
95 span<element_type> last(index_type _count) const {
96 assert(_count <= size_ && "Count out of range");
97 return {data() + size_ - _count, _count};
98 }
99
100 span<element_type> subspan(index_type _offset, index_type _count) const {
101 assert(_offset <= size_ && "Offset out of range");
102 assert(_count <= size_ && "Count out of range");
103 assert(_offset <= size_ - _count && "Offset + count out of range");
104 return {data_ + _offset, _count};
105 }
106
107 // Returns a const reference to element _i of range [begin,end[.
108 _Ty& operator[](size_t _i) const {
109 assert(_i < size_ && "Index out of range.");
110 return data_[_i];
111 }
112
113 bool empty() const { return size_ == 0; }
114
115 // Complies with other contiguous containers.
116 _Ty* data() const { return data_; }
117
118 // Gets the number of elements of the range.
119 // This size isn't stored but computed from begin and end pointers.
120 size_t size() const { return size_; }
121
122 // Gets the size in byte of the range.
123 size_t size_bytes() const { return size_ * sizeof(element_type); }
124
125 // Iterator support
126 iterator begin() const { return data_; }
127 iterator end() const { return data_ + size_; }
128
129 private:
130 // span begin pointer.
131 _Ty* data_;
132
133 // span end pointer, should never be dereferenced.
134 size_t size_;
135};
136
137// Returns a span from an array.
138template <typename _Ty, size_t _Size>
139inline span<_Ty> make_span(_Ty (&_arr)[_Size]) {
140 return {_arr};
141}
142
143// Returns a mutable span from a container.
144template <typename _Container>
145inline span<typename _Container::value_type> make_span(_Container& _container) {
146 return {_container.data(), _container.size()};
147}
148
149// Returns a non mutable span from a container.
150template <typename _Container>
151inline span<const typename _Container::value_type> make_span(
152 const _Container& _container) {
153 return {_container.data(), _container.size()};
154}
155
156// As bytes
157template <typename _Ty>
158inline span<const byte> as_bytes(const span<_Ty>& _span) {
159 return {reinterpret_cast<const byte*>(_span.data()), _span.size_bytes()};
160}
161
162template <typename _Ty>
163inline span<byte> as_writable_bytes(const span<_Ty>& _span) {
164 // Compilation will fail here if _Ty is const. This prevents from writing to
165 // const data.
166 return {reinterpret_cast<byte*>(_span.data()), _span.size_bytes()};
167}
168
169// Fills a typed span from a byte source span. Source byte span is modified to
170// reflect remain size.
171template <typename _Ty>
172inline span<_Ty> fill_span(span<byte>& _src, size_t _count) {
173 assert(ozz::IsAligned(_src.data(), alignof(_Ty)) && "Invalid alignment.");
174 const span<_Ty> ret = {reinterpret_cast<_Ty*>(_src.data()), _count};
175 // Validity assertion is done by span constructor.
176 _src = {reinterpret_cast<byte*>(ret.end()), _src.end()};
177 return ret;
178}
179
180} // namespace ozz
181#endif // OZZ_OZZ_BASE_SPAN_H_
Definition span.h:37