| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <ostream> |
| 14 |
|
| 15 |
//------------------------------------------------------------------------------ |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
namespace array { |
| 19 |
|
| 20 |
//------------------------------------------------------------------------------ |
| 21 |
|
| 22 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 23 |
namespace helpers { |
| 24 |
|
| 25 |
//------------------------------------------------------------------------------ |
| 26 |
|
| 27 |
class RangeBase {}; |
| 28 |
|
| 29 |
//------------------------------------------------------------------------------ |
| 30 |
|
| 31 |
class RangeFrom : public RangeBase { |
| 32 |
public: |
| 33 |
RangeFrom(int start): start_(start) {} |
| 34 |
|
| 35 |
int start() const { return start_; } |
| 36 |
|
| 37 |
template <int Dim, typename View> |
| 38 |
int end(const View& view) const { |
| 39 |
return view.shape(Dim); |
| 40 |
} |
| 41 |
|
| 42 |
template <typename View> |
| 43 |
int end(const View& view, int i) const { |
| 44 |
return view.shape(i); |
| 45 |
} |
| 46 |
|
| 47 |
private: |
| 48 |
int start_; |
| 49 |
}; |
| 50 |
|
| 51 |
//------------------------------------------------------------------------------ |
| 52 |
|
| 53 |
class RangeTo : public RangeBase { |
| 54 |
public: |
| 55 |
RangeTo(int end): end_(end) {} |
| 56 |
|
| 57 |
int start() const { return 0; } |
| 58 |
|
| 59 |
int end() const { return end_; } |
| 60 |
|
| 61 |
private: |
| 62 |
int end_; |
| 63 |
}; |
| 64 |
|
| 65 |
//------------------------------------------------------------------------------ |
| 66 |
|
| 67 |
class RangeAll : public RangeBase { |
| 68 |
public: |
| 69 |
int start() const { return 0; } |
| 70 |
|
| 71 |
template <int Dim, typename View> |
| 72 |
int end(const View& view) const { |
| 73 |
return view.shape(Dim); |
| 74 |
} |
| 75 |
|
| 76 |
template <typename View> |
| 77 |
int end(const View& view, int i) const { |
| 78 |
return view.shape(i); |
| 79 |
} |
| 80 |
friend std::ostream& operator<<(std::ostream& out, const RangeAll& range) { |
| 81 |
out << ":"; |
| 82 |
return out; |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
class RangeDummy : public RangeBase {}; |
| 87 |
|
| 88 |
//------------------------------------------------------------------------------ |
| 89 |
|
| 90 |
} // namespace helpers |
| 91 |
#endif |
| 92 |
|
| 93 |
//------------------------------------------------------------------------------ |
| 94 |
|
| 95 |
class Range : public helpers::RangeBase { |
| 96 |
private: |
| 97 |
using From = helpers::RangeFrom; |
| 98 |
using To = helpers::RangeTo; |
| 99 |
using All = helpers::RangeAll; |
| 100 |
using Dummy = helpers::RangeDummy; |
| 101 |
|
| 102 |
public: |
| 103 |
static From from(int start) { return From(start); } |
| 104 |
static To to(int end) { return To(end); } |
| 105 |
static constexpr All all() { return All(); } |
| 106 |
static constexpr Dummy dummy() { return Dummy(); } |
| 107 |
|
| 108 |
public: |
| 109 |
template <typename Start, typename End> |
| 110 |
Range(Start start, End end): start_(static_cast<int>(start)), end_(static_cast<int>(end)) {} |
| 111 |
int start() const { return start_; } |
| 112 |
int end() const { return end_; } |
| 113 |
|
| 114 |
Range(): Range(0, 0) {} |
| 115 |
|
| 116 |
private: |
| 117 |
int start_; |
| 118 |
int end_; |
| 119 |
}; |
| 120 |
|
| 121 |
//------------------------------------------------------------------------------ |
| 122 |
|
| 123 |
} // namespace array |
| 124 |
} // namespace atlas |