a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
DenseSinglyIndexed.hpp
Go to the documentation of this file.
1/*
2 * DenseSinglyIndexed.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 DENSESINGLYINDEXED_HPP_
15#define DENSESINGLYINDEXED_HPP_
16
18
19namespace LHPC
20{
21 namespace SLHA
22 {
23 namespace InterpreterClass
24 {
25 // this template class interprets SLHA blocks that have a single int
26 // index with a single ValueClass value.
27 template< class ValueClass >
28 class DenseSinglyIndexed : public IndexedInterpreter< ValueClass >
29 {
30 public:
32 virtual
34
35 ValueClass&
36 operator()( int const soughtIndex );
37 /* this returns the ValueClass indexed by soughtIndex.. if there is no
38 * element at soughtIndex, valueVector is extended with copies of
39 * defaultUnsetValue until there is an element at soughtIndex.
40 */
41 ValueClass const&
42 operator()( int const soughtIndex ) const;
43 /* const version of above, though it returns defaultUnsetValue rather
44 * than copying in a new element at soughtIndex if there isn't an
45 * entry there already.
46 */
47 ValueClass&
48 operator[]( int const soughtIndex )
49 { return (*this)( soughtIndex ); }
50 ValueClass const&
51 operator[]( int const soughtIndex ) const
52 { return (*this)( soughtIndex ); }
53 bool
54 hasEntry( int const soughtIndex ) const;
55 // this returns true if there is an entry at soughtIndex.
56 virtual std::string const&
58 // see base version's description.
59 virtual void
61 // derived classes should clear their interpreted values.
62
63
64 protected:
65 std::vector< ValueClass > valueVector;
67
68 ValueClass&
69 findOrMakeEntry( int const soughtIndex );
70 // this ensures that the entry at soughtIndex exists, filling out with
71 // copies of defaultUnsetValue, & returns it.
72 virtual void
74 };
75
76
77
78
79
80 template< class ValueClass >
81 inline
83 IndexedInterpreter< ValueClass >(),
84 valueVector(),
85 recordingIndex( 0 )
86 {
87 // just an initialization list.
88 }
89
90 template< class ValueClass >
91 inline
93 {
94 // does nothing.
95 }
96
97
98 template< class ValueClass >
99 inline ValueClass&
101 /* this returns the ValueClass indexed by soughtIndex. if there is no
102 * element at soughtIndex, valueVector is extended with copies of
103 * defaultUnsetValue until there is an element at soughtIndex.
104 */
105 {
106 return findOrMakeEntry( soughtIndex );
107 }
108
109 template< class ValueClass >
110 inline ValueClass const&
112 int const soughtIndex ) const
113 /* const version of above, though it returns defaultUnsetValue rather
114 * than copying in a new element at soughtIndex if there isn't an
115 * entry there already.
116 */
117 {
118 if( hasEntry( soughtIndex ) )
119 {
120 return valueVector[ soughtIndex - 1 ];
121 }
122 else
123 {
124 return this->defaultUnsetValue;
125 }
126 }
127
128 template< class ValueClass >
129 inline bool
130 DenseSinglyIndexed< ValueClass >::hasEntry( int const soughtIndex ) const
131 {
132 if( ( 0 < soughtIndex )
133 &&
134 ( (size_t)soughtIndex <= valueVector.size() ) )
135 {
136 return true;
137 }
138 else
139 {
140 return false;
141 }
142 }
143
144 template< class ValueClass >
145 inline std::string const&
147 // see base version's description.
148 {
149 this->stringInterpretation.clear();
150 for( size_t soughtIndex( 1 );
151 valueVector.size() >= soughtIndex;
152 ++soughtIndex )
153 {
154 this->indexPrintingVector[ 0 ] = soughtIndex;
155 this->stringInterpretation.append( this->indicesToPrintingString() );
156 // SLHA indices are in the sane starts-at-one format, while C++ code
157 // uses the silly starts-at-zero format.
158 this->stringInterpretation.append( this->valueToPrintingString(
159 findOrMakeEntry( soughtIndex ) ) );
160 this->stringInterpretation.append( "\n" );
161 }
162 return this->stringInterpretation;
163 }
164
165 template< class ValueClass >
166 inline void
168 // this ensures that the entry at soughtIndex exists, filling out with
169 // copies of defaultUnsetValue, & returns it.
170 {
171 valueVector.clear();
172 }
173
174 template< class ValueClass >
175 inline ValueClass&
177 int const soughtIndex )
178 // this ensures that the entry at soughtIndex exists, filling out with
179 // copies of defaultUnsetValue, & returns it.
180 {
181 if( valueVector.size() < (size_t)soughtIndex )
182 {
183 valueVector.resize( soughtIndex,
184 this->defaultUnsetValue );
185 }
186 return valueVector[ soughtIndex - 1 ];
187 }
188
189 template< class ValueClass >
190 inline void
192 {
193 for( int whichLine( this->currentStringBlock->getNumberOfBodyLines() );
194 0 < whichLine;
195 --whichLine )
196 {
197 this->currentWord.assign( BOL::StringParser::firstWordOf(
198 (*(this->currentStringBlock))[ whichLine ].first,
199 &(this->lineRemainderA),
201 if( !(this->currentWord.empty()) )
202 {
203 recordingIndex
204 = BOL::StringParser::stringToInt( this->currentWord );
205 this->currentWord.assign( BOL::StringParser::trimFromFrontAndBack(
206 this->lineRemainderA,
208 if( ( 0 < recordingIndex )
209 &&
210 !(this->currentWord.empty()) )
211 {
212 findOrMakeEntry( recordingIndex )
213 = this->stringToValue( this->currentWord );
214 }
215 else if( this->isVerbose )
216 {
217 std::cout
218 << std::endl
219 << "LHPC::SLHA::error! expected to find 1 positive index then a"
220 << " value, instead found \""
221 << (*(this->currentStringBlock))[ whichLine ].first << "\"";
222 std::cout << std::endl;
223 }
224 }
225 }
226 }
227
228 }
229
230 }
231
232}
233
234#endif /* DENSESINGLYINDEXED_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[](int const soughtIndex) const
ValueClass & findOrMakeEntry(int const soughtIndex)
ValueClass & operator()(int const soughtIndex)
ValueClass & operator[](int const soughtIndex)