CMS 3D CMS Logo

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