OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
SubBareField.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * The IPPL Framework
4  *
5  ***************************************************************************/
6 
7 #ifndef SUB_BARE_FIELD_H
8 #define SUB_BARE_FIELD_H
9 
10 /***************************************************************************
11  SubBareField - represent a view on a given BareField, referring to a
12  subset of the original field data. This is meant as an eventual
13  replacement for IndexedBareField.
14 
15  SubBareField is templated on the T and Dim of the Field, and also on the
16  type of subset object used to refer to the field subsection. This can
17  range from NDIndex for a rectangular block, to SIndex for an arbitrary
18  list of points, to SOffset to store a single point. The behavior of this
19  class, and in particular the iterator for this class, is specialized to
20  each subset object using a traits class 'SFTraits' and a couple of global
21  functions.
22 
23  From a BareField, you create a SubBareField by using the bracket
24  operators [], giving a particular type of data to further index the object.
25  Indexing with and Index or NDIndex object results in a SubBareField object
26  templated on NDIndex, etc. A SubBareField keeps track of the number of
27  'Brackets' its been given; an expression can only involve a SubBareField
28  if Brackets == Dim. Using [] on a SubBareField will generally increase the
29  number of brackets, and result in a new SubBareField incorporating the
30  info about how to select the subset as specified by the object in brackets.
31  For example, Field<T,Dim>[Index] results in a SubBareField<T,Dim,NDIndex<Dim>>
32  object with Brackets==1; further application of [Index] operators increases
33  the number of Brackets and stores each Index in the internal NDIndex at the
34  proper location. SFTraits provides the information on what types of objects
35  can be used to subset, and what type of subset object they produce.
36 
37  For the future, it might be useful to make this 'SubBareField' be the actual
38  class employed by the user, with the current 'BareField' some form of
39  internal class used only to store the data in whatever representation is
40  most efficient.
41  ***************************************************************************/
42 
43 // include files
44 #include "SubField/SubFieldIter.h"
46 #include "PETE/IpplExpressions.h"
47 #include <iostream>
48 
49 // forward declarations
50 template <class T, unsigned Dim, class S> class SubBareField;
51 template <class T, unsigned Dim, class S> class SubFieldIter;
52 template <class T, unsigned Dim, class S>
53 std::ostream& operator<<(std::ostream&,const SubBareField<T,Dim,S>&);
54 
55 
56 template <class T, unsigned Dim, class S>
57 class SubBareField : public PETE_Expr< SubBareField<T,Dim,S> > {
58 
59  friend class BareField<T,Dim>;
60 
61 public:
62  //# public typedefs
63  typedef T T_t;
64  typedef S Index_t;
66 
67  //# public enumerations
68  enum { Dim_u = Dim };
69 
70  constexpr SubBareField(const SubBareField<T, Dim, S>&) = default;
71 
72  // Return the beginning and end iterators for this class.
73  iterator begin() const;
74  iterator end() const;
75 
76  //
77  // accessor functions
78  //
79 
80  // return the 'domain', that is, the information which subsets the field
81  const S& getDomain() const { return MyDomain; }
82 
83  // fill in the second argument with the data for the 'bounding box' of
84  // the domain. This could be the whole field domain, a single point, or
85  // perhaps just a subset of the whole domain.
86  void makeNDIndex(NDIndex<Dim> &i) { iterator::makeNDIndex(MyDomain, i); }
87 
88  // return a reference to the field we are subsetting
89  BareField<T,Dim>& getBareField() const { return A; }
90 
91  // Return a single value.
92  T get() { T r; get(r); return r; }
93  void get(T& r);
94 
95  // Return a typecode for the subset object
96  static int getSubsetType() { return iterator::getSubsetType(); }
97 
98  // check to make sure Dim == Brackets.
99  bool checkBrackets() const { return Brackets == Dim; }
100 
101  //
102  // bracket operators
103  //
104 
105  // bracket operator, which select subsets of the BareField.
106  //mwerks template<class S2>
107  //mwerks SubBareField<T,Dim,typename SubFieldTraits<T,Dim,S,S2>::Return_t>
108  //mwerks operator[](const S2&);
110  // bracket operators, which select subsets of the BareField. This
111  // further subsets from the current SubBareField based on the type of
112  // input subset object.
113  template<class S2>
115  operator[](const S2& s) {
116  // create a new instance of the resulting subset object
117  typename SubFieldTraits<T,Dim,S,S2>::Return_t newdomain;
118 
119 
120 
121  // make sure we can subset by the number of dimensions requested, then
122  // combine the current subset value with the new one
124  if (checkAddBrackets(B)) {
126  Brackets += B;
127  }
128 
129  // return a new SubBareField
130  return SubBareField<T,Dim,
131  typename SubFieldTraits<T,Dim,S,S2>::Return_t>(A,newdomain);
132  }
133 
134 
135  //
136  // assignment operators
137  //
138 
139  // assignment of another SubBareField
141 
142  // assignment of a scalar
144 
145  // assignment of an arbitrary expression
146  //mwerks template<class B>
147  //mwerks SubBareField<T,Dim,S>& operator=(const PETE_Expr<B> &);
149  // assignment of an arbitrary expression
150  template<class B>
153 
154 
155  assign(*this, b);
156  return *this;
157  }
158 
159 
160  //
161  // I/O
162  //
163 
164  void write(std::ostream&);
165 
166  //
167  // PETE interface
168  //
169 
170  enum { IsExpr = 1 };
172  iterator MakeExpression() const { return begin(); }
173 
174  // Pass operator() down to each element of type T.
175  // Could also build versions with 2 or more arguments...
176  // Without member templates we are restricted to passing an integer down.
178  checkBrackets();
179  typedef PETE_TUTree<OpParens<int>, iterator> Elem_t;
180  return Elem_t(arg, begin());
181  }
183  checkBrackets();
185  return Elem_t(std::pair<int,int>(a1,a2), begin());
186  }
187 
188 protected:
189  // the field we are subsetting
191 
192  // the 'domain', that is, the information which subsets the field
194 
195  // the current number of dimensions we have indexed via bracket operators.
196  // An expression can only be carried out if the number of brackets == Dim.
197  unsigned int Brackets;
198 
199  // check to see if it is ok to add the given number of brackets to our
200  // current number
201  bool checkAddBrackets(unsigned int);
202 
203 public:
204  // the class constructor, used by BareField or SubBareField to make a new one
205  //mwerks template<class S2>
206  //mwerks SubBareField(BareField<T,Dim>&, const S2&);
208  // Make the constructor private so that only this class and it's friends
209  // can construct them.
210  template<class S2>
211  SubBareField(BareField<T,Dim>& f, const S2& s) : A(f) {
212 
213 
214 
215  // initialize the subset object, to a state where it can be combined
216  // with the given input data. Then, put in data from given subset object.
218  }
219 
220 };
221 
222 // I/O
223 
224 // write a subfield to the given ostream
225 template<class T, unsigned int Dim, class S>
226 inline
227 std::ostream& operator<<(std::ostream& o, const SubBareField<T,Dim,S>& s) {
228 
229 
230  SubBareField<T,Dim,S>& ncs = const_cast<SubBareField<T,Dim,S>&>(s);
231  ncs.write(o);
232  return o;
233 }
234 
235 
236 #include "SubField/SubBareField.hpp"
237 
238 #endif
const unsigned Dim
void assign(const BareField< T, Dim > &a, RHS b, OP op, ExprTag< true >)
arg(a))
std::ostream & operator<<(std::ostream &, const SubBareField< T, Dim, S > &)
Definition: SubBareField.h:227
Definition: PETE.h:77
iterator begin() const
BareField< T, Dim > & A
Definition: SubBareField.h:190
iterator MakeExpression() const
Definition: SubBareField.h:172
SubFieldIter< T, Dim, S > iterator
Definition: SubBareField.h:65
bool checkBrackets() const
Definition: SubBareField.h:99
const S & getDomain() const
Definition: SubBareField.h:81
SubBareField< T, Dim, S > & operator=(const SubBareField< T, Dim, S > &)
BareField< T, Dim > & getBareField() const
Definition: SubBareField.h:89
PETE_TUTree< OpParens< int >, iterator > operator()(int arg)
Definition: SubBareField.h:177
iterator PETE_Expr_t
Definition: SubBareField.h:171
constexpr SubBareField(const SubBareField< T, Dim, S > &)=default
SubBareField< T, Dim, typename SubFieldTraits< T, Dim, S, S2 >::Return_t > operator[](const S2 &s)
Definition: SubBareField.h:115
unsigned int Brackets
Definition: SubBareField.h:197
iterator end() const
void makeNDIndex(NDIndex< Dim > &i)
Definition: SubBareField.h:86
bool checkAddBrackets(unsigned int)
static int getSubsetType()
Definition: SubBareField.h:96
SubBareField< T, Dim, S > & operator=(const PETE_Expr< B > &b)
Definition: SubBareField.h:152
SubBareField(BareField< T, Dim > &f, const S2 &s)
Definition: SubBareField.h:211
void write(std::ostream &)
PETE_TUTree< OpParens< std::pair< int, int > >, iterator > operator()(int a1, int a2)
Definition: SubBareField.h:182
static int construct(S1 &, const S2 &, BareField< T, Dim > &)
static void combine(const S1 &, const S2 &, S3 &, unsigned int &, BareField< T, Dim > &)