ATLAS Offline Software
Loading...
Searching...
No Matches
NestedContainer.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5
56
57// $Id: NestedContainer.h,v 1.6 2009-02-11 13:46:55 dwhittin Exp $
58
59
60#ifndef TRTConditionsData_NestedContainer_h
61#define TRTConditionsData_NestedContainer_h
62
63#include <vector>
64#include <map>
65#include <functional>
66#include <algorithm>
69#include "GaudiKernel/MsgStream.h"
70
71
72namespace TRTCond
73{
74
78 template <class Daughter, class T, class Trait>
80 {
82 static const T& get(const Daughter& d, const ExpandedIdentifier& id) { return d.get(id) ; }
84 static const T& get(const Daughter& d, const ExpandedIdentifier& id , size_t& resolvelevel) { return d.get(id,resolvelevel) ; }
86 static const T& get(const Daughter& d) { return d.get() ; }
88 static void set(Daughter& d, const ExpandedIdentifier& id, const T& t) { d.set(id,t) ; }
90 static bool empty(Daughter& d) { return d.daughters().empty() ; }
92 static void clear(Daughter& d, const ExpandedIdentifier& id) { d.clear(id) ; } ;
94 static void clear(Daughter& d) { d.clear() ; } ;
96 static void print(const Daughter& d) { d.print() ; }
98 static void getall(const Daughter& d, std::vector< std::pair<ExpandedIdentifier, const T*> >& entries) {
99 d.getall(entries) ; }
100
101 static Daughter initialvalue() { return Daughter() ; }
103 static void copy(Daughter& out, const Daughter& in) { out = in ; }
105 static size_t footprint(const Daughter& d) { return d.footprint() ; }
107 static void crunch(Daughter& d) { return d.crunch() ; }
109 static bool isequal(const Daughter& lhs, const Daughter& rhs) { return lhs==rhs ; }
111 static size_t numObjects(const Daughter& d) { return d.numObjects() ; }
112 } ;
113
118 template <class T, class Trait>
120 {
121 static const T& get(const T& d, const ExpandedIdentifier&) { return d ; }
122 static const T& get(const T& d, const ExpandedIdentifier& , size_t& ) { return d ; }
123 static const T& get(const T& d) { return d ; }
124 static void set(T& d, const ExpandedIdentifier&, const T& t) { Trait::copy(d,t) ; }
125 static bool empty(T&) { return true ; }
126 static void clear(T& d, const ExpandedIdentifier&) { Trait::erase(d) ; }
127 static void clear(T& d) { Trait::erase(d) ; }
128 static void print(const T& d) { Trait::print(d) ; }
129 static void getall(const T& d, std::vector< std::pair<ExpandedIdentifier, const T*> >& entries) {
130 if(Trait::isvalid(d) ) {
132 entries.push_back(std::pair<ExpandedIdentifier, const T*>(id,&d)) ;
133 }
134 }
135 static T initialvalue() { return Trait::initialvalue() ; }
136 static void copy(T& out, const T& in) { Trait::copy(out,in) ; }
137 static size_t footprint(const T& d) { return Trait::footprint(d) ; }
138 static void crunch(T&) {}
139 static bool isequal(const T& lhs, const T& rhs) { return Trait::isequal(lhs,rhs) ; }
140 static size_t numObjects(const T& d) { return Trait::isvalid(d) ? 1 : 0 ; }
141 } ;
142
143
144
149 template <int NestingLevel, class Daughter, class T, class Trait>
151 {
152 private:
153
154 typedef std::vector<Daughter> DaughterContainer ;
155 //typedef std::vector<Daughter>::const_iterator ConstIterator ;
158
159 public:
160 typedef T value_type ;
161 typedef const T const_value_type ;
162 typedef Trait trait_type ;
163
165 NestedContainerBase() : m_daughters(0), m_default(Trait::initialvalue()) {}
166 // NestedContainerBase(const NestedContainerBase& rhs)
167 // : m_daughters(0), m_default(Trait::initialvalue()) { *this = rhs ; }
168
171 //{
172 //cannot delete the daughter here, because of copy constructors
173 //std::cout << "in ~NestedContainerBase(): " << std::flush ;
174 //Trait::print(m_default) ;
175 //Trait::erase(m_default) ;
176 //std::cout << "done." << std::endl ;
177 //}
178
179 // NestedContainerBase& operator=(const NestedContainerBase& rhs) {
180 // std::cout << "operator=" << m_daughters.size() << " " << rhs.m_daughters.size() << std::endl ;
181 // clear() ;
182 // Trait::copy(m_default,rhs.m_default) ;
183 // m_daughters.resize(rhs.m_daughters.size(),NestedContainerDaughterAccessor<Daughter,T,Trait>::initialvalue()) ;
184 // for(unsigned int i=0; i<m_daughters.size(); ++i)
185 // NestedContainerDaughterAccessor<Daughter,T,Trait>::copy( m_daughters[i], rhs.m_daughters[i] ) ;
186 // std::cout << "done with operator=" << std::endl ;
187 // return *this ;
188 // }
189
191 void set( const ExpandedIdentifier& id, const T& t) {
192 if( NestingLevel == id.level() ) {
193 // set the default value
194 Trait::copy(m_default,t) ;
195 } else {
196 unsigned int thisindex = id.index(NestingLevel+1) ;
197 // expand daughter array if necessary
198 if(thisindex>=m_daughters.size())
201 }
202 }
203
205 const T& get( const ExpandedIdentifier& id ) const {
206 if( NestingLevel < id.level() ) {
207 unsigned int thisindex = id.index(NestingLevel+1) ;
208 if(thisindex<m_daughters.size()) {
210 // only return this value if it is valid. otherwise, return the default.
211 if( Trait::isvalid(rc) ) return rc ;
212 }
213 }
214 return m_default ;
215 }
216
217 const T& get( const ExpandedIdentifier& id, size_t& resolvelevel) const
218 {
219 if( NestingLevel < id.level() ) {
220 unsigned int thisindex = id.index(NestingLevel+1) ;
221 if(thisindex<m_daughters.size()) {
222 const T& rc = NestedContainerDaughterAccessor<Daughter,T,Trait>::get(m_daughters[thisindex],id,resolvelevel) ;
223 if( Trait::isvalid(rc) ) return rc ;
224 }
225 }
226 resolvelevel = NestingLevel ;
227 return m_default ;
228 }
229
230
232 const T& get() const { return m_default ; }
233
235 void set(const T& t) { Trait::copy(m_default,t) ; }
236
238 void clear() {
239 for( typename DaughterContainer::iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
241 m_daughters.clear() ;
242 Trait::erase(m_default) ;
243 }
244
246 void clear( const ExpandedIdentifier& id ) {
247 if( NestingLevel == id.level() ) clear() ;
248 else {
249 unsigned int thisindex = id.index(NestingLevel+1) ;
250 if(thisindex<m_daughters.size())
252 }
253 }
254
256 const DaughterContainer& daughters() const { return m_daughters ; }
257
258
260 size_t numObjects() const {
261 size_t rc(0) ;
262 assert( std::distance(m_daughters.begin(),m_daughters.end())==(int)m_daughters.size()) ;
263 assert( m_daughters.size() < 1000 ) ;
264 for( typename DaughterContainer::const_iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
266 if( Trait::isvalid(m_default) ) ++rc ;
267 return rc ;
268 }
269
270
272 bool operator==(const NestedContainerBase& rhs) const {
273 return m_daughters.size()==0 && rhs.m_daughters.size()==0 && Trait::isequal(m_default,rhs.m_default) ;
274 }
275
277 bool operator==(const T& rhs) const {
278 return m_daughters.size()==0 && Trait::isequal(m_default,rhs) ;
279 }
280
282 size_t footprint() const {
283 size_t total = Trait::footprint(m_default) + (m_daughters.capacity()-m_daughters.size())*sizeof(Daughter) ;
284 for( typename DaughterContainer::const_iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
286 return total ;
287 }
288
292 void crunch() {
293 // first the daughters
294 for( typename DaughterContainer::iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
296 // now use, if anything is there
297 if( m_daughters.size()>0 ) {
298 // find the first daughter that is valid
299 size_t firstvaliddaughter(0);
300 bool allequal(true) ;
301 for(size_t idau=0; idau<m_daughters.size() && allequal; ++idau) {
303 if( allequal ) {
304 bool isvalid = Trait::isvalid( NestedContainerDaughterAccessor<Daughter,T,Trait>::get(m_daughters[idau])) ;
305 if( isvalid ) {
306 if( firstvaliddaughter==0 ) firstvaliddaughter = idau ;
307 } else {
309 }
310 }
311 }
312 if(allequal) {
313 // set the default value to the value form the first daughter
314 T defaultvalue(Trait::initialvalue()) ;
315 if( firstvaliddaughter < m_daughters.size() )
316 Trait::copy(defaultvalue,NestedContainerDaughterAccessor<Daughter,T,Trait>::get(m_daughters[firstvaliddaughter])) ;
317 clear() ; // this clears pointers in the daughters. would rather replace that with destructors.
319 m_default = defaultvalue ;
320 } else if(m_daughters.size()<m_daughters.capacity()) {
321 DaughterContainer newdaughters(0) ;
322 newdaughters.reserve( m_daughters.size() ) ;
323 newdaughters.insert( newdaughters.end(), m_daughters.begin(), m_daughters.end() ) ;
324 newdaughters.swap(m_daughters) ;
325 }
326 }
327 }
328
330 void printindent() const {
331 for(int i=0; i<NestingLevel; ++i)
332 std::cout << '\t' ;
333 }
334 void print() const {
335 printindent() ;
337 std::cout << "level = " << id.name(NestingLevel) << " (" << NestingLevel << ")" << std::endl ;
338 for(unsigned int i=0; i< m_daughters.size(); ++i) {
339 printindent() ;
340 std::cout << "daughter " << i << std::endl ;
342 }
343 }
344
346 typedef std::vector< std::pair<ExpandedIdentifier, const T*> > FlatContainer ;
347 void getall( FlatContainer& entries ) const {
348 // add the daughters
349 for(unsigned int idau = 0; idau<m_daughters.size(); ++idau) {
350 FlatContainer dauentries ;
351 // first get them
353 // now update the identifiers to have the dauhgter index set correctly
354 for(unsigned int j=0; j< dauentries.size(); ++j )
355 dauentries[j].first.index(NestingLevel+1) = idau ;
356 // and append
357 entries.insert(entries.end(),dauentries.begin(),dauentries.end()) ;
358 }
359 // add the default value
360 if( Trait::isvalid(m_default) ) {
361 ExpandedIdentifier id(0,0,0,0,0,NestingLevel) ;
362 entries.push_back( std::pair<ExpandedIdentifier, const T*>(id,&m_default)) ;
363 }
364 }
365 } ;
366
367
370 template <int NestingLevel, class T, class Trait>
372 : public NestedContainerBase<NestingLevel, NestedContainer<NestingLevel+1,T,Trait>, T, Trait>
373 {
374 } ;
375
376
378 template <class T, class Trait>
379 class NestedContainer<ExpandedIdentifier::STRAWLAYER,T,Trait>
380 : public NestedContainerBase<ExpandedIdentifier::STRAWLAYER,T,T,Trait>
381 {
382 } ;
383
384
387 template <class T>
389 {
390 public:
391 using pointer = T;
392 using base_type = std::remove_pointer_t<pointer>;
393 using const_pointer = const base_type*;
394 static void erase( pointer& x) { delete x ; x = 0 ; }
395 static void copy( pointer& out, const_pointer in) { if(out) erase(out) ; if(in) out = in->clone() ; }
396 static bool isvalid( const_pointer x ) { return x!=0 ; }
397 static void initialize( pointer& x) { x = 0 ; }
398 static size_t footprint( const_pointer x) { return sizeof(pointer) + (x ? x->footprint() : 0) ; }
399 static void print(const_pointer x) { std::cout << x << std::endl ; }
400 static pointer initialvalue() { return 0 ; }
401 static bool isequal(const_pointer lhs, const_pointer rhs) { return lhs==rhs || (lhs && rhs && *lhs==*rhs) ; }
402 } ;
403}
404
405
406#endif
static Double_t rc
#define x
void clear()
clear entire container
bool operator==(const NestedContainerBase &rhs) const
equality operator, used for compressing
void crunch()
reduce the footprint as much as possible by removing the extra space allocated by the vectors
std::vector< std::pair< ExpandedIdentifier, const T * > > FlatContainer
fill vector with all entries in the container. needed to dump to Peter's flat file format
T m_default
Type of atomic element.
~NestedContainerBase()=default
destructor
bool operator==(const T &rhs) const
another equality operator, used for compressing
void set(const T &t)
set the default value
void clear(const ExpandedIdentifier &id)
clear only entries specified
Trait trait_type
Type of trait.
DaughterContainer m_daughters
Container of Daughters.
const T & get() const
get the default value
T value_type
Type of atomic element.
size_t numObjects() const
return the total number of valid calibration objects
const T const_value_type
const Type of atomic element.
const DaughterContainer & daughters() const
return the vector of daughters
const T & get(const ExpandedIdentifier &id, size_t &resolvelevel) const
std::vector< Daughter > DaughterContainer
typedef of container
const T & get(const ExpandedIdentifier &id) const
get a value.
void getall(FlatContainer &entries) const
size_t footprint() const
return the memory allocated by the container and its daughters
void printindent() const
some IO for debugging
void set(const ExpandedIdentifier &id, const T &t)
set a value.
Default trait class if T is an owned pointer.
static void print(const_pointer x)
static bool isvalid(const_pointer x)
static bool isequal(const_pointer lhs, const_pointer rhs)
static void copy(pointer &out, const_pointer in)
std::remove_pointer_t< pointer > base_type
static size_t footprint(const_pointer x)
Nested container class, ie a base class at level l with daugthers at level l+1.
singleton-like access to IMessageSvc via open function and helper
double entries
Definition listroot.cxx:49
static void getall(const T &d, std::vector< std::pair< ExpandedIdentifier, const T * > > &entries)
static const T & get(const T &d, const ExpandedIdentifier &)
static void set(T &d, const ExpandedIdentifier &, const T &t)
static const T & get(const T &d, const ExpandedIdentifier &, size_t &)
static void clear(T &d, const ExpandedIdentifier &)
static bool isequal(const T &lhs, const T &rhs)
static void print(const Daughter &d)
print daugther info
static bool empty(Daughter &d)
empty daughter container
static void copy(Daughter &out, const Daughter &in)
copy daugthers
static void clear(Daughter &d)
clear container
static const T & get(const Daughter &d, const ExpandedIdentifier &id, size_t &resolvelevel)
resolve the nesting level for a given id
static bool isequal(const Daughter &lhs, const Daughter &rhs)
equality operator
static void getall(const Daughter &d, std::vector< std::pair< ExpandedIdentifier, const T * > > &entries)
copy all object pointers into a vector of (id,pointer) pairs
static const T & get(const Daughter &d)
get default for daughter container
static void clear(Daughter &d, const ExpandedIdentifier &id)
clear container for given id
static Daughter initialvalue()
make a default daugther
static void crunch(Daughter &d)
compress daugther size
static size_t numObjects(const Daughter &d)
total number of atomic objects in daugther
static size_t footprint(const Daughter &d)
return allocated memory
static void set(Daughter &d, const ExpandedIdentifier &id, const T &t)
set value for given id
static const T & get(const Daughter &d, const ExpandedIdentifier &id)
get value for given id