Skip to content

Commit

Permalink
Merge: Some utilities for dim and span
Browse files Browse the repository at this point in the history
This PR adds some useful utilities for dim and span.
1. An ostream << operator overload for dim.
2. Getting the length of a span.
  • Loading branch information
pratikvn committed Jul 12, 2021
2 parents c07a00e + e8c4daf commit 1cf7da6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
20 changes: 20 additions & 0 deletions core/test/base/dim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ TEST(Dim, ConvertsToBool)
}


TEST(Dim, CanAppendToStream1)
{
gko::dim<2> d2{2, 3};

std::ostringstream os;
os << d2;
ASSERT_EQ(os.str(), "(2, 3)");
}


TEST(Dim, CanAppendToStream2)
{
gko::dim<3> d2{2, 3, 4};

std::ostringstream os;
os << d2;
ASSERT_EQ(os.str(), "(2, 3, 4)");
}


TEST(Dim, EqualityReturnsTrueWhenEqual)
{
ASSERT_TRUE(gko::dim<2>(2, 3) == gko::dim<2>(2, 3));
Expand Down
15 changes: 11 additions & 4 deletions core/test/base/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ TEST(Span, CreatesPoint)
}


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 +652,7 @@ TEST(Range, DividesScalarAndRange)
}


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

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


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

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


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

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


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

Expand Down
38 changes: 38 additions & 0 deletions include/ginkgo/core/base/dim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define GKO_PUBLIC_CORE_BASE_DIM_HPP_


#include <iostream>


#include <ginkgo/core/base/types.hpp>


Expand All @@ -51,6 +54,7 @@ namespace gko {
template <size_type Dimensionality, typename DimensionType = size_type>
struct dim {
static constexpr size_type dimensionality = Dimensionality;
friend class dim<dimensionality + 1>;

using dimension_type = DimensionType;

Expand Down Expand Up @@ -152,7 +156,30 @@ struct dim {
return dim(x.first_ * y.first_, x.rest_ * y.rest_);
}

/**
* A stream operator overload for dim
*
* @param os stream object
* @param x dim object
*
* @return a stream object appended with the dim output
*/
friend std::ostream &operator<<(std::ostream &os, const dim &x)
{
os << "(";
x.print_to(os);
os << ")";
return os;
}

private:
void inline print_to(std::ostream &os) const
{
os << first_ << ", ";
rest_.print_to(os);
}


constexpr GKO_ATTRIBUTES dim(const dimension_type first,
dim<dimensionality - 1> rest)
: first_{first}, rest_{rest}
Expand All @@ -167,6 +194,7 @@ struct dim {
template <typename DimensionType>
struct dim<1u, DimensionType> {
static constexpr size_type dimensionality = 1u;
friend class dim<2>;

using dimension_type = DimensionType;

Expand Down Expand Up @@ -200,7 +228,17 @@ struct dim<1u, DimensionType> {
return dim(x.first_ * y.first_);
}

friend std::ostream &operator<<(std::ostream &os, const dim &x)
{
os << "(";
x.print_to(os);
os << ")";
return os;
}

private:
void inline print_to(std::ostream &os) const { os << first_; }

dimension_type first_;
};

Expand Down
13 changes: 12 additions & 1 deletion include/ginkgo/core/base/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,18 @@ struct span {
*
* @return true if and only if `this->begin < this->end`
*/
constexpr bool is_valid() const { return begin < end; }
GKO_ATTRIBUTES constexpr bool is_valid() const { return begin < end; }

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

/**
* Beginning of the span.
Expand Down

0 comments on commit 1cf7da6

Please sign in to comment.