Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
catalan_numbers Class Reference

computes and caches Catalan numbers More...

Collaboration diagram for catalan_numbers:
[legend]

Public Member Functions

value_type get (std::size_t n)
 computes the n-th Catalan number and updates the cache.
 

Private Types

using value_type = std::uint64_t
 

Private Member Functions

value_type compute_next ()
 
void add ()
 

Private Attributes

std::vector< value_typeknown {1, 1}
 

Detailed Description

computes and caches Catalan numbers

for assert for std::uint64_t for std::size_t for std::transform_reduce for std::vector

Member Function Documentation

◆ add()

void catalan_numbers::add ( )
inlineprivate
31{ known.push_back(this->compute_next()); }

◆ compute_next()

value_type catalan_numbers::compute_next ( )
inlineprivate
25 {
26 return std::transform_reduce(known.begin(), known.end(), known.rbegin(),
27 static_cast<value_type>(), std::plus<>(),
29 }

◆ get()

value_type catalan_numbers::get ( std::size_t n)
inline

computes the n-th Catalan number and updates the cache.

Returns
the n-th Catalan number
38 {
39 while (known.size() <= n) {
40 this->add();
41 }
42 return known[n];
43 }

Member Data Documentation

◆ known

std::vector<value_type> catalan_numbers::known {1, 1}
private
23{1, 1};

The documentation for this class was generated from the following file: