]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/AliHLTMUONCountedList.h
Debug msg
[u/mrichter/AliRoot.git] / HLT / MUON / AliHLTMUONCountedList.h
1 #ifndef ALIHLTMUONCOUNTEDLIST_H
2 #define ALIHLTMUONCOUNTEDLIST_H
3 /**************************************************************************
4  * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Author: The ALICE Off-line Project.                                    *
7  * Contributors are mentioned in the code where appropriate.              *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          *
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18 // $Id$
19
20 /**
21  * @file   AliHLTMUONCountedList.h
22  * @author Artur Szostak <artursz@iafrica.com>
23  * @date   
24  * @brief  Declaration of a linked-list class which counts the number of
25  *         elements it contains.
26  */
27
28 #include "AliHLTMUONList.h"
29
30 /**
31  * The AliHLTMUONCountedList class behaves just like the AliHLTMUONList class
32  * but uses an internal counter to count the number of elements in the list.
33  * This means calls to Count() are much more efficient.
34  */
35 template <typename DataType>
36 class AliHLTMUONCountedList : public AliHLTMUONList<DataType>
37 {
38 public:
39
40         typedef typename AliHLTMUONList<DataType>::Iterator Iterator;
41         typedef typename AliHLTMUONList<DataType>::ConstIterator ConstIterator;
42         
43         AliHLTMUONCountedList(AliHLTUInt32_t maxentries = 1024*4) :
44                 AliHLTMUONList<DataType>(maxentries), fCount(0)
45         {}
46
47         // Perform a deep copy. 
48         AliHLTMUONCountedList(const AliHLTMUONCountedList& list)
49                 : AliHLTMUONList<DataType>(list), fCount(list.fCount)
50         {}
51
52         // Perform a deep copy.
53         AliHLTMUONCountedList& operator = (const AliHLTMUONCountedList& list)
54         {
55                 AliHLTMUONList<DataType>::operator = (list);
56                 fCount = list.fCount;
57                 return *this;
58         }
59         
60         virtual ~AliHLTMUONCountedList() {} // Just to make gcc -Weffc++ option shutup.
61         
62         /**
63          * Adds a new element to the start of the linked list.
64          * @return  The pointer to the new element to fill its fields.
65          */
66         DataType* Add()
67         {
68                 DataType* newdata = AliHLTMUONList<DataType>::Add();
69                 fCount++;
70                 return newdata;
71         }
72
73         /**
74          * Adds a new element to the start of the linked list and fills it with
75          * the data specified in 'data'.
76          * @param data  The value to set the new element to.
77          */
78         void Add(const DataType& data)
79         {
80                 AliHLTMUONList<DataType>::Add(data);
81                 fCount++;
82         }
83
84         /**
85          * Searches the list if the element 'data' is already in the list. If it
86          * is then a pointer to the existing element is returned, otherwise a new
87          * element is created and a pointer to it is returned.
88          * @param data  The value to search for or set the new element to.
89          * @return  A pointer to the existing or new element.
90          */
91         DataType* AddUniquely(const DataType& data)
92         {
93                 Iterator result = Find(data);
94                 if (result == ConstIterator(NULL))
95                 {
96                         DataType* newdata = Add();
97                         *newdata = data;
98                         return newdata;
99                 }
100                 else
101                         return result;
102         }
103
104         /**
105          * Removes the index'th element from the list.
106          * No error checking is done so there better be at least 'index' number
107          * of entries in the list. You can use Count() to find out how many
108          * entries there are.
109          */
110         void Remove(const AliHLTUInt32_t index)
111         {
112                 AliHLTMUONList<DataType>::Remove(index);
113                 fCount--;
114         }
115         
116         /**
117          * Looks for the entry with the same values as 'data' and removes it
118          * from the list. If the entry could not be found then false is returned.
119          * However if it is found then it is deleted and true is returned.
120          */
121         bool Remove(const DataType& data)
122         {
123                 Iterator current = Find(data);
124                 if ( current != ConstIterator(NULL) )
125                 {
126                         Remove(current);
127                         return true;
128                 }
129                 else
130                         return false;
131         }
132         
133         /**
134          * Removes the entry pointed to by the iterator which must have been
135          * extracted from the list with a call to First() and/or several calls
136          * to the iterators increment operators.
137          * @param iter  The entry to remove from the list.
138          */
139         void Remove(Iterator& iter)
140         {
141                 AliHLTMUONList<DataType>::Remove(iter);
142                 fCount--;
143         }
144
145         /**
146          * This deletes all elements from the list.
147          */
148         void Clear()
149         {
150                 AliHLTMUONList<DataType>::Clear();
151                 fCount = 0;
152         }
153         
154         /**
155          * This deletes all elements from the list and resizes the buffer which
156          * is used to store the entries for the list.
157          */
158         void Clear(AliHLTUInt32_t maxentries) throw(std::bad_alloc)
159         {
160                 AliHLTMUONList<DataType>::Clear(maxentries);
161                 fCount = 0;
162         }
163         
164         /**
165          * Counts and returns the number of elements in the list.
166          */
167         AliHLTUInt32_t Count() const { return fCount; }
168
169 protected:
170
171         AliHLTUInt32_t fCount;
172 };
173
174
175 #endif // ALIHLTMUONCOUNTEDLIST_H