Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGridCollection.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JGRIDCOLLECTION__
2 #define __JTOOLS__JGRIDCOLLECTION__
3 
4 #include "JLang/JClass.hh"
5 #include "JTools/JCollection.hh"
6 
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JTOOLS {}
13 namespace JPP { using namespace JTOOLS; }
14 
15 namespace JTOOLS {
16 
17  using JLANG::JClass;
18 
19 
20  /**
21  * General purpose class for collection of equidistant elements.
22  *
23  * For a collection with equidistant elements, the index of the nearest element can directly be computed
24  * based on the minimal abscissa value, the maximal abscissa value and the number of elements in the collection.
25  * The lower_bound methods are re-implemented in this class which otherwise simply derives from JCollection.
26  *
27  * For convenience, the implementation of the standard map operator <tt>[]</tt> of the JCollection class is maintained.
28  */
29  template<class JElement_t, class JDistance_t = JDistance<typename JElement_t::abscissa_type> >
31  public JCollection<JElement_t, JDistance_t>
32  {
33  public:
34 
36 
40 
45 
47 
48 
49  /**
50  * Default constructor.
51  */
53  {}
54 
55 
56  /**
57  * Get index of given abscissa value.
58  *
59  * For values within the range of this collection, the index starts at zero and ends at number of elements minus one.
60  * Note that the index could be less than zero or larger than (or equal to) the number of elements,
61  * if the given abscissa value is outside the range of this collection.
62  *
63  * \param x abscissa value
64  * \return index
65  */
67  {
68  return (int) ((this->size() - 1) * (x - this->begin()->getX()) / (this->rbegin()->getX() - this->begin()->getX()));
69  }
70 
71 
72  /**
73  * Get first position of element <tt>i</tt>, where <tt>x >= i->getX()</tt>.
74  *
75  * \param x abscissa value
76  * \return position of corresponding element
77  */
79  {
80  const int index = getIndex(x) + 1;
81 
82  if (index <= 0)
83  return this->begin();
84  else if (index >= (int) this->size())
85  return this->end();
86  else
87  return this->begin() + index;
88  }
89 
90 
91  /**
92  * Get first position of element <tt>i</tt>, where <tt>x >= i->getX()</tt>.
93  *
94  * \param x abscissa value
95  * \return position of corresponding element
96  */
98  {
99  const int index = getIndex(x) + 1;
100 
101  if (index <= 0)
102  return this->begin();
103  else if (index >= (int) this->size())
104  return this->end();
105  else
106  return this->begin() + index;
107  }
108  };
109 }
110 
111 #endif
General purpose class for collection of elements, see: &lt;a href=&quot;JTools.PDF&quot;;&gt;Collection of elements...
Definition: JCollection.hh:73
collection_type::const_iterator const_iterator
JGridCollection()
Default constructor.
JElement2D< JKey_t, JHistogram_t >::abscissa_type abscissa_type
Definition: JCollection.hh:82
collection_type::value_type value_type
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
collection_type::abscissa_type abscissa_type
int getIndex(typename JClass< abscissa_type >::argument_type x) const
Get index of given abscissa value.
General purpose class for collection of equidistant elements.
const_iterator lower_bound(typename JClass< abscissa_type >::argument_type x) const
Get first position of element i, where x &gt;= i-&gt;getX().
General purpose class for a collection of sorted elements.
collection_type::iterator iterator
Template for generic class types.
Definition: JClass.hh:80
JElement2D< JKey_t, JHistogram_t >::ordinate_type ordinate_type
Definition: JCollection.hh:83
collection_type::const_reverse_iterator const_reverse_iterator
collection_type::reverse_iterator reverse_iterator
collection_type::pair_type pair_type
2D Element.
Definition: JElement.hh:46
collection_type::ordinate_type ordinate_type
iterator lower_bound(typename JClass< abscissa_type >::argument_type x)
Get first position of element i, where x &gt;= i-&gt;getX().
JCollection< JElement_t, JDistance_t > collection_type