/*****************************************************************************
 * 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
// RooNameReg is a registry for 'const char*' name. For each unique
// name (which is not necessarily a unique pointer in the C++ standard),
// a unique pointer to a TNamed object is return that can be used for
// fast searches and comparisons.
// END_HTML
//

#include "RooFit.h"
#include "RooSentinel.h"

#include "RooNameReg.h"
#include "RooNameReg.h"
#include <iostream>
using namespace std ;

ClassImp(RooNameReg)
;

RooNameReg* RooNameReg::_instance = 0 ;


RooNameReg::RooNameReg(Int_t hashSize) : TNamed("RooNameReg","RooFit Name Registry"), _htable(hashSize) {} 

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

  _list.Delete() ;
}


//_____________________________________________________________________________
RooNameReg::RooNameReg(const RooNameReg& other) : TNamed(other)
{
  // Copy constructor
}


//_____________________________________________________________________________
RooNameReg& RooNameReg::instance()
{
  // Return reference to singleton instance

  if (_instance==0) {
    _instance = new RooNameReg(100000) ;  // there's only one of these, so we can afford to make it large
    RooSentinel::activate() ;
  }
  return *_instance ;
}


//_____________________________________________________________________________
void RooNameReg::cleanup()
{
  // Cleanup function called by atexit() handler installed by RooSentinel
  // to delete global objects on heap at end of program
  if(_instance) {
    delete _instance ;
    _instance = 0 ;
  }
}



//_____________________________________________________________________________
const TNamed* RooNameReg::constPtr(const char* inStr) 
{
  // Return a unique TNamed pointer for given C++ string

  // Handle null pointer case explicitly
  if (inStr==0) return 0 ;

//   cout << "RooNameReg::constPtr(inStr=" << inStr << ") _htable entries = " << _htable.entries() << endl ;

  // See if name is already registered ;
  TNamed* t = (TNamed*) _htable.find(inStr) ;
  if (t) return t ;

  // If not, register now
  t = new TNamed(inStr,inStr) ;
  _htable.add(t) ;
  _list.Add(t) ;
  
  return t ;
}



//_____________________________________________________________________________
const char* RooNameReg::constStr(const TNamed* namePtr) 
{
  // Return C++ string corresponding to given TNamed pointer

  if (namePtr) return namePtr->GetName() ;
  return 0 ;  
}


//_____________________________________________________________________________
const TNamed* RooNameReg::ptr(const char* stringPtr) 
{ 
  // Return a unique TNamed pointer for given C++ string
  if (stringPtr==0) return 0 ;
  return instance().constPtr(stringPtr) ; 
}


//_____________________________________________________________________________
const char* RooNameReg::str(const TNamed* ptr) 
{ 
  // Return C++ string corresponding to given TNamed pointer
  if (ptr==0) return 0 ;
  return instance().constStr(ptr) ; 
}


//_____________________________________________________________________________
const TNamed* RooNameReg::known(const char* inStr)
{
  // If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name).

  // Handle null pointer case explicitly
  if (inStr==0) return 0 ;
  if (_instance==0) return 0;
  return (const TNamed*) _instance->_htable.find(inStr) ;
}
 RooNameReg.cxx:1
 RooNameReg.cxx:2
 RooNameReg.cxx:3
 RooNameReg.cxx:4
 RooNameReg.cxx:5
 RooNameReg.cxx:6
 RooNameReg.cxx:7
 RooNameReg.cxx:8
 RooNameReg.cxx:9
 RooNameReg.cxx:10
 RooNameReg.cxx:11
 RooNameReg.cxx:12
 RooNameReg.cxx:13
 RooNameReg.cxx:14
 RooNameReg.cxx:15
 RooNameReg.cxx:16
 RooNameReg.cxx:17
 RooNameReg.cxx:18
 RooNameReg.cxx:19
 RooNameReg.cxx:20
 RooNameReg.cxx:21
 RooNameReg.cxx:22
 RooNameReg.cxx:23
 RooNameReg.cxx:24
 RooNameReg.cxx:25
 RooNameReg.cxx:26
 RooNameReg.cxx:27
 RooNameReg.cxx:28
 RooNameReg.cxx:29
 RooNameReg.cxx:30
 RooNameReg.cxx:31
 RooNameReg.cxx:32
 RooNameReg.cxx:33
 RooNameReg.cxx:34
 RooNameReg.cxx:35
 RooNameReg.cxx:36
 RooNameReg.cxx:37
 RooNameReg.cxx:38
 RooNameReg.cxx:39
 RooNameReg.cxx:40
 RooNameReg.cxx:41
 RooNameReg.cxx:42
 RooNameReg.cxx:43
 RooNameReg.cxx:44
 RooNameReg.cxx:45
 RooNameReg.cxx:46
 RooNameReg.cxx:47
 RooNameReg.cxx:48
 RooNameReg.cxx:49
 RooNameReg.cxx:50
 RooNameReg.cxx:51
 RooNameReg.cxx:52
 RooNameReg.cxx:53
 RooNameReg.cxx:54
 RooNameReg.cxx:55
 RooNameReg.cxx:56
 RooNameReg.cxx:57
 RooNameReg.cxx:58
 RooNameReg.cxx:59
 RooNameReg.cxx:60
 RooNameReg.cxx:61
 RooNameReg.cxx:62
 RooNameReg.cxx:63
 RooNameReg.cxx:64
 RooNameReg.cxx:65
 RooNameReg.cxx:66
 RooNameReg.cxx:67
 RooNameReg.cxx:68
 RooNameReg.cxx:69
 RooNameReg.cxx:70
 RooNameReg.cxx:71
 RooNameReg.cxx:72
 RooNameReg.cxx:73
 RooNameReg.cxx:74
 RooNameReg.cxx:75
 RooNameReg.cxx:76
 RooNameReg.cxx:77
 RooNameReg.cxx:78
 RooNameReg.cxx:79
 RooNameReg.cxx:80
 RooNameReg.cxx:81
 RooNameReg.cxx:82
 RooNameReg.cxx:83
 RooNameReg.cxx:84
 RooNameReg.cxx:85
 RooNameReg.cxx:86
 RooNameReg.cxx:87
 RooNameReg.cxx:88
 RooNameReg.cxx:89
 RooNameReg.cxx:90
 RooNameReg.cxx:91
 RooNameReg.cxx:92
 RooNameReg.cxx:93
 RooNameReg.cxx:94
 RooNameReg.cxx:95
 RooNameReg.cxx:96
 RooNameReg.cxx:97
 RooNameReg.cxx:98
 RooNameReg.cxx:99
 RooNameReg.cxx:100
 RooNameReg.cxx:101
 RooNameReg.cxx:102
 RooNameReg.cxx:103
 RooNameReg.cxx:104
 RooNameReg.cxx:105
 RooNameReg.cxx:106
 RooNameReg.cxx:107
 RooNameReg.cxx:108
 RooNameReg.cxx:109
 RooNameReg.cxx:110
 RooNameReg.cxx:111
 RooNameReg.cxx:112
 RooNameReg.cxx:113
 RooNameReg.cxx:114
 RooNameReg.cxx:115
 RooNameReg.cxx:116
 RooNameReg.cxx:117
 RooNameReg.cxx:118
 RooNameReg.cxx:119
 RooNameReg.cxx:120
 RooNameReg.cxx:121
 RooNameReg.cxx:122
 RooNameReg.cxx:123
 RooNameReg.cxx:124
 RooNameReg.cxx:125
 RooNameReg.cxx:126
 RooNameReg.cxx:127
 RooNameReg.cxx:128
 RooNameReg.cxx:129
 RooNameReg.cxx:130
 RooNameReg.cxx:131
 RooNameReg.cxx:132
 RooNameReg.cxx:133
 RooNameReg.cxx:134
 RooNameReg.cxx:135
 RooNameReg.cxx:136
 RooNameReg.cxx:137
 RooNameReg.cxx:138
 RooNameReg.cxx:139
 RooNameReg.cxx:140
 RooNameReg.cxx:141
 RooNameReg.cxx:142
 RooNameReg.cxx:143
 RooNameReg.cxx:144
 RooNameReg.cxx:145
 RooNameReg.cxx:146