Revision: | 1.9 |
Committed: | Mon Mar 23 13:07:18 2009 UTC (16 years, 1 month ago) by loizides |
Content type: | text/plain |
Branch: | MAIN |
CVS Tags: | Mit_032, Mit_031, Mit_025c_branch2, Mit_025c_branch1, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_025c_branch0, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e, Mit_014d, Mit_014c, Mit_014b, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012h, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, Mit_009, Mit_008, HEAD |
Branch point for: | Mit_025c_branch |
Changes since 1.8: | +5 -3 lines |
Log Message: | Reset entries cache when objects are added |
# | User | Rev | Content |
---|---|---|---|
1 | loizides | 1.1 | //-------------------------------------------------------------------------------------------------- |
2 | loizides | 1.9 | // $Id: Vector.h,v 1.8 2009/03/12 18:19:48 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 | |||
32 | loizides | 1.9 | void AddCopy(const ArrayElement &ae) |
33 | { BaseCollection::Clear(); fV.push_back(ae); } | ||
34 | void AddCopy(const ArrayElement *ae) | ||
35 | { BaseCollection::Clear(); fV.push_back(*ae); } | ||
36 | loizides | 1.1 | ArrayElement *At(UInt_t idx) { return &fV.at(idx); } |
37 | const ArrayElement *At(UInt_t idx) const { return &fV.at(idx); } | ||
38 | void Clear(Option_t *opt=""); | ||
39 | UInt_t Entries() const { return fV.size(); } | ||
40 | UInt_t GetEntries() const { return fV.size(); } | ||
41 | loizides | 1.3 | UInt_t GetSize() const { return fV.capacity(); } |
42 | loizides | 1.5 | Bool_t HasObject(const ArrayElement *obj) const; |
43 | loizides | 1.1 | Bool_t IsOwner() const { return kTRUE; } |
44 | bendavid | 1.7 | TObject *ObjAt(UInt_t idx) { return &fV.at(idx); } |
45 | const TObject *ObjAt(UInt_t idx) const { return &fV.at(idx); } | ||
46 | loizides | 1.1 | ArrayElement &Ref(UInt_t idx) { return fV.at(idx); } |
47 | const ArrayElement &Ref(UInt_t idx) const { return fV.at(idx); } | ||
48 | loizides | 1.8 | void Reset(); |
49 | loizides | 1.1 | void Trim(); |
50 | ArrayElement *UncheckedAt(UInt_t idx) { return &fV[idx]; } | ||
51 | const ArrayElement *UncheckedAt(UInt_t idx) const { return &fV[idx]; } | ||
52 | const std::vector<ArrayElement> &Vect() const { return fV; } | ||
53 | std::vector<ArrayElement> &Vect() { return fV; } | ||
54 | ArrayElement *operator[](UInt_t idx) { return &fV.at(idx); } | ||
55 | const ArrayElement *operator[](UInt_t idx) const { return &fV.at(idx); } | ||
56 | |||
57 | protected: | ||
58 | std::vector<ArrayElement> fV; //std::vector | ||
59 | |||
60 | private: | ||
61 | Vector(const Vector &v); | ||
62 | |||
63 | loizides | 1.6 | ClassDef(Vector, 1) // Wrapper around std::vector class |
64 | loizides | 1.1 | }; |
65 | } | ||
66 | |||
67 | //-------------------------------------------------------------------------------------------------- | ||
68 | template<class ArrayElement> | ||
69 | inline void mithep::Vector<ArrayElement>::Clear(Option_t */*opt*/) | ||
70 | { | ||
71 | // Destruct container. | ||
72 | |||
73 | using namespace std; | ||
74 | fV.~vector<ArrayElement>(); | ||
75 | } | ||
76 | |||
77 | //-------------------------------------------------------------------------------------------------- | ||
78 | template<class ArrayElement> | ||
79 | bendavid | 1.4 | inline Bool_t mithep::Vector<ArrayElement>::HasObject(const ArrayElement *obj) const |
80 | { | ||
81 | bendavid | 1.7 | // Check whether object is in array, using the isEqual function for the comparison. |
82 | bendavid | 1.4 | |
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 | bendavid | 1.7 | if ( fV.at(i).IsEqual(obj) ) |
89 | loizides | 1.5 | return kTRUE; |
90 | bendavid | 1.4 | } |
91 | |||
92 | loizides | 1.5 | return kFALSE; |
93 | bendavid | 1.4 | } |
94 | |||
95 | loizides | 1.8 | |
96 | //-------------------------------------------------------------------------------------------------- | ||
97 | template<class ArrayElement> | ||
98 | inline void mithep::Vector<ArrayElement>::Reset() | ||
99 | { | ||
100 | // Reset container. | ||
101 | |||
102 | fV.clear(); | ||
103 | BaseCollection::Clear(); | ||
104 | } | ||
105 | |||
106 | bendavid | 1.4 | //-------------------------------------------------------------------------------------------------- |
107 | template<class ArrayElement> | ||
108 | loizides | 1.1 | inline void mithep::Vector<ArrayElement>::Trim() |
109 | { | ||
110 | // Trim vector to minimal needed size. | ||
111 | |||
112 | std::vector<ArrayElement>(fV).swap(fV); | ||
113 | } | ||
114 | #endif |