a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
SparseManyIndexed.hpp
Go to the documentation of this file.
1/*
2 * SparseManyIndexed.hpp
3 *
4 * Created on: Aug 28, 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 SPARSEMANYINDEXED_HPP_
15#define SPARSEMANYINDEXED_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 specified
27 // number of int indices with a single ValueClass value.
28 template< class ValueClass >
29 class SparseManyIndexed : public IndexedInterpreter< ValueClass >
30 {
31 public:
33 virtual
35
36 ValueClass&
37 operator()( std::vector< int > const& indexVector );
38 /* this returns the ValueClass mapped to by indexVector. if there is no
39 * element at the sought indices, a new one is made & copied from
40 * defaultUnsetValue.
41 */
42 ValueClass const&
43 operator()( std::vector< int > const& indexVector ) const;
44 /* const version of above, though it returns defaultUnsetValue rather
45 * than copying in a new element at the sought indices if there isn't
46 * an entry there already.
47 */
48 ValueClass&
49 operator()( std::string const& indicesAsString );
50 // this takes a list of indices in string form, separated by
51 // commas or semicolons, with optional whitespace, or just whitespace.
52 ValueClass const&
53 operator()( std::string const& indicesAsString ) const;
54 /* const version of above, though it returns defaultUnsetValue rather
55 * than copying in a new element at the sought indices if there isn't
56 * an entry there already.
57 */
58 bool
59 hasEntry( std::vector< int > const& indexVector ) const;
60 // this returns true if there is an entry at the sought indices.
61 bool
62 hasEntry( std::string const& indicesAsString ) const;
63 // this returns true if there is an entry at the vector parsed from
64 // indicesAsString see operator() for the format).
65 virtual std::string const&
67 // see base version's description.
68 virtual void
70 // derived classes should clear their interpreted values.
71
72
73 protected:
74 typedef typename
75 std::map< std::vector< int >, ValueClass >::const_iterator
77
78 std::map< std::vector< int >, ValueClass > valueMap;
79 std::vector< int > mapKey;
80 std::pair< std::vector< int >, ValueClass > valueRecorder;
81
82 virtual void
84 };
85
86
87
88
89
90 template< class ValueClass >
91 inline
93 IndexedInterpreter< ValueClass >(),
94 valueMap(),
95 mapKey(),
96 valueRecorder()
97 {
98 // just an initialization list.
99 }
100
101 template< class ValueClass >
102 inline
104 {
105 // does nothing.
106 }
107
108
109 template< class ValueClass >
110 inline ValueClass&
112 std::vector< int > const& indexVector )
113 /* this returns the ValueClass mapped to by indexVector. if there is no
114 * element at the sought indices, a new one is made & copied from
115 * defaultUnsetValue.
116 */
117 {
118 if( 0 >= valueMap.count( indexVector ) )
119 {
120 valueMap[ indexVector ] = this->defaultUnsetValue;
121 }
122 return valueMap[ indexVector ];
123 }
124
125 template< class ValueClass >
126 inline ValueClass const&
128 std::vector< int > const& indexVector ) const
129 /* const version of above, though it returns defaultUnsetValue rather
130 * than copying in a new element at the sought indices if there isn't
131 * an entry there already.
132 */
133 {
134 mapIterator valueFinder( valueMap.find( indexVector ) );
135 if( valueMap.end() != valueFinder )
136 {
137 return valueFinder->second;
138 }
139 else
140 {
141 return this->defaultUnsetValue;
142 }
143 }
144
145 template< class ValueClass >
146 inline ValueClass&
148 std::string const& indicesAsString )
149 // this takes a list of indices in string form, separated by
150 // commas or semicolons, with optional whitespace, or just whitespace.
151 {
153 indicesAsString ) );
154 }
155
156
157 template< class ValueClass >
158 inline ValueClass const&
160 std::string const& indicesAsString ) const
161 /* const version of above, though it returns defaultUnsetValue rather
162 * than copying in a new element at the sought indices if there isn't
163 * an entry there already.
164 */
165 {
167 indicesAsString ) );
168 }
169
170 template< class ValueClass >
171 inline bool
173 std::vector< int > const& indexVector ) const
174 // this returns true if there is an entry at the sought indices.
175 {
176 return ( 0 < valueMap.count( indexVector ) );
177 }
178
179 template< class ValueClass >
180 inline bool
182 std::string const& indicesAsString ) const
183 // this returns true if there is an entry at the vector parsed from
184 // indicesAsString see operator() for the format).
185 {
187 indicesAsString ) );
188 }
189
190 template< class ValueClass >
191 inline std::string const&
193 // see base version's description.
194 {
195 this->stringInterpretation.clear();
196 mapIterator valueFinder( valueMap.begin() );
197 while( valueFinder != valueMap.end() )
198 {
199 this->indexPrintingVector = valueFinder->first;
200 this->stringInterpretation.append( this->indicesToPrintingString() );
201 this->stringInterpretation.append( this->valueToPrintingString(
202 valueFinder->second ) );
203 this->stringInterpretation.append( "\n" );
204 ++valueFinder;
205 }
206 return this->stringInterpretation;
207 }
208
209 template< class ValueClass >
210 inline void
212 {
213 valueMap.clear();
214 }
215
216 template< class ValueClass >
217 inline void
219 {
220 for( int whichLine( this->currentStringBlock->getNumberOfBodyLines() );
221 0 < whichLine;
222 --whichLine )
223 {
224 valueRecorder.first.clear();
225 this->lineRemainderB.assign(
226 (*(this->currentStringBlock))[ whichLine ].first );
227 for( int indexCount( this->indexDigitsVector.size() );
228 0 < indexCount;
229 --indexCount )
230 {
231 this->lineRemainderA.assign( this->lineRemainderB );
232 this->currentWord.assign( BOL::StringParser::firstWordOf(
233 this->lineRemainderA,
234 &(this->lineRemainderB),
236 if( this->currentWord.empty() )
237 {
238 break;
239 }
240 valueRecorder.first.push_back(
241 BOL::StringParser::stringToInt( this->currentWord ) );
242 }
243 valueRecorder.second
244 = this->stringToValue( BOL::StringParser::trimFromFrontAndBack(
245 this->lineRemainderB,
247 valueMap.insert( valueRecorder );
248 }
249 }
250
251 }
252
253 }
254
255}
256
257
258
259#endif /* SPARSEMANYINDEXED_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::vector< int > stringToIntVector(std::string stringToInterpret)
static std::string trimFromFrontAndBack(std::string const &stringToTrim, std::string const &charsToTrim=whitespaceAndNewlineChars)
static int stringToInt(std::string const &stringToInterpret)
std::pair< std::vector< int >, ValueClass > valueRecorder
std::map< std::vector< int >, ValueClass >::const_iterator mapIterator
bool hasEntry(std::vector< int > const &indexVector) const
std::map< std::vector< int >, ValueClass > valueMap
ValueClass & operator()(std::vector< int > const &indexVector)