a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
SparseQuadruplyIndexed.hpp
Go to the documentation of this file.
1/*
2 * SparseQuadruplyIndexed.hpp
3 *
4 * Created on: Mar 19, 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 SPARSEQUADRUPLYINDEXED_HPP_
15#define SPARSEQUADRUPLYINDEXED_HPP_
16
17#include <map>
19
20namespace LHPC
21{
22 namespace SLHA
23 {
24 namespace InterpreterClass
25 {
26 // this template class interprets SLHA blocks that have a single int
27 // index with a single ValueClass value.
28 template< class ValueClass >
29 class SparseQuadruplyIndexed : public IndexedInterpreter< ValueClass >
30 {
31 public:
32 typedef typename std::pair< int, int > IntPair;
33 typedef typename std::pair< IntPair, IntPair > IntQuadruple;
35 virtual
37
38 ValueClass&
39 operator()( IntQuadruple const& indexQuadruple );
40 /* this returns the ValueClass mapped to by indexQuadruple.first.first,
41 * indexQuadruple.first.second, indexQuadruple.second.first &
42 * indexPair.second.second. if there is no element at the sought
43 * indices, a new one is made & copied from defaultUnsetValue.
44 */
45 ValueClass const&
46 operator()( IntQuadruple const& indexQuadruple ) const;
47 /* const version of above, though it returns defaultUnsetValue rather
48 * than copying in a new element at the sought indices if there isn't
49 * an entry there already.
50 */
51 ValueClass&
52 operator()( int const firstIndex,
53 int const secondIndex,
54 int const thirdIndex,
55 int const fourthIndex )
56 { return (*this)( std::make_pair( std::make_pair( firstIndex,
57 secondIndex ),
58 std::make_pair( thirdIndex,
59 fourthIndex ) ) ); }
60 ValueClass const&
61 operator()( int const firstIndex,
62 int const secondIndex,
63 int const thirdIndex,
64 int const fourthIndex ) const
65 { return (*this)( std::make_pair( std::make_pair( firstIndex,
66 secondIndex ),
67 std::make_pair( thirdIndex,
68 fourthIndex ) ) ); }
69 /* const version of above, though it returns defaultUnsetValue rather
70 * than copying in a new element at the sought indices if there isn't
71 * an entry there already.
72 */
73 bool
74 hasEntry( IntQuadruple const& indexQuadruple ) const;
75 // this returns true if there is an entry at the sought indices.
76 bool
77 hasEntry( int const firstIndex,
78 int const secondIndex,
79 int const thirdIndex,
80 int const fourthIndex ) const
81 { return (*this)( hasEntry( std::make_pair( std::make_pair( firstIndex,
82 secondIndex ),
83 std::make_pair( thirdIndex,
84 fourthIndex ) ) ) ) ; }
85 virtual std::string const&
87 // see base version's description.
88 virtual void
90 // derived classes should clear their interpreted values.
91
92
93 protected:
94 typedef typename
95 std::map< IntQuadruple, ValueClass >::const_iterator
97
98 std::map< IntQuadruple, ValueClass > valueMap;
100 std::pair< IntQuadruple, ValueClass > valueRecorder;
101
102 virtual void
104 };
105
106
107
108
109
110 template< class ValueClass >
111 inline
113 IndexedInterpreter< ValueClass >(),
114 valueMap(),
115 mapKey( std::pair< int, int >( 0,
116 0 ),
117 std::pair< int, int >( 0,
118 0 ) ),
119 valueRecorder()
120 {
121 // just an initialization list.
122 }
123
124 template< class ValueClass >
125 inline
127 {
128 // does nothing.
129 }
130
131
132 template< class ValueClass >
133 inline ValueClass&
135 IntQuadruple const& indexQuadruple )
136 /* this returns the ValueClass mapped to by indexPair.first &
137 * indexPair.second. if there is no element at the sought indices, a
138 * new one is made & copied from defaultUnsetValue.
139 */
140 {
141 if( 0 >= valueMap.count( indexQuadruple ) )
142 {
143 valueMap[ indexQuadruple ] = this->defaultUnsetValue;
144 }
145 return valueMap[ indexQuadruple ];
146 }
147
148 template< class ValueClass >
149 inline ValueClass const&
151 IntQuadruple const& indexQuadruple ) const
152 /* const version of above, though it returns defaultUnsetValue rather
153 * than copying in a new element at the sought indices if there isn't
154 * an entry there already.
155 */
156 {
157 mapIterator valueFinder( valueMap.find( indexQuadruple ) );
158 if( valueMap.end() != valueFinder )
159 {
160 return valueFinder->second;
161 }
162 else
163 {
164 return this->defaultUnsetValue;
165 }
166 }
167
168 template< class ValueClass >
169 inline bool
171 IntQuadruple const& indexQuadruple ) const
172 // this returns true if there is an entry at soughtIndex.
173 {
174 return ( 0 < valueMap.count( indexQuadruple ) );
175 }
176
177 template< class ValueClass >
178 inline std::string const&
180 // see base version's description.
181 {
182 this->stringInterpretation.clear();
183 mapIterator valueFinder( valueMap.begin() );
184 while( valueFinder != valueMap.end() )
185 {
186 this->indexPrintingVector[ 0 ] = valueFinder->first.first.first;
187 this->indexPrintingVector[ 1 ] = valueFinder->first.first.second;
188 this->indexPrintingVector[ 2 ] = valueFinder->first.second.first;
189 this->indexPrintingVector[ 3 ] = valueFinder->first.second.second;
190 this->stringInterpretation.append( this->indicesToPrintingString() );
191 this->stringInterpretation.append( this->valueToPrintingString(
192 valueFinder->second ) );
193 this->stringInterpretation.append( "\n" );
194 ++valueFinder;
195 }
196 return this->stringInterpretation;
197 }
198
199 template< class ValueClass >
200 inline void
202 {
203 valueMap.clear();
204 }
205
206 template< class ValueClass >
207 inline void
209 {
210 for( int whichLine( this->currentStringBlock->getNumberOfBodyLines() );
211 0 < whichLine;
212 --whichLine )
213 {
214 this->currentWord.assign( BOL::StringParser::firstWordOf(
215 (*(this->currentStringBlock))[ whichLine ].first,
216 &(this->lineRemainderA),
218 if( !(this->currentWord.empty()) )
219 {
220 valueRecorder.first.first.first
221 = BOL::StringParser::stringToInt( this->currentWord );
222 this->currentWord.assign( BOL::StringParser::firstWordOf(
223 this->lineRemainderA,
224 &(this->lineRemainderB),
226 if( !(this->currentWord.empty()) )
227 {
228 valueRecorder.first.first.second
229 = BOL::StringParser::stringToInt( this->currentWord );
230 this->currentWord.assign( BOL::StringParser::firstWordOf(
231 this->lineRemainderB,
232 &(this->lineRemainderA),
234 if( !(this->currentWord.empty()) )
235 {
236 valueRecorder.first.second.first
237 = BOL::StringParser::stringToInt( this->currentWord );
238 this->currentWord.assign( BOL::StringParser::firstWordOf(
239 this->lineRemainderA,
240 &(this->lineRemainderB),
242 if( !(this->currentWord.empty()) )
243 {
244 valueRecorder.first.second.second
245 = BOL::StringParser::stringToInt( this->currentWord );
246 valueRecorder.second = this->stringToValue(
248 this->lineRemainderB,
250 valueMap.insert( valueRecorder );
251 }
252 }
253 }
254 }
255 }
256 }
257
258 }
259
260 }
261
262}
263
264#endif /* SPARSEQUADRUPLYINDEXED_HPP_ */
static std::string firstWordOf(std::string const &stringToParse, std::string *const remainderString=NULL, std::string const &separatorChars=whitespaceChars)
static std::string const whitespaceAndNewlineChars
static std::string trimFromFrontAndBack(std::string const &stringToTrim, std::string const &charsToTrim=whitespaceAndNewlineChars)
static int stringToInt(std::string const &stringToInterpret)
ValueClass & operator()(IntQuadruple const &indexQuadruple)
std::map< IntQuadruple, ValueClass >::const_iterator mapIterator
ValueClass const & operator()(int const firstIndex, int const secondIndex, int const thirdIndex, int const fourthIndex) const
bool hasEntry(int const firstIndex, int const secondIndex, int const thirdIndex, int const fourthIndex) const
ValueClass & operator()(int const firstIndex, int const secondIndex, int const thirdIndex, int const fourthIndex)
bool hasEntry(IntQuadruple const &indexQuadruple) const