Skip to content

Implements iterators for the Span class to be compatible with std::span #13252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions platform/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define MBED_PLATFORM_SPAN_H_

#include <algorithm>
#include <iterator>
#include <stddef.h>
#include <stdint.h>

Expand Down Expand Up @@ -233,6 +234,16 @@ struct Span {
*/
typedef element_type &reference;

/**
* Iterator to an ElementType
*/
typedef pointer iterator;

/**
* Reverse iterator to an ElementType
*/
typedef std::reverse_iterator<iterator> reverse_iterator;

/**
* Size of the Extent; -1 if dynamic.
*/
Expand Down Expand Up @@ -349,6 +360,46 @@ struct Span {
return size() == 0;
}

/**
* Return an iterator to the first element of the sequence.
*
* @return An iterator to the first element of the sequence.
*/
iterator begin() const
{
return _data;
}

/**
* Return an iterator to the element following the last element of the sequence.
*
* @return An iterator to the element following the last element of the sequence.
*/
iterator end() const
{
return _data + Extent;
}

/**
* Return a reverse_iterator to the first element of the reversed sequence.
*
* @return A reverse_iterator to the first element of the reversed sequence.
*/
reverse_iterator rbegin() const
{
return reverse_iterator(end());
}

/**
* Return a reverse_iterator to the element following the last element of the reversed sequence.
*
* @return A reverse_iterator to the element following the last element of the reversed sequence.
*/
reverse_iterator rend() const
{
return reverse_iterator(begin());
}

/**
* Returns a reference to the element at position @p index.
*
Expand Down Expand Up @@ -534,6 +585,16 @@ struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
*/
typedef element_type &reference;

/**
* Iterator to an ElementType
*/
typedef pointer iterator;

/**
* Reverse iterator to an ElementType
*/
typedef std::reverse_iterator<iterator> reverse_iterator;

/**
* Size of the Extent; -1 if dynamic.
*/
Expand Down Expand Up @@ -644,6 +705,46 @@ struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
return size() == 0;
}

/**
* Return an iterator to the first element of the sequence.
*
* @return An iterator to the first element of the sequence.
*/
iterator begin() const
{
return _data;
}

/**
* Return an iterator to the element following the last element of the sequence.
*
* @return An iterator to the element following the last element of the sequence.
*/
iterator end() const
{
return _data + _size;
}

/**
* Return a reverse_iterator to the first element of the reversed sequence.
*
* @return A reverse_iterator to the first element of the reversed sequence.
*/
reverse_iterator rbegin() const
{
return reverse_iterator(end());
}

/**
* Return a reverse_iterator to the element following the last element of the reversed sequence.
*
* @return A reverse_iterator to the element following the last element of the reversed sequence.
*/
reverse_iterator rend() const
{
return reverse_iterator(begin());
}

/**
* Access to an element of the sequence.
*
Expand Down