/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 *    File: $Id: RooNormSetCache.h,v 1.12 2007/08/09 19:55:47 wouter Exp $
 * 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)             *
 *****************************************************************************/
#ifndef ROO_NORMSET_CACHE
#define ROO_NORMSET_CACHE

#include <utility>
#include <vector>
#include <map>

#include "Rtypes.h"
#include "Riosfwd.h"
#include "RooNameSet.h"

class RooAbsArg;
class RooArgSet;

typedef RooArgSet* pRooArgSet ;

class RooNormSetCache {
protected:
  typedef std::pair<const RooArgSet*, const RooArgSet*> Pair;
  struct PairCmp {
      inline bool operator()(const Pair& a, const Pair& b) const
      {
	  if (a.first < b.first) return true;
	  if (b.first < a.first) return false;
	  return a.second < b.second;
      }
  };
  typedef std::vector<Pair> PairVectType;
  typedef std::map<Pair, ULong_t> PairIdxMapType;

public:
  RooNormSetCache(ULong_t max = 32);
  virtual ~RooNormSetCache();

  void add(const RooArgSet* set1, const RooArgSet* set2 = 0);

  inline Int_t index(const RooArgSet* set1, const RooArgSet* set2 = 0,
      const TNamed* set2RangeName = 0)
  {
    // Match range name first
    if (set2RangeName != _set2RangeName) return -1;
    const Pair pair(set1, set2);
    PairIdxMapType::const_iterator it = _pairToIdx.lower_bound(pair);
    if (_pairToIdx.end() != it &&
	!PairCmp()(it->first, pair) && !PairCmp()(pair, it->first))
      return it->second;
    return -1;
  }

  inline Bool_t contains(const RooArgSet* set1, const RooArgSet* set2 = 0,
      const TNamed* set2RangeName = 0)
  { return (index(set1,set2,set2RangeName) >= 0); }

  inline Bool_t containsSet1(const RooArgSet* set1)
  {
    const Pair pair(set1, (const RooArgSet*)0);
    PairIdxMapType::const_iterator it = _pairToIdx.lower_bound(pair);
    if (_pairToIdx.end() != it && it->first.first == set1)
      return kTRUE;
    return kFALSE;
  }

  const RooArgSet* lastSet1() const { return _pairs.empty()?0:_pairs.back().first; }
  const RooArgSet* lastSet2() const { return _pairs.empty()?0:_pairs.back().second; }
  const RooNameSet& nameSet1() const { return _name1; }
  const RooNameSet& nameSet2() const { return _name2; }

  Bool_t autoCache(const RooAbsArg* self, const RooArgSet* set1,
      const RooArgSet* set2 = 0, const TNamed* set2RangeName = 0,
      Bool_t autoRefill = kTRUE);
    
  void clear();
  Int_t entries() const { return _pairs.size(); }

  void initialize(const RooNormSetCache& other) { clear(); *this = other; }

protected:

  PairVectType _pairs; //!
  PairIdxMapType _pairToIdx; //!
  ULong_t _max; //!
  ULong_t _next; //!

  RooNameSet _name1;   //!
  RooNameSet _name2;   //!
  TNamed*    _set2RangeName; //!

  ClassDef(RooNormSetCache, 0) // Management tool for tracking sets of similar integration/normalization sets
};

#endif 
 RooNormSetCache.h:1
 RooNormSetCache.h:2
 RooNormSetCache.h:3
 RooNormSetCache.h:4
 RooNormSetCache.h:5
 RooNormSetCache.h:6
 RooNormSetCache.h:7
 RooNormSetCache.h:8
 RooNormSetCache.h:9
 RooNormSetCache.h:10
 RooNormSetCache.h:11
 RooNormSetCache.h:12
 RooNormSetCache.h:13
 RooNormSetCache.h:14
 RooNormSetCache.h:15
 RooNormSetCache.h:16
 RooNormSetCache.h:17
 RooNormSetCache.h:18
 RooNormSetCache.h:19
 RooNormSetCache.h:20
 RooNormSetCache.h:21
 RooNormSetCache.h:22
 RooNormSetCache.h:23
 RooNormSetCache.h:24
 RooNormSetCache.h:25
 RooNormSetCache.h:26
 RooNormSetCache.h:27
 RooNormSetCache.h:28
 RooNormSetCache.h:29
 RooNormSetCache.h:30
 RooNormSetCache.h:31
 RooNormSetCache.h:32
 RooNormSetCache.h:33
 RooNormSetCache.h:34
 RooNormSetCache.h:35
 RooNormSetCache.h:36
 RooNormSetCache.h:37
 RooNormSetCache.h:38
 RooNormSetCache.h:39
 RooNormSetCache.h:40
 RooNormSetCache.h:41
 RooNormSetCache.h:42
 RooNormSetCache.h:43
 RooNormSetCache.h:44
 RooNormSetCache.h:45
 RooNormSetCache.h:46
 RooNormSetCache.h:47
 RooNormSetCache.h:48
 RooNormSetCache.h:49
 RooNormSetCache.h:50
 RooNormSetCache.h:51
 RooNormSetCache.h:52
 RooNormSetCache.h:53
 RooNormSetCache.h:54
 RooNormSetCache.h:55
 RooNormSetCache.h:56
 RooNormSetCache.h:57
 RooNormSetCache.h:58
 RooNormSetCache.h:59
 RooNormSetCache.h:60
 RooNormSetCache.h:61
 RooNormSetCache.h:62
 RooNormSetCache.h:63
 RooNormSetCache.h:64
 RooNormSetCache.h:65
 RooNormSetCache.h:66
 RooNormSetCache.h:67
 RooNormSetCache.h:68
 RooNormSetCache.h:69
 RooNormSetCache.h:70
 RooNormSetCache.h:71
 RooNormSetCache.h:72
 RooNormSetCache.h:73
 RooNormSetCache.h:74
 RooNormSetCache.h:75
 RooNormSetCache.h:76
 RooNormSetCache.h:77
 RooNormSetCache.h:78
 RooNormSetCache.h:79
 RooNormSetCache.h:80
 RooNormSetCache.h:81
 RooNormSetCache.h:82
 RooNormSetCache.h:83
 RooNormSetCache.h:84
 RooNormSetCache.h:85
 RooNormSetCache.h:86
 RooNormSetCache.h:87
 RooNormSetCache.h:88
 RooNormSetCache.h:89
 RooNormSetCache.h:90
 RooNormSetCache.h:91
 RooNormSetCache.h:92
 RooNormSetCache.h:93
 RooNormSetCache.h:94
 RooNormSetCache.h:95
 RooNormSetCache.h:96
 RooNormSetCache.h:97
 RooNormSetCache.h:98
 RooNormSetCache.h:99
 RooNormSetCache.h:100
 RooNormSetCache.h:101
 RooNormSetCache.h:102
 RooNormSetCache.h:103
 RooNormSetCache.h:104
 RooNormSetCache.h:105
 RooNormSetCache.h:106