a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
PointersWithValue.hpp
Go to the documentation of this file.
1/*
2 * PointersWithValue.hpp
3 *
4 * Created on: Jan 8, 2012
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 * Copyright 2012 Ben O'Leary
7 *
8 * This file is part of LesHouchesParserClasses, released under the
9 * GNU General Public License. Please see the accompanying
10 * README.LHPC_CPP.txt file for a full list of files, brief documentation
11 * on how to use these classes, and further details on the license.
12 */
13
14#ifndef POINTERSWITHVALUE_HPP_
15#define POINTERSWITHVALUE_HPP_
16
17#include <list>
18#include <algorithm>
20
21namespace LHPC
22{
23 /* this is a class to hold pointers (intended to be MassEigenstate pointers)
24 * paired with values (intended to be branching ratios as double), with
25 * functions for matching.
26 */
27 template< class PointerClass, class ValueClass >
29 {
30 public:
35
36 void
39 void
42 becomeCopyOf( copySource ); }
46 std::list< PointerClass >&
48 std::list< PointerClass > const&
50 int
51 getListSize() const;
52 void
53 addPointer( PointerClass const pointerToAdd );
54 ValueClass
56 void
58 void
60 bool
61 matchesExactly( PointerClass const firstPointer,
62 PointerClass const secondPointer ) const;
63 bool
64 matchesExactly( std::list< PointerClass > const& sortedList ) const;
65 bool
66 matchesExactly( int const firstCode,
67 int const secondCode,
68 bool (*comparisonFunction)( PointerClass const,
69 int const ) ) const;
70 bool
71 containsAllAsSubset( std::list< PointerClass > const& sortedList ) const;
72 bool
73 containsAnyAsSubset( std::list< PointerClass > const& sortedList ) const;
74
75
76 protected:
77 std::list< PointerClass > pointerList;
78 ValueClass pairedValue;
79 };
80
81
82
83
84
85 template< class PointerClass, class ValueClass >
86 inline
88 pointerList(),
89 pairedValue( BOL::UsefulStuff::notANumber )
90 {
91 // just an initialization list.
92 }
93
94 template< class PointerClass, class ValueClass >
95 inline
98 pointerList( copySource.pointerList ),
99 pairedValue( copySource.pairedValue )
100 {
101 // just an initialization list.
102 }
103
104 template< class PointerClass, class ValueClass >
105 inline
107 {
108 // does nothing.
109 }
110
111
112 template< class PointerClass, class ValueClass >
113 inline void
116 {
117 this->pointerList = copySource.pointerList;
118 this->pairedValue = copySource.pairedValue;
119 }
120
121 template< class PointerClass, class ValueClass >
125 {
126 becomeCopyOf( copySource );
127 return this;
128 }
129
130 template< class PointerClass, class ValueClass >
131 inline std::list< PointerClass >&
133 {
134 return pointerList;
135 }
136
137 template< class PointerClass, class ValueClass >
138 inline std::list< PointerClass > const&
140 {
141 return pointerList;
142 }
143
144 template< class PointerClass, class ValueClass >
145 inline int
147 {
148 return (int)(pointerList.size());
149 }
150
151 template< class PointerClass, class ValueClass >
152 inline void
154 PointerClass const pointerToAdd )
155 {
156 pointerList.push_back( pointerToAdd );
157 }
158
159 template< class PointerClass, class ValueClass >
160 inline ValueClass
162 {
163 return pairedValue;
164 }
165
166 template< class PointerClass, class ValueClass >
167 inline void
169 {
170 pointerList.clear();
171 }
172
173 template< class PointerClass, class ValueClass >
174 inline void
176 ValueClass const pairedValue )
177 {
178 this->pairedValue = pairedValue;
179 pointerList.sort();
180 }
181
182 template< class PointerClass, class ValueClass >
183 inline bool
185 PointerClass const firstPointer,
186 PointerClass const secondPointer ) const
187 {
188 if( ( 2 == pointerList.size() )
189 &&
190 ( ( ( firstPointer == pointerList.front() )
191 &&
192 ( secondPointer == pointerList.back() ) )
193 ||
194 ( ( secondPointer == pointerList.front() )
195 &&
196 ( firstPointer == pointerList.back() ) ) ) )
197 {
198 return true;
199 }
200 else
201 {
202 return false;
203 }
204 }
205
206 template< class PointerClass, class ValueClass >
207 inline bool
209 std::list< PointerClass > const& sortedList ) const
210 {
211 if( sortedList.size() == pointerList.size() )
212 {
213 return std::equal( sortedList.begin(),
214 sortedList.end(),
215 pointerList.begin() );
216 }
217 else
218 {
219 return false;
220 }
221 }
222
223 template< class PointerClass, class ValueClass >
224 inline bool
226 int const firstCode,
227 int const secondCode,
228 bool (*comparisonFunction)( PointerClass const,
229 int const ) ) const
230 {
231 if( ( 2 == pointerList.size() )
232 &&
233 ( ( (*comparisonFunction)( pointerList.front(),
234 firstCode )
235 &&
236 (*comparisonFunction)( pointerList.back(),
237 secondCode ) )
238 ||
239 ( (*comparisonFunction)( pointerList.back(),
240 firstCode )
241 &&
242 (*comparisonFunction)( pointerList.front(),
243 secondCode ) ) ) )
244 {
245 return true;
246 }
247 else
248 {
249 return false;
250 }
251 }
252
253 template< class PointerClass, class ValueClass >
254 inline bool
256 std::list< PointerClass > const& sortedList ) const
257 {
258 bool returnBool( false );
259 if( pointerList.size() >= sortedList.size() )
260 {
261 int allowedMismatches( pointerList.size() - sortedList.size() );
262 // the difference in list sizes is the most comparisons that can fail.
263 typename std::list< PointerClass >::const_iterator
264 fromThisSet( pointerList.begin() );
265 typename std::list< PointerClass >::const_iterator
266 toBeCompared( sortedList.begin() );
267 while( ( allowedMismatches >= 0 )
268 &&
269 ( fromThisSet != pointerList.end() ) )
270 {
271 if( *fromThisSet < *toBeCompared )
272 {
273 --allowedMismatches;
274 // note that a comparison failed.
275 }
276 else
277 {
278 if( *fromThisSet == *toBeCompared )
279 {
280 // move on to the next sought pointer, as this one was just found:
281 ++toBeCompared;
282 }
283 else
284 {
285 allowedMismatches = -1;
286 /* otherwise, since the 2 lists are ordered & sortedList is smaller
287 * than pointerList, if *thisSetPointer > *comparisonPointer then
288 * there's no way that all of sortedList can be in pointerList.
289 */
290 }
291 }
292 ++fromThisSet;
293 }
294 if( allowedMismatches >= 0 )
295 {
296 returnBool = true;
297 }
298 }
299 return returnBool;
300 }
301
302 template< class PointerClass, class ValueClass >
303 inline bool
305 std::list< PointerClass > const& sortedList ) const
306 {
307 bool returnBool( false );
308 typename std::list< PointerClass >::const_iterator
309 fromThisSet( pointerList.begin() );
310 typename std::list< PointerClass >::const_iterator
311 toBeCompared( sortedList.begin() );
312 /* both lists are sorted, so we walk each iterator along until it catches
313 * up with the other: either it overtakes, & we switch to iterating the
314 * other, or they are equal, in which case we should stop looking, and
315 * return true.
316 */
317 while( !returnBool
318 &&
319 ( fromThisSet != pointerList.end() )
320 &&
321 ( toBeCompared != sortedList.end() ) )
322 {
323 if( *fromThisSet < *toBeCompared )
324 {
325 ++fromThisSet;
326 }
327 else if( *fromThisSet == *toBeCompared )
328 {
329 returnBool = true;
330 }
331 else
332 {
333 ++toBeCompared;
334 }
335 }
336 return returnBool;
337 }
338
339}
340
341#endif /* POINTERSWITHVALUE_HPP_ */
void operator=(PointersWithValue< PointerClass, ValueClass > const &copySource)
bool matchesExactly(std::list< PointerClass > const &sortedList) const
bool containsAllAsSubset(std::list< PointerClass > const &sortedList) const
std::list< PointerClass > pointerList
void addPointer(PointerClass const pointerToAdd)
bool containsAnyAsSubset(std::list< PointerClass > const &sortedList) const
std::list< PointerClass > & getPointerList()
PointersWithValue< PointerClass, ValueClass > * copyAndReturnPointer(PointersWithValue< PointerClass, ValueClass > const &copySource)
PointersWithValue(PointersWithValue< PointerClass, ValueClass > const &copySource)
std::list< PointerClass > const & getPointerList() const
bool matchesExactly(int const firstCode, int const secondCode, bool(*comparisonFunction)(PointerClass const, int const)) const
bool matchesExactly(PointerClass const firstPointer, PointerClass const secondPointer) const
void setPairedValueAndSortPointers(ValueClass const pairedValue)
void becomeCopyOf(PointersWithValue< PointerClass, ValueClass > const &copySource)
ValueClass getPairedValue() const