The Gaudi Framework  master (d98a2936)
KeyedContainer.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 
16 #include <algorithm>
17 #include <iterator>
18 
19 namespace GaudiDict {
20  template <class T>
22 }
23 
60 template <class DATATYPE, class MAPPING = Containers::HashMap>
62  friend struct GaudiDict::KeyedContainerDict<DATATYPE>;
63 
64 public:
66  typedef DATATYPE contained_type;
68  typedef MAPPING container_type;
69 
76  typedef typename std::vector<contained_type*> seq_type;
81  typedef typename seq_type::value_type value_type;
83  typedef typename seq_type::reference reference;
85  typedef typename seq_type::const_reference const_reference;
87  typedef typename seq_type::iterator iterator;
89  typedef typename seq_type::const_iterator const_iterator;
91  typedef typename seq_type::reverse_iterator reverse_iterator;
95  typedef typename seq_type::const_reverse_iterator const_reverse_iterator;
97 private:
102 
106  container_type m_cont;
112 
114 #ifdef CHECK_KEYED_CONTAINER
115  value_type i_object( const key_type& k ) const {
116  if ( 0 == m_cont.isDirect() ) {
117  if ( traits::checkBounds( m_random, k ) ) {
118  value_type p = *( m_random->begin() + traits::hash( k ) );
119  if ( traits::checkKey( p, k ) ) { return p; }
120  }
121  return 0;
122  }
123  value_type p = value_type( m_cont.object( traits::hash( k ) ) );
124  return traits::checkKey( p, k ) ? p : 0;
125  }
126 #else
127  inline value_type i_object( const key_type& k ) const {
128  return 0 == m_cont.isDirect() ? value_type( *( m_random->begin() + traits::hash( k ) ) )
129  : value_type( m_cont.object( traits::hash( k ) ) );
130  }
131 #endif
132  long i_erase( const_reference v, const key_type& k ) {
134  value_type p = value_type( m_cont.erase( traits::hash( k ), v ) );
135  if ( p ) {
136  if ( p->parent() == this ) { p->setParent( 0 ); }
137  }
138  return traits::release( p ) <= 0 ? (long)Containers::OBJ_ERASED : (long)Containers::OBJ_DELETED;
139  }
140 
142  struct _InsertRelease {
145  void operator()( value_type p ) {
146  m_obj->insert( p );
147  traits::release( p );
148  }
149  };
150 
152  struct _RemoveRelease {
154  _RemoveRelease( ObjectContainerBase* p ) : m_obj( p ) {}
155  void operator()( value_type p ) {
156  const ObjectContainerBase* par = p->parent();
157  if ( par == m_obj ) { p->setParent( 0 ); }
158  traits::release( p );
159  }
160  };
162 
163 public:
167  KeyedContainer( void ) {
169  // avoid problems with strict-aliasing rules
170  seq_type** rptr = &m_random;
171  seq_type* sptr = &m_sequential;
172  m_cont.setup( (void*)sptr, (void**)rptr );
173  }
175  : ObjectContainerBase( std::move( other ) )
176  , m_cont( std::move( other.m_cont ) )
177  , m_sequential( std::move( other.m_sequential ) ) {
178  m_cont.setup( (void*)&m_sequential, (void**)&m_random );
179  std::for_each( begin(), end(), [this]( ContainedObject* obj ) { obj->setParent( this ); } );
180 
181  other.m_cont.setup( (void*)&other.m_sequential, (void**)&other.m_random );
182  }
183  KeyedContainer( const KeyedContainer& ) = delete;
185  ~KeyedContainer() override;
187 
193  const CLID& clID() const override { return this->classID(); }
196  static const CLID& classID() {
197  static CLID clid = contained_type::classID() + container_type::classID();
198  return clid;
199  }
201 
218  size_type numberOfObjects() const override { return m_sequential.size(); }
231  long add( ContainedObject* pObject ) override;
232 
245  long remove( ContainedObject* pObject ) override;
246 
250  ContainedObject* containedObject( long key_value ) override { return i_object( traits::makeKey( key_value ) ); }
251  ContainedObject const* containedObject( long key_value ) const override {
252  return i_object( traits::makeKey( key_value ) );
253  }
257  long index( const ContainedObject* p ) const override;
263  virtual size_type containedObjects( std::vector<ContainedObject*>& v ) const;
265 
271  size_type size() const { return m_sequential.size(); }
274  bool empty() const { return m_sequential.empty(); }
276  void reserve( size_type value ) { m_cont.reserve( value ); }
278  void clear() { erase( begin(), end() ); }
284  virtual const std::vector<const ContainedObject*>* containedObjects() const;
289  StatusCode update() override;
291 
305  iterator begin() { return m_sequential.begin(); }
308  const_iterator begin() const { return m_sequential.begin(); }
310  iterator end() { return m_sequential.end(); }
312  const_iterator end() const { return m_sequential.end(); }
314  reverse_iterator rbegin() { return m_sequential.rbegin(); }
316  const_reverse_iterator rbegin() const { return m_sequential.rbegin(); }
318  reverse_iterator rend() { return m_sequential.rend(); }
320  const_reverse_iterator rend() const { return m_sequential.rend(); }
322 
340  value_type object( const key_type& kval ) const { return i_object( kval ); }
341 
351  value_type operator()( const key_type& kval ) const { return i_object( kval ); }
353 
376  long erase( const key_type& kval ) { return i_erase( 0, kval ); }
377 
398  long erase( const value_type val ) { return ( val ) ? i_erase( val, val->key() ) : (long)Containers::OBJ_NOT_FOUND; }
399 
420  long erase( iterator pos ) { return erase( *pos ); }
421 
430  void erase( iterator pos_start, iterator pos_stop, bool use_temp = false );
431 
450  const key_type& insert( const value_type val, const key_type& kval );
451 
473  const key_type& insert( const value_type val );
475 };
476 
484 // Destructor
485 template <class DATATYPE, class MAPPING>
487  clear();
488  m_cont.clear();
489 }
490 
491 // Configure direct access
492 template <class DATATYPE, class MAPPING>
494  int count = 0;
495  m_cont.clearDirect();
496  for ( typename seq_type::value_type v : m_sequential ) {
497  if ( v ) {
498  if ( !v->hasKey() ) {
499  traits::setKey( v, v->key() );
500  traits::addRef( v );
501  }
502  long k0 = traits::hash( v->key() );
503  if ( m_cont.insertDirect( this, v, v, k0 ) == Containers::OBJ_INSERTED ) {}
504  } else {
505  ++count;
506  }
507  }
508  if ( count > 0 ) { Containers::cannotInsertToContainer(); }
509  return StatusCode::SUCCESS;
510 }
511 
512 // Retrieve the full content of the object container by reference.
513 template <class DATATYPE, class MAPPING>
514 inline const std::vector<const ContainedObject*>* KeyedContainer<DATATYPE, MAPPING>::containedObjects() const {
515  return (const std::vector<const ContainedObject*>*)( ( 0 == m_cont.isDirect() ) ? m_random : &m_sequential );
516 }
517 
518 template <class DATATYPE, class MAPPING>
519 inline const typename KeyedContainer<DATATYPE, MAPPING>::key_type&
521  if ( val ) {
522  long k0 = traits::hash( kval );
523  if ( !val->hasKey() || ( traits::hash( val->key() ) == k0 ) ) {
524  if ( m_cont.insert( this, val, val, k0 ) == Containers::OBJ_INSERTED ) {
525  if ( !val->hasKey() ) traits::setKey( val, kval );
526  traits::addRef( val );
527  return val->key();
528  }
529  }
530  }
531  // Cannot insert object...indicate bad object insertion...
533  return val->key();
534 }
535 
536 // Insert object
537 template <class DATATYPE, class MAPPING> // inline
540  if ( 0 != val ) {
541  if ( val->hasKey() ) {
542  if ( m_cont.insert( this, val, val, traits::hash( val->key() ) ) == Containers::OBJ_INSERTED ) {
543  traits::addRef( val );
544  return val->key();
545  }
546  }
547  long k0;
548  if ( m_cont.insert( this, val, val, &k0 ) == Containers::OBJ_INSERTED ) {
549  traits::setKey( val, traits::makeKey( k0 ) );
550  traits::addRef( val );
551  return val->key();
552  }
553  }
554  // Cannot insert object...indicate bad object insertion...
556  return val->key();
557 }
558 
559 template <class DATATYPE, class MAPPING>
561  const contained_type* ptr = dynamic_cast<const contained_type*>( p );
562  if ( ptr ) return traits::identifier( ptr->key() );
563  return -1;
564 }
565 
566 // Retrieve the full content of the object container.
567 template <class DATATYPE, class MAPPING>
569 KeyedContainer<DATATYPE, MAPPING>::containedObjects( std::vector<ContainedObject*>& vec ) const {
570  vec.clear();
571  vec.reserve( size() );
572  for ( typename seq_type::value_type v : m_sequential ) {
573  ContainedObject* p = const_cast<typename seq_type::value_type>( v );
574  vec.push_back( p );
575  }
576  return vec.size();
577 }
578 
579 // ObjectContainerBase overload: Add an object to the container.
580 template <class DATATYPE, class MAPPING>
582  return traits::identifier( insert( dynamic_cast<typename seq_type::value_type>( pObject ) ) );
583 }
584 
585 // ObjectContainerBase overload: Remove an object from the container.
586 template <class DATATYPE, class MAPPING>
588  contained_type* p1 = dynamic_cast<contained_type*>( p );
589  if ( p1 ) { // Normal case; object still fully intact
590  return this->erase( p1 );
591  } else if ( p ) {
592  const ObjectContainerBase* par = p->parent();
593  // The following should never occur: object is in a funny state,
594  // Because the parent was explicitly set to NULL in the
595  // KeyeObject destructor.
596  // - It cannot be a KeyedObject: It would not have a parent
597  // - Still the parent is present: We are not in the destructor
598  // of KeyedObject
600  return m_cont.erase( 0, p ) == 0 ? (long)Containers::OBJ_ERASED : (long)Containers::OBJ_NOT_FOUND;
601  }
602  return (long)Containers::OBJ_NOT_FOUND;
603 }
604 
605 template <class DATATYPE, class MAPPING>
606 inline void KeyedContainer<DATATYPE, MAPPING>::erase( iterator start_pos, iterator stop_pos, bool use_tmp ) {
607  bool is_start = start_pos == m_sequential.begin();
608  bool is_stop = stop_pos == m_sequential.end();
609  if ( is_start && is_stop ) {
610  // Nothing special. Taken care of by Keyed object manager
611  } else if ( is_start || is_stop || use_tmp ) {
612  std::vector<DATATYPE*> tmp( m_sequential.begin(), start_pos );
613  tmp.insert( tmp.end(), stop_pos, m_sequential.end() );
614  std::for_each( tmp.begin(), tmp.end(), traits::addRef );
615  this->erase( m_sequential.begin(), m_sequential.end() );
616  std::for_each( tmp.begin(), tmp.end(), _InsertRelease( this ) );
617  return;
618  }
619  std::for_each( start_pos, stop_pos, _RemoveRelease( this ) );
620  seq_type* sptr = &m_sequential; // avoid problems with strict-aliasing rules
621  std::vector<void*>* v = (std::vector<void*>*)sptr;
622  std::vector<void*>::iterator i1 = v->begin() + std::distance( m_sequential.begin(), start_pos );
623  std::vector<void*>::iterator i2 = v->begin() + std::distance( m_sequential.begin(), stop_pos );
624  m_cont.erase( i1, i2 ); // cppcheck-suppress iterators1
625 }
KeyedContainer::erase
long erase(const key_type &kval)
Remove/erase object (identified by key) from the container.
Definition: KeyedContainer.h:376
KeyedContainer::empty
bool empty() const
For consistency with STL: check if container is empty.
Definition: KeyedContainer.h:274
Containers::OBJ_ERASED
@ OBJ_ERASED
Object was removed, but not deleted
Definition: KeyedTraits.h:32
KeyedContainer::end
iterator end()
Retrieve terminating iterator.
Definition: KeyedContainer.h:310
Containers::cannotInsertToContainer
GAUDI_API void cannotInsertToContainer()
Function to be called to indicate that an object cannot be inserted to the container.
Definition: KeyedObjectManager.cpp:82
Containers::KeyedObjectManager
KeyedObjectManager Class to manage keyed objects.
Definition: KeyedObjectManager.h:47
KeyedContainer::containedObject
ContainedObject * containedObject(long key_value) override
ObjectContainerBase overload: Retrieve the object by reference given the long integer representation ...
Definition: KeyedContainer.h:250
Containers::OBJ_INSERTED
@ OBJ_INSERTED
Object was inserted into the container.
Definition: KeyedTraits.h:33
KeyedObjectManager.h
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
Containers::KeyedObjectManager::insertDirect
long insertDirect(ObjectContainerBase *b, ContainedObject *c, void *o, long k)
Insert element into direct access map.
Definition: KeyedObjectManager.cpp:175
KeyedContainer::key_type
contained_type::key_type key_type
Definition of the key type: re-use definition of contained type.
Definition: KeyedContainer.h:79
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
KeyedContainer::m_random
seq_type * m_random
Array to allow random access to objects (not exposed)
Definition: KeyedContainer.h:111
KeyedContainer::const_iterator
seq_type::const_iterator const_iterator
Sequential access: const iterator type used in sequential container.
Definition: KeyedContainer.h:89
KeyedContainer::update
StatusCode update() override
Reconfigure direct access to elements (Needed by POOL data loading) This function reuses the "update"...
Definition: KeyedContainer.h:493
KeyedContainer::begin
const_iterator begin() const
Retrieve start const iterator.
Definition: KeyedContainer.h:308
KeyedContainer::rend
reverse_iterator rend()
reverse_iterator pointing to the end of the reversed container
Definition: KeyedContainer.h:318
KeyedContainer::operator()
value_type operator()(const key_type &kval) const
STL algorithms support for object access.
Definition: KeyedContainer.h:351
KeyedContainer::const_reverse_iterator
seq_type::const_reverse_iterator const_reverse_iterator
Sequential access: const reverse iterator type used in sequential container.
Definition: KeyedContainer.h:95
Containers::KeyedObjectManager::insert
long insert(ObjectContainerBase *b, ContainedObject *c, void *o, long *k)
Insert new object into container.
Definition: KeyedObjectManager.cpp:142
Containers::KeyedObjectManager::clearDirect
void clearDirect()
Clear all direct access fields.
Definition: KeyedObjectManager.cpp:250
conf.release
string release
Definition: conf.py:27
KeyedContainer::_RemoveRelease::operator()
void operator()(value_type p)
Definition: KeyedContainer.h:155
KeyedObject.h
KeyedContainer::value_type
seq_type::value_type value_type
Sequential access: definition of type stored in sequential container.
Definition: KeyedContainer.h:81
KeyedContainer::containedObject
ContainedObject const * containedObject(long key_value) const override
Pointer to an object of a given distance.
Definition: KeyedContainer.h:251
Containers::invalidContainerOperation
GAUDI_API void invalidContainerOperation()
Function to be called to indicate that an operation should be performed on the container or it's cont...
Definition: KeyedObjectManager.cpp:91
KeyedContainer::_RemoveRelease
Internal functor for insertion of objects.
Definition: KeyedContainer.h:152
KeyedContainer::reserve
void reserve(size_type value)
Reserve place for "value" objects in the container.
Definition: KeyedContainer.h:276
KeyedContainer::_InsertRelease::operator()
void operator()(value_type p)
Definition: KeyedContainer.h:145
KeyedContainer::clear
void clear()
Clear the entire content and erase the objects from the container.
Definition: KeyedContainer.h:278
compareOutputFiles.par
par
Definition: compareOutputFiles.py:477
KeyedContainer::_InsertRelease::m_obj
KeyedContainer< DATATYPE, MAPPING > * m_obj
Definition: KeyedContainer.h:143
KeyedContainer::add
long add(ContainedObject *pObject) override
ObjectContainerBase overload: Add an object to the container.
Definition: KeyedContainer.h:581
Containers::KeyedObjectManager::object
void * object(long key) const
Retrieve object identified by a key from the container.
Definition: KeyedObjectManager.cpp:222
KeyedContainer::container_type
MAPPING container_type
Definition of the implementing container type.
Definition: KeyedContainer.h:68
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
KeyedContainer::_RemoveRelease::m_obj
ObjectContainerBase * m_obj
Definition: KeyedContainer.h:153
KeyedContainer::object
value_type object(const key_type &kval) const
Object access by key.
Definition: KeyedContainer.h:340
StatusCode
Definition: StatusCode.h:64
Gaudi::cxx::for_each
void for_each(ContainerOfSynced &c, Fun &&f)
Definition: SynchronizedValue.h:98
KeyedContainer::contained_type
DATATYPE contained_type
Definition of the contained object type.
Definition: KeyedContainer.h:66
KeyedContainer::erase
long erase(const value_type val)
Remove/erase object (identified by pointer value) from the container.
Definition: KeyedContainer.h:398
Containers::OBJ_DELETED
@ OBJ_DELETED
Object was removed from the container and deleted.
Definition: KeyedTraits.h:31
KeyedContainer::const_reference
seq_type::const_reference const_reference
Sequential access: const reference type used in sequential container.
Definition: KeyedContainer.h:85
AlgSequencer.p1
p1
Definition: AlgSequencer.py:29
Containers::OBJ_NOT_FOUND
@ OBJ_NOT_FOUND
Object not present in the container.
Definition: KeyedTraits.h:30
ObjectContainerBase::size_type
size_t size_type
size_type, to conform the STL container interface
Definition: ObjectContainerBase.h:37
KeyedContainer
template class KeyedContainer, KeyedContainer.h
Definition: KeyedContainer.h:61
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:16
KeyedContainer::rbegin
reverse_iterator rbegin()
reverse_iterator returns the beginning of the reversed container
Definition: KeyedContainer.h:314
KeyedContainer::insert
const key_type & insert(const value_type val, const key_type &kval)
Insert entry to the container with a valid key.
Definition: KeyedContainer.h:520
GaudiDict::KeyedContainerDict
Definition: KeyedContainer.h:21
KeyedContainer::iterator
seq_type::iterator iterator
Sequential access: iterator type used in sequential container.
Definition: KeyedContainer.h:87
Containers::KeyedObjectManager::erase
void * erase(long key, const void *obj)
Remove object from container (very inefficient if key is invalid)
Definition: KeyedObjectManager.cpp:200
KeyedContainer::classID
static const CLID & classID()
Retrieve class ID.
Definition: KeyedContainer.h:196
ContainedObject::parent
const ObjectContainerBase * parent() const
Access to parent object.
Definition: ContainedObject.h:59
KeyedObject< int >::key_type
int key_type
Definition of the key-type to access object.
Definition: KeyedObject.h:42
KeyedContainer::KeyedContainer
KeyedContainer(const KeyedContainer &)=delete
Containers::KeyedObjectManager::reserve
void reserve(long size)
Reserve buffer space.
Definition: KeyedObjectManager.cpp:230
KeyedContainer::_InsertRelease::_InsertRelease
_InsertRelease(KeyedContainer< DATATYPE, MAPPING > *p)
Definition: KeyedContainer.h:144
KeyedContainer::end
const_iterator end() const
Retrieve terminating const iterator.
Definition: KeyedContainer.h:312
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
KeyedContainer::index
long index(const ContainedObject *p) const override
ObjectContainerBase overload: Retrieve the full long integer representation of the object's key from ...
Definition: KeyedContainer.h:560
ContainedObject::setParent
void setParent(ObjectContainerBase *value)
Update parent member.
Definition: ContainedObject.h:61
KeyedContainer::_RemoveRelease::_RemoveRelease
_RemoveRelease(ObjectContainerBase *p)
Definition: KeyedContainer.h:154
ObjectContainerBase
Definition: ObjectContainerBase.h:26
KeyedContainer::containedObjects
virtual const std::vector< const ContainedObject * > * containedObjects() const
Retrieve the full content of the object container by reference.
Definition: KeyedContainer.h:514
GaudiDict
Definition: KeyedContainer.h:19
KeyedContainer::_InsertRelease
Internal functor for insertion of objects.
Definition: KeyedContainer.h:142
KeyedContainer::erase
long erase(iterator pos)
Remove/erase object (identified by iterator) from the container.
Definition: KeyedContainer.h:420
KeyedContainer::erase
void erase(iterator pos_start, iterator pos_stop, bool use_temp=false)
Remove/erase objects by iterator range.
Definition: KeyedContainer.h:606
Containers::traits
Container traits class.
Definition: KeyedTraits.h:39
Containers::KeyedObjectManager::setup
void setup(void *seq, void **rndm)
Setup of the Map and the parent object.
Definition: KeyedObjectManager.cpp:127
fixtures.reference
Generator[dict, None, None] reference(request, Optional[Path] reference_path)
Definition: fixtures.py:211
KeyedContainer::i_object
value_type i_object(const key_type &k) const
Internal function to access objects within the container.
Definition: KeyedContainer.h:127
KeyedContainer::m_sequential
seq_type m_sequential
Array to allow sequential access to the object (can be ordered).
Definition: KeyedContainer.h:109
ObjectContainerBase.h
KeyedContainer::reverse_iterator
seq_type::reverse_iterator reverse_iterator
Sequential access: reverse iterator type used in sequential container.
Definition: KeyedContainer.h:91
KeyedContainer::remove
long remove(ContainedObject *pObject) override
ObjectContainerBase overload: Remove an object from the container.
Definition: KeyedContainer.h:587
Properties.v
v
Definition: Properties.py:122
KeyedContainer::reference
seq_type::reference reference
Sequential access: reference type used in sequential container.
Definition: KeyedContainer.h:83
Containers::KeyedObjectManager::isDirect
long isDirect() const
Check if the container is dirty.
Definition: KeyedObjectManager.h:75
ObjectContainerBase::numberOfObjects
virtual size_type numberOfObjects() const =0
Number of objects in the container.
KeyedContainer::traits
Containers::traits< container_type, contained_type > traits
Traits class definition.
Definition: KeyedContainer.h:101
IOTest.end
end
Definition: IOTest.py:125
KeyedContainer::containedObjects
virtual size_type containedObjects(std::vector< ContainedObject * > &v) const
Retrieve the full content of the object container.
Definition: KeyedContainer.h:569
DataObject::classID
static const CLID & classID()
Retrieve reference to class definition structure (static access)
Definition: DataObject.cpp:69
DataObject::clID
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:66
Gaudi::Functional::details::insert
constexpr struct Gaudi::Functional::details::insert_t insert
KeyedContainer::~KeyedContainer
~KeyedContainer() override
Destructor.
Definition: KeyedContainer.h:486
KeyedContainer::KeyedContainer
KeyedContainer(KeyedContainer &&other)
Definition: KeyedContainer.h:174
KeyedContainer::rend
const_reverse_iterator rend() const
const reverse_iterator pointing to the end of the reversed container
Definition: KeyedContainer.h:320
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
KeyedContainer::insert
const key_type & insert(const value_type val)
Insert entry to the container with automatic key assignment.
Definition: KeyedContainer.h:539
KeyedContainer::rbegin
const_reverse_iterator rbegin() const
const reverse_iterator returns the beginning of the reversed container
Definition: KeyedContainer.h:316
ContainedObject
Definition: ContainedObject.h:37
Containers::KeyedObjectManager::clear
void clear()
Clear content of the vector.
Definition: KeyedObjectManager.cpp:244
KeyedContainer< Gaudi::TestSuite::MyTrack >::seq_type
std::vector< contained_type * > seq_type
General container specific type definitions.
Definition: KeyedContainer.h:77