Jpp  debug
the software that should make you happy
JVectorize.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JVECTORIZE__
2 #define __JLANG__JVECTORIZE__
3 
4 #include <ostream>
5 #include <vector>
6 #include <functional>
7 
8 #include "JLang/JSTDTypes.hh"
9 #include "JLang/JClass.hh"
10 
11 
12 /**
13  * \file
14  * Auxiliary methods to convert data members or return values of member methods of a set of objects to a single vector.
15  * \author mdejong
16  */
17 namespace JLANG {}
18 namespace JPP { using namespace JLANG; }
19 
20 namespace JLANG {
21 
22  /**
23  * Auxiliary data structure for return type of make methods.
24  */
25  template<class JElement_t, class JAllocator_t = std::allocator<JElement_t> >
26  struct array_type :
27  public std::vector<JElement_t, JAllocator_t>
28  {
29  /**
30  * Write array to output stream.
31  *
32  * \param out output stream
33  * \param object array
34  * \return output stream
35  */
36  friend inline std::ostream& operator<<(std::ostream& out, const array_type& object)
37  {
38  for (typename array_type::const_iterator i = object.begin(); i != object.end(); ++i) {
39  out << ' ' << *i;
40  }
41 
42  return out;
43  }
44  };
45 
46 
47  /**
48  * Method to create array of values.
49  *
50  * \param array c-array of values
51  * \return data
52  */
53  template<class JValue_t, size_t N>
54  inline const array_type<JValue_t>& make_array(const JValue_t (&array)[N])
55  {
56  static array_type<JValue_t> buffer;
57 
58  buffer.resize(N);
59 
60  for (size_t i = 0; i != N; ++i) {
61  buffer[i] = array[i];
62  }
63 
64  return buffer;
65  }
66 
67 
68  /**
69  * Method to create array of values.
70  *
71  * \param __begin begin of data
72  * \param __end end of data
73  * \return data
74  */
75  template<class T>
77  {
79 
80  buffer.assign(__begin, __end);
81 
82  return buffer;
83  }
84 
85 
86  /**
87  * Method to create array of values of data member.
88  *
89  * \param __begin begin of data
90  * \param __end end of data
91  * \param value pointer to data member
92  * \return data
93  */
94  template<class T, class JType_t, class JValue_t>
95  inline const array_type<typename JClass<JValue_t>::value_type>& make_array(T __begin, T __end, JValue_t const JType_t::*value)
96  {
98 
99  buffer.clear();
100 
101  for (T __p = __begin; __p != __end; ++__p) {
102  buffer.push_back(*__p.*value);
103  }
104 
105  return buffer;
106  }
107 
108 
109  /**
110  * Method to create array of return values of member method.
111  *
112  * \param __begin begin of data
113  * \param __end end of data
114  * \param function pointer to member method
115  * \return data
116  */
117  template<class T, class JType_t, class JValue_t>
118  inline const array_type<typename JClass<JValue_t>::value_type>& make_array(T __begin, T __end, JValue_t (JType_t::*function)() const)
119  {
121 
122  buffer.clear();
123 
124  for (T __p = __begin; __p != __end; ++__p) {
125  buffer.push_back((*__p.*function)());
126  }
127 
128  return buffer;
129  }
130 
131 
132  /**
133  * Method to create array of keys of map.
134  *
135  * \param data data
136  * \return data
137  */
138  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
140  {
142  }
143 
144 
145  /**
146  * Method to create array of values of map.
147  *
148  * \param data data
149  * \return data
150  */
151  template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
153  {
155  }
156 
157 
158  /**
159  * Method to exclude outliers from already sorted data.\n
160  * Note that the part after the returned iterator will be overwritten.
161  *
162  * \param __begin begin of data
163  * \param __end end of data
164  * \param value pointer to data member,
165  * \param comparator comparison method
166  * \return end of sorted data
167  */
168  template<class T, class JResult_t, class JComparator_t>
169  T make_set(T __begin,
170  T __end,
171  JResult_t std::iterator_traits<T>::value_type::*value,
172  const JComparator_t& comparator)
173 
174  {
175  T p2 = __begin;
176  T p0 = p2++;
177  T p1 = p2++;
178 
179  if (p0 == __end) { return p0; }
180  if (p1 == __end) { return p1; }
181 
182  if (p2 == __end) {
183  if (comparator((*p0).*value, (*p1).*value))
184  return p2;
185  else
186  return p0;
187  }
188 
189  for ( ; p2 != __end; ++p2) {
190 
191  if (comparator((*p0).*value, (*p1).*value)) {
192  if (comparator((*p1).*value, (*p2).*value)) {
193  *(++p0) = *p1;
194  *(++p1) = *p2;
195  } else if (comparator((*p0).*value, (*p2).*value)) {
196  *p1 = *p2;
197  }
198  } else {
199  if (comparator((*p2).*value, (*p0).*value)) {
200  *p0 = *p1;
201  }
202  *p1 = *p2;
203  }
204  }
205 
206  return ++p1;
207  }
208 
209 
210  /**
211  * Get maximum of values.
212  *
213  * \param buffer input values
214  * \param value start value
215  * \return maximum value
216  */
217  template<class T>
218  inline T getMaximum(const array_type<T>& buffer, const T value)
219  {
220  T max = value;
221 
222  for (typename array_type<T>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
223  if (*i > max) {
224  max = *i;
225  }
226  }
227 
228  return max;
229  }
230 
231 
232  /**
233  * Get minimum of values.
234  *
235  * \param buffer input values
236  * \param value start value
237  * \return minimum value
238  */
239  template<class T>
240  inline T getMinimum(const array_type<T>& buffer, const T value)
241  {
242  T min = value;
243 
244  for (typename array_type<T>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
245  if (*i < min) {
246  min = *i;
247  }
248  }
249 
250  return min;
251  }
252 
253 
254  /**
255  * Count number of unique values.
256  *
257  * \param compare compare
258  * \return number of unique values
259  */
260  template<class T, class JCompare_t>
261  size_t getCount(const array_type<T>& buffer, const JCompare_t& compare)
262  {
263  size_t n = 0;
264 
265  for (typename array_type<T>::const_iterator p = buffer.begin(); p != buffer.end(); ++p) {
266 
267  typename array_type<T>::const_iterator q = p;
268 
269  while (++q != buffer.end() && !compare(*p,*q)) {}
270 
271  if (q == buffer.end()) {
272  n += 1;
273  }
274  }
275 
276  return n;
277  }
278 
279 
280  /**
281  * Count number of unique values.
282  *
283  * \return number of unique values
284  */
285  template<class T>
286  size_t getCount(const array_type<T>& buffer)
287  {
288  return getCount(buffer, std::equal_to<T>());
289  }
290 }
291 
292 #endif
TPaveText * p1
Forward declarations of STD containers.
Auxiliary classes and methods for language specific functionality.
const array_type< JValue_t > & get_values(const std::map< JKey_t, JValue_t, JComparator_t, JAllocator_t > &data)
Method to create array of values of map.
Definition: JVectorize.hh:152
const array_type< JKey_t > & get_keys(const std::map< JKey_t, JValue_t, JComparator_t, JAllocator_t > &data)
Method to create array of keys of map.
Definition: JVectorize.hh:139
T getMaximum(const array_type< T > &buffer, const T value)
Get maximum of values.
Definition: JVectorize.hh:218
T make_set(T __begin, T __end, JResult_t std::iterator_traits< T >::value_type::*value, const JComparator_t &comparator)
Method to exclude outliers from already sorted data.
Definition: JVectorize.hh:169
size_t getCount(const array_type< T > &buffer)
Count number of unique values.
Definition: JVectorize.hh:286
const array_type< typename JClass< JValue_t >::value_type > & make_array(T __begin, T __end, JValue_t(JType_t::*function)() const)
Method to create array of return values of member method.
Definition: JVectorize.hh:118
T getMinimum(const array_type< T > &buffer, const T value)
Get minimum of values.
Definition: JVectorize.hh:240
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
const int n
Definition: JPolint.hh:786
Auxiliary data structure for return type of make methods.
Definition: JVectorize.hh:28
friend std::ostream & operator<<(std::ostream &out, const array_type &object)
Write array to output stream.
Definition: JVectorize.hh:36