2 #ifndef GAUDIKERNEL_KEYEDCONTAINER_H
3 #define GAUDIKERNEL_KEYEDCONTAINER_H
10 template <
class T>
struct KeyedContainerDict;
14 #include "GaudiKernel/ObjectContainerBase.h"
15 #include "GaudiKernel/KeyedObjectManager.h"
16 #include "GaudiKernel/KeyedObject.h"
22 #define FORCE_INLINE __forceinline
24 #define FORCE_INLINE inline
63 template <
class DATATYPE,
class MAPPING=Containers::HashMap >
70 typedef DATATYPE contained_type;
72 typedef MAPPING container_type;
80 typedef typename std::vector<contained_type*> seq_type;
83 typedef typename contained_type::key_type key_type;
85 typedef typename seq_type::value_type value_type;
89 typedef typename seq_type::const_reference const_reference;
91 typedef typename seq_type::iterator iterator;
93 typedef typename seq_type::const_iterator const_iterator;
95 typedef typename seq_type::reverse_iterator reverse_iterator;
99 typedef typename seq_type::const_reverse_iterator const_reverse_iterator;
110 container_type m_cont;
113 seq_type m_sequential;
118 #ifdef CHECK_KEYED_CONTAINER
119 value_type i_object(
const key_type& k)
const {
120 if ( 0 == m_cont.isDirect() ) {
121 if ( traits::checkBounds(m_random, k) ) {
122 value_type p = *(m_random->begin()+traits::hash(k));
123 if ( traits::checkKey(p, k) ) {
129 value_type p = value_type(m_cont.object(traits::hash(k)));
130 return traits::checkKey(p, k) ? p : 0;
133 FORCE_INLINE value_type i_object(
const key_type& k)
const {
134 return 0==m_cont.isDirect()
135 ? value_type(*(m_random->begin()+traits::hash(k)))
136 : value_type(m_cont.object(traits::hash(k)));
139 long i_erase(const_reference v,
const key_type& k) {
141 value_type p = value_type(m_cont.erase(traits::hash(k), v));
143 if ( p->parent() == this ) {
152 struct _InsertRelease {
155 void operator()(value_type p) {
162 struct _RemoveRelease {
165 void operator()(value_type p) {
167 if ( par == m_obj ) {
183 seq_type** rptr = &m_random;
184 seq_type* sptr = &m_sequential;
185 m_cont.setup((
void*)sptr,(
void**)rptr);
200 static CLID clid = contained_type::classID() + container_type::classID();
221 virtual size_type
numberOfObjects()
const {
return m_sequential.size(); }
254 return i_object( traits::makeKey( key_value ) );
265 virtual size_type containedObjects(std::vector<ContainedObject*>& v)
const;
273 size_type size()
const {
return m_sequential.size(); }
276 bool empty()
const {
return m_sequential.empty(); }
278 void reserve(size_type
value) { m_cont.reserve(value); }
280 void clear() { erase(begin(),
end()); }
286 virtual const std::vector<const ContainedObject*>* containedObjects()
const;
307 iterator begin() {
return m_sequential.begin(); }
310 const_iterator begin()
const {
return m_sequential.begin(); }
312 iterator
end() {
return m_sequential.end(); }
314 const_iterator
end()
const {
return m_sequential.end(); }
316 reverse_iterator rbegin() {
return m_sequential.rbegin(); }
318 const_reverse_iterator rbegin()
const {
return m_sequential.rbegin(); }
320 reverse_iterator rend() {
return m_sequential.rend(); }
322 const_reverse_iterator rend()
const {
return m_sequential.rend(); }
342 value_type object(
const key_type& kval)
const {
return i_object(kval); }
353 value_type operator()(
const key_type& kval)
const {
return i_object(kval); }
378 long erase(
const key_type& kval) {
return i_erase(0, kval); }
400 long erase(
const value_type val) {
424 long erase(iterator pos) {
return erase(*pos); }
434 void erase(iterator pos_start, iterator pos_stop,
bool use_temp=
false);
454 const key_type& insert(
const value_type val,
const key_type& kval);
477 const key_type& insert(
const value_type val);
490 template <
class DATATYPE,
class MAPPING>
inline
493 erase(begin(),
end());
498 template <
class DATATYPE,
class MAPPING>
inline
502 m_cont.clearDirect();
503 typename seq_type::iterator
i = m_sequential.begin();
504 typename seq_type::iterator
s = m_sequential.end();
505 for ( ; i !=
s; i++ ) {
506 typename seq_type::value_type v = *
i;
508 if ( !v->hasKey() ) {
509 traits::setKey(v, v->key());
512 long k0 = traits::hash(v->key());
528 template <
class DATATYPE,
class MAPPING>
inline
529 const std::vector<const ContainedObject*>*
531 return (
const std::vector<const ContainedObject*>*)
532 ((0==m_cont.isDirect()) ? m_random : &m_sequential);
535 template <
class DATATYPE,
class MAPPING>
inline
538 const key_type& kval)
541 long k0 = traits::hash(kval);
542 if ( !val->hasKey() || (traits::hash(val->key()) == k0) ) {
544 if ( !val->hasKey() ) traits::setKey(val, kval);
556 template <
class DATATYPE,
class MAPPING>
561 if ( val->hasKey() ) {
562 if (m_cont.insert(
this,val,val,traits::hash(val->key()))
570 traits::setKey(val, traits::makeKey(k0));
580 template <
class DATATYPE,
class MAPPING>
inline
583 const contained_type* ptr =
dynamic_cast<const contained_type*
>(p);
584 if ( ptr )
return traits::identifier(ptr->key());
589 template <
class DATATYPE,
class MAPPING>
inline
591 (std::vector<ContainedObject*>& vec)
const
593 typename seq_type::const_iterator i = m_sequential.begin();
594 typename seq_type::const_iterator s = m_sequential.end();
597 for ( ; i !=
s; i++ ) {
605 template <
class DATATYPE,
class MAPPING>
inline
608 return traits::identifier(insert(dynamic_cast<typename seq_type::value_type>(pObject)));
612 template <
class DATATYPE,
class MAPPING>
inline
615 contained_type* p1 =
dynamic_cast<contained_type*
>(p);
617 return this->erase(p1);
636 template <
class DATATYPE,
class MAPPING>
inline
642 bool is_start = start_pos == m_sequential.begin();
643 bool is_stop = stop_pos == m_sequential.end();
644 if ( is_start && is_stop ) {
647 else if ( is_start || is_stop || use_tmp ) {
648 std::vector<DATATYPE*> tmp(m_sequential.begin(), start_pos);
649 tmp.insert(tmp.end(), stop_pos, m_sequential.end());
650 std::for_each(tmp.begin(), tmp.end(), traits::addRef);
651 this->erase(m_sequential.begin(), m_sequential.end());
652 std::for_each(tmp.begin(), tmp.end(), _InsertRelease(
this));
655 std::for_each(start_pos, stop_pos, _RemoveRelease(
this));
656 seq_type *sptr = &m_sequential;
657 std::vector<void*>* v = (std::vector<void*>*)sptr;
658 std::vector<void*>::iterator i1 =
659 v->begin() + std::distance(m_sequential.begin(), start_pos);
660 std::vector<void*>::iterator i2 =
661 v->begin() + std::distance(m_sequential.begin(), stop_pos);
662 m_cont.erase(i1, i2);
666 #endif // GAUDIKERNEL_KEYEDCONTAINER_H
virtual const std::vector< const ContainedObject * > * containedObjects() const
Retrieve the full content of the object container by reference.
virtual StatusCode update()
Provide empty placeholder for internal object reconfiguration callback.
static const CLID & classID()
Retrieve reference to class definition structure (static access)
virtual StatusCode update()
Reconfigure direct access to elements (Needed by POOL data loading) This function reuses the "update"...
size_t size_type
size_type, to conform the STL container interface
virtual long add(ContainedObject *pObject)
ObjectContainerBase overload: Add an object to the container.
virtual size_type numberOfObjects() const =0
Number of objects in the container.
Object was removed, but not deleted.
template class KeyedContainer, KeyedContainer.h
virtual ContainedObject * containedObject(long dist) const =0
Pointer to an object of a given distance.
long erase(const key_type &kval)
Remove/erase object (identified by key) from the container.
virtual long add(ContainedObject *pObject)=0
Virtual functions (forwards to the concrete container definitions) Add an object to the container...
const ObjectContainerBase * parent() const
Access to parent object.
const key_type & insert(const value_type val, const key_type &kval)
Insert entry to the container with a valid key.
virtual ~KeyedContainer()
Destructor.
GAUDI_API void cannotInsertToContainer()
Function to be called to indicate that an object cannot be inserted to the container.
virtual const CLID & clID() const
Retrieve reference to class definition structure.
This class is used for returning status codes from appropriate routines.
unsigned int CLID
Class ID definition.
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
virtual long index(const ContainedObject *p) const
ObjectContainerBase overload: Retrieve the full long integer representation of the object's key from ...
Object was removed from the container and deleted.
Object was inserted into the container.
virtual long index(const ContainedObject *obj) const =0
Distance of a given object from the beginning of its container.
contained_type::key_type key_type
Definition of the key type: re-use definition of contained type.
virtual long remove(ContainedObject *pObject)
ObjectContainerBase overload: Remove an object from the container.
ObjectContainerBase is the base class for Gaudi container classes.
GAUDI_API void invalidContainerOperation()
Function to be called to indicate that an operation should be performed on the container or it's cont...
Object not present in the container.
TO * reference(FROM *from)