a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
SparseDoublyIndexed.hpp
Go to the documentation of this file.
1/*
2 * SparseDoublyIndexed.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 SPARSEDOUBLYINDEXED_HPP_
15#define SPARSEDOUBLYINDEXED_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 SparseDoublyIndexed : public IndexedInterpreter< ValueClass >
30 {
31 public:
33 virtual
35
36 ValueClass&
37 operator()( std::pair< int, int > const& indexPair );
38 /* this returns the ValueClass mapped to by indexPair.first &
39 * indexPair.second. if there is no element at the sought indices, a
40 * new one is made & copied from defaultUnsetValue.
41 */
42 ValueClass const&
43 operator()( std::pair< int, int > const& indexPair ) 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()( int const firstIndex,
50 int const secondIndex )
51 { return (*this)( std::make_pair( firstIndex,
52 secondIndex ) ); }
53 ValueClass const&
54 operator()( int const firstIndex,
55 int const secondIndex ) const
56 { return (*this)( std::make_pair( firstIndex,
57 secondIndex ) ); }
58 bool
59 hasEntry( std::pair< int, int > const& indexPair ) const;
60 // this returns true if there is an entry at the sought indices.
61 bool
62 hasEntry( int const firstIndex,
63 int const secondIndex ) const
64 { return hasEntry( std::make_pair( firstIndex,
65 secondIndex ) ); }
66 virtual std::string const&
68 // see base version's description.
69 virtual void
71 // derived classes should clear their interpreted values.
72
73
74 protected:
75 typedef typename
76 std::map< std::pair< int, int >, ValueClass >::const_iterator
78
79 std::map< std::pair< int, int >, ValueClass > valueMap;
80 std::pair< int, int > mapKey;
81 std::pair< std::pair< int, int >, ValueClass > valueRecorder;
82
83 virtual void
85 };
86
87
88
89
90
91 template< class ValueClass >
92 inline
94 IndexedInterpreter< ValueClass >(),
95 valueMap(),
96 mapKey( 0,
97 0 ),
98 valueRecorder()
99 {
100 // just an initialization list.
101 }
102
103 template< class ValueClass >
104 inline
106 {
107 // does nothing.
108 }
109
110
111 template< class ValueClass >
112 inline ValueClass&
114 std::pair< int, int > const& indexPair )
115 /* this returns the ValueClass mapped to by indexPair.first &
116 * indexPair.second. if there is no element at the sought indices, a
117 * new one is made & copied from defaultUnsetValue.
118 */
119 {
120 if( 0 >= valueMap.count( indexPair ) )
121 {
122 valueMap[ indexPair ] = this->defaultUnsetValue;
123 }
124 return valueMap[ indexPair ];
125 }
126
127 template< class ValueClass >
128 inline ValueClass const&
130 std::pair< int, int > const& indexPair ) const
131 /* const version of above, though it returns defaultUnsetValue rather
132 * than copying in a new element at the sought indices if there isn't
133 * an entry there already.
134 */
135 {
136 mapIterator valueFinder( valueMap.find( indexPair ) );
137 if( valueMap.end() != valueFinder )
138 {
139 return valueFinder->second;
140 }
141 else
142 {
143 return this->defaultUnsetValue;
144 }
145 }
146
147 template< class ValueClass >
148 inline bool
150 std::pair< int, int > const& indexPair ) const
151 // this returns true if there is an entry at soughtIndex.
152 {
153 return ( 0 < valueMap.count( indexPair ) );
154 }
155
156 template< class ValueClass >
157 inline std::string const&
159 // see base version's description.
160 {
161 this->stringInterpretation.clear();
162 mapIterator valueFinder( valueMap.begin() );
163 while( valueFinder != valueMap.end() )
164 {
165 this->indexPrintingVector[ 0 ] = valueFinder->first.first;
166 this->indexPrintingVector[ 1 ] = valueFinder->first.second;
167 this->stringInterpretation.append( this->indicesToPrintingString() );
168 this->stringInterpretation.append( this->valueToPrintingString(
169 valueFinder->second ) );
170 this->stringInterpretation.append( "\n" );
171 ++valueFinder;
172 }
173 return this->stringInterpretation;
174 }
175
176 template< class ValueClass >
177 inline void
179 {
180 valueMap.clear();
181 }
182
183 template< class ValueClass >
184 inline void
186 {
187 for( int whichLine( this->currentStringBlock->getNumberOfBodyLines() );
188 0 < whichLine;
189 --whichLine )
190 {
191 this->currentWord.assign( BOL::StringParser::firstWordOf(
192 (*(this->currentStringBlock))[ whichLine ].first,
193 &(this->lineRemainderA),
195 if( !(this->currentWord.empty()) )
196 {
197 valueRecorder.first.first
198 = BOL::StringParser::stringToInt( this->currentWord );
199 this->currentWord.assign( BOL::StringParser::firstWordOf(
200 this->lineRemainderA,
201 &(this->lineRemainderB),
203 if( !(this->currentWord.empty()) )
204 {
205 valueRecorder.first.second
206 = BOL::StringParser::stringToInt( this->currentWord );
207 valueRecorder.second
208 = this->stringToValue( BOL::StringParser::trimFromFrontAndBack(
209 this->lineRemainderB,
211 valueMap.insert( valueRecorder );
212 }
213 }
214 }
215 }
216
217 }
218
219 }
220
221}
222
223#endif /* SPARSEDOUBLYINDEXED_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)
bool hasEntry(int const firstIndex, int const secondIndex) const
bool hasEntry(std::pair< int, int > const &indexPair) const
std::map< std::pair< int, int >, ValueClass > valueMap
ValueClass & operator()(int const firstIndex, int const secondIndex)
ValueClass const & operator()(int const firstIndex, int const secondIndex) const
ValueClass & operator()(std::pair< int, int > const &indexPair)
std::map< std::pair< int, int >, ValueClass >::const_iterator mapIterator
std::pair< std::pair< int, int >, ValueClass > valueRecorder