2018-08-19 19:20:05 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2018-2018 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MBED_PLATFORM_SPAN_H_
|
|
|
|
#define MBED_PLATFORM_SPAN_H_
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "platform/mbed_assert.h"
|
|
|
|
|
|
|
|
namespace mbed {
|
|
|
|
|
|
|
|
/**
|
2018-08-20 10:58:48 +00:00
|
|
|
* Special value for the Extent parameter of Span.
|
2018-08-19 19:20:05 +00:00
|
|
|
* If the type use this value then the size of the array is stored in the object
|
|
|
|
* at runtime.
|
|
|
|
*/
|
2018-08-20 09:29:43 +00:00
|
|
|
#define SPAN_DYNAMIC_EXTENT -1
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* View to an array.
|
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* Spans encapsulate the pointer to an array and its size into a single object.
|
|
|
|
* However, it does not manage the lifetime of the array viewed. You can use
|
|
|
|
* instances of Span to replace the traditional pair of pointer and size
|
|
|
|
* arguments in function calls.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
|
|
|
* You can use the size member function to query the number of elements present
|
2018-08-21 17:20:58 +00:00
|
|
|
* in the array, and the subscript operator allow code using this object to
|
|
|
|
* access the content of the array viewed.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* Subspans can be created with the help of the functions first(), last() and
|
|
|
|
* subspan().
|
|
|
|
*
|
|
|
|
* @note You can create Span instances with the help of the function template
|
|
|
|
* make_Span() and make_const_Span().
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-20 10:58:48 +00:00
|
|
|
* @note Span<T, Extent> objects can be implicitly converted to Span<T> objects
|
2018-08-19 19:20:05 +00:00
|
|
|
* where required.
|
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam ElementType type of objects held in the array viewed.
|
|
|
|
*
|
2018-08-20 10:58:48 +00:00
|
|
|
* @tparam Extent The size of the array viewed. The default value
|
2018-08-19 19:20:05 +00:00
|
|
|
* SPAN_DYNAMIC_SIZE is special as it allows construction of Span objects of
|
|
|
|
* any size (set at runtime).
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<typename ElementType, ptrdiff_t Extent = SPAN_DYNAMIC_EXTENT>
|
2018-08-19 19:20:05 +00:00
|
|
|
struct Span {
|
|
|
|
|
2018-08-21 17:20:58 +00:00
|
|
|
/**
|
|
|
|
* Type of the element contained
|
|
|
|
*/
|
|
|
|
typedef ElementType element_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type of the index.
|
|
|
|
*/
|
|
|
|
typedef ptrdiff_t index_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to an ElementType
|
|
|
|
*/
|
|
|
|
typedef element_type *pointer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to an ElementType
|
|
|
|
*/
|
|
|
|
typedef element_type &reference;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the Extent; -1 if dynamic.
|
|
|
|
*/
|
|
|
|
static const index_type extent = Extent;
|
|
|
|
|
2018-08-20 10:58:48 +00:00
|
|
|
MBED_STATIC_ASSERT(Extent >= 0, "Invalid extent for a Span");
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Construct an empty span.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
|
|
|
* @post a call to size() will return 0, and data() will return NULL.
|
2018-08-21 17:20:58 +00:00
|
|
|
*
|
|
|
|
* @note This function is not accessible if Extent != SPAN_DYNAMIC_EXTENT or
|
|
|
|
* Extent != 0 .
|
|
|
|
*/
|
|
|
|
Span() : _data(NULL) {
|
|
|
|
MBED_STATIC_ASSERT(Extent == 0, "Invalid extent for a Span");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a Span from a pointer to a buffer and its size.
|
|
|
|
*
|
|
|
|
* @param ptr Pointer to the beginning of the data viewed.
|
|
|
|
*
|
|
|
|
* @param count Number of elements viewed.
|
|
|
|
*
|
|
|
|
* @pre [ptr, ptr + count) must be be a valid range.
|
|
|
|
* @pre count must be equal to extent.
|
|
|
|
*
|
|
|
|
* @post a call to size() will return Extent and data() will return @p ptr.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span(pointer ptr, index_type count) :
|
|
|
|
_data(ptr) {
|
|
|
|
MBED_ASSERT(count == Extent);
|
|
|
|
MBED_ASSERT(Extent == 0 || ptr != NULL);
|
|
|
|
}
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Construct a Span from the range [first, last)
|
|
|
|
*
|
|
|
|
* @param first Pointer to the beginning of the data viewed.
|
|
|
|
* @param last End of the range (element after the last element).
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @pre [first, last) must be be a valid range.
|
|
|
|
* @pre first <= last
|
|
|
|
* @pre last - first must be equal to Extent.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @post a call to size() will return Extent and data() will return @p first.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span(pointer first, pointer last) :
|
|
|
|
_data(first) {
|
|
|
|
MBED_ASSERT(first <= last);
|
|
|
|
MBED_ASSERT((last - first) == Extent);
|
|
|
|
MBED_ASSERT(Extent == 0 || first != NULL);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a Span from the reference to an array.
|
|
|
|
*
|
|
|
|
* @param elements Reference to the array viewed.
|
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @post a call to size() will return Extent, and data() will return a
|
|
|
|
* pointer to elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span(element_type (&elements)[Extent]):
|
|
|
|
_data(elements) { }
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size of the array viewed.
|
|
|
|
*
|
|
|
|
* @return The number of elements present in the array viewed.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
index_type size() const
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-21 17:20:58 +00:00
|
|
|
return Extent;
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return if the array is empty or not.
|
|
|
|
*
|
|
|
|
* @return true if the array is empty and false otherwise
|
|
|
|
*/
|
|
|
|
bool empty() const
|
|
|
|
{
|
2018-08-20 09:30:35 +00:00
|
|
|
return size() == 0;
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Returns a reference to the element at position @p index
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @param index Index of the element to access.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
|
|
|
* @return A reference to the element at the index specified in input.
|
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @pre 0 <= index < Extent
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
reference operator[](index_type index) const
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-21 17:20:58 +00:00
|
|
|
return _data[index];
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Return a pointer to the first element of the sequence or NULL if the span
|
|
|
|
* is empty().
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @return The pointer to the first element of the span.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
pointer data() const
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-21 17:20:58 +00:00
|
|
|
return _data;
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Create a new span over the first @p Count elements of the existing view.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam Count The number of element viewed by the new Span
|
|
|
|
*
|
|
|
|
* @return A new Span over the first @p Count elements.
|
|
|
|
*
|
|
|
|
* @pre Count >= 0 && Count <= size().
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<ptrdiff_t Count>
|
|
|
|
Span<element_type, Count> first() const {
|
|
|
|
MBED_STATIC_ASSERT(
|
2018-08-22 08:40:13 +00:00
|
|
|
(0 <= Count) && (Count <= Extent),
|
2018-08-21 17:20:58 +00:00
|
|
|
"Invalid subspan extent"
|
|
|
|
);
|
|
|
|
return Span<element_type, Count>(_data, Count);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Create a new span over the last @p Count elements of the existing view.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam Count The number of element viewed by the new Span
|
|
|
|
*
|
|
|
|
* @return A new Span over the last @p Count elements.
|
|
|
|
*
|
|
|
|
* @pre Count >= 0 && Count <= size().
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<ptrdiff_t Count>
|
|
|
|
Span<element_type, Count> last() const {
|
|
|
|
MBED_STATIC_ASSERT(
|
2018-08-22 08:40:13 +00:00
|
|
|
(0 <= Count) && (Count <= Extent),
|
2018-08-21 17:20:58 +00:00
|
|
|
"Invalid subspan extent"
|
|
|
|
);
|
|
|
|
return Span<element_type, Count>(_data + (Extent - Count), Count);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Create a subspan that is a view other Count elements; the view starts at
|
|
|
|
* element Offset.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam Offset The offset of the first element viewed by the subspan.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam Count The number of elements present in the subspan. If Count
|
|
|
|
* is equal to SPAN_DYNAMIC_EXTENT then a span starting at offset and
|
|
|
|
* containing the rest of the elements is returned.
|
|
|
|
*
|
|
|
|
* @return
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
|
2018-08-22 08:35:17 +00:00
|
|
|
Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count>
|
|
|
|
subspan() const {
|
2018-08-21 17:20:58 +00:00
|
|
|
MBED_STATIC_ASSERT(
|
2018-08-22 08:40:13 +00:00
|
|
|
0 <= Offset && Offset <= Extent,
|
2018-08-21 17:20:58 +00:00
|
|
|
"Invalid subspan offset"
|
|
|
|
);
|
|
|
|
MBED_STATIC_ASSERT(
|
|
|
|
(Count == SPAN_DYNAMIC_EXTENT) ||
|
2018-08-22 08:40:13 +00:00
|
|
|
(0 <= Count && (Count + Offset) <= Extent),
|
2018-08-21 17:20:58 +00:00
|
|
|
"Invalid subspan count"
|
|
|
|
);
|
2018-08-22 08:35:17 +00:00
|
|
|
return Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count>(
|
2018-08-21 17:20:58 +00:00
|
|
|
_data + Offset,
|
|
|
|
Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count
|
|
|
|
);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Create a new Span over the first @p count elements of the existing view.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @param count The number of element viewed by the new Span
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @return A new Span over the first @p count elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span<element_type, SPAN_DYNAMIC_EXTENT> first(index_type count) const {
|
|
|
|
MBED_ASSERT(0 <= count && count <= Extent);
|
|
|
|
return Span<element_type, SPAN_DYNAMIC_EXTENT>(_data, count);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new span over the last @p count elements of the existing view.
|
|
|
|
*
|
|
|
|
* @param count The number of element viewed by the new Span
|
|
|
|
*
|
|
|
|
* @return A new Span over the last @p count elements.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span<element_type, SPAN_DYNAMIC_EXTENT> last(index_type count) const {
|
|
|
|
MBED_ASSERT(0 <= count && count <= Extent);
|
|
|
|
return Span<element_type, SPAN_DYNAMIC_EXTENT>(
|
|
|
|
_data + (Extent - count),
|
|
|
|
count
|
|
|
|
);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Create a subspan that is a view other count elements; the view starts at
|
|
|
|
* element offset.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @param offset The offset of the first element viewed by the subspan.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @param count The number of elements present in the subspan. If Count
|
|
|
|
* is equal to SPAN_DYNAMIC_EXTENT then a span starting at offset and
|
|
|
|
* containing the rest of the elements is returned.
|
|
|
|
*
|
|
|
|
* @return
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span<element_type, SPAN_DYNAMIC_EXTENT> subspan(
|
|
|
|
index_type offset, index_type count = SPAN_DYNAMIC_EXTENT
|
|
|
|
) const {
|
|
|
|
MBED_ASSERT(0 <= offset && offset <= Extent);
|
|
|
|
MBED_ASSERT(
|
|
|
|
(count == SPAN_DYNAMIC_EXTENT) ||
|
2018-08-22 08:40:13 +00:00
|
|
|
(0 <= count && (count + offset) <= Extent)
|
2018-08-21 17:20:58 +00:00
|
|
|
);
|
|
|
|
return Span<element_type, SPAN_DYNAMIC_EXTENT>(
|
|
|
|
_data + offset,
|
|
|
|
count == SPAN_DYNAMIC_EXTENT ? Extent - offset : count
|
|
|
|
);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-21 17:20:58 +00:00
|
|
|
pointer _data;
|
2018-08-19 19:20:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Span specialisation that handle dynamic array size.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<typename ElementType>
|
|
|
|
struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Type of the element contained
|
|
|
|
*/
|
|
|
|
typedef ElementType element_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type of the index.
|
|
|
|
*/
|
|
|
|
typedef ptrdiff_t index_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to an ElementType
|
|
|
|
*/
|
|
|
|
typedef element_type *pointer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to an ElementType
|
|
|
|
*/
|
|
|
|
typedef element_type &reference;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the Extent; -1 if dynamic.
|
|
|
|
*/
|
|
|
|
static const index_type extent = SPAN_DYNAMIC_EXTENT;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an empty span.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
|
|
|
* @post a call to size() will return 0, and data() will return NULL.
|
2018-08-21 17:20:58 +00:00
|
|
|
*
|
|
|
|
* @note This function is not accessible if Extent != SPAN_DYNAMIC_EXTENT or
|
|
|
|
* Extent != 0 .
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span() : _data(NULL), _size(0) { }
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a Span from a pointer to a buffer and its size.
|
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @param ptr Pointer to the beginning of the data viewed.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @param count Number of elements viewed.
|
|
|
|
*
|
|
|
|
* @pre [ptr, ptr + count) must be be a valid range.
|
|
|
|
* @pre count must be equal to extent.
|
|
|
|
*
|
|
|
|
* @post a call to size() will return count and data() will return @p ptr.
|
|
|
|
*/
|
|
|
|
Span(pointer ptr, index_type count) :
|
|
|
|
_data(ptr), _size(count) {
|
|
|
|
MBED_ASSERT(count >= 0);
|
|
|
|
MBED_ASSERT(ptr != NULL || count == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a Span from the range [first, last)
|
|
|
|
*
|
|
|
|
* @param first Pointer to the beginning of the data viewed.
|
|
|
|
* @param last End of the range (element after the last element).
|
|
|
|
*
|
|
|
|
* @pre [first, last) must be be a valid range.
|
|
|
|
* @pre first <= last
|
|
|
|
*
|
|
|
|
* @post a call to size() will return the result of (last - first) and
|
|
|
|
* data() will return @p first.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span(pointer first, pointer last) :
|
|
|
|
_data(first), _size(last - first) {
|
|
|
|
MBED_ASSERT(first <= last);
|
|
|
|
MBED_ASSERT(first != NULL || (last - first) == 0);
|
|
|
|
}
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a Span from the reference to an array.
|
|
|
|
*
|
|
|
|
* @param elements Reference to the array viewed.
|
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam Count Number of elements of T presents in the array.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @post a call to size() will return Count, and data() will return a
|
|
|
|
* pointer to elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<size_t Count>
|
|
|
|
Span(element_type (&elements)[Count]):
|
|
|
|
_data(elements), _size(Count) { }
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Construct a Span object from another Span.
|
|
|
|
*
|
2018-08-19 19:20:05 +00:00
|
|
|
* @param other The Span object used to construct this.
|
2018-08-21 17:20:58 +00:00
|
|
|
*
|
|
|
|
* @note For span with a positive extent, this function is not accessible.
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<ptrdiff_t OtherExtent>
|
|
|
|
Span(const Span<element_type, OtherExtent> &other):
|
|
|
|
_data(other.data()), _size(other.size()) { }
|
2018-08-19 19:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size of the array viewed.
|
|
|
|
*
|
|
|
|
* @return The number of elements present in the array viewed.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
index_type size() const
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
|
|
|
return _size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Return if the array is empty or not.
|
|
|
|
*
|
2018-08-19 19:20:05 +00:00
|
|
|
* @return true if the array is empty and false otherwise
|
|
|
|
*/
|
|
|
|
bool empty() const
|
|
|
|
{
|
2018-08-20 09:30:35 +00:00
|
|
|
return size() == 0;
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Access to an element of the array.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
|
|
|
* @param index Element index to access.
|
|
|
|
*
|
|
|
|
* @return A reference to the element at the index specified in input.
|
|
|
|
*
|
|
|
|
* @pre index shall be less than size().
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
reference operator[](index_type index) const
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-21 17:20:58 +00:00
|
|
|
return _data[index];
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Get the raw pointer to the array.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @return The raw pointer to the array.
|
|
|
|
*/
|
|
|
|
pointer data() const
|
|
|
|
{
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new span over the first @p Count elements of the existing view.
|
|
|
|
*
|
|
|
|
* @tparam Count The number of element viewed by the new Span
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @return A new Span over the first @p Count elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @pre Count >= 0 && Count <= size().
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<ptrdiff_t Count>
|
|
|
|
Span<element_type, Count> first() const {
|
|
|
|
MBED_ASSERT((Count >= 0) && (Count <= _size));
|
|
|
|
return Span<element_type, Count>(_data, Count);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Create a new span over the last @p Count elements of the existing view.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam Count The number of element viewed by the new Span
|
|
|
|
*
|
|
|
|
* @return A new Span over the last @p Count elements.
|
|
|
|
*
|
|
|
|
* @pre Count >= 0 && Count <= size().
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<ptrdiff_t Count>
|
|
|
|
Span<element_type, Count> last() const {
|
2018-08-22 08:40:13 +00:00
|
|
|
MBED_ASSERT((0 <= Count) && (Count <= _size));
|
2018-08-21 17:20:58 +00:00
|
|
|
return Span<element_type, Count>(_data + (_size - Count), Count);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 17:20:58 +00:00
|
|
|
* Create a subspan that is a view other Count elements; the view starts at
|
|
|
|
* element Offset.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
2018-08-21 17:20:58 +00:00
|
|
|
* @tparam Offset The offset of the first element viewed by the subspan.
|
|
|
|
*
|
|
|
|
* @tparam Count The number of elements present in the subspan. If Count
|
|
|
|
* is equal to SPAN_DYNAMIC_EXTENT then a span starting at offset and
|
|
|
|
* containing the rest of the elements is returned.
|
|
|
|
*
|
|
|
|
* @return
|
2018-08-19 19:20:05 +00:00
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
|
2018-08-22 08:35:17 +00:00
|
|
|
Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? SPAN_DYNAMIC_EXTENT : Count>
|
|
|
|
subspan() const {
|
|
|
|
MBED_ASSERT(0 <= Offset && Offset <= _size);
|
2018-08-21 17:20:58 +00:00
|
|
|
MBED_ASSERT(
|
|
|
|
(Count == SPAN_DYNAMIC_EXTENT) ||
|
2018-08-22 08:40:13 +00:00
|
|
|
(0 <= Count && (Count + Offset) <= _size)
|
2018-08-21 17:20:58 +00:00
|
|
|
);
|
2018-08-22 08:35:17 +00:00
|
|
|
return Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? SPAN_DYNAMIC_EXTENT : Count>(
|
2018-08-21 17:20:58 +00:00
|
|
|
_data + Offset,
|
|
|
|
Count == SPAN_DYNAMIC_EXTENT ? _size - Offset : Count
|
|
|
|
);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Span over the first @p count elements of the existing view.
|
|
|
|
*
|
|
|
|
* @param count The number of element viewed by the new Span
|
|
|
|
*
|
|
|
|
* @return A new Span over the first @p count elements.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span<element_type, SPAN_DYNAMIC_EXTENT> first(index_type count) const {
|
|
|
|
MBED_ASSERT(0 <= count && count <= _size);
|
|
|
|
return Span<element_type, SPAN_DYNAMIC_EXTENT>(_data, count);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new span over the last @p count elements of the existing view.
|
|
|
|
*
|
|
|
|
* @param count The number of element viewed by the new Span
|
|
|
|
*
|
|
|
|
* @return A new Span over the last @p count elements.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
Span<element_type, SPAN_DYNAMIC_EXTENT> last(index_type count) const {
|
|
|
|
MBED_ASSERT(0 <= count && count <= _size);
|
|
|
|
return Span<element_type, SPAN_DYNAMIC_EXTENT>(
|
|
|
|
_data + (_size - count),
|
|
|
|
count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a subspan that is a view other count elements; the view starts at
|
|
|
|
* element offset.
|
|
|
|
*
|
|
|
|
* @param offset The offset of the first element viewed by the subspan.
|
|
|
|
*
|
|
|
|
* @param count The number of elements present in the subspan. If Count
|
|
|
|
* is equal to SPAN_DYNAMIC_EXTENT then a span starting at offset and
|
|
|
|
* containing the rest of the elements is returned.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
Span<element_type, SPAN_DYNAMIC_EXTENT> subspan(
|
|
|
|
index_type offset, index_type count = SPAN_DYNAMIC_EXTENT
|
|
|
|
) const {
|
|
|
|
MBED_ASSERT(0 <= offset && offset <= _size);
|
|
|
|
MBED_ASSERT(
|
|
|
|
(count == SPAN_DYNAMIC_EXTENT) ||
|
2018-08-22 08:40:13 +00:00
|
|
|
(0 <= count && (count + offset) <= _size)
|
2018-08-21 17:20:58 +00:00
|
|
|
);
|
|
|
|
return Span<element_type, SPAN_DYNAMIC_EXTENT>(
|
|
|
|
_data + offset,
|
|
|
|
count == SPAN_DYNAMIC_EXTENT ? _size - offset : count
|
|
|
|
);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-21 17:20:58 +00:00
|
|
|
pointer _data;
|
|
|
|
index_type _size;
|
2018-08-19 19:20:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equality operator between two Span objects.
|
|
|
|
*
|
|
|
|
* @param lhs Left hand side of the binary operation.
|
|
|
|
* @param rhs Right hand side of the binary operation.
|
|
|
|
*
|
|
|
|
* @return True if arrays in input have the same size and the same content
|
|
|
|
* and false otherwise.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<typename T, typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
|
|
|
|
bool operator==(const Span<T, LhsExtent> &lhs, const Span<U, RhsExtent> &rhs)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
|
|
|
if (lhs.size() != rhs.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs.data() == rhs.data()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equality operation between a span and a reference to a C++ array.
|
|
|
|
*
|
|
|
|
* @param lhs Left hand side of the binary operation.
|
|
|
|
* @param rhs Right hand side of the binary operation.
|
|
|
|
*
|
|
|
|
* @return True if arrays in input have the same size and the same content
|
|
|
|
* and false otherwise.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
|
|
|
|
bool operator==(const Span<T, LhsExtent> &lhs, T (&rhs)[RhsExtent])
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
|
|
|
return lhs == Span<T>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equality operation between a span and a reference to a C++ array.
|
|
|
|
*
|
|
|
|
* @param lhs Left hand side of the binary operation.
|
|
|
|
* @param rhs Right hand side of the binary operation.
|
|
|
|
*
|
|
|
|
* @return True if arrays in input have the same size and the same content
|
|
|
|
* and false otherwise.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
|
|
|
|
bool operator==(T (&lhs)[LhsExtent], const Span<T, RhsExtent> &rhs)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
|
|
|
return Span<T>(lhs) == rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not equal operator
|
|
|
|
*
|
|
|
|
* @param lhs Left hand side of the binary operation.
|
|
|
|
* @param rhs Right hand side of the binary operation.
|
|
|
|
*
|
|
|
|
* @return True if arrays in input do not have the same size or the same
|
|
|
|
* content and false otherwise.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<typename T, typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
|
|
|
|
bool operator!=(const Span<T, LhsExtent> &lhs, const Span<U, RhsExtent> &rhs)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not Equal operation between a span and a reference to a C++ array.
|
|
|
|
*
|
|
|
|
* @param lhs Left hand side of the binary operation.
|
|
|
|
* @param rhs Right hand side of the binary operation.
|
|
|
|
*
|
|
|
|
* @return True if arrays in input have the same size and the same content
|
|
|
|
* and false otherwise.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
|
|
|
|
bool operator!=(const Span<T, LhsExtent> &lhs, T (&rhs)[RhsExtent])
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-20 10:58:48 +00:00
|
|
|
return !(lhs == Span<T, RhsExtent>(rhs));
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not Equal operation between a span and a reference to a C++ array.
|
|
|
|
*
|
|
|
|
* @param lhs Left hand side of the binary operation.
|
|
|
|
* @param rhs Right hand side of the binary operation.
|
|
|
|
*
|
|
|
|
* @return True if arrays in input have the same size and the same content
|
|
|
|
* and false otherwise.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
|
|
|
|
bool operator!=(T (&lhs)[LhsExtent], const Span<T, RhsExtent> &rhs)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-20 10:58:48 +00:00
|
|
|
return !(Span<T, LhsExtent>(lhs) == rhs);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a Span from a reference to a C/C++ array.
|
|
|
|
*
|
|
|
|
* @tparam T Type of elements held in elements.
|
2018-08-20 10:58:48 +00:00
|
|
|
* @tparam Extent Number of items held in elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
|
|
|
* @param elements The reference to the array viewed.
|
|
|
|
*
|
|
|
|
* @return The Span to elements.
|
|
|
|
*
|
|
|
|
* @note This helper avoids the typing of template parameter when Span is
|
|
|
|
* created 'inline'.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<typename T, size_t Size>
|
|
|
|
Span<T, Size> make_Span(T (&elements)[Size])
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-21 17:20:58 +00:00
|
|
|
return Span<T, Size>(elements);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a Span from a pointer to a C/C++ array.
|
|
|
|
*
|
2018-08-20 10:58:48 +00:00
|
|
|
* @tparam Extent Number of items held in elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
* @tparam T Type of elements held in elements.
|
|
|
|
*
|
|
|
|
* @param elements The reference to the array viewed.
|
|
|
|
*
|
|
|
|
* @return The Span to elements.
|
|
|
|
*
|
|
|
|
* @note This helper avoids the typing of template parameter when Span is
|
|
|
|
* created 'inline'.
|
|
|
|
*/
|
2018-08-21 17:20:58 +00:00
|
|
|
template<ptrdiff_t Extent, typename T>
|
2018-08-20 10:58:48 +00:00
|
|
|
Span<T, Extent> make_Span(T *elements)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-20 10:58:48 +00:00
|
|
|
return Span<T, Extent>(elements, Extent);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a Span from a C/C++ pointer and the size of the array.
|
|
|
|
*
|
|
|
|
* @tparam T Type of elements held in array_ptr.
|
|
|
|
*
|
|
|
|
* @param array_ptr The pointer to the array to viewed.
|
|
|
|
* @param array_size The number of T elements in the array.
|
|
|
|
*
|
|
|
|
* @return The Span to array_ptr with a size of array_size.
|
|
|
|
*
|
|
|
|
* @note This helper avoids the typing of template parameter when Span is
|
|
|
|
* created 'inline'.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2018-08-21 17:20:58 +00:00
|
|
|
Span<T> make_Span(T *array_ptr, ptrdiff_t array_size)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
|
|
|
return Span<T>(array_ptr, array_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a Span to a const content from a reference to a C/C++ array.
|
|
|
|
*
|
|
|
|
* @tparam T Type of elements held in elements.
|
2018-08-20 10:58:48 +00:00
|
|
|
* @tparam Extent Number of items held in elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
*
|
|
|
|
* @param elements The array viewed.
|
|
|
|
* @return The Span to elements.
|
|
|
|
*
|
|
|
|
* @note This helper avoids the typing of template parameter when Span is
|
|
|
|
* created 'inline'.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<typename T, size_t Extent>
|
|
|
|
Span<const T, Extent> make_const_Span(const T (&elements)[Extent])
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-20 10:58:48 +00:00
|
|
|
return Span<const T, Extent>(elements);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a Span to a const content from a pointer to a C/C++ array.
|
|
|
|
*
|
2018-08-20 10:58:48 +00:00
|
|
|
* @tparam Extent Number of items held in elements.
|
2018-08-19 19:20:05 +00:00
|
|
|
* @tparam T Type of elements held in elements.
|
|
|
|
*
|
|
|
|
* @param elements The reference to the array viewed.
|
|
|
|
*
|
|
|
|
* @return The Span to elements.
|
|
|
|
*
|
|
|
|
* @note This helper avoids the typing of template parameter when Span is
|
|
|
|
* created 'inline'.
|
|
|
|
*/
|
2018-08-20 10:58:48 +00:00
|
|
|
template<size_t Extent, typename T>
|
|
|
|
Span<const T, Extent> make_const_Span(const T *elements)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
2018-08-20 10:58:48 +00:00
|
|
|
return Span<const T, Extent>(elements, Extent);
|
2018-08-19 19:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a Span to a const content from a C/C++ pointer and the size of the
|
|
|
|
* array.
|
|
|
|
*
|
|
|
|
* @tparam T Type of elements held in array_ptr.
|
|
|
|
*
|
|
|
|
* @param array_ptr The pointer to the array to viewed.
|
|
|
|
* @param array_size The number of T elements in the array.
|
|
|
|
*
|
|
|
|
* @return The Span to array_ptr with a size of array_size.
|
|
|
|
*
|
|
|
|
* @note This helper avoids the typing of template parameter when Span is
|
|
|
|
* created 'inline'.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2018-08-20 09:47:41 +00:00
|
|
|
Span<const T> make_const_Span(T *array_ptr, size_t array_size)
|
2018-08-19 19:20:05 +00:00
|
|
|
{
|
|
|
|
return Span<const T>(array_ptr, array_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif /* MBED_PLATFORM_SPAN_H_ */
|