CMS 3D CMS Logo

DDValue.cc
Go to the documentation of this file.
2 
3 #include <cassert>
4 
7 
8 static std::atomic<unsigned int> lastIndex{0};
9 
10 void
12 {
13  auto result = indexer().insert( { name, 0 });
14 
15  auto& indexToUse = result.first->second;
16 
17  //A 0 index means either
18  // 1) this result was just added or
19  // 2) another thread just added this but has not yet assigned an index
20  if(0 == indexToUse.value_) {
21  auto newIndex = ++lastIndex;
22  unsigned int previous = 0;
23  indexToUse.value_.compare_exchange_strong(previous,newIndex);
24  }
25  id_ = indexToUse.value_.load();
26 
27  //Make sure the name is recorded at the proper index
28  auto& allNames = names();
29  allNames.grow_to_at_least(id_+1);
30  auto& storedName = allNames[id_];
31  if(not storedName.string_) {
32  std::unique_ptr<std::string> newName(new std::string{name});
33  std::string* previous = nullptr;
34  if( storedName.string_.compare_exchange_strong(previous,newName.get())) {
35  newName.release();
36  }
37  }
38 }
39 
41  : id_( 0 ),
42  vecPair_()
43 {
44  init( name );
45 }
46 
47 DDValue::DDValue( const char * name )
48  : id_( 0 ),
49  vecPair_()
50 {
51  init( name );
52 }
53 
54 DDValue::DDValue( const std::string & name, const std::vector<DDValuePair>& v )
55  : id_( 0 )
56 {
57  init( name );
58 
59  auto it = v.begin();
60  std::vector<std::string> svec;
61  std::vector<double> dvec;
62  vecPair_.reset(new vecpair_type( false, std::make_pair( svec, dvec )));
63  for(; it != v.end(); ++it )
64  {
65  vecPair_->second.first.push_back( it->first );
66  vecPair_->second.second.push_back( it->second );
67  }
68 }
69 
70 
71 DDValue::DDValue( const std::string & name, double val )
72  : id_( 0 )
73 {
74  init( name );
75 
76  std::vector<std::string> svec( 1, "" );
77  std::vector<double> dvec( 1, val );
78 
79  vecPair_.reset( new vecpair_type( false, std::make_pair( svec, dvec )));
80  setEvalState( true );
81 }
82 
83 DDValue::DDValue( const std::string & name, const std::string & sval, double dval )
84  : id_( 0 )
85 {
86  init( name );
87 
88  std::vector<std::string> svec( 1, sval );
89  std::vector<double> dvec( 1, dval );
90  vecPair_.reset(new vecpair_type( false, std::make_pair( svec, dvec )));
91  setEvalState( true );
92 }
93 
94 DDValue::DDValue( const std::string & name, const std::string & sval )
95  : id_( 0 )
96 {
97  init( name );
98 
99  std::vector<std::string> svec( 1, sval );
100  std::vector<double> dvec( 1, 0 );
101  vecPair_.reset(new vecpair_type( false, std::make_pair( svec, dvec )));
102  setEvalState( false );
103 }
104 
105 DDValue::DDValue( unsigned int i )
106  : id_( 0 ),
107  vecPair_()
108 {
109  if( lastIndex >= i )
110  id_ = i;
111 }
112 
115 {
116  static NamesToIndicies indexer_;
117  return indexer_;
118 }
119 
121  //Make sure memory is zeroed before allocating StringHolder
122  // this allows us to check the value of the held std::atomic
123  // as the object is being added to the container
125  names.push_back(StringHolder(std::string{}));
126  return names;
127 }
128 
131 {
132  static Names names_{ initializeNames() };
133  return names_;
134 }
135 
136 const std::vector<double> &
137 DDValue::doubles( void ) const
138 {
139  if( vecPair_->first )
140  {
141  return vecPair_->second.second;
142  }
143  else
144  {
145  std::string message = "DDValue " + name() + " is not numerically evaluated! Use DDValue::std::strings()!";
146  edm::LogError("DDValue") << message << std::endl;
147  throw cms::Exception("DDException") << message;
148  }
149 }
150 
151 std::ostream & operator<<( std::ostream & o, const DDValue & v )
152 {
153  o << v.name() << " = ";
154  unsigned int i = 0;
155  if( v.isEvaluated())
156  {
157  for(; i < v.size(); ++i )
158  {
159  o << '(' << v[i].first << ',' << v[i].second << ") ";
160  }
161  }
162  else
163  {
164  const std::vector<std::string> & s = v.strings();
165  for(; i < v.size(); ++i )
166  {
167  o << s[i] << ' ';
168  }
169  }
170  return o;
171 }
172 
173 //FIXME move it elsewhere; DO NOT put out the name for now... need to fix DDCoreToDDXMLOutput
174 std::ostream & operator<<( std::ostream & o, const DDValuePair & v )
175 {
176  return o << v.second;
177 }
178 
180 DDValue::operator[]( unsigned int i ) const
181 {
182  if( vecPair_->first )
183  {
184  return DDValuePair( vecPair_->second.first[i], vecPair_->second.second[i] );
185  }
186  else
187  {
188  std::string message = "DDValue " + name() + " is not numerically evaluated! Use DDValue::std::strings()!";
189  edm::LogError( "DDValue" ) << message;
190  throw cms::Exception("DDException") << message;
191  }
192 }
193 
194 void
195 DDValue::setEvalState( bool newState )
196 {
197  vecPair_->first = newState;
198 }
199 
200 bool
201 DDValue::isEvaluated( void ) const
202 {
203  return vecPair_->first;
204 }
205 
206 bool
207 DDValue::operator==( const DDValue & v ) const
208 {
209  bool result( false );
210  if( id() == v.id())
211  {
212  assert( vecPair_ );
213  assert( v.vecPair_ );
214  if( vecPair_->first ) { // numerical values
215  result = ( vecPair_->second.second == v.vecPair_->second.second );
216  }
217  else { // std::string values
218  result = ( vecPair_->second.first == v.vecPair_->second.first );
219  }
220  }
221  return result;
222 }
223 
224 bool
225 DDValue::operator<( const DDValue & v ) const
226 {
227  bool result( false );
228  if( id() < v.id())
229  {
230  result = true;
231  }
232  else
233  {
234  if( id() == v.id())
235  {
236  assert( vecPair_ );
237  assert( v.vecPair_ );
238  if( vecPair_->first && v.vecPair_->first ) { // numerical values
239  result = ( vecPair_->second.second < v.vecPair_->second.second );
240  }
241  else { // std::string values
242  result = ( vecPair_->second.first < v.vecPair_->second.first );
243  }
244  }
245  }
246  return result;
247 }
const std::string & name(void) const
the name of the DDValue
Definition: DDValue.h:54
DDValue(void)
create a unnamed emtpy value. One can assing a named DDValue to it.
Definition: DDValue.h:25
const std::vector< double > & doubles() const
a reference to the double-valued values stored in the given instance of DDValue
Definition: DDValue.cc:137
Only used internally.
Definition: DDValue.h:90
static Names & names()
Definition: DDValue.cc:130
tbb::concurrent_vector< StringHolder, tbb::zero_allocator< StringHolder >> Names
Definition: DDValue.h:115
void setEvalState(bool newState)
set to true, if the double-values (method DDValue::doubles()) make sense
Definition: DDValue.cc:195
string newName
Definition: mps_merge.py:84
unsigned int id(void) const
returns the ID of the DDValue
Definition: DDValue.h:48
bool isEvaluated(void) const
true, if values are numerical evaluated; else false.
Definition: DDValue.cc:201
static Names initializeNames()
Definition: DDValue.cc:120
unsigned int id_
Definition: DDValue.h:122
static NamesToIndicies & indexer()
Definition: DDValue.cc:114
void init(const std::string &)
Definition: DDValue.cc:11
bool operator==(const DDValue &v) const
Two DDValues are equal only if their id() is equal AND their values are equal.
Definition: DDValue.cc:207
tbb::concurrent_unordered_map< std::string, AtomicUInt > NamesToIndicies
Definition: DDValue.h:119
static std::atomic< unsigned int > lastIndex
Definition: DDValue.cc:8
const std::vector< std::string > & strings() const
a reference to the std::string-valued values stored in the given instance of DDValue ...
Definition: DDValue.h:61
std::shared_ptr< vecpair_type > vecPair_
Definition: DDValue.h:124
DDValuePair operator[](unsigned int i) const
Definition: DDValue.cc:180
bool operator<(const DDValue &) const
A DDValue a is smaller than a DDValue b if (a.id()<b.id()) OR (a.id()==b.id() and value(a)<value(b)) ...
Definition: DDValue.cc:225
unsigned int size() const
the size of the stored value-pairs (std::string,double)
Definition: DDValue.h:68
std::pair< bool, std::pair< std::vector< std::string >, std::vector< double >>> vecpair_type
Definition: DDValue.h:123
std::ostream & operator<<(std::ostream &o, const DDValue &v)
Definition: DDValue.cc:151