/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id$
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// Class RooSharedPropertiesList maintains the properties of RooRealVars
// and RooCategories that are clones of each other.
// END_HTML
//

#include "RooFit.h"
#include "RooSharedPropertiesList.h"
#include "RooSharedProperties.h"
#include "RooLinkedListIter.h"
#include "TIterator.h"
#include "RooMsgService.h"
#include "Riostream.h"
using std::cout ;
using std::endl ;

using namespace std;

ClassImp(RooSharedPropertiesList)
;



//_____________________________________________________________________________
RooSharedPropertiesList::RooSharedPropertiesList() 
{
  // Constructor
   _propList.setHashTableSize(1000);
} 



//_____________________________________________________________________________
RooSharedPropertiesList::~RooSharedPropertiesList() 
{
  // Destructor

  // Delete all objects in property list
  RooFIter iter = _propList.fwdIterator() ;
  RooSharedProperties* prop ;
  while((prop=(RooSharedProperties*)iter.next())) {
    delete prop ;
  }
} 



//_____________________________________________________________________________
RooSharedProperties* RooSharedPropertiesList::registerProperties(RooSharedProperties* prop, Bool_t canDeleteIncoming) 
{
  // Register property into list and take ownership. 
  //
  // If an existing entry has a UUID that identical to that of the argument prop, 
  // the argument prop is deleted and a pointer to the already stored is returned to
  // eliminate the duplication of instances with a unique identity.
  //
  // The caller should therefore not refer anymore to the input argument pointer as
  // as the object cannot be assumed to be live.

  if (prop==0) {
    oocoutE((TObject*)0,InputArguments) << "RooSharedPropertiesList::ERROR null pointer!:" << endl ;
    return 0 ;
  }


  // If the reference count is non-zero, it is already in the list, so no need
  // to look it up anymore
  if (prop->inSharedList()) {
    prop->increaseRefCount() ;
    return prop ;
  }

  //std::cout << "REGISTER properties " << prop->asString() << " - list size = " << _propList.GetSize() << std::endl;
  RooSharedProperties* tmp ;
  
  std::map<std::string, RooSharedProperties *>::iterator it; 

  it = _newPropList.find( std::string(prop->asString()) ); 
  if (it != _newPropList.end() ) {
     tmp = it->second; 
     if (tmp != prop) { 
        // found another instance with same UUID
        if (canDeleteIncoming) delete prop; 
     }
     tmp->increaseRefCount(); 
     return tmp;
  }
  prop->setInSharedList() ;
  prop->increaseRefCount() ;
  _newPropList[ std::string(prop->asString()) ] = prop; 

  return prop ;
}



//_____________________________________________________________________________
void RooSharedPropertiesList::unregisterProperties(RooSharedProperties* prop) 
{
  // Decrease reference count of property. If reference count is at zero,
  // delete the propery

  prop->decreaseRefCount() ;

  if (prop->refCount()==0) {
    _propList.Remove(prop) ;

    std::map<std::string, RooSharedProperties *>::iterator it; 
    it = _newPropList.find( std::string(prop->asString()) ); 
    if (it != _newPropList.end() )  _newPropList.erase(it);
    
    // We own object if ref-counted list. If count drops to zero, delete object
    delete prop ;
  }
  
}


 RooSharedPropertiesList.cxx:1
 RooSharedPropertiesList.cxx:2
 RooSharedPropertiesList.cxx:3
 RooSharedPropertiesList.cxx:4
 RooSharedPropertiesList.cxx:5
 RooSharedPropertiesList.cxx:6
 RooSharedPropertiesList.cxx:7
 RooSharedPropertiesList.cxx:8
 RooSharedPropertiesList.cxx:9
 RooSharedPropertiesList.cxx:10
 RooSharedPropertiesList.cxx:11
 RooSharedPropertiesList.cxx:12
 RooSharedPropertiesList.cxx:13
 RooSharedPropertiesList.cxx:14
 RooSharedPropertiesList.cxx:15
 RooSharedPropertiesList.cxx:16
 RooSharedPropertiesList.cxx:17
 RooSharedPropertiesList.cxx:18
 RooSharedPropertiesList.cxx:19
 RooSharedPropertiesList.cxx:20
 RooSharedPropertiesList.cxx:21
 RooSharedPropertiesList.cxx:22
 RooSharedPropertiesList.cxx:23
 RooSharedPropertiesList.cxx:24
 RooSharedPropertiesList.cxx:25
 RooSharedPropertiesList.cxx:26
 RooSharedPropertiesList.cxx:27
 RooSharedPropertiesList.cxx:28
 RooSharedPropertiesList.cxx:29
 RooSharedPropertiesList.cxx:30
 RooSharedPropertiesList.cxx:31
 RooSharedPropertiesList.cxx:32
 RooSharedPropertiesList.cxx:33
 RooSharedPropertiesList.cxx:34
 RooSharedPropertiesList.cxx:35
 RooSharedPropertiesList.cxx:36
 RooSharedPropertiesList.cxx:37
 RooSharedPropertiesList.cxx:38
 RooSharedPropertiesList.cxx:39
 RooSharedPropertiesList.cxx:40
 RooSharedPropertiesList.cxx:41
 RooSharedPropertiesList.cxx:42
 RooSharedPropertiesList.cxx:43
 RooSharedPropertiesList.cxx:44
 RooSharedPropertiesList.cxx:45
 RooSharedPropertiesList.cxx:46
 RooSharedPropertiesList.cxx:47
 RooSharedPropertiesList.cxx:48
 RooSharedPropertiesList.cxx:49
 RooSharedPropertiesList.cxx:50
 RooSharedPropertiesList.cxx:51
 RooSharedPropertiesList.cxx:52
 RooSharedPropertiesList.cxx:53
 RooSharedPropertiesList.cxx:54
 RooSharedPropertiesList.cxx:55
 RooSharedPropertiesList.cxx:56
 RooSharedPropertiesList.cxx:57
 RooSharedPropertiesList.cxx:58
 RooSharedPropertiesList.cxx:59
 RooSharedPropertiesList.cxx:60
 RooSharedPropertiesList.cxx:61
 RooSharedPropertiesList.cxx:62
 RooSharedPropertiesList.cxx:63
 RooSharedPropertiesList.cxx:64
 RooSharedPropertiesList.cxx:65
 RooSharedPropertiesList.cxx:66
 RooSharedPropertiesList.cxx:67
 RooSharedPropertiesList.cxx:68
 RooSharedPropertiesList.cxx:69
 RooSharedPropertiesList.cxx:70
 RooSharedPropertiesList.cxx:71
 RooSharedPropertiesList.cxx:72
 RooSharedPropertiesList.cxx:73
 RooSharedPropertiesList.cxx:74
 RooSharedPropertiesList.cxx:75
 RooSharedPropertiesList.cxx:76
 RooSharedPropertiesList.cxx:77
 RooSharedPropertiesList.cxx:78
 RooSharedPropertiesList.cxx:79
 RooSharedPropertiesList.cxx:80
 RooSharedPropertiesList.cxx:81
 RooSharedPropertiesList.cxx:82
 RooSharedPropertiesList.cxx:83
 RooSharedPropertiesList.cxx:84
 RooSharedPropertiesList.cxx:85
 RooSharedPropertiesList.cxx:86
 RooSharedPropertiesList.cxx:87
 RooSharedPropertiesList.cxx:88
 RooSharedPropertiesList.cxx:89
 RooSharedPropertiesList.cxx:90
 RooSharedPropertiesList.cxx:91
 RooSharedPropertiesList.cxx:92
 RooSharedPropertiesList.cxx:93
 RooSharedPropertiesList.cxx:94
 RooSharedPropertiesList.cxx:95
 RooSharedPropertiesList.cxx:96
 RooSharedPropertiesList.cxx:97
 RooSharedPropertiesList.cxx:98
 RooSharedPropertiesList.cxx:99
 RooSharedPropertiesList.cxx:100
 RooSharedPropertiesList.cxx:101
 RooSharedPropertiesList.cxx:102
 RooSharedPropertiesList.cxx:103
 RooSharedPropertiesList.cxx:104
 RooSharedPropertiesList.cxx:105
 RooSharedPropertiesList.cxx:106
 RooSharedPropertiesList.cxx:107
 RooSharedPropertiesList.cxx:108
 RooSharedPropertiesList.cxx:109
 RooSharedPropertiesList.cxx:110
 RooSharedPropertiesList.cxx:111
 RooSharedPropertiesList.cxx:112
 RooSharedPropertiesList.cxx:113
 RooSharedPropertiesList.cxx:114
 RooSharedPropertiesList.cxx:115
 RooSharedPropertiesList.cxx:116
 RooSharedPropertiesList.cxx:117
 RooSharedPropertiesList.cxx:118
 RooSharedPropertiesList.cxx:119
 RooSharedPropertiesList.cxx:120
 RooSharedPropertiesList.cxx:121
 RooSharedPropertiesList.cxx:122
 RooSharedPropertiesList.cxx:123
 RooSharedPropertiesList.cxx:124
 RooSharedPropertiesList.cxx:125
 RooSharedPropertiesList.cxx:126
 RooSharedPropertiesList.cxx:127
 RooSharedPropertiesList.cxx:128
 RooSharedPropertiesList.cxx:129
 RooSharedPropertiesList.cxx:130
 RooSharedPropertiesList.cxx:131
 RooSharedPropertiesList.cxx:132
 RooSharedPropertiesList.cxx:133
 RooSharedPropertiesList.cxx:134
 RooSharedPropertiesList.cxx:135
 RooSharedPropertiesList.cxx:136