Skip to content

Commit

Permalink
Add span length and addition
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikvn committed Jul 6, 2021
1 parent d4fe45e commit 65b8cab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
21 changes: 17 additions & 4 deletions core/test/base/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ TEST(Span, CreatesPoint)
}


TEST(Span, AddsSpan)
{
ASSERT_EQ(gko::span(6, 10), gko::span(2, 3) + gko::span(4, 7));
}


TEST(Span, KnowsItsLength)
{
gko::span s{3, 5};
ASSERT_EQ(2, s.length());
}


TEST(Span, LessThanEvaluatesToTrue)
{
ASSERT_TRUE(gko::span(2, 3) < gko::span(4, 7));
Expand Down Expand Up @@ -645,7 +658,7 @@ TEST(Range, DividesScalarAndRange)
}


TEST(Range, AddsRangeAndSclar)
TEST(Range, AddsRangeAndScalar)
{
dummy_range r{5u, 1, 2};

Expand All @@ -656,7 +669,7 @@ TEST(Range, AddsRangeAndSclar)
}


TEST(Range, SubtractsRangeAndSclar)
TEST(Range, SubtractsRangeAndScalar)
{
dummy_range r{5u, 1, 2};

Expand All @@ -667,7 +680,7 @@ TEST(Range, SubtractsRangeAndSclar)
}


TEST(Range, MultipliesRangeAndSclar)
TEST(Range, MultipliesRangeAndScalar)
{
dummy_range r{5u, 1, 2};

Expand All @@ -678,7 +691,7 @@ TEST(Range, MultipliesRangeAndSclar)
}


TEST(Range, DividesRangeAndSclar)
TEST(Range, DividesRangeAndScalar)
{
dummy_range r{5u, 1, 2};

Expand Down
18 changes: 18 additions & 0 deletions include/ginkgo/core/base/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ struct span {
*/
constexpr bool is_valid() const { return begin < end; }

/**
* Returns the length of a span.
*
* @return `this->end - this->begin`
*/
constexpr size_type length() const
{
GKO_ASSERT(is_valid());
return end - begin;
}

/**
* Beginning of the span.
*/
Expand Down Expand Up @@ -155,6 +166,13 @@ GKO_ATTRIBUTES GKO_INLINE constexpr bool operator!=(const span &first,
}


GKO_ATTRIBUTES GKO_INLINE constexpr span operator+(const span &first,
const span &second)
{
return gko::span{first.begin + second.begin, first.end + second.end};
}


namespace detail {


Expand Down

0 comments on commit 65b8cab

Please sign in to comment.