1 #ifndef ALIHLTMUONLIST_H
2 #define ALIHLTMUONLIST_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 AliHLTMUONList.h
22 * @author Artur Szostak <artursz@iafrica.com>
24 * @brief Declaration of a singly linked-list class which preallocates memory to give faster online performance.
27 #include "AliHLTDataTypes.h"
34 * The AliHLTMUONList class is a unidirectional linked-list which preallocates
35 * a large memory buffer where it stores its elements. This strategy gives higher
38 template <typename DataType>
43 struct Node; // forward declaration for ConstIterator and Iterator
48 * This is an iterator class which allows one to iterate through the list
49 * using the prefix or postfix operators ++.
55 ConstIterator() : fCurrent(NULL) {}
57 ConstIterator(const ConstIterator& iter) : fCurrent(iter.fCurrent) {}
59 ConstIterator(Node* node) : fCurrent(node) {}
61 ConstIterator& operator = (const ConstIterator& iter)
63 fCurrent = iter.fCurrent;
67 virtual ~ConstIterator() {} // Just to make gcc -Weffc++ option shutup.
69 // Dereference operator returns the elements data.
70 const DataType& operator * () const { return fCurrent->fData; }
72 // Pointer dereferencing returns the elements data.
73 const DataType* operator -> () const { return &fCurrent->fData; }
75 // Move to the next element in the list.
76 ConstIterator& operator ++ ()
78 assert( fCurrent != NULL );
79 fCurrent = fCurrent->fNext;
83 // Move to the next element in the list.
84 ConstIterator operator ++ (int)
86 assert( fCurrent != NULL );
87 ConstIterator copy = *this;
88 fCurrent = fCurrent->fNext;
92 // Typecast operator returns a pointer to the data.
93 operator const DataType* () const { return &fCurrent->fData; }
95 friend bool operator == (const ConstIterator& a, const ConstIterator& b)
97 return a.fCurrent == b.fCurrent;
100 friend bool operator != (const ConstIterator& a, const ConstIterator& b)
102 return a.fCurrent != b.fCurrent;
107 friend class AliHLTMUONList;
108 Node* fCurrent; // The pointer to the current element selected by the iterator.
112 * This is an iterator class which allows one to iterate through the list
113 * just like ConstIterator, but also allows modification of the elements.
115 class Iterator : public ConstIterator
119 Iterator() : ConstIterator(), fPrevious(NULL) {}
121 Iterator(const Iterator& iter) : ConstIterator(iter), fPrevious(iter.fPrevious) {}
123 Iterator(Node* current, Node* prev) : ConstIterator(current), fPrevious(prev) {}
125 Iterator& operator = (const Iterator& iter)
127 ConstIterator::operator = (iter);
128 fPrevious = iter.fPrevious;
132 virtual ~Iterator() {} // Just to make gcc -Weffc++ option shutup.
134 // Dereference operator returns the elements data.
135 DataType& operator * () { return ConstIterator::fCurrent->fData; }
137 // Pointer dereferencing returns the elements data.
138 DataType* operator -> () { return &ConstIterator::fCurrent->fData; }
140 // Move to the next element in the list.
141 Iterator& operator ++ ()
143 assert( ConstIterator::fCurrent != NULL );
144 fPrevious = ConstIterator::fCurrent;
145 ConstIterator::fCurrent = ConstIterator::fCurrent->fNext;
149 // Move to the next element in the list.
150 Iterator operator ++ (int)
152 assert( ConstIterator::fCurrent != NULL );
153 Iterator copy = *this;
154 fPrevious = ConstIterator::fCurrent;
155 ConstIterator::fCurrent = ConstIterator::fCurrent->fNext;
159 // Typecast operator returns a pointer to the data.
160 operator DataType* ()
162 if (ConstIterator::fCurrent != NULL)
163 return &ConstIterator::fCurrent->fData;
170 friend class AliHLTMUONList;
171 Node* fPrevious; // The pointer to the previous element.
175 * Constructor allocates a buffer to hold at least 'minentries' number
176 * of nodes for the list.
178 AliHLTMUONList(AliHLTUInt32_t maxentries = 1024*4) :
179 fFirst(NULL), fNextFree(0), fMaxEntries(maxentries), fEntry(NULL)
181 // Allocate buffer space.
182 fEntry = reinterpret_cast<NodeEntry*>(
183 malloc( sizeof(NodeEntry) * fMaxEntries )
185 if (fEntry == NULL) throw std::bad_alloc();
186 // Set the availability flags.
187 for (AliHLTUInt32_t i = 0; i < fMaxEntries; i++)
188 fEntry[i].fFree = true;
192 * Deep copy the list.
194 AliHLTMUONList(const AliHLTMUONList& list) :
195 fFirst(NULL), fNextFree(0), fMaxEntries(list.fMaxEntries),
198 if (list.fFirst != NULL)
200 // First allocate the buffer space.
201 fEntry = reinterpret_cast<NodeEntry*>(
202 malloc( sizeof(NodeEntry) * fMaxEntries )
204 if (fEntry == NULL) throw std::bad_alloc();
205 // Set the availability flags.
206 for (AliHLTUInt32_t i = 0; i < fMaxEntries; i++)
207 fEntry[i].fFree = true;
209 // Now copy the list by adding new node entries.
210 Add(list.fFirst->fData);
211 Node* current = fFirst;
212 Node* listcurrent = list.fFirst->fNext;
213 while (listcurrent != NULL)
215 current->fNext = NewNode(listcurrent->fData);
216 current = current->fNext;
217 listcurrent = listcurrent->fNext;
223 * Delete the list and release all allocated memory.
225 virtual ~AliHLTMUONList()
227 Node* current = fFirst;
228 while (current != NULL)
230 Node* temp = current;
231 current = current->fNext;
234 // Delete the buffer space after all nodes are deleted,
235 // else we will cause a crash.
236 assert(fEntry != NULL);
241 * Deep copy the list.
243 AliHLTMUONList& operator = (const AliHLTMUONList& list)
245 if (list.fFirst == NULL)
252 // The list in 'this' object is empty so we need to create
253 // new nodes for everything.
254 Add(list.fFirst->fData);
255 Node* current = fFirst;
256 Node* listcurrent = list.fFirst->fNext;
257 while (listcurrent != NULL)
259 current->fNext = NewNode(listcurrent->fData);
260 current = current->fNext;
261 listcurrent = listcurrent->fNext;
266 // At least the first node does not need to be created.
267 fFirst->fData = list.fFirst->fData;
269 Node* current = fFirst;
270 Node* listcurrent = list.fFirst->fNext;
271 while (listcurrent != NULL)
273 if (current->fNext == NULL)
275 // The list of 'this' object is shorter so we need
276 // to create new nodes.
279 current->fNext = NewNode(listcurrent->fData);
280 current = current->fNext;
281 listcurrent = listcurrent->fNext;
283 while (listcurrent != NULL);
286 current->fNext->fData = listcurrent->fData;
287 current = current->fNext;
288 listcurrent = listcurrent->fNext;
291 // Unlink the remaining nodes.
292 Node* temp = current->fNext;
293 current->fNext = NULL;
295 // Remove any excess nodes from 'this' list.
296 while (current != NULL)
299 current = current->fNext;
307 * Returns true if the list is empty and false otherwise.
309 bool Empty() const { return fFirst == NULL; }
312 * Adds a new element to the start of the linked list.
313 * @return The pointer to the new element to fill its fields.
317 Node* newnode = NewNode();
318 newnode->fNext = fFirst;
320 return &newnode->fData;
324 * Adds a new element to the start of the linked list and fills it with
325 * the data specified in 'data'.
326 * @param data The value to set the new element to.
328 void Add(const DataType& data)
330 Node* newnode = NewNode(data);
331 newnode->fNext = fFirst;
336 * Searches the list if the element 'data' is already in the list. If it
337 * is then a pointer to the existing element is returned, otherwise a new
338 * element is created and a pointer to it is returned.
339 * @param data The value to search for or set the new element to.
340 * @return A pointer to the existing or new element.
342 DataType* AddUniquely(const DataType& data)
344 Iterator result = Find(data);
345 if (result == ConstIterator(NULL))
347 DataType* newdata = Add();
356 * Removes the index'th element from the list.
357 * No error checking is done so there better be at least 'index' number
358 * of entries in the list. You can use Count() to find out how many
361 void Remove(const AliHLTUInt32_t index)
363 Node* previous = NULL;
364 Node* current = fFirst;
365 for (AliHLTUInt32_t i = 0; i < index; i++)
367 assert( current != NULL );
369 current = current->fNext;
372 if (previous == NULL)
375 fFirst = fFirst->fNext;
380 previous->fNext = current->fNext;
386 * Looks for the entry with the same values as 'data' and removes it
387 * from the list. If the entry could not be found then false is returned.
388 * However if it is found then it is deleted and true is returned.
390 bool Remove(const DataType& data)
392 Iterator current = Find(data);
393 if (current != ConstIterator(NULL))
403 * Removes the entry pointed to by the iterator which must have been
404 * extracted from the list with a call to First() and/or several calls
405 * to the iterators increment operators.
406 * @param iter The entry to remove from the list.
408 void Remove(Iterator& iter)
410 Node* previous = iter.fPrevious;
411 Node* current = iter.fCurrent;
412 assert( current != NULL );
415 if (previous == NULL)
418 fFirst = fFirst->fNext;
423 previous->fNext = current->fNext;
429 * Finds the first element with the same value as 'data' and returns an
430 * iterator to it. The iterator points to the end of the list i.e. End()
431 * if nothing was found.
432 * @param data The value to look for.
433 * @return The iterator pointing to the found element or End().
435 Iterator Find(const DataType& data)
437 Node* previous = NULL;
438 Node* current = fFirst;
439 while (current != NULL)
441 if (current->fData == data)
442 return Iterator(current, previous);
444 current = current->fNext;
449 ConstIterator Find(const DataType& data) const
451 Node* current = fFirst;
452 while (current != NULL)
454 if (current->fData == data)
455 return ConstIterator(current);
456 current = current->fNext;
462 * Finds the first element in the list that returns true for the predicate.
463 * That is, the first element for which the data evaluates to true in the
464 * test: predicate(fData). The iterator points to the end of the list
465 * i.e. End() if nothing was found.
466 * @param predicate Predicate that the data must return true for.
467 * This can usually be some one variable function.
468 * @return The iterator pointing to the found element or End().
470 template <typename PredicateType>
471 Iterator Find(PredicateType predicate)
473 Node* previous = NULL;
474 Node* current = fFirst;
475 while (current != NULL)
477 if ( predicate(current->fData) )
478 return Iterator(current, previous);
480 current = current->fNext;
485 template <typename PredicateType>
486 ConstIterator Find(PredicateType predicate) const
488 Node* current = fFirst;
489 while (current != NULL)
491 if ( predicate(current->fData) )
492 return ConstIterator(current);
493 current = current->fNext;
499 * Returns true if the list contains the specified element, else false.
500 * @param data The value to search for in the list.
502 bool Contains(const DataType& data) const { return Find(data) != End(); }
505 * This deletes all elements from the list.
509 Node* current = fFirst;
510 while (current != NULL)
512 Node* temp = current;
513 current = current->fNext;
520 * This deletes all elements from the list and resizes the buffer which
521 * is used to store the entries for the list.
523 void Clear(AliHLTUInt32_t maxentries) throw(std::bad_alloc)
527 NodeEntry* newp = reinterpret_cast<NodeEntry*>(
528 realloc(fEntry, sizeof(NodeEntry) * fMaxEntries)
530 if (newp == NULL) throw std::bad_alloc();
532 // Set the availability flags for the node entries
533 for (AliHLTUInt32_t i = fMaxEntries; i < maxentries; i++)
534 fEntry[i].fFree = true;
537 fMaxEntries = maxentries;
542 * Returns an iterator to the first element of the list.
544 Iterator First() { return Iterator(fFirst, NULL); }
545 ConstIterator First() const { return fFirst; }
548 * Returns an iterator pointing to the end of the list. The returned
549 * iterator does not actually point to a real data value but rather a
550 * sentinel value, and so it should not be dereferenced directly.
552 Iterator End() { return Iterator(NULL, NULL); }
553 ConstIterator End() const { return ConstIterator(NULL); }
556 * Counts and returns the number of elements in the list.
558 AliHLTUInt32_t Count() const
560 AliHLTUInt32_t n = 0;
561 Node* current = fFirst;
562 while (current != NULL)
565 current = current->fNext;
573 * The internal node structure for the linked list.
577 Node* fNext; // Next element in the list.
578 DataType fData; // The data for the current link.
580 Node() : fNext(NULL), fData() {};
581 Node(const DataType& data) : fNext(NULL), fData(data) {};
582 Node(const Node& node) : fNext(node.fNext), fData(node.data) {};
584 // Shallow copy the node.
585 Node& operator = (const Node& node)
593 Node* fFirst; // The first element in the linked list.
597 bool fFree; // Indicates if this block is free.
598 Node fNode; // The node structure.
601 AliHLTUInt32_t fNextFree; // The next entry that is presumably free.
602 AliHLTUInt32_t fMaxEntries; // The number of node entries that can be stored in fEntries.
603 NodeEntry* fEntry; // Buffer of preallocated node entries.
606 * Locates the next free location in the fEntry buffer, creates a new node
607 * at that location and returns the pointer.
609 Node* NewNode() throw(std::bad_alloc)
612 assert( fNextFree < fMaxEntries );
613 AliHLTUInt32_t i = fNextFree;
614 while (not fEntry[i].fFree and i < fMaxEntries) i++;
617 fEntry[i].fFree = false;
618 fNextFree = (i+1) % fMaxEntries;
619 return new (&fEntry[i].fNode) Node();
622 while (not fEntry[i].fFree and i < fNextFree) i++;
625 fEntry[i].fFree = false;
626 fNextFree = (i+1) % fMaxEntries;
627 return new (&fEntry[i].fNode) Node();
629 throw std::bad_alloc();
632 Node* NewNode(const DataType& data) throw(std::bad_alloc)
634 //return new Node(data);
635 assert( fNextFree < fMaxEntries );
636 AliHLTUInt32_t i = fNextFree;
637 while (not fEntry[i].fFree and i < fMaxEntries) i++;
640 fEntry[i].fFree = false;
641 fNextFree = (i+1) % fMaxEntries;
642 return new (&fEntry[i].fNode) Node(data);
645 while (not fEntry[i].fFree and i < fNextFree) i++;
648 fEntry[i].fFree = false;
649 fNextFree = (i+1) % fMaxEntries;
650 return new (&fEntry[i].fNode) Node(data);
652 throw std::bad_alloc();
656 * Destructs the node object.
658 void DeleteNode(Node* node)
662 // Now mark the entry as free.
663 char* p = reinterpret_cast<char*>(node);
664 p -= (sizeof(NodeEntry) - sizeof(Node));
665 NodeEntry* entry = reinterpret_cast<NodeEntry*>(p);
672 * Stream operator to write the list to std::ostream. The output is printed in
673 * the following format: [element1, element2, ..., elementN]
675 template <typename DataType>
676 std::ostream& operator << (
677 std::ostream& stream, const AliHLTMUONList<DataType>& list
680 typename AliHLTMUONList<DataType>::ConstIterator current;
681 current = list.First();
683 if (current == list.End())
688 stream << (*current++);
689 while (current != list.End())
690 stream << ", " << (*current++);
695 #endif // ALIHLTMUONLIST_H