ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Vector.h
Revision: 1.8
Committed: Thu Mar 12 18:19:48 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre2
Changes since 1.7: +13 -2 lines
Log Message:
Proper resetting of the cached entry in BaseCollection.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.8 // $Id: Vector.h,v 1.7 2009/03/03 18:01:17 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    
32     void AddCopy(const ArrayElement &ae) { fV.push_back(ae); }
33     void AddCopy(const ArrayElement *ae) { fV.push_back(*ae); }
34     ArrayElement *At(UInt_t idx) { return &fV.at(idx); }
35     const ArrayElement *At(UInt_t idx) const { return &fV.at(idx); }
36     void Clear(Option_t *opt="");
37     UInt_t Entries() const { return fV.size(); }
38     UInt_t GetEntries() const { return fV.size(); }
39 loizides 1.3 UInt_t GetSize() const { return fV.capacity(); }
40 loizides 1.5 Bool_t HasObject(const ArrayElement *obj) const;
41 loizides 1.1 Bool_t IsOwner() const { return kTRUE; }
42 bendavid 1.7 TObject *ObjAt(UInt_t idx) { return &fV.at(idx); }
43     const TObject *ObjAt(UInt_t idx) const { return &fV.at(idx); }
44 loizides 1.1 ArrayElement &Ref(UInt_t idx) { return fV.at(idx); }
45     const ArrayElement &Ref(UInt_t idx) const { return fV.at(idx); }
46 loizides 1.8 void Reset();
47 loizides 1.1 void Trim();
48     ArrayElement *UncheckedAt(UInt_t idx) { return &fV[idx]; }
49     const ArrayElement *UncheckedAt(UInt_t idx) const { return &fV[idx]; }
50     const std::vector<ArrayElement> &Vect() const { return fV; }
51     std::vector<ArrayElement> &Vect() { return fV; }
52     ArrayElement *operator[](UInt_t idx) { return &fV.at(idx); }
53     const ArrayElement *operator[](UInt_t idx) const { return &fV.at(idx); }
54    
55     protected:
56     std::vector<ArrayElement> fV; //std::vector
57    
58     private:
59     Vector(const Vector &v);
60    
61 loizides 1.6 ClassDef(Vector, 1) // Wrapper around std::vector class
62 loizides 1.1 };
63     }
64    
65     //--------------------------------------------------------------------------------------------------
66     template<class ArrayElement>
67     inline void mithep::Vector<ArrayElement>::Clear(Option_t */*opt*/)
68     {
69     // Destruct container.
70    
71     using namespace std;
72     fV.~vector<ArrayElement>();
73     }
74    
75     //--------------------------------------------------------------------------------------------------
76     template<class ArrayElement>
77 bendavid 1.4 inline Bool_t mithep::Vector<ArrayElement>::HasObject(const ArrayElement *obj) const
78     {
79 bendavid 1.7 // Check whether object is in array, using the isEqual function for the comparison.
80 bendavid 1.4
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 bendavid 1.7 if ( fV.at(i).IsEqual(obj) )
87 loizides 1.5 return kTRUE;
88 bendavid 1.4 }
89    
90 loizides 1.5 return kFALSE;
91 bendavid 1.4 }
92    
93 loizides 1.8
94     //--------------------------------------------------------------------------------------------------
95     template<class ArrayElement>
96     inline void mithep::Vector<ArrayElement>::Reset()
97     {
98     // Reset container.
99    
100     fV.clear();
101     BaseCollection::Clear();
102     }
103    
104 bendavid 1.4 //--------------------------------------------------------------------------------------------------
105     template<class ArrayElement>
106 loizides 1.1 inline void mithep::Vector<ArrayElement>::Trim()
107     {
108     // Trim vector to minimal needed size.
109    
110     std::vector<ArrayElement>(fV).swap(fV);
111     }
112     #endif