// RooLinkedList is an collection class for internal use, storing
// a collection of RooAbsArg pointers in a doubly linked list.
// It can optionally add a hash table to speed up random access
// in large collections
// Use RooAbsCollection derived objects for public use
// (e.g. RooArgSet or RooArgList)
// END_HTML
#include <algorithm>
#include "RooFit.h"
#include "Riostream.h"
#include "RooLinkedList.h"
#include "RooLinkedListIter.h"
#include "RooHashTable.h"
#include "RooAbsArg.h"
#include "RooMsgService.h"
using namespace std;
ClassImp(RooLinkedList)
;
namespace RooLinkedListImplDetails {
class Chunk {
public:
Chunk(Int_t sz) :
_sz(sz), _free(capacity()),
_chunk(new RooLinkedListElem[_free]), _freelist(_chunk)
{
for (Int_t i = 0; i < _free; ++i)
_chunk[i]._next = (i + 1 < _free) ? &_chunk[i + 1] : 0;
}
~Chunk() { delete[] _chunk; }
Int_t capacity() const
{ return (1 << _sz) / sizeof(RooLinkedListElem); }
Int_t free() const { return _free; }
Int_t size() const { return capacity() - free(); }
int szclass() const { return _sz; }
bool full() const { return !free(); }
bool empty() const { return capacity() == free(); }
const void* chunkaddr() const { return _chunk; }
bool contains(RooLinkedListElem* el) const
{ return _chunk <= el && el < &_chunk[capacity()]; }
RooLinkedListElem* pop_free_elem()
{
if (!_freelist) return 0;
RooLinkedListElem* retVal = _freelist;
_freelist = retVal->_next;
retVal->_arg = 0; retVal->_refCount = 0;
retVal->_prev = retVal->_next = 0;
--_free;
return retVal;
}
void push_free_elem(RooLinkedListElem* el)
{
el->_next = _freelist;
_freelist = el;
++_free;
}
private:
Int_t _sz;
Int_t _free;
RooLinkedListElem* _chunk;
RooLinkedListElem* _freelist;
Chunk(const Chunk&);
Chunk& operator=(const Chunk&);
};
class Pool {
private:
enum {
minsz = 7,
maxsz = 18,
szincr = 1
};
typedef RooLinkedListImplDetails::Chunk Chunk;
typedef std::list<Chunk*> ChunkList;
typedef std::map<const void*, Chunk*> AddrMap;
public:
Pool();
~Pool();
inline void acquire() { ++_refCount; }
inline bool release() { return 0 == --_refCount; }
RooLinkedListElem* pop_free_elem();
void push_free_elem(RooLinkedListElem* el);
private:
AddrMap _addrmap;
ChunkList _freelist;
UInt_t _szmap[(maxsz - minsz) / szincr];
Int_t _cursz;
UInt_t _refCount;
void updateCurSz(Int_t sz, Int_t incr);
Int_t nextChunkSz() const;
};
Pool::Pool() : _cursz(minsz), _refCount(0)
{
std::fill(_szmap, _szmap + ((maxsz - minsz) / szincr), 0);
}
Pool::~Pool()
{
_freelist.clear();
for (AddrMap::iterator it = _addrmap.begin(); _addrmap.end() != it; ++it)
delete it->second;
_addrmap.clear();
}
RooLinkedListElem* Pool::pop_free_elem()
{
if (_freelist.empty()) {
const Int_t sz = nextChunkSz();
Chunk *c = new Chunk(sz);
_addrmap[c->chunkaddr()] = c;
_freelist.push_back(c);
updateCurSz(sz, +1);
}
Chunk* c = _freelist.front();
RooLinkedListElem* retVal = c->pop_free_elem();
if (c->full()) _freelist.pop_front();
return retVal;
}
void Pool::push_free_elem(RooLinkedListElem* el)
{
AddrMap::iterator ci = _addrmap.end();
if (!_addrmap.empty()) {
ci = _addrmap.lower_bound(el);
if (ci == _addrmap.end()) {
ci = (++_addrmap.rbegin()).base();
} else {
if (_addrmap.begin() != ci && ci->first != el) --ci;
}
}
if (_addrmap.empty() || !ci->second->contains(el)) {
delete el;
return;
}
Chunk *c = ci->second;
const bool moveToFreelist = c->full();
c->push_free_elem(el);
if (c->empty()) {
ChunkList::iterator it = std::find( _freelist.begin(), _freelist.end(), c);
if (_freelist.end() != it) _freelist.erase(it);
_addrmap.erase(ci->first);
updateCurSz(c->szclass(), -1);
delete c;
} else if (moveToFreelist) {
_freelist.push_back(c);
}
}
void Pool::updateCurSz(Int_t sz, Int_t incr)
{
_szmap[(sz - minsz) / szincr] += incr;
_cursz = minsz;
for (int i = (maxsz - minsz) / szincr; i--; ) {
if (_szmap[i]) {
_cursz += i * szincr;
break;
}
}
}
Int_t Pool::nextChunkSz() const
{
Int_t sz = _cursz;
if (_addrmap.empty()) {
sz = minsz;
} else {
if (minsz >= sz) {
sz = minsz + szincr;
} else {
if (1 != _addrmap.size()) {
sz += szincr;
} else {
sz -= szincr;
}
}
}
if (sz > maxsz) sz = maxsz;
if (sz < minsz) sz = minsz;
return sz;
}
}
RooLinkedList::Pool* RooLinkedList::_pool = 0;
RooLinkedList::RooLinkedList(Int_t htsize) :
_hashThresh(htsize), _size(0), _first(0), _last(0), _htableName(0), _htableLink(0), _useNptr(kTRUE)
{
if (!_pool) _pool = new Pool;
_pool->acquire();
}
RooLinkedList::RooLinkedList(const RooLinkedList& other) :
TObject(other), _hashThresh(other._hashThresh), _size(0), _first(0), _last(0), _htableName(0), _htableLink(0),
_name(other._name),
_useNptr(other._useNptr)
{
if (!_pool) _pool = new Pool;
_pool->acquire();
if (other._htableName) _htableName = new RooHashTable(other._htableName->size()) ;
if (other._htableLink) _htableLink = new RooHashTable(other._htableLink->size(),RooHashTable::Pointer) ;
for (RooLinkedListElem* elem = other._first; elem; elem = elem->_next) {
Add(elem->_arg, elem->_refCount) ;
}
}
RooLinkedListElem* RooLinkedList::createElement(TObject* obj, RooLinkedListElem* elem)
{
RooLinkedListElem* ret = _pool->pop_free_elem();
ret->init(obj, elem);
return ret ;
}
void RooLinkedList::deleteElement(RooLinkedListElem* elem)
{
elem->release() ;
_pool->push_free_elem(elem);
}
RooLinkedList& RooLinkedList::operator=(const RooLinkedList& other)
{
if (&other==this) return *this ;
Clear();
for (RooLinkedListElem* elem = other._first; elem; elem = elem->_next) {
Add(elem->_arg) ;
}
return *this ;
}
void RooLinkedList::setHashTableSize(Int_t size)
{
if (size<0) {
coutE(InputArguments) << "RooLinkedList::setHashTable() ERROR size must be positive" << endl ;
return ;
}
if (size==0) {
if (!_htableName) {
return ;
} else {
delete _htableName ;
delete _htableLink ;
_htableName = 0 ;
_htableLink = 0 ;
}
} else {
if (_htableName) delete _htableName ;
_htableName = new RooHashTable(size) ;
if (_htableLink) delete _htableLink ;
_htableLink = new RooHashTable(size,RooHashTable::Pointer) ;
RooLinkedListElem* ptr = _first ;
while(ptr) {
_htableName->add(ptr->_arg) ;
_htableLink->add((TObject*)ptr,ptr->_arg) ;
ptr = ptr->_next ;
}
}
}
RooLinkedList::~RooLinkedList()
{
if (_htableName) {
delete _htableName ;
_htableName=0 ;
}
if (_htableLink) {
delete _htableLink ;
_htableLink=0 ;
}
Clear() ;
if (_pool->release()) {
delete _pool;
_pool = 0;
}
}
RooLinkedListElem* RooLinkedList::findLink(const TObject* arg) const
{
if (_htableLink) {
return _htableLink->findLinkTo(arg) ;
}
RooLinkedListElem* ptr = _first;
while(ptr) {
if (ptr->_arg == arg) {
return ptr ;
}
ptr = ptr->_next ;
}
return 0 ;
}
void RooLinkedList::Add(TObject* arg, Int_t refCount)
{
if (!arg) return ;
if (!dynamic_cast<RooAbsArg*>(arg)) _useNptr = kFALSE;
if (_htableName) {
if (_size > _htableName->size()) {
setHashTableSize(_size*2) ;
}
} else if (_hashThresh>0 && _size>_hashThresh) {
setHashTableSize(_hashThresh) ;
}
if (_last) {
_last = createElement(arg,_last) ;
} else {
_last = createElement(arg) ;
_first=_last ;
}
if (_htableName){
_htableName->add(arg) ;
_htableLink->add((TObject*)_last,arg) ;
}
_size++ ;
_last->_refCount = refCount ;
}
Bool_t RooLinkedList::Remove(TObject* arg)
{
RooLinkedListElem* elem = findLink(arg) ;
if (!elem) return kFALSE ;
if (_htableName) {
_htableName->remove(arg) ;
}
if (_htableLink) {
_htableLink->remove((TObject*)elem,arg) ;
}
if (elem==_first) _first=elem->_next ;
if (elem==_last) _last=elem->_prev ;
_size-- ;
deleteElement(elem) ;
return kTRUE ;
}
TObject* RooLinkedList::At(Int_t index) const
{
if (index<0 || index>=_size) return 0 ;
RooLinkedListElem* ptr = _first;
while(index--) ptr = ptr->_next ;
return ptr->_arg ;
}
Bool_t RooLinkedList::Replace(const TObject* oldArg, const TObject* newArg)
{
RooLinkedListElem* elem = findLink(oldArg) ;
if (!elem) return kFALSE ;
if (_htableName) {
_htableName->replace(oldArg,newArg) ;
}
if (_htableLink) {
_htableLink->remove((TObject*)elem,(TObject*)oldArg) ;
_htableLink->add((TObject*)elem,(TObject*)newArg) ;
}
elem->_arg = (TObject*)newArg ;
return kTRUE ;
}
TObject* RooLinkedList::FindObject(const char* name) const
{
return find(name) ;
}
TObject* RooLinkedList::FindObject(const TObject* obj) const
{
RooLinkedListElem *elem = findLink((TObject*)obj) ;
return elem ? elem->_arg : 0 ;
}
void RooLinkedList::Clear(Option_t *)
{
for (RooLinkedListElem *elem = _first, *next; elem; elem = next) {
next = elem->_next ;
deleteElement(elem) ;
}
_first = 0 ;
_last = 0 ;
_size = 0 ;
if (_htableName) {
Int_t hsize = _htableName->size() ;
delete _htableName ;
_htableName = new RooHashTable(hsize) ;
}
if (_htableLink) {
Int_t hsize = _htableLink->size() ;
delete _htableLink ;
_htableLink = new RooHashTable(hsize,RooHashTable::Pointer) ;
}
}
void RooLinkedList::Delete(Option_t *)
{
RooLinkedListElem* elem = _first;
while(elem) {
RooLinkedListElem* next = elem->_next ;
delete elem->_arg ;
deleteElement(elem) ;
elem = next ;
}
_first = 0 ;
_last = 0 ;
_size = 0 ;
if (_htableName) {
Int_t hsize = _htableName->size() ;
delete _htableName ;
_htableName = new RooHashTable(hsize) ;
}
if (_htableLink) {
Int_t hsize = _htableLink->size() ;
delete _htableLink ;
_htableLink = new RooHashTable(hsize,RooHashTable::Pointer) ;
}
}
TObject* RooLinkedList::find(const char* name) const
{
if (_htableName) {
RooAbsArg* a = (RooAbsArg*) _htableName->find(name) ;
if (a) return a;
if (_useNptr) {
const TNamed* nptr= RooNameReg::known(name);
if (nptr && nptr->TestBit(RooNameReg::kRenamedArg)) {
RooLinkedListElem* ptr = _first ;
while(ptr) {
if ((((RooAbsArg*)ptr->_arg)->namePtr() == nptr)) {
return ptr->_arg ;
}
ptr = ptr->_next ;
}
}
return 0 ;
}
}
RooLinkedListElem* ptr = _first ;
if (_useNptr && _size>9) {
const TNamed* nptr= RooNameReg::known(name);
if (!nptr) return 0;
while(ptr) {
if ((((RooAbsArg*)ptr->_arg)->namePtr() == nptr)) {
return ptr->_arg ;
}
ptr = ptr->_next ;
}
return 0 ;
}
while(ptr) {
if (!strcmp(ptr->_arg->GetName(),name)) {
return ptr->_arg ;
}
ptr = ptr->_next ;
}
return 0 ;
}
RooAbsArg* RooLinkedList::findArg(const RooAbsArg* arg) const
{
if (_htableName) {
RooAbsArg* a = (RooAbsArg*) _htableName->findArg(arg) ;
if (a) return a;
if (!arg->namePtr()->TestBit(RooNameReg::kRenamedArg)) return 0;
}
RooLinkedListElem* ptr = _first ;
const TNamed* nptr = arg->namePtr();
while(ptr) {
if (((RooAbsArg*)(ptr->_arg))->namePtr() == nptr) {
return (RooAbsArg*) ptr->_arg ;
}
ptr = ptr->_next ;
}
return 0 ;
}
Int_t RooLinkedList::IndexOf(const TObject* arg) const
{
RooLinkedListElem* ptr = _first;
Int_t idx(0) ;
while(ptr) {
if (ptr->_arg==arg) return idx ;
ptr = ptr->_next ;
idx++ ;
}
return -1 ;
}
Int_t RooLinkedList::IndexOf(const char* name) const
{
RooLinkedListElem* ptr = _first;
Int_t idx(0) ;
while(ptr) {
if (strcmp(ptr->_arg->GetName(),name)==0) return idx ;
ptr = ptr->_next ;
idx++ ;
}
return -1 ;
}
void RooLinkedList::Print(const char* opt) const
{
RooLinkedListElem* elem = _first ;
while(elem) {
cout << elem->_arg << " : " ;
elem->_arg->Print(opt) ;
elem = elem->_next ;
}
}
RooLinkedListIter RooLinkedList::iterator(Bool_t dir) const
{
return RooLinkedListIter(this,dir) ;
}
RooFIter RooLinkedList::fwdIterator() const
{
return RooFIter(this) ;
}
TIterator* RooLinkedList::MakeIterator(Bool_t dir) const
{
return new RooLinkedListIter(this,dir) ;
}
void RooLinkedList::Sort(Bool_t ascend)
{
if (ascend) _first = mergesort_impl<true>(_first, _size, &_last);
else _first = mergesort_impl<false>(_first, _size, &_last);
}
template <bool ascending>
RooLinkedListElem* RooLinkedList::mergesort_impl(
RooLinkedListElem* l1, const unsigned sz, RooLinkedListElem** tail)
{
if (!l1 || sz < 2) {
if (tail) *tail = l1;
return l1;
}
if (sz <= 16) {
#if !defined(_WIN32) && !defined(R__SOLARIS_CC50)
RooLinkedListElem *arr[sz];
#else // _WIN32 && Solaris
RooLinkedListElem *arr[16];
#endif // _WIN32
for (int i = 0; l1; l1 = l1->_next, ++i) arr[i] = l1;
{
int i = 1;
do {
int j = i - 1;
RooLinkedListElem *tmp = arr[i];
while (0 <= j) {
const bool inOrder = ascending ?
(tmp->_arg->Compare(arr[j]->_arg) <= 0) :
(arr[j]->_arg->Compare(tmp->_arg) <= 0);
if (!inOrder) break;
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = tmp;
++i;
} while (int(sz) != i);
}
arr[0]->_prev = arr[sz - 1]->_next = 0;
for (int i = 0; i < int(sz - 1); ++i) {
arr[i]->_next = arr[i + 1];
arr[i + 1]->_prev = arr[i];
}
if (tail) *tail = arr[sz - 1];
return arr[0];
}
RooLinkedListElem *l2 = l1;
for (RooLinkedListElem *end = l2; end->_next; end = end->_next) {
end = end->_next;
l2 = l2->_next;
if (!end->_next) break;
}
l2->_prev->_next = 0;
l2->_prev = 0;
if (l1->_next) l1 = mergesort_impl<ascending>(l1, sz / 2);
if (l2->_next) l2 = mergesort_impl<ascending>(l2, sz - sz / 2);
RooLinkedListElem *l = (ascending ? (l1->_arg->Compare(l2->_arg) <= 0) :
(l2->_arg->Compare(l1->_arg) <= 0)) ? l1 : l2;
RooLinkedListElem *t = l;
if (l == l2) {
RooLinkedListElem* tmp = l1;
l1 = l2;
l2 = tmp;
}
l1 = l1->_next;
while (l1 && l2) {
const bool inOrder = ascending ? (l1->_arg->Compare(l2->_arg) <= 0) :
(l2->_arg->Compare(l1->_arg) <= 0);
if (!inOrder) {
if (l1->_prev) {
l1->_prev->_next = l2;
l2->_prev = l1->_prev;
}
RooLinkedListElem *tmp = l1;
l1 = l2;
l2 = tmp;
}
t = l1;
l1 = l1->_next;
}
if (l2) {
l2->_prev = t;
if (t) t->_next = l2;
}
if (tail) {
for (l1 = t; l1; l1 = l1->_next) t = l1;
*tail = t;
}
return l;
}
void RooLinkedList::Streamer(TBuffer &R__b)
{
if (R__b.IsReading()) {
Version_t v = R__b.ReadVersion();
TObject::Streamer(R__b);
Int_t size ;
TObject* arg ;
R__b >> size ;
while(size--) {
R__b >> arg ;
Add(arg) ;
}
if (v>1 ) {
R__b >> _name ;
}
} else {
R__b.WriteVersion(RooLinkedList::IsA());
TObject::Streamer(R__b);
R__b << _size ;
RooLinkedListElem* ptr = _first;
while(ptr) {
R__b << ptr->_arg ;
ptr = ptr->_next ;
}
R__b << _name ;
}
}