a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
DenseDoublyIndexed.hpp
Go to the documentation of this file.
1/*
2 * DenseDoublyIndexed.hpp
3 *
4 * Created on: Feb 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 DENSEDOUBLYINDEXED_HPP_
15#define DENSEDOUBLYINDEXED_HPP_
16
18
19namespace LHPC
20{
21 namespace SLHA
22 {
23 namespace InterpreterClass
24 {
25 /* this template class derives from InterpreterTemplate to interpret
26 * SLHA blocks that have a pair of int indices with a single ValueClass
27 * value.
28 */
29 template< class ValueClass >
30 class DenseDoublyIndexed : public IndexedInterpreter< ValueClass >
31 {
32 public:
34 virtual
36
37 ValueClass&
38 operator()( int const firstIndex,
39 int const secondIndex );
40 /* this returns the ValueClass indexed by firstIndex & secondIndex. if
41 * there is no element at the sought indices, valueMatrix is extended
42 * with copies of defaultUnsetValue until there is an element at the
43 * sought indices.
44 */
45 ValueClass const&
46 operator()( int const firstIndex,
47 int const secondIndex ) const;
48 /* const version of above, though it returns defaultUnsetValue rather
49 * than copying in a new element at the sought indices if there isn't
50 * an entry there already.
51 */
52 ValueClass&
53 operator()( std::pair< int, int > const& indexPair )
54 { return (*this)( indexPair.first,
55 indexPair.second ); }
56 ValueClass const&
57 operator()( std::pair< int, int > const& indexPair ) const
58 { return (*this)( indexPair.first,
59 indexPair.second ); }
60 bool
61 hasEntry( int const firstIndex,
62 int const secondIndex ) const;
63 // this returns true if there is an entry at the sought indices.
64 bool
65 hasEntry( std::pair< int, int > const& indexPair ) const
66 { return hasEntry( indexPair.first,
67 indexPair.second ); }
68 virtual std::string const&
70 // see base version's description.
71 virtual void
73 // derived classes should clear their interpreted values.
74
75
76 protected:
82
83 ValueClass&
84 findOrMakeEntry( int firstIndex,
85 int const secondIndex );
86 // this ensures that the entry at the sought indices exists, filling
87 // out with copies of defaultUnsetValue, & returns it.
88 virtual void
90 };
91
92
93
94
95
96 template< class ValueClass >
97 inline
99 IndexedInterpreter< ValueClass >(),
100 valueMatrix(),
101 firstRecordingIndex( 0 ),
102 secondRecordingIndex( 0 ),
103 largestFirstIndex( 0 ),
104 largestSecondIndex( 0 )
105 {
106 // just an initialization list.
107 }
108
109 template< class ValueClass >
110 inline
112 {
113 // does nothing.
114 }
115
116
117 template< class ValueClass >
118 inline ValueClass&
120 int const secondIndex )
121 /* this returns the ValueClass indexed by firstIndex & secondIndex. if
122 * there is no element at the sought indices, valueMatrix is extended
123 * with copies of defaultUnsetValue until there is an element at the
124 * sought indices.
125 */
126 {
127 return findOrMakeEntry( firstIndex,
128 secondIndex );
129 }
130
131 template< class ValueClass >
132 inline ValueClass const&
134 int const secondIndex ) const
135 /* const version of above, though it returns defaultUnsetValue rather
136 * than copying in a new element at the sought indices if there isn't an
137 * entry there already.
138 */
139 {
140 if( hasEntry( firstIndex,
141 secondIndex ) )
142 {
143 return valueMatrix[ firstIndex - 1 ][ secondIndex - 1 ];
144 }
145 else
146 {
147 return this->defaultUnsetValue;
148 }
149 }
150
151
152 template< class ValueClass >
153 inline bool
155 int const secondIndex ) const
156 {
157 if( ( 0 < firstIndex )
158 &&
159 ( 0 < secondIndex )
160 &&
161 ( firstIndex <= valueMatrix.getSize() )
162 &&
163 ( (size_t)secondIndex <= valueMatrix[ firstIndex - 1 ].size() ) )
164 {
165 return true;
166 }
167 else
168 {
169 return false;
170 }
171 }
172
173 template< class ValueClass >
174 inline std::string const&
176 // see base version's description.
177 {
178 this->stringInterpretation.clear();
179 for( int firstIndex( 1 );
180 largestFirstIndex >= firstIndex;
181 ++firstIndex )
182 {
183 for( size_t secondIndex( 1 );
184 largestSecondIndex >= secondIndex;
185 ++secondIndex )
186 {
187 this->indexPrintingVector[ 0 ] = firstIndex;
188 this->indexPrintingVector[ 1 ] = secondIndex;
189 this->stringInterpretation.append(
190 this->indicesToPrintingString() );
191 // SLHA indices are in the sane starts-at-one format, while C++
192 // code uses the silly starts-at-zero format.
193 this->stringInterpretation.append( this->valueToPrintingString(
194 findOrMakeEntry( firstIndex,
195 secondIndex ) ) );
196 this->stringInterpretation.append( "\n" );
197 }
198 }
199 return this->stringInterpretation;
200 }
201
202 template< class ValueClass >
203 inline void
205 // this ensures that the entry at soughtIndex exists, filling out with
206 // copies of defaultUnsetValue, & returns it.
207 {
208 valueMatrix.clearEntries();
209 }
210
211 template< class ValueClass >
212 inline ValueClass&
214 int const secondIndex )
215 // this ensures that the entry at the sought indices exists, filling
216 // out with copies of defaultUnsetValue, & returns it.
217 {
218 if( firstIndex > largestFirstIndex )
219 {
220 largestFirstIndex = firstIndex;
221 }
222 if( (size_t)secondIndex > largestSecondIndex )
223 {
224 largestSecondIndex = secondIndex;
225 }
226 while( valueMatrix.getSize() < firstIndex )
227 {
228 valueMatrix.newEnd().clear();
229 // empty std::vectors are added as necessary.
230 }
231 if( valueMatrix[ (--firstIndex) ].size() < (size_t)secondIndex )
232 // the conditional does the job of converting from sane starts-at-one
233 // format to silly starts-at-zero format.
234 {
235 valueMatrix[ firstIndex ].resize( secondIndex,
236 this->defaultUnsetValue );
237 }
238 return valueMatrix[ firstIndex ][ secondIndex - 1 ];
239 }
240
241 template< class ValueClass >
242 inline void
244 {
245 valueMatrix.clearEntries();
246 for( int whichLine( this->currentStringBlock->getNumberOfBodyLines() );
247 0 < whichLine;
248 --whichLine )
249 {
250 this->currentWord.assign( BOL::StringParser::firstWordOf(
251 (*(this->currentStringBlock))[ whichLine ].first,
252 &(this->lineRemainderA),
254 if( !(this->currentWord.empty()) )
255 {
256 firstRecordingIndex
257 = BOL::StringParser::stringToInt( this->currentWord );
258 this->currentWord.assign( BOL::StringParser::firstWordOf(
259 this->lineRemainderA,
260 &(this->lineRemainderB),
262 secondRecordingIndex
263 = BOL::StringParser::stringToInt( this->currentWord );
264 this->currentWord.assign( BOL::StringParser::trimFromFrontAndBack(
265 this->lineRemainderB,
267 if( ( 0 < firstRecordingIndex )
268 &&
269 ( 0 < secondRecordingIndex )
270 &&
271 !(this->currentWord.empty()) )
272 {
273 findOrMakeEntry( firstRecordingIndex,
274 secondRecordingIndex )
275 = this->stringToValue( this->currentWord );
276 }
277 else if( this->isVerbose )
278 {
279 std::cout
280 << std::endl
281 << "LHPC::SLHA::error! expected to find 2 positive indices then"
282 << " a value, instead found \""
283 << (*(this->currentStringBlock))[ whichLine ].first << "\"";
284 std::cout << std::endl;
285 }
286 }
287 }
288 }
289
290 }
291
292 }
293
294}
295
296#endif /* DENSEDOUBLYINDEXED_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 const & operator()(std::pair< int, int > const &indexPair) const
bool hasEntry(std::pair< int, int > const &indexPair) const
bool hasEntry(int const firstIndex, int const secondIndex) const
ValueClass & findOrMakeEntry(int firstIndex, int const secondIndex)
BOL::VectorlikeArray< std::vector< ValueClass > > valueMatrix
ValueClass & operator()(std::pair< int, int > const &indexPair)
ValueClass & operator()(int const firstIndex, int const secondIndex)