Jpp 21.0.0-rc.1
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 * Smart pointer.
31 *
32 * \return pointer to static object
33 */
34 const array_type* operator->() const
35 {
36 static array_type buffer;
37
38 buffer.assign(this->begin(), this->end());
39
40 return &buffer;
41 }
42
43
44 /**
45 * Write array to output stream.
46 *
47 * \param out output stream
48 * \param object array
49 * \return output stream
50 */
51 friend inline std::ostream& operator<<(std::ostream& out, const array_type& object)
52 {
53 for (typename array_type::const_iterator i = object.begin(); i != object.end(); ++i) {
54 out << ' ' << *i;
55 }
56
57 return out;
58 }
59 };
60
61
62 /**
63 * Method to create array of values.
64 *
65 * \param array c-array of values
66 * \return data
67 */
68 template<class JValue_t, size_t N>
69 inline array_type<JValue_t> make_array(const JValue_t (&array)[N])
70 {
72
73 buffer.resize(N);
74
75 for (size_t i = 0; i != N; ++i) {
76 buffer[i] = array[i];
77 }
78
79 return buffer;
80 }
81
82
83 /**
84 * Method to create array of values.
85 *
86 * \param __begin begin of data
87 * \param __end end of data
88 * \return data
89 */
90 template<class T>
92 {
94
95 buffer.assign(__begin, __end);
96
97 return buffer;
98 }
99
100
101 /**
102 * Method to create array of values of data member.
103 *
104 * \param __begin begin of data
105 * \param __end end of data
106 * \param value pointer to data member
107 * \return data
108 */
109 template<class T, class JType_t, class JValue_t>
110 inline array_type<typename JClass<JValue_t>::value_type> make_array(T __begin, T __end, JValue_t const JType_t::*value)
111 {
113
114 for (T __p = __begin; __p != __end; ++__p) {
115 buffer.push_back(*__p.*value);
116 }
117
118 return buffer;
119 }
120
121
122 /**
123 * Method to create array of return values of member method.
124 *
125 * \param __begin begin of data
126 * \param __end end of data
127 * \param function pointer to member method
128 * \return data
129 */
130 template<class T, class JType_t, class JValue_t>
131 inline array_type<typename JClass<JValue_t>::value_type> make_array(T __begin, T __end, JValue_t (JType_t::*function)() const)
132 {
134
135 for (T __p = __begin; __p != __end; ++__p) {
136 buffer.push_back((*__p.*function)());
137 }
138
139 return buffer;
140 }
141
142
143 /**
144 * Method to create array of keys of map.
145 *
146 * \param data data
147 * \return data
148 */
149 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
154
155
156 /**
157 * Method to create array of values of map.
158 *
159 * \param data data
160 * \return data
161 */
162 template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
167
168
169 /**
170 * Method to exclude outliers from already sorted data.\n
171 * Note that the part after the returned iterator will be overwritten.
172 *
173 * \param __begin begin of data
174 * \param __end end of data
175 * \param value pointer to data member,
176 * \param comparator comparison method
177 * \return end of sorted data
178 */
179 template<class T, class JResult_t, class JComparator_t>
180 T make_set(T __begin,
181 T __end,
182 JResult_t std::iterator_traits<T>::value_type::*value,
183 const JComparator_t& comparator)
184
185 {
186 T p2 = __begin;
187 T p0 = p2++;
188 T p1 = p2++;
189
190 if (p0 == __end) { return p0; }
191 if (p1 == __end) { return p1; }
192
193 if (p2 == __end) {
194 if (comparator((*p0).*value, (*p1).*value))
195 return p2;
196 else
197 return p0;
198 }
199
200 for ( ; p2 != __end; ++p2) {
201
202 if (comparator((*p0).*value, (*p1).*value)) {
203 if (comparator((*p1).*value, (*p2).*value)) {
204 *(++p0) = *p1;
205 *(++p1) = *p2;
206 } else if (comparator((*p0).*value, (*p2).*value)) {
207 *p1 = *p2;
208 }
209 } else {
210 if (comparator((*p2).*value, (*p0).*value)) {
211 *p0 = *p1;
212 }
213 *p1 = *p2;
214 }
215 }
216
217 return ++p1;
218 }
219
220
221 /**
222 * Get maximum of values.
223 *
224 * \param buffer input values
225 * \param value start value
226 * \return maximum value
227 */
228 template<class T>
229 inline T getMaximum(const array_type<T>& buffer, const T value)
230 {
231 T max = value;
232
233 for (typename array_type<T>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
234 if (*i > max) {
235 max = *i;
236 }
237 }
238
239 return max;
240 }
241
242
243 /**
244 * Get minimum of values.
245 *
246 * \param buffer input values
247 * \param value start value
248 * \return minimum value
249 */
250 template<class T>
251 inline T getMinimum(const array_type<T>& buffer, const T value)
252 {
253 T min = value;
254
255 for (typename array_type<T>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
256 if (*i < min) {
257 min = *i;
258 }
259 }
260
261 return min;
262 }
263
264
265 /**
266 * Count number of unique values.
267 *
268 * \param buffer buffer
269 * \param compare compare
270 * \return number of unique values
271 */
272 template<class T, class JCompare_t>
273 size_t getCount(const array_type<T>& buffer, const JCompare_t& compare)
274 {
275 size_t n = 0;
276
277 for (typename array_type<T>::const_iterator p = buffer.begin(); p != buffer.end(); ++p) {
278
279 typename array_type<T>::const_iterator q = p;
280
281 while (++q != buffer.end() && !compare(*p,*q)) {}
282
283 if (q == buffer.end()) {
284 n += 1;
285 }
286 }
287
288 return n;
289 }
290
291
292 /**
293 * Count number of unique values.
294 *
295 * \return number of unique values
296 */
297 template<class T>
298 size_t getCount(const array_type<T>& buffer)
299 {
300 return getCount(buffer, std::equal_to<T>());
301 }
302}
303
304#endif
TPaveText * p1
Forward declarations of STD containers.
Auxiliary classes and methods for language specific functionality.
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.
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.
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.
array_type< JValue_t > make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition JVectorize.hh:69
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
const array_type * operator->() const
Smart pointer.
Definition JVectorize.hh:34
friend std::ostream & operator<<(std::ostream &out, const array_type &object)
Write array to output stream.
Definition JVectorize.hh:51