16 #ifndef ROOFIT_ROOFITCORE_INC_ROOSTLREFCOUNTLIST_H_
17 #define ROOFIT_ROOFITCORE_INC_ROOSTLREFCOUNTLIST_H_
35 class RooSTLRefCountList {
37 using Container_t = std::vector<T*>;
39 RooSTLRefCountList() {}
40 RooSTLRefCountList(
const RooSTLRefCountList&) =
default;
41 RooSTLRefCountList& operator=(
const RooSTLRefCountList&) =
default;
42 RooSTLRefCountList& operator=(RooSTLRefCountList&&) =
default;
44 virtual ~RooSTLRefCountList() {}
49 void Add(T * obj, std::size_t initialCount = 1) {
50 auto foundItem = findByPointer(obj);
52 if (foundItem != _storage.end()) {
53 _refCount[foundItem - _storage.begin()] += initialCount;
56 _storage.emplace_back(obj);
57 _refCount.emplace_back(initialCount);
63 std::size_t refCount(
typename Container_t::const_iterator item)
const {
64 assert(_storage.size() == _refCount.size());
66 return item != _storage.end() ? _refCount[item - _storage.begin()] : 0;
71 template<
typename Obj_t>
72 std::size_t refCount(
const Obj_t * obj)
const {
73 return refCount(findByPointer(obj));
77 typename Container_t::const_iterator begin()
const {
78 return _storage.begin();
82 typename Container_t::const_iterator end()
const {
83 return _storage.end();
88 const Container_t& containedObjects()
const {
94 std::size_t size()
const {
95 assert(_storage.size() == _refCount.size());
97 return _storage.size();
100 void reserve(std::size_t amount) {
101 _storage.reserve(amount);
102 _refCount.reserve(amount);
108 return _storage.empty();
113 template<
typename Obj_t>
114 typename Container_t::const_iterator findByPointer(
const Obj_t * item)
const {
115 auto byPointer = [item](
const T * listItem) {
116 return listItem == item;
119 return std::find_if(_storage.begin(), _storage.end(), byPointer);
124 typename Container_t::const_iterator findByName(
const char * name)
const {
127 const std::string theName(name);
128 auto byName = [&theName](
const T * element) {
129 return element->GetName() == theName;
132 return std::find_if(_storage.begin(), _storage.end(), byName);
137 typename Container_t::const_iterator findByNamePointer(
const T * item)
const {
138 auto nptr = item->namePtr();
139 auto byNamePointer = [nptr](
const T * element) {
140 return element->namePtr() == nptr;
143 return std::find_if(_storage.begin(), _storage.end(), byNamePointer);
148 template<
typename Obj_t>
149 bool containsByPointer(
const Obj_t * obj)
const {
150 return findByPointer(obj) != _storage.end();
155 bool containsByNamePtr(
const T * obj)
const {
156 return findByNamePointer(obj) != _storage.end();
161 bool containsSameName(
const char * name)
const {
162 return findByName(name) != _storage.end();
169 void Remove(
const T * obj,
bool force =
false) {
170 auto item = findByPointer(obj);
172 if (item != _storage.end()) {
173 const std::size_t pos = item - _storage.begin();
175 if (force || --_refCount[pos] == 0) {
178 _storage.erase(_storage.begin() + pos);
179 _refCount.erase(_refCount.begin() + pos);
186 void RemoveAll(
const T * obj) {
192 Container_t _storage;
193 std::vector<std::size_t> _refCount;
195 ClassDef(RooSTLRefCountList<T>,1);
201 class RooRefCountList;
204 namespace STLRefCountListHelpers {
206 RooSTLRefCountList<RooAbsArg> convert(
const RooRefCountList& old);