Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
otb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julien Osman
otb
Commits
38185c42
Commit
38185c42
authored
4 years ago
by
Luc Hermitte
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Improve `Span` documentation and add `subspan`
parent
6c6b133f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Modules/Core/Common/include/otbSpan.h
+29
-2
29 additions, 2 deletions
Modules/Core/Common/include/otbSpan.h
with
29 additions
and
2 deletions
Modules/Core/Common/include/otbSpan.h
+
29
−
2
View file @
38185c42
...
@@ -24,12 +24,25 @@
...
@@ -24,12 +24,25 @@
#include
<type_traits>
#include
<type_traits>
#include
<iterator>
#include
<iterator>
#include
<limits>
#include
<cassert>
#include
<cassert>
namespace
otb
namespace
otb
{
{
/** Span class inspired by C++20 standard.
/** Span class inspired by C++20 standard.
*
* \invariant `size() == 0 or data() != nullptr`
*
* \note Unlike C++20 `std::span` this implementation doesn't follow Lakos
* Rule but instead non-throwing functions are `noexcept` as suggested in
* https://wg21.link/p1656. Beware to not expect `operator[]` to always be
* `noexcept` as it won't be anymore once this class is deprecated in favour
* of `std::span` in a few years.
*
* \note This implementation only support spans with dynamic extents. Static
* extents are not supported (yet?)
*
* \todo fix RW / RO interface
* \todo fix RW / RO interface
* \author Luc Hermitte (CS Group)
* \author Luc Hermitte (CS Group)
* \copyright CNES
* \copyright CNES
...
@@ -75,7 +88,7 @@ template <typename T> struct Span
...
@@ -75,7 +88,7 @@ template <typename T> struct Span
* \pre The Container shall be contiguous
* \pre The Container shall be contiguous
* \warning The lifetime of the span shall not exceed the one of the container.
* \warning The lifetime of the span shall not exceed the one of the container.
* Be sure to not store the span locally, and initialize it from a rvalue.
* Be sure to not store the span locally, and initialize it from a rvalue.
* The use case where a span is initialize from a rvalue shall be restricted
* The use case where a span is initialize
d
from a rvalue shall be restricted
* to function parameters.
* to function parameters.
* \code
* \code
* std::vector<T> f();
* std::vector<T> f();
...
@@ -88,10 +101,11 @@ template <typename T> struct Span
...
@@ -88,10 +101,11 @@ template <typename T> struct Span
* \todo static_assert the container is contiguous
* \todo static_assert the container is contiguous
*/
*/
template
<
class
Container
>
constexpr
Span
(
Container
&&
cont
)
noexcept
template
<
class
Container
>
constexpr
Span
(
Container
&&
cont
)
noexcept
:
Span
(
&
cont
[
0
]
,
cont
.
size
())
:
Span
(
cont
.
data
()
,
cont
.
size
())
{
{
// We cannot use op[] which has an assertion sometimes.
// We cannot use op[] which has an assertion sometimes.
// assert(&const[size()] == (&cont[0] + size()));
// assert(&const[size()] == (&cont[0] + size()));
// Beside, it's not noexcept.
}
}
template
<
class
U
>
constexpr
Span
(
const
otb
::
Span
<
U
>&
s
)
noexcept
template
<
class
U
>
constexpr
Span
(
const
otb
::
Span
<
U
>&
s
)
noexcept
:
Span
(
s
.
data
(),
s
.
size
())
:
Span
(
s
.
data
(),
s
.
size
())
...
@@ -153,6 +167,19 @@ template <typename T> struct Span
...
@@ -153,6 +167,19 @@ template <typename T> struct Span
{
assert
(
n
<
size
());
return
Span
(
data
(),
n
);}
{
assert
(
n
<
size
());
return
Span
(
data
(),
n
);}
constexpr
Span
last
(
index_type
n
)
const
noexcept
constexpr
Span
last
(
index_type
n
)
const
noexcept
{
assert
(
n
<
size
());
return
Span
(
data
()
-
n
,
n
);}
{
assert
(
n
<
size
());
return
Span
(
data
()
-
n
,
n
);}
constexpr
Span
subspan
(
index_type
offset
,
index_type
count
=
std
::
numeric_limits
<
index_type
>::
max
())
noexcept
{
assert
(
offset
<=
size
());
if
(
count
==
std
::
numeric_limits
<
index_type
>::
max
())
{
count
=
size
()
-
offset
;
}
assert
(
count
<=
(
size
()
-
offset
));
return
Span
(
data
()
+
offset
,
count
);
}
//@}
//@}
private
:
private
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment