Skip to content

Commit

Permalink
Merge pull request #13252 from marcemmers/span-add-begin-end
Browse files Browse the repository at this point in the history
Implements iterators for the Span class to be compatible with std::span
  • Loading branch information
0xc0170 authored Jul 9, 2020
2 parents 947eb7a + a20ff63 commit aa605bb
Showing 1 changed file with 101 additions and 0 deletions.
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

0 comments on commit aa605bb

Please sign in to comment.