ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Vector.h
Revision: 1.5
Committed: Wed Dec 10 11:26:52 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006b, Mit_006a
Changes since 1.4: +10 -9 lines
Log Message:
Implemented ObjAt function.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.5 // $Id: Vector.h,v 1.4 2008/12/01 17:17:20 bendavid 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 loizides 1.5 Bool_t HasObject(const ArrayElement *obj) const;
42 loizides 1.1 Bool_t IsOwner() const { return kTRUE; }
43 loizides 1.5 TObject *ObjAt(UInt_t idx) { return 0; } //TODO_008
44     const TObject *ObjAt(UInt_t idx) const { return 0; } //TODO_008
45 loizides 1.1 ArrayElement &Ref(UInt_t idx) { return fV.at(idx); }
46     const ArrayElement &Ref(UInt_t idx) const { return fV.at(idx); }
47     void Reset() { fV.clear(); }
48     void Trim();
49     ArrayElement *UncheckedAt(UInt_t idx) { return &fV[idx]; }
50     const ArrayElement *UncheckedAt(UInt_t idx) const { return &fV[idx]; }
51     const std::vector<ArrayElement> &Vect() const { return fV; }
52     std::vector<ArrayElement> &Vect() { return fV; }
53     ArrayElement *operator[](UInt_t idx) { return &fV.at(idx); }
54     const ArrayElement *operator[](UInt_t idx) const { return &fV.at(idx); }
55    
56     protected:
57     std::vector<ArrayElement> fV; //std::vector
58    
59     private:
60     Vector(const Vector &v);
61    
62     ClassDefT(Vector, 1) // Wrapper around std::vector class
63     };
64     }
65    
66     //--------------------------------------------------------------------------------------------------
67     template<class ArrayElement>
68     inline void mithep::Vector<ArrayElement>::Clear(Option_t */*opt*/)
69     {
70     // Destruct container.
71    
72     using namespace std;
73     fV.~vector<ArrayElement>();
74     }
75    
76     //--------------------------------------------------------------------------------------------------
77     template<class ArrayElement>
78 bendavid 1.4 inline Bool_t mithep::Vector<ArrayElement>::HasObject(const ArrayElement *obj) const
79     {
80 loizides 1.5 // Check whether object is in array. If ArrayElement inherits from TObject, use the
81 bendavid 1.4 // isEqual function for the comparison, otherwise use the default pointer comparison.
82    
83     const TObject *tobj = 0;
84     if (TClass::GetClass(typeid(ArrayElement))->IsTObject())
85     tobj = reinterpret_cast<const TObject*>(obj);
86    
87     for (UInt_t i=0; i<fV.size(); ++i) {
88     if (tobj)
89 loizides 1.5 if (reinterpret_cast<const TObject*>(&fV.at(i))->IsEqual(tobj))
90     return kTRUE;
91     else if (&fV.at(i) == obj)
92     return kTRUE;
93 bendavid 1.4 }
94    
95 loizides 1.5 return kFALSE;
96 bendavid 1.4 }
97    
98     //--------------------------------------------------------------------------------------------------
99     template<class ArrayElement>
100 loizides 1.1 inline void mithep::Vector<ArrayElement>::Trim()
101     {
102     // Trim vector to minimal needed size.
103    
104     std::vector<ArrayElement>(fV).swap(fV);
105     }
106     #endif