1 #ifndef ALIHLTMUONCOUNTEDLIST_H
2 #define ALIHLTMUONCOUNTEDLIST_H
3 /**************************************************************************
4 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
6 * Author: The ALICE Off-line Project. *
7 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
21 * @file AliHLTMUONCountedList.h
22 * @author Artur Szostak <artursz@iafrica.com>
24 * @brief Declaration of a linked-list class which counts the number of
25 * elements it contains.
28 #include "AliHLTMUONList.h"
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.
35 template <typename DataType>
36 class AliHLTMUONCountedList : public AliHLTMUONList<DataType>
40 typedef typename AliHLTMUONList<DataType>::Iterator Iterator;
41 typedef typename AliHLTMUONList<DataType>::ConstIterator ConstIterator;
43 AliHLTMUONCountedList(AliHLTUInt32_t maxentries = 1024*4) :
44 AliHLTMUONList<DataType>(maxentries), fCount(0)
47 // Perform a deep copy.
48 AliHLTMUONCountedList(const AliHLTMUONCountedList& list)
49 : AliHLTMUONList<DataType>(list), fCount(list.fCount)
52 // Perform a deep copy.
53 AliHLTMUONCountedList& operator = (const AliHLTMUONCountedList& list)
55 AliHLTMUONList<DataType>::operator = (list);
60 virtual ~AliHLTMUONCountedList() {} // Just to make gcc -Weffc++ option shutup.
63 * Adds a new element to the start of the linked list.
64 * @return The pointer to the new element to fill its fields.
68 DataType* newdata = AliHLTMUONList<DataType>::Add();
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.
78 void Add(const DataType& data)
80 AliHLTMUONList<DataType>::Add(data);
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.
91 DataType* AddUniquely(const DataType& data)
93 Iterator result = Find(data);
94 if (result == ConstIterator(NULL))
96 DataType* newdata = Add();
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
110 void Remove(const AliHLTUInt32_t index)
112 AliHLTMUONList<DataType>::Remove(index);
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.
121 bool Remove(const DataType& data)
123 Iterator current = Find(data);
124 if ( current != ConstIterator(NULL) )
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.
139 void Remove(Iterator& iter)
141 AliHLTMUONList<DataType>::Remove(iter);
146 * This deletes all elements from the list.
150 AliHLTMUONList<DataType>::Clear();
155 * This deletes all elements from the list and resizes the buffer which
156 * is used to store the entries for the list.
158 void Clear(AliHLTUInt32_t maxentries) throw(std::bad_alloc)
160 AliHLTMUONList<DataType>::Clear(maxentries);
165 * Counts and returns the number of elements in the list.
167 AliHLTUInt32_t Count() const { return fCount; }
171 AliHLTUInt32_t fCount;
175 #endif // ALIHLTMUONCOUNTEDLIST_H