Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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 */
17namespace JLANG {}
18namespace JPP { using namespace JLANG; }
19
20namespace 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>
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>
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 buffer buffer
258 * \param compare compare
259 * \return number of unique values
260 */
261 template<class T, class JCompare_t>
262 size_t getCount(const array_type<T>& buffer, const JCompare_t& compare)
263 {
264 size_t n = 0;
265
266 for (typename array_type<T>::const_iterator p = buffer.begin(); p != buffer.end(); ++p) {
267
268 typename array_type<T>::const_iterator q = p;
269
270 while (++q != buffer.end() && !compare(*p,*q)) {}
271
272 if (q == buffer.end()) {
273 n += 1;
274 }
275 }
276
277 return n;
278 }
279
280
281 /**
282 * Count number of unique values.
283 *
284 * \return number of unique values
285 */
286 template<class T>
287 size_t getCount(const array_type<T>& buffer)
288 {
289 return getCount(buffer, std::equal_to<T>());
290 }
291}
292
293#endif
TPaveText * p1
Forward declarations of STD containers.
Auxiliary classes and methods for language specific functionality.
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition JVectorize.hh:54
T getMaximum(const array_type< T > &buffer, const T value)
Get maximum of values.
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.
size_t getCount(const array_type< T > &buffer, const JCompare_t &compare)
Count number of unique values.
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.
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.
T getMinimum(const array_type< T > &buffer, const T value)
Get minimum of values.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
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