ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Vector.h
Revision: 1.4
Committed: Mon Dec 1 17:17:20 2008 UTC (16 years, 5 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.3: +27 -1 lines
Log Message:
Add HasObject to Collection interface

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.4 // $Id: Vector.h,v 1.3 2008/11/20 17:49:15 loizides Exp $
3 loizides 1.1 //
4     // Vector
5     //
6     // Implementation of Collection interface using std:vector class.
7     //
8     // Note, in case you one to store pointers to allocated objects on heap,
9     // you should use the class PVector.
10     //
11     // Authors: C.Loizides
12     //--------------------------------------------------------------------------------------------------
13    
14 loizides 1.2 #ifndef MITANA_DATACONT_VECTOR_H
15     #define MITANA_DATACONT_VECTOR_H
16 loizides 1.1
17     #include <vector>
18     #include <Rtypes.h>
19 bendavid 1.4 #include <TClass.h>
20     #include <TObject.h>
21 loizides 1.1 #include "MitAna/DataCont/interface/Collection.h"
22    
23     namespace mithep
24     {
25     template<class ArrayElement>
26     class Vector : public Collection<ArrayElement>
27     {
28     public:
29     Vector(UInt_t rsv=0) : fV(0) { fV.reserve(rsv); }
30     Vector(std::vector<ArrayElement> &v) : fV(v) {}
31     ~Vector() {}
32    
33     void AddCopy(const ArrayElement &ae) { fV.push_back(ae); }
34     void AddCopy(const ArrayElement *ae) { fV.push_back(*ae); }
35     ArrayElement *At(UInt_t idx) { return &fV.at(idx); }
36     const ArrayElement *At(UInt_t idx) const { return &fV.at(idx); }
37     void Clear(Option_t *opt="");
38     UInt_t Entries() const { return fV.size(); }
39     UInt_t GetEntries() const { return fV.size(); }
40 loizides 1.3 UInt_t GetSize() const { return fV.capacity(); }
41 bendavid 1.4 Bool_t HasObject(const ArrayElement *obj) const;// { return false; }
42 loizides 1.1 Bool_t IsOwner() const { return kTRUE; }
43     ArrayElement &Ref(UInt_t idx) { return fV.at(idx); }
44     const ArrayElement &Ref(UInt_t idx) const { return fV.at(idx); }
45     void Reset() { fV.clear(); }
46     void Trim();
47     ArrayElement *UncheckedAt(UInt_t idx) { return &fV[idx]; }
48     const ArrayElement *UncheckedAt(UInt_t idx) const { return &fV[idx]; }
49     const std::vector<ArrayElement> &Vect() const { return fV; }
50     std::vector<ArrayElement> &Vect() { return fV; }
51     ArrayElement *operator[](UInt_t idx) { return &fV.at(idx); }
52     const ArrayElement *operator[](UInt_t idx) const { return &fV.at(idx); }
53    
54     protected:
55     std::vector<ArrayElement> fV; //std::vector
56    
57     private:
58     Vector(const Vector &v);
59    
60     ClassDefT(Vector, 1) // Wrapper around std::vector class
61     };
62     }
63    
64     //--------------------------------------------------------------------------------------------------
65     template<class ArrayElement>
66     inline void mithep::Vector<ArrayElement>::Clear(Option_t */*opt*/)
67     {
68     // Destruct container.
69    
70     using namespace std;
71     fV.~vector<ArrayElement>();
72     }
73    
74     //--------------------------------------------------------------------------------------------------
75     template<class ArrayElement>
76 bendavid 1.4 inline Bool_t mithep::Vector<ArrayElement>::HasObject(const ArrayElement *obj) const
77     {
78     // Check whether object is in array. If ArrayElement inherits from TObject, use the
79     // isEqual function for the comparison, otherwise use the default pointer comparison.
80    
81     const TObject *tobj = 0;
82     if (TClass::GetClass(typeid(ArrayElement))->IsTObject())
83     tobj = reinterpret_cast<const TObject*>(obj);
84    
85     for (UInt_t i=0; i<fV.size(); ++i) {
86     if (tobj)
87     if ( reinterpret_cast<const TObject*>(&fV.at(i))->IsEqual(tobj) )
88     return true;
89     else if ( &fV.at(i) == obj )
90     return true;
91     }
92    
93     return false;
94     }
95    
96    
97     //--------------------------------------------------------------------------------------------------
98     template<class ArrayElement>
99 loizides 1.1 inline void mithep::Vector<ArrayElement>::Trim()
100     {
101     // Trim vector to minimal needed size.
102    
103     std::vector<ArrayElement>(fV).swap(fV);
104     }
105     #endif