atlas
Range.h
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 //------------------------------------------------------------------------------
14 
15 namespace atlas {
16 namespace array {
17 
18 //------------------------------------------------------------------------------
19 
20 #ifndef DOXYGEN_SHOULD_SKIP_THIS
21 namespace helpers {
22 
23 //------------------------------------------------------------------------------
24 
25 class RangeBase {};
26 
27 //------------------------------------------------------------------------------
28 
29 class RangeFrom : public RangeBase {
30 public:
31  RangeFrom( int start ) : start_( start ) {}
32 
33  int start() const { return start_; }
34 
35  template <int Dim, typename View>
36  int end( const View& view ) const {
37  return view.shape( Dim );
38  }
39 
40  template <typename View>
41  int end( const View& view, int i ) const {
42  return view.shape( i );
43  }
44 
45 private:
46  int start_;
47 };
48 
49 //------------------------------------------------------------------------------
50 
51 class RangeTo : public RangeBase {
52 public:
53  RangeTo( int end ) : end_( end ) {}
54 
55  int start() const { return 0; }
56 
57  int end() const { return end_; }
58 
59 private:
60  int end_;
61 };
62 
63 //------------------------------------------------------------------------------
64 
65 class RangeAll : public RangeBase {
66 public:
67  int start() const { return 0; }
68 
69  template <int Dim, typename View>
70  int end( const View& view ) const {
71  return view.shape( Dim );
72  }
73 
74  template <typename View>
75  int end( const View& view, int i ) const {
76  return view.shape( i );
77  }
78 };
79 
80 class RangeDummy : public RangeBase {};
81 
82 //------------------------------------------------------------------------------
83 
84 } // namespace helpers
85 #endif
86 
87 //------------------------------------------------------------------------------
88 
89 class Range : public helpers::RangeBase {
90 private:
91  using From = helpers::RangeFrom;
92  using To = helpers::RangeTo;
93  using All = helpers::RangeAll;
94  using Dummy = helpers::RangeDummy;
95 
96 public:
97  static From from( int start ) { return From( start ); }
98  static To to( int end ) { return To( end ); }
99  static All all() { return All(); }
100  static Dummy dummy() { return Dummy(); }
101 
102 public:
103  template <typename Start, typename End>
104  Range( Start start, End end ) : start_( static_cast<int>( start ) ), end_( static_cast<int>( end ) ) {}
105  int start() const { return start_; }
106  int end() const { return end_; }
107 
108  Range() : Range( 0, 0 ) {}
109 
110 private:
111  int start_;
112  int end_;
113 };
114 
115 //------------------------------------------------------------------------------
116 
117 } // namespace array
118 } // namespace atlas
Definition: Range.h:29
Definition: Range.h:51
Definition: Range.h:65
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
Definition: Range.h:25
Definition: ArrayViewDefs.h:20
Definition: Range.h:89
Definition: Range.h:80