BinomialCoefficients.h
Go to the documentation of this file.
1 #ifndef _utl_BinomialCache_h_
2 #define _utl_BinomialCache_h_
3 
4 #include <vector>
5 #include <utl/Singleton.h>
6 
7 
8 namespace utl {
9 
38  class BinomialCoefficients : public Singleton<BinomialCoefficients> {
39  public:
40  BinomialCoefficients(const int initialN = 3)
41  : fMaxN(0), fCoefficients(1, 1) { Generate(initialN); }
42 
43  double
44  operator()(const int n, const int k)
45  {
46  if (k < 0 || k > n)
47  return 0;
48  if (n > fMaxN)
49  Generate(n);
50  return GetUnchecked(n, k);
51  }
52 
53  private:
54  double& GetUnchecked(const int n, const int k)
55  { return fCoefficients[n*(n+1)/2 + k]; }
56 
57  void Generate(const int n);
58 
59  int fMaxN;
60  std::vector<double> fCoefficients;
61  };
62 
63 }
64 
65 
66 #endif
double operator()(const int n, const int k)
double & GetUnchecked(const int n, const int k)
std::vector< double > fCoefficients
BinomialCoefficients(const int initialN=3)
Calculates binomial coefficients and caches the results.
Curiously Recurring Template Pattern (CRTP) for Meyers singleton.
Definition: Singleton.h:36

, generated on Tue Sep 26 2023.