Jpp
JGrid.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JGRID__
2 #define __JTOOLS__JGRID__
3 
4 #include <istream>
5 #include <ostream>
6 
8 #include "JLang/JClass.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JTOOLS {}
16 namespace JPP { using namespace JTOOLS; }
17 
18 namespace JTOOLS {
19 
20  using JLANG::JClass;
21 
22  template<class JElement_t, class JDistance_t>
23  class JCollection;
24 
25 
26  /**
27  * Simple data structure for an abstract collection of equidistant abscissa values.
28  *
29  * This class implements the JAbstractCollection interface.
30  */
31  template<class JAbscissa_t>
32  struct JGrid :
33  public JAbstractCollection<JAbscissa_t>
34  {
36 
37 
38  /**
39  * Default constructor.
40  */
41  JGrid() :
42  size(0),
43  xmin(),
44  xmax()
45  {}
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param nx number of elements
52  * \param Xmin lower limit
53  * \param Xmax upper limit
54  */
55  JGrid(const int nx,
56  const abscissa_type Xmin,
57  const abscissa_type Xmax) :
58  size(nx),
59  xmin(Xmin),
60  xmax(Xmax)
61  {}
62 
63 
64  /**
65  * Get number of elements.
66  *
67  * \return number of elements
68  */
69  virtual int getSize() const
70  {
71  return size;
72  }
73 
74 
75  /**
76  * Get abscissa value.
77  *
78  * \param index index
79  * \return abscissa value
80  */
81  virtual abscissa_type getX(int index) const
82  {
83  return xmin + index * ((xmax - xmin) / (size - 1));
84  }
85 
86 
87  /**
88  * Get minimal abscissa value.
89  *
90  * \return abscissa value
91  */
92  virtual abscissa_type getXmin() const
93  {
94  return xmin;
95  }
96 
97 
98  /**
99  * Get maximal abscissa value.
100  *
101  * \return abscissa value
102  */
103  virtual abscissa_type getXmax() const
104  {
105  return xmax;
106  }
107 
108 
109  /**
110  * Get index of given abscissa value.
111  *
112  * \param x abscissa value
113  * \return index
114  */
116  {
117  return (int) ((size - 1) * (x - xmin) / (xmax - xmin));
118  }
119 
120 
121  /**
122  * Configure collection.
123  *
124  * \param collection collection
125  * \return this grid
126  */
127  template<class JElement_t, class JDistance_t>
129  {
130  collection.configure(*this);
131 
132  return *this;
133  }
134 
135 
136  /**
137  * Read grid from input.
138  *
139  * \param in input stream
140  * \param grid grid
141  * \return input stream
142  */
143  friend inline std::istream& operator>>(std::istream& in, JGrid<JAbscissa_t>& grid)
144  {
145  return in >> grid.size >> grid.xmin >> grid.xmax;
146  }
147 
148 
149  /**
150  * Write grid to output.
151  *
152  * \param out output stream
153  * \param grid grid
154  * \return output stream
155  */
156  friend inline std::ostream& operator<<(std::ostream& out, const JGrid<JAbscissa_t>& grid)
157  {
158  return out << grid.size << ' ' << grid.xmin << ' ' << grid.xmax;
159  }
160 
161  protected:
162  int size;
165  };
166 
167 
168  /**
169  * Helper method for JGrid.
170  *
171  * \param nx number of elements
172  * \param Xmin lower limit
173  * \param Xmax upper limit
174  * \return grid
175  */
176  template<class JAbscissa_t>
177  inline JGrid<JAbscissa_t> make_grid(const int nx,
178  const JAbscissa_t Xmin,
179  const JAbscissa_t Xmax)
180  {
181  return JGrid<JAbscissa_t>(nx, Xmin, Xmax);
182  }
183 }
184 
185 #endif
JTOOLS::JAbstractCollection
Abstract interface for abscissa values of a collection of elements.
Definition: JAbstractCollection.hh:18
JTOOLS::JGrid::operator()
const JGrid & operator()(JCollection< JElement_t, JDistance_t > &collection) const
Configure collection.
Definition: JGrid.hh:128
JTOOLS::JGrid::abscissa_type
JAbstractCollection< JAbscissa_t >::abscissa_type abscissa_type
Definition: JGrid.hh:35
JLANG::JClass::argument_type
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
JTOOLS::JGrid::xmax
abscissa_type xmax
Definition: JGrid.hh:164
JTOOLS::JGrid::getX
virtual abscissa_type getX(int index) const
Get abscissa value.
Definition: JGrid.hh:81
JTOOLS::JGrid::JGrid
JGrid(const int nx, const abscissa_type Xmin, const abscissa_type Xmax)
Constructor.
Definition: JGrid.hh:55
JLANG::JClass
Template for generic class types.
Definition: JClass.hh:80
JTOOLS::JGrid::operator<<
friend std::ostream & operator<<(std::ostream &out, const JGrid< JAbscissa_t > &grid)
Write grid to output.
Definition: JGrid.hh:156
JTOOLS::JGrid::JGrid
JGrid()
Default constructor.
Definition: JGrid.hh:41
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTOOLS::JGrid
Simple data structure for an abstract collection of equidistant abscissa values.
Definition: JGrid.hh:32
JTOOLS::make_grid
JGrid< JAbscissa_t > make_grid(const int nx, const JAbscissa_t Xmin, const JAbscissa_t Xmax)
Helper method for JGrid.
Definition: JGrid.hh:177
JTOOLS::JGrid::size
int size
Definition: JGrid.hh:162
JTOOLS::JGrid::getSize
virtual int getSize() const
Get number of elements.
Definition: JGrid.hh:69
JClass.hh
JTOOLS::JCollection
General purpose class for collection of elements, see: <a href="JTools.PDF";>Collection of elements...
Definition: JCollection.hh:71
JTOOLS::JGrid::xmin
abscissa_type xmin
Definition: JGrid.hh:163
JTOOLS
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Definition: JAbstractCollection.hh:9
JAbstractCollection.hh
JTOOLS::JCollection::configure
void configure(const JAbstractCollection< abscissa_type > &bounds)
Configure collection.
Definition: JCollection.hh:314
JTOOLS::JGrid::operator>>
friend std::istream & operator>>(std::istream &in, JGrid< JAbscissa_t > &grid)
Read grid from input.
Definition: JGrid.hh:143
JTOOLS::JAbstractCollection::abscissa_type
JAbscissa_t abscissa_type
Definition: JAbstractCollection.hh:20
JTOOLS::JGrid::getXmax
virtual abscissa_type getXmax() const
Get maximal abscissa value.
Definition: JGrid.hh:103
JTOOLS::JGrid::getIndex
int getIndex(typename JClass< abscissa_type >::argument_type x) const
Get index of given abscissa value.
Definition: JGrid.hh:115
JTOOLS::JGrid::getXmin
virtual abscissa_type getXmin() const
Get minimal abscissa value.
Definition: JGrid.hh:92