CMS 3D CMS Logo

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