Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooLinkedListIter.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id: RooLinkedListIter.h,v 1.11 2007/05/11 09:11:30 verkerke Exp $
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16#ifndef ROO_LINKED_LIST_ITER
17#define ROO_LINKED_LIST_ITER
18
19#include "TIterator.h"
20#include "RooLinkedList.h"
21
22#include <memory>
23#include <stdexcept>
24#include <cassert>
25
26/// Interface for RooFIter-compatible iterators
28{
29 public:
30 /// Return next element or nullptr if at end.
31 virtual RooAbsArg * next() = 0;
32 virtual ~GenericRooFIter() {}
33};
34
35////////////////////////////////////////////////////////////////////////////////////////////
36/// A one-time forward iterator working on RooLinkedList or RooAbsCollection.
37/// This wrapper separates the interface visible to the outside from the actual
38/// implementation of the iterator.
39class RooFIter final
40{
41 public:
42 RooFIter(std::unique_ptr<GenericRooFIter> && itImpl) : fIterImpl{std::move(itImpl)} {}
43 RooFIter(const RooFIter &) = delete;
44 RooFIter(RooFIter &&) = default;
45 RooFIter & operator=(const RooFIter &) = delete;
46 RooFIter & operator=(RooFIter &&) = default;
47
48 /// Return next element or nullptr if at end.
50 return fIterImpl->next();
51 }
52
53 private:
54 std::unique_ptr<GenericRooFIter> fIterImpl;
55};
56
57////////////////////////////////////////////////////////////////////////////////////////////
58/// Implementation of the GenericRooFIter interface for the RooLinkedList
60{
61 public:
64
65 /// Return next element in collection
66 RooAbsArg *next() override {
67 return fList && fIndex < fList->GetSize() ? (RooAbsArg *)fList->At(fIndex++) : nullptr;
68 }
69
70 private:
71 const RooLinkedList *fList = nullptr; ///<! Collection iterated over
72 Int_t fIndex = 0; ///<! Index of the next element
73};
74
75
76
77
78////////////////////////////////////////////////////////////////////////////////////////////
79/// TIterator and GenericRooFIter front end with STL back end.
80///
81/// By default, this iterators counts, at which position the current element should be.
82/// On request, it does an index access to the underlying collection, and returns the element.
83/// This happens because the RooLinkedList, which used to be the default collection in RooFit,
84/// will not invalidate iterators when inserting elements. Since the default is now an STL collection,
85/// reallocations might invalidate the iterator.
86///
87/// With an iterator that counts, only inserting before or at the iterator position will create problems.
88/// deal with reallocations while iterating. Therefore, this iterator will also check that the last element
89/// it was pointing to is the current element when it is invoked again. This ensures that
90/// inserting or removing before this iterator does not happen, which was possible with
91/// the linked list iterators of RooFit.
92/// When NDEBUG is defined, these checks will disappear.
93/// \note This is a legacy iterator that only exists to not break old code. Use begin(), end() and
94/// range-based for loops with RooArgList and RooArgSet.
95template<class STLContainer>
96class TIteratorToSTLInterface final : public TIterator , public GenericRooFIter {
97public:
98
99 TIteratorToSTLInterface(const STLContainer & container) :
100 TIterator(),
102 fSTLContainer(container),
103 fIndex(0)
104#ifdef NDEBUG
105 ,fCurrentElem{nullptr}
106#else
107 ,fCurrentElem{fSTLContainer.empty() ? nullptr : fSTLContainer.front()}
108#endif
109 {
110
111 }
112
113 TIterator & operator=(const TIterator &) override {
114 throw;
115 }
116
117 const TCollection *GetCollection() const override {
118 return nullptr;
119 }
120
121 RooAbsArg * next() override {
122 if (atEnd())
123 return nullptr;
124#ifdef NDEBUG
125 return fSTLContainer[fIndex++];
126#else
127 return nextChecked();
128#endif
129 }
130
131
132 TObject * Next() override {
133 return static_cast<TObject*>(next());
134 }
135
136 void Reset() override {
137 fIndex = 0;
138#ifndef NDEBUG
139 fCurrentElem = fSTLContainer.empty() ? nullptr : fSTLContainer.front();
140#endif
141
142 }
143
144 bool operator!=(const TIterator & other) const override {
145 const auto * castedOther =
146 dynamic_cast<const TIteratorToSTLInterface<STLContainer>*>(&other);
147 return !castedOther || &fSTLContainer != &(castedOther->fSTLContainer)
148 || fIndex == castedOther->fIndex;
149 }
150
151 TObject * operator*() const override {
152 if (atEnd())
153 return nullptr;
154
155 #ifndef NDEBUG
157 #endif
158
159 return static_cast<TObject*>(fSTLContainer[fIndex]);
160 }
161
162
163private:
164 bool atEnd() const {
165 return fSTLContainer.empty()
166 || fIndex >= fSTLContainer.size();
167 }
168
169
171 RooAbsArg * ret = fSTLContainer.at(fIndex);
172 if (fCurrentElem != nullptr && ret != fCurrentElem) {
173 throw std::logic_error("A RooCollection should not be modified while iterating. "
174 "Only inserting at end is acceptable.");
175 }
176 fCurrentElem = ++fIndex < fSTLContainer.size() ? fSTLContainer[fIndex] : nullptr;
177
178 return ret;
179 }
180
181
182 const STLContainer & fSTLContainer; ///<!
183 std::size_t fIndex; ///<!
184 const RooAbsArg * fCurrentElem; ///<!
185};
186
187
188
189
190////////////////////////////////////////////////////////////////////////////////////////////
191/// A wrapper around TIterator derivatives.
192///
193/// It is called RooLinkedListIter because all classes assume that the RooAbsCollections use
194/// a RooLinkedList, which is not true, any more.
195/// The purpose of this wrapper is to act on the outside like a RooLinkedListIter, even though
196/// the underlying implementation may work an a different container, like e.g.
197/// an STL container. This is needed to not break user code that is using a RooLinkedList or
198/// a RooAbsCollection.
199///
200/// \note All code using this iterator as an iterator over a RooAbsCollection should move
201/// to begin() and end() or range-based for loops. These are faster.
202class RooLinkedListIter final : public TIterator {
203
204 public:
205 RooLinkedListIter(std::shared_ptr<TIterator> iterImpl) :
206 fIterImpl{std::move(iterImpl)} {
207
208 }
209
212
213 // Setting the move constructor and assignment operator to = default might
214 // seem to work, but it causes linker errors when using it because
215 // TIterator::operator= is not implemented.
217 : fIterImpl{std::move(other.fIterImpl)}
218 {}
220 fIterImpl = std::move(other.fIterImpl);
221 return *this;
222 }
223
224 TIterator &operator=(const TIterator & other) override {fIterImpl->operator=(other); return *this;}
225 const TCollection *GetCollection() const override {return nullptr;}
226
227 TObject * Next() override {return fIterImpl->Next();}
228 void Reset() override {fIterImpl->Reset();}
229 bool operator!=(const TIterator & other) const override {return fIterImpl->operator!=(other);}
230 TObject * operator*() const override {return fIterImpl->operator*();}
231
232 private:
233 std::shared_ptr<TIterator> fIterImpl; ///<!
234};
235
236
237////////////////////////////////////////////////////////////////////////////////////////////
238/// Implementation of the actual iterator on RooLinkedLists.
239/// It holds an index, so elements can be appended while iterating. Next()
240/// continues with the element that followed the one it returned last, also if
241/// elements were removed in the meantime. In range-based for loops, removing
242/// the current or an earlier element skips the next one.
243class RooLinkedListIterImpl final : public TIterator {
244public:
245
246 RooLinkedListIterImpl(const RooLinkedList* list, Int_t index, bool forward) :
247 _list(list), _index(index), _forward(forward) {}
248
249 RooLinkedListIterImpl(const RooLinkedList* list, bool forward) :
250 RooLinkedListIterImpl(list, forward ? 0 : list->GetSize() - 1, forward) {}
251
252 TIterator& operator=(const TIterator& other) override {
253
254 // Iterator assignment operator
255
256 if (&other==this) return *this ;
257 const RooLinkedListIterImpl* iter = dynamic_cast<const RooLinkedListIterImpl*>(&other) ;
258 if (iter) {
259 _list = iter->_list ;
260 _index = iter->_index ;
261 _next = iter->_next ;
262 _forward = iter->_forward ;
263 }
264 return *this ;
265 }
266
267 const TCollection *GetCollection() const override {
268 // Dummy
269 return nullptr ;
270 }
271
272 TObject *Next() override {
273 // Return next element in collection
274 return NextNV();
275 }
276
278 // Return next element in collection
279 if (_next && _list->At(_index) != _next) {
280 // The list was changed, find the element again
281 const Int_t index = _list->IndexOf(_next);
282 if (index >= 0) _index = index;
283 }
284 TObject *arg = **this;
285 ++*this;
286 _next = **this;
287 return arg;
288 }
289
290 void Reset() override {
291 // Return iterator to first element in collection
292 _index = _forward ? 0 : _list->GetSize() - 1;
293 _next = nullptr;
294 }
295
296 bool operator!=(const TIterator &aIter) const override {
297 const RooLinkedListIterImpl *iter(dynamic_cast<const RooLinkedListIterImpl*>(&aIter));
298 if (iter) return *this != *iter;
299 return false; // for base class we don't implement a comparison
300 }
301
302 bool operator!=(const RooLinkedListIterImpl &aIter) const {
303 return atEnd() || aIter.atEnd() ? atEnd() != aIter.atEnd() : _index != aIter._index;
304 }
305
306 TObject *operator*() const override {
307 // Return element iterator points to
308 return _list->At(_index);
309 }
310
312 if (!atEnd()) _index += _forward ? 1 : -1;
313 return *this;
314 }
315
317 RooLinkedListIterImpl tmp(*this);
318 operator++();
319 return tmp;
320 }
321
322protected:
323 bool atEnd() const { return _index < 0 || _index >= _list->GetSize(); }
324
325 const RooLinkedList* _list ; ///<! Collection iterated over
326 Int_t _index ; ///<! Index of the current element
327 TObject *_next = nullptr; ///<! Element that NextNV() returns next
328 bool _forward ; ///<! Iterator direction
329};
330
331
332
333
334#endif
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Interface for RooFIter-compatible iterators.
virtual ~GenericRooFIter()
virtual RooAbsArg * next()=0
Return next element or nullptr if at end.
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Implementation of the GenericRooFIter interface for the RooLinkedList.
RooAbsArg * next() override
Return next element in collection.
const RooLinkedList * fList
! Collection iterated over
Int_t fIndex
! Index of the next element
RooFIterForLinkedList(const RooLinkedList *list)
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
RooFIter(RooFIter &&)=default
RooFIter(const RooFIter &)=delete
RooAbsArg * next()
Return next element or nullptr if at end.
RooFIter & operator=(const RooFIter &)=delete
std::unique_ptr< GenericRooFIter > fIterImpl
RooFIter(std::unique_ptr< GenericRooFIter > &&itImpl)
RooFIter & operator=(RooFIter &&)=default
Implementation of the actual iterator on RooLinkedLists.
RooLinkedListIterImpl & operator++()
bool operator!=(const RooLinkedListIterImpl &aIter) const
Int_t _index
! Index of the current element
RooLinkedListIterImpl(const RooLinkedList *list, bool forward)
bool _forward
! Iterator direction
TObject * _next
! Element that NextNV() returns next
const RooLinkedList * _list
! Collection iterated over
RooLinkedListIterImpl(const RooLinkedList *list, Int_t index, bool forward)
const TCollection * GetCollection() const override
TObject * Next() override
bool operator!=(const TIterator &aIter) const override
Compare two iterator objects.
TObject * operator*() const override
Return current object or nullptr.
RooLinkedListIterImpl operator++(int)
TIterator & operator=(const TIterator &other) override
A wrapper around TIterator derivatives.
RooLinkedListIter(std::shared_ptr< TIterator > iterImpl)
bool operator!=(const TIterator &other) const override
Compare two iterator objects.
const TCollection * GetCollection() const override
RooLinkedListIter(const RooLinkedListIter &)=delete
void Reset() override
TObject * Next() override
TObject * operator*() const override
Return current object or nullptr.
RooLinkedListIter & operator=(const RooLinkedListIter &)=delete
RooLinkedListIter(RooLinkedListIter &&other)
TIterator & operator=(const TIterator &other) override
std::shared_ptr< TIterator > fIterImpl
!
RooLinkedListIter & operator=(RooLinkedListIter &&other)
Collection class for internal use, storing a collection of TObject pointers in a vector.
Int_t GetSize() const
TObject * At(int index) const
Return object stored in sequential position given by index.
Int_t IndexOf(const char *name) const
Return position of given object in list.
Collection abstract base class.
Definition TCollection.h:65
TIterator and GenericRooFIter front end with STL back end.
TObject * Next() override
bool operator!=(const TIterator &other) const override
Compare two iterator objects.
RooAbsArg * next() override
Return next element or nullptr if at end.
const RooAbsArg * fCurrentElem
!
TIterator & operator=(const TIterator &) override
TIteratorToSTLInterface(const STLContainer &container)
TObject * operator*() const override
Return current object or nullptr.
const TCollection * GetCollection() const override
const STLContainer & fSTLContainer
!
Iterator abstract base class.
Definition TIterator.h:30
Mother of all ROOT objects.
Definition TObject.h:42
#define NDEBUG