Logo ROOT  
Reference Guide
RooSTLRefCountList.h
Go to the documentation of this file.
1// Author: Stephan Hageboeck, CERN, 12/2018
2/*****************************************************************************
3 * Project: RooFit *
4 * Authors: *
5 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
6 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
7 * *
8 * Copyright (c) 2000-2005, Regents of the University of California *
9 * and Stanford University. All rights reserved. *
10 * *
11 * Redistribution and use in source and binary forms, *
12 * with or without modification, are permitted according to the terms *
13 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
14 *****************************************************************************/
15
16#ifndef ROOFIT_ROOFITCORE_INC_ROOSTLREFCOUNTLIST_H_
17#define ROOFIT_ROOFITCORE_INC_ROOSTLREFCOUNTLIST_H_
18
19#include "Rtypes.h"
20
21#include <vector>
22#include <string>
23#include <algorithm>
24#include <cassert>
25
26
27/**
28 * \class RooSTLRefCountList
29 * The RooSTLRefCountList is a simple collection of **pointers** to the template objects with
30 * reference counters.
31 * The pointees are not owned, hence not deleted when removed from the collection.
32 * Objects can be searched for either by pointer or by name (confusion possible when
33 * objects with same name are present). This replicates the behaviour of the RooRefCountList.
34 */
35
36template <class T>
38 public:
39 using Container_t = std::vector<T*>;
40
45
47
48
49 ///Add an object or increase refCount if it is already present. Only compares
50 ///pointers to check for existing objects
51 void Add(T * obj, std::size_t initialCount = 1) {
52 auto foundItem = findByPointer(obj);
53
54 if (foundItem != _storage.end()) {
55 _refCount[foundItem - _storage.begin()] += initialCount;
56 }
57 else {
58 _storage.emplace_back(obj);
59 _refCount.emplace_back(initialCount);
60 }
61 }
62
63
64 ///Return ref count of item that iterator points to.
65 std::size_t refCount(typename Container_t::const_iterator item) const {
66 assert(_storage.size() == _refCount.size());
67
68 return item != _storage.end() ? _refCount[item - _storage.begin()] : 0;
69 }
70
71
72 ///Return ref count of item with given address.
73 template<typename Obj_t>
74 std::size_t refCount(const Obj_t * obj) const {
75 return refCount(findByPointer(obj));
76 }
77
78 ///Iterator over contained objects.
79 typename Container_t::const_iterator begin() const {
80 return _storage.begin();
81 }
82
83 ///End of contained objects.
84 typename Container_t::const_iterator end() const {
85 return _storage.end();
86 }
87
88 /// Retrieve an element from the list.
89 typename Container_t::value_type operator[](std::size_t index) const {
90 return _storage[index];
91 }
92
93
94 ///Direct reference to container of objects held by this list.
96 return _storage;
97 }
98
99
100 ///Number of contained objects (neglecting the ref count).
101 std::size_t size() const {
102 assert(_storage.size() == _refCount.size());
103
104 return _storage.size();
105 }
106
107 void reserve(std::size_t amount) {
108 _storage.reserve(amount);
109 _refCount.reserve(amount);
110 }
111
112
113 ///Check if empty.
114 bool empty() const {
115 return _storage.empty();
116 }
117
118
119 ///Find an item by comparing its adress.
120 template<typename Obj_t>
121 typename Container_t::const_iterator findByPointer(const Obj_t * item) const {
122 auto byPointer = [item](const T * listItem) {
123 return listItem == item;
124 };
125
126 return std::find_if(_storage.begin(), _storage.end(), byPointer);
127 }
128
129
130 ///Find an item by comparing strings returned by RooAbsArg::GetName()
131 typename Container_t::const_iterator findByName(const char * name) const {
132 //If this turns out to be a bottleneck,
133 //one could use the RooNameReg to obtain the pointer to the arg's name and compare these
134 const std::string theName(name);
135 auto byName = [&theName](const T * element) {
136 return element->GetName() == theName;
137 };
138
139 return std::find_if(_storage.begin(), _storage.end(), byName);
140 }
141
142
143 ///Find an item by comparing RooAbsArg::namePtr() adresses.
144 typename Container_t::const_iterator findByNamePointer(const T * item) const {
145 auto nptr = item->namePtr();
146 auto byNamePointer = [nptr](const T * element) {
147 return element->namePtr() == nptr;
148 };
149
150 return std::find_if(_storage.begin(), _storage.end(), byNamePointer);
151 }
152
153
154 ///Check if list contains an item using findByPointer().
155 template<typename Obj_t>
156 bool containsByPointer(const Obj_t * obj) const {
157 return findByPointer(obj) != _storage.end();
158 }
159
160
161 ///Check if list contains an item using findByNamePointer().
162 bool containsByNamePtr(const T * obj) const {
163 return findByNamePointer(obj) != _storage.end();
164 }
165
166
167 ///Check if list contains an item using findByName().
168 bool containsSameName(const char * name) const {
169 return findByName(name) != _storage.end();
170 }
171
172
173 ///Decrease ref count of given object. Shrink list if ref count reaches 0.
174 ///\param obj Decrease ref count of given object. Compare by pointer.
175 ///\param force If true, remove irrespective of ref count.
176 void Remove(const T * obj, bool force = false) {
177 auto item = findByPointer(obj);
178
179 if (item != _storage.end()) {
180 const std::size_t pos = item - _storage.begin();
181
182 if (force || --_refCount[pos] == 0) {
183 //gcc4.x doesn't know how to erase at the position of a const_iterator
184 //Therefore, erase at begin + pos instead of 'item'
185 _storage.erase(_storage.begin() + pos);
186 _refCount.erase(_refCount.begin() + pos);
187 }
188 }
189 }
190
191
192 ///Remove from list irrespective of ref count.
193 void RemoveAll(const T * obj) {
194 Remove(obj, true);
195 }
196
197
198 private:
200 std::vector<std::size_t> _refCount;
201
203};
204
205
206
207class RooAbsArg;
208class RooRefCountList;
209
210namespace RooFit {
211namespace STLRefCountListHelpers {
212 /// Converter from the old RooRefCountList to RooSTLRefCountList.
214}
215}
216
217#endif /* ROOFIT_ROOFITCORE_INC_ROOSTLREFCOUNTLIST_H_ */
#define ClassDef(name, id)
Definition: Rtypes.h:325
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
A RooRefCountList is a RooLinkedList that keeps a reference counter with each added node.
The RooSTLRefCountList is a simple collection of pointers to the template objects with reference coun...
std::size_t refCount(typename Container_t::const_iterator item) const
Return ref count of item that iterator points to.
Container_t::const_iterator findByNamePointer(const T *item) const
Find an item by comparing RooAbsArg::namePtr() adresses.
bool containsSameName(const char *name) const
Check if list contains an item using findByName().
bool containsByPointer(const Obj_t *obj) const
Check if list contains an item using findByPointer().
std::size_t refCount(const Obj_t *obj) const
Return ref count of item with given address.
RooSTLRefCountList & operator=(RooSTLRefCountList &&)=default
void Remove(const T *obj, bool force=false)
Decrease ref count of given object.
Container_t::const_iterator begin() const
Iterator over contained objects.
std::vector< T * > Container_t
void Add(T *obj, std::size_t initialCount=1)
Add an object or increase refCount if it is already present.
Container_t::value_type operator[](std::size_t index) const
Retrieve an element from the list.
RooSTLRefCountList(const RooSTLRefCountList &)=default
Container_t::const_iterator end() const
End of contained objects.
RooSTLRefCountList & operator=(const RooSTLRefCountList &)=default
bool empty() const
Check if empty.
std::vector< std::size_t > _refCount
Container_t::const_iterator findByName(const char *name) const
Find an item by comparing strings returned by RooAbsArg::GetName()
std::size_t size() const
Number of contained objects (neglecting the ref count).
Container_t::const_iterator findByPointer(const Obj_t *item) const
Find an item by comparing its adress.
const Container_t & containedObjects() const
Direct reference to container of objects held by this list.
void RemoveAll(const T *obj)
Remove from list irrespective of ref count.
void reserve(std::size_t amount)
bool containsByNamePtr(const T *obj) const
Check if list contains an item using findByNamePointer().
double T(double x)
Definition: ChebyshevPol.h:34
static const std::string name("name")
RooSTLRefCountList< RooAbsArg > convert(const RooRefCountList &old)
Converter from the old RooRefCountList to RooSTLRefCountList.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...