00001 /***************************************************************************** 00002 * Project: RooFit * 00003 * Package: RooFitCore * 00004 * @(#)root/roofitcore:$Id: RooRefCountList.cxx 36209 2010-10-08 21:37:36Z wouter $ 00005 * Authors: * 00006 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu * 00007 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu * 00008 * * 00009 * Copyright (c) 2000-2005, Regents of the University of California * 00010 * and Stanford University. All rights reserved. * 00011 * * 00012 * Redistribution and use in source and binary forms, * 00013 * with or without modification, are permitted according to the terms * 00014 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) * 00015 *****************************************************************************/ 00016 00017 ////////////////////////////////////////////////////////////////////////////// 00018 // 00019 // BEGIN_HTML 00020 // A RooRefCountList is a RooLinkedList that keeps a reference counter 00021 // with each added node. Multiple Add()s of the same object will increase 00022 // the counter instead of adding multiple copies. Remove() decrements the 00023 // reference count until zero, when the object is actually removed. 00024 // END_HTML 00025 // 00026 00027 #include "RooFit.h" 00028 00029 #include "RooRefCountList.h" 00030 #include "RooRefCountList.h" 00031 00032 #include "Riostream.h" 00033 #include <stdlib.h> 00034 00035 ClassImp(RooRefCountList) 00036 ; 00037 00038 00039 00040 //_____________________________________________________________________________ 00041 RooRefCountList::RooRefCountList() 00042 : RooLinkedList(17) 00043 { 00044 // Default constructor construct lists with initial hash table size of 17 00045 } 00046 00047 00048 00049 //_____________________________________________________________________________ 00050 void RooRefCountList::Add(TObject* obj, Int_t count) 00051 { 00052 // Add object to list with given reference count increment 00053 // List takes ownership of object. 00054 00055 // Check if we already have it 00056 TObject* listObj = FindObject(obj) ; 00057 if (!listObj) { 00058 // Add to list with reference count 00059 RooLinkedList::Add(obj, count) ; 00060 //cout << "RooRefCountList::AddLast(" << obj << ") adding object" << endl ; 00061 } else { 00062 RooLinkedListElem* link = findLink(obj) ; 00063 if(link) { 00064 while(count--) link->incRefCount() ; 00065 } 00066 //cout << "RooRefCountList::AddLast(" << obj << ") incremented reference count to " << link->refCount() << endl ; 00067 } 00068 00069 } 00070 00071 00072 00073 //_____________________________________________________________________________ 00074 Bool_t RooRefCountList::Remove(TObject* obj) 00075 { 00076 // Remove object from list and if reference count 00077 // reaches zero delete object itself as well. 00078 00079 RooLinkedListElem* link = findLink(obj) ; 00080 if (!link) { 00081 return 0 ; 00082 } else { 00083 if (link->decRefCount()==0) { 00084 //cout << "RooRefCountList::AddLast(" << obj << ") removed object" << endl ; 00085 return RooLinkedList::Remove(obj) ; 00086 } 00087 //cout << "RooRefCountList::AddLast(" << obj << ") decremented reference count to " << link->refCount() << endl ; 00088 } 00089 return 0 ; 00090 } 00091 00092 00093 00094 //_____________________________________________________________________________ 00095 Bool_t RooRefCountList::RemoveAll(TObject* obj) 00096 { 00097 // Remove object from list and delete object itself 00098 // regardless of reference count 00099 00100 return RooLinkedList::Remove(obj) ; 00101 } 00102 00103 00104 00105 //_____________________________________________________________________________ 00106 Int_t RooRefCountList::refCount(TObject* obj) 00107 { 00108 // Return reference count associated with 'obj' 00109 00110 RooLinkedListElem* link = findLink(obj) ; 00111 if (!link) { 00112 return 0 ; 00113 } else { 00114 return link->refCount() ; 00115 } 00116 }