diff --git a/bit_manipulation/count_of_set_bits.cpp b/bit_manipulation/count_of_set_bits.cpp index 497346a53..f2d802061 100644 --- a/bit_manipulation/count_of_set_bits.cpp +++ b/bit_manipulation/count_of_set_bits.cpp @@ -5,7 +5,8 @@ * integer. * * @details - * We are given an integer number. We need to calculate the number of set bits in it. + * We are given an integer number. We need to calculate the number of set bits + * in it. * * A binary number consists of two digits. They are 0 & 1. Digit 1 is known as * set bit in computer terms. @@ -15,7 +16,7 @@ * @author [Prashant Thakur](https://github.com/prashant-th18) */ #include /// for assert -#include /// for IO operations +#include /// for IO operations /** * @namespace bit_manipulation * @brief Bit manipulation algorithms @@ -33,21 +34,21 @@ namespace count_of_set_bits { * @param n is the number whose set bit will be counted * @returns total number of set-bits in the binary representation of number `n` */ -std::uint64_t countSetBits(std :: int64_t n) { // int64_t is preferred over int so that - // no Overflow can be there. +std::uint64_t countSetBits( + std ::int64_t n) { // int64_t is preferred over int so that + // no Overflow can be there. - int count = 0; // "count" variable is used to count number of set-bits('1') in - // binary representation of number 'n' - while (n != 0) - { + int count = 0; // "count" variable is used to count number of set-bits('1') + // in binary representation of number 'n' + while (n != 0) { ++count; n = (n & (n - 1)); } return count; // Why this algorithm is better than the standard one? // Because this algorithm runs the same number of times as the number of - // set-bits in it. Means if my number is having "3" set bits, then this while loop - // will run only "3" times!! + // set-bits in it. Means if my number is having "3" set bits, then this + // while loop will run only "3" times!! } } // namespace count_of_set_bits } // namespace bit_manipulation diff --git a/ciphers/atbash_cipher.cpp b/ciphers/atbash_cipher.cpp index 04c330598..4f0d793f2 100644 --- a/ciphers/atbash_cipher.cpp +++ b/ciphers/atbash_cipher.cpp @@ -22,7 +22,8 @@ */ namespace ciphers { /** \namespace atbash - * \brief Functions for the [Atbash Cipher](https://en.wikipedia.org/wiki/Atbash) implementation + * \brief Functions for the [Atbash + * Cipher](https://en.wikipedia.org/wiki/Atbash) implementation */ namespace atbash { std::map atbash_cipher_map = { @@ -43,7 +44,7 @@ std::map atbash_cipher_map = { * @param text Plaintext to be encrypted * @returns encoded or decoded string */ -std::string atbash_cipher(std::string text) { +std::string atbash_cipher(const std::string& text) { std::string result; for (char letter : text) { result += atbash_cipher_map[letter]; diff --git a/data_structures/stack_using_queue.cpp b/data_structures/stack_using_queue.cpp index 54a81a135..bd6a60e2b 100644 --- a/data_structures/stack_using_queue.cpp +++ b/data_structures/stack_using_queue.cpp @@ -3,13 +3,14 @@ * @details * Using 2 Queues inside the Stack class, we can easily implement Stack * data structure with heavy computation in push function. - * - * References used: [StudyTonight](https://www.studytonight.com/data-structures/stack-using-queue) + * + * References used: + * [StudyTonight](https://www.studytonight.com/data-structures/stack-using-queue) * @author [tushar2407](https://github.com/tushar2407) */ -#include /// for IO operations -#include /// for queue data structure -#include /// for assert +#include /// for assert +#include /// for IO operations +#include /// for queue data structure /** * @namespace data_strcutres @@ -18,66 +19,59 @@ namespace data_structures { /** * @namespace stack_using_queue - * @brief Functions for the [Stack Using Queue](https://www.studytonight.com/data-structures/stack-using-queue) implementation + * @brief Functions for the [Stack Using + * Queue](https://www.studytonight.com/data-structures/stack-using-queue) + * implementation */ namespace stack_using_queue { +/** + * @brief Stack Class implementation for basic methods of Stack Data Structure. + */ +struct Stack { + std::queue main_q; ///< stores the current state of the stack + std::queue auxiliary_q; ///< used to carry out intermediate + ///< operations to implement stack + uint32_t current_size = 0; ///< stores the current size of the stack + /** - * @brief Stack Class implementation for basic methods of Stack Data Structure. + * Returns the top most element of the stack + * @returns top element of the queue */ - struct Stack - { - std::queue main_q; ///< stores the current state of the stack - std::queue auxiliary_q; ///< used to carry out intermediate operations to implement stack - uint32_t current_size = 0; ///< stores the current size of the stack - - /** - * Returns the top most element of the stack - * @returns top element of the queue - */ - int top() - { - return main_q.front(); - } + int top() { return main_q.front(); } - /** - * @brief Inserts an element to the top of the stack. - * @param val the element that will be inserted into the stack - * @returns void - */ - void push(int val) - { - auxiliary_q.push(val); - while(!main_q.empty()) - { - auxiliary_q.push(main_q.front()); - main_q.pop(); - } - swap(main_q, auxiliary_q); - current_size++; - } - - /** - * @brief Removes the topmost element from the stack - * @returns void - */ - void pop() - { - if(main_q.empty()) { - return; - } + /** + * @brief Inserts an element to the top of the stack. + * @param val the element that will be inserted into the stack + * @returns void + */ + void push(int val) { + auxiliary_q.push(val); + while (!main_q.empty()) { + auxiliary_q.push(main_q.front()); main_q.pop(); - current_size--; } + swap(main_q, auxiliary_q); + current_size++; + } - /** - * @brief Utility function to return the current size of the stack - * @returns current size of stack - */ - int size() - { - return current_size; + /** + * @brief Removes the topmost element from the stack + * @returns void + */ + void pop() { + if (main_q.empty()) { + return; } - }; + main_q.pop(); + current_size--; + } + + /** + * @brief Utility function to return the current size of the stack + * @returns current size of stack + */ + int size() { return current_size; } +}; } // namespace stack_using_queue } // namespace data_structures @@ -85,30 +79,29 @@ namespace stack_using_queue { * @brief Self-test implementations * @returns void */ -static void test() -{ +static void test() { data_structures::stack_using_queue::Stack s; - s.push(1); /// insert an element into the stack - s.push(2); /// insert an element into the stack - s.push(3); /// insert an element into the stack - - assert(s.size()==3); /// size should be 3 - - assert(s.top()==3); /// topmost element in the stack should be 3 - - s.pop(); /// remove the topmost element from the stack - assert(s.top()==2); /// topmost element in the stack should now be 2 - - s.pop(); /// remove the topmost element from the stack - assert(s.top()==1); - - s.push(5); /// insert an element into the stack - assert(s.top()==5); /// topmost element in the stack should now be 5 - - s.pop(); /// remove the topmost element from the stack - assert(s.top()==1); /// topmost element in the stack should now be 1 - - assert(s.size()==1); /// size should be 1 + s.push(1); /// insert an element into the stack + s.push(2); /// insert an element into the stack + s.push(3); /// insert an element into the stack + + assert(s.size() == 3); /// size should be 3 + + assert(s.top() == 3); /// topmost element in the stack should be 3 + + s.pop(); /// remove the topmost element from the stack + assert(s.top() == 2); /// topmost element in the stack should now be 2 + + s.pop(); /// remove the topmost element from the stack + assert(s.top() == 1); + + s.push(5); /// insert an element into the stack + assert(s.top() == 5); /// topmost element in the stack should now be 5 + + s.pop(); /// remove the topmost element from the stack + assert(s.top() == 1); /// topmost element in the stack should now be 1 + + assert(s.size() == 1); /// size should be 1 } /** @@ -119,8 +112,7 @@ static void test() * declared above. * @returns 0 on exit */ -int main() -{ +int main() { test(); // run self-test implementations return 0; } diff --git a/math/integral_approximation2.cpp b/math/integral_approximation2.cpp index 706672d12..eed605e03 100644 --- a/math/integral_approximation2.cpp +++ b/math/integral_approximation2.cpp @@ -1,29 +1,34 @@ /** * @file - * @brief [Monte Carlo Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration) + * @brief [Monte Carlo + * Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration) * * @details - * In mathematics, Monte Carlo integration is a technique for numerical integration using random numbers. - * It is a particular Monte Carlo method that numerically computes a definite integral. - * While other algorithms usually evaluate the integrand at a regular grid, Monte Carlo randomly chooses points at which the integrand is evaluated. - * This method is particularly useful for higher-dimensional integrals. + * In mathematics, Monte Carlo integration is a technique for numerical + * integration using random numbers. It is a particular Monte Carlo method that + * numerically computes a definite integral. While other algorithms usually + * evaluate the integrand at a regular grid, Monte Carlo randomly chooses points + * at which the integrand is evaluated. This method is particularly useful for + * higher-dimensional integrals. * * This implementation supports arbitrary pdfs. - * These pdfs are sampled using the [Metropolis-Hastings algorithm](https://en.wikipedia.org/wiki/Metropolis–Hastings_algorithm). - * This can be swapped out by every other sampling techniques for example the inverse method. - * Metropolis-Hastings was chosen because it is the most general and can also be extended for a higher dimensional sampling space. + * These pdfs are sampled using the [Metropolis-Hastings + * algorithm](https://en.wikipedia.org/wiki/Metropolis–Hastings_algorithm). This + * can be swapped out by every other sampling techniques for example the inverse + * method. Metropolis-Hastings was chosen because it is the most general and can + * also be extended for a higher dimensional sampling space. * * @author [Domenic Zingsheim](https://github.com/DerAndereDomenic) */ -#define _USE_MATH_DEFINES /// for M_PI on windows -#include /// for math functions -#include /// for fixed size data types -#include /// for time to initialize rng -#include /// for function pointers -#include /// for std::cout -#include /// for random number generation -#include /// for std::vector +#define _USE_MATH_DEFINES /// for M_PI on windows +#include /// for math functions +#include /// for fixed size data types +#include /// for time to initialize rng +#include /// for function pointers +#include /// for std::cout +#include /// for random number generation +#include /// for std::vector /** * @namespace math @@ -32,25 +37,34 @@ namespace math { /** * @namespace monte_carlo - * @brief Functions for the [Monte Carlo Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration) implementation + * @brief Functions for the [Monte Carlo + * Integration](https://en.wikipedia.org/wiki/Monte_Carlo_integration) + * implementation */ namespace monte_carlo { -using Function = std::function; /// short-hand for std::functions used in this implementation +using Function = std::function; /// short-hand for std::functions used in this implementation /** * @brief Generate samples according to some pdf - * @details This function uses Metropolis-Hastings to generate random numbers. It generates a sequence of random numbers by using a markov chain. - * Therefore, we need to define a start_point and the number of samples we want to generate. - * Because the first samples generated by the markov chain may not be distributed according to the given pdf, one can specify how many samples + * @details This function uses Metropolis-Hastings to generate random numbers. + * It generates a sequence of random numbers by using a markov chain. Therefore, + * we need to define a start_point and the number of samples we want to + * generate. Because the first samples generated by the markov chain may not be + * distributed according to the given pdf, one can specify how many samples * should be discarded before storing samples. * @param start_point The starting point of the markov chain * @param pdf The pdf to sample * @param num_samples The number of samples to generate * @param discard How many samples should be discarded at the start - * @returns A vector of size num_samples with samples distributed according to the pdf + * @returns A vector of size num_samples with samples distributed according to + * the pdf */ -std::vector generate_samples(const double& start_point, const Function& pdf, const uint32_t& num_samples, const uint32_t& discard = 100000) { +std::vector generate_samples(const double& start_point, + const Function& pdf, + const uint32_t& num_samples, + const uint32_t& discard = 100000) { std::vector samples; samples.reserve(num_samples); @@ -61,19 +75,19 @@ std::vector generate_samples(const double& start_point, const Function& std::normal_distribution normal(0.0, 1.0); generator.seed(time(nullptr)); - for(uint32_t t = 0; t < num_samples + discard; ++t) { + for (uint32_t t = 0; t < num_samples + discard; ++t) { // Generate a new proposal according to some mutation strategy. // This is arbitrary and can be swapped. double x_dash = normal(generator) + x_t; - double acceptance_probability = std::min(pdf(x_dash)/pdf(x_t), 1.0); + double acceptance_probability = std::min(pdf(x_dash) / pdf(x_t), 1.0); double u = uniform(generator); // Accept "new state" according to the acceptance_probability - if(u <= acceptance_probability) { + if (u <= acceptance_probability) { x_t = x_dash; } - if(t >= discard) { + if (t >= discard) { samples.push_back(x_t); } } @@ -92,13 +106,17 @@ std::vector generate_samples(const double& start_point, const Function& * @param function The function to integrate * @param pdf The pdf to sample * @param num_samples The number of samples used to approximate the integral - * @returns The approximation of the integral according to 1/N \sum_{i}^N f(x_i) / p(x_i) + * @returns The approximation of the integral according to 1/N \sum_{i}^N f(x_i) + * / p(x_i) */ -double integral_monte_carlo(const double& start_point, const Function& function, const Function& pdf, const uint32_t& num_samples = 1000000) { +double integral_monte_carlo(const double& start_point, const Function& function, + const Function& pdf, + const uint32_t& num_samples = 1000000) { double integral = 0.0; - std::vector samples = generate_samples(start_point, pdf, num_samples); + std::vector samples = + generate_samples(start_point, pdf, num_samples); - for(double sample : samples) { + for (double sample : samples) { integral += function(sample) / pdf(sample); } @@ -113,8 +131,13 @@ double integral_monte_carlo(const double& start_point, const Function& function, * @returns void */ static void test() { - std::cout << "Disclaimer: Because this is a randomized algorithm," << std::endl; - std::cout << "it may happen that singular samples deviate from the true result." << std::endl << std::endl;; + std::cout << "Disclaimer: Because this is a randomized algorithm," + << std::endl; + std::cout + << "it may happen that singular samples deviate from the true result." + << std::endl + << std::endl; + ; math::monte_carlo::Function f; math::monte_carlo::Function pdf; @@ -122,60 +145,58 @@ static void test() { double lower_bound = 0, upper_bound = 0; /* \int_{-2}^{2} -x^2 + 4 dx */ - f = [&](double& x) { - return -x*x + 4.0; - }; + f = [&](double& x) { return -x * x + 4.0; }; lower_bound = -2.0; upper_bound = 2.0; pdf = [&](double& x) { - if(x >= lower_bound && x <= -1.0) { + if (x >= lower_bound && x <= -1.0) { return 0.1; } - if(x <= upper_bound && x >= 1.0) { + if (x <= upper_bound && x >= 1.0) { return 0.1; } - if(x > -1.0 && x < 1.0) { + if (x > -1.0 && x < 1.0) { return 0.4; } return 0.0; }; - integral = math::monte_carlo::integral_monte_carlo((upper_bound - lower_bound) / 2.0, f, pdf); + integral = math::monte_carlo::integral_monte_carlo( + (upper_bound - lower_bound) / 2.0, f, pdf); - std::cout << "This number should be close to 10.666666: " << integral << std::endl; + std::cout << "This number should be close to 10.666666: " << integral + << std::endl; /* \int_{0}^{1} e^x dx */ - f = [&](double& x) { - return std::exp(x); - }; + f = [&](double& x) { return std::exp(x); }; lower_bound = 0.0; upper_bound = 1.0; pdf = [&](double& x) { - if(x >= lower_bound && x <= 0.2) { + if (x >= lower_bound && x <= 0.2) { return 0.1; } - if(x > 0.2 && x <= 0.4) { + if (x > 0.2 && x <= 0.4) { return 0.4; } - if(x > 0.4 && x < upper_bound) { + if (x > 0.4 && x < upper_bound) { return 1.5; } return 0.0; }; - integral = math::monte_carlo::integral_monte_carlo((upper_bound - lower_bound) / 2.0, f, pdf); + integral = math::monte_carlo::integral_monte_carlo( + (upper_bound - lower_bound) / 2.0, f, pdf); - std::cout << "This number should be close to 1.7182818: " << integral << std::endl; + std::cout << "This number should be close to 1.7182818: " << integral + << std::endl; /* \int_{-\infty}^{\infty} sinc(x) dx, sinc(x) = sin(pi * x) / (pi * x) This is a difficult integral because of its infinite domain. Therefore, it may deviate largely from the expected result. */ - f = [&](double& x) { - return std::sin(M_PI * x) / (M_PI * x); - }; + f = [&](double& x) { return std::sin(M_PI * x) / (M_PI * x); }; pdf = [&](double& x) { return 1.0 / std::sqrt(2.0 * M_PI) * std::exp(-x * x / 2.0); @@ -183,7 +204,8 @@ static void test() { integral = math::monte_carlo::integral_monte_carlo(0.0, f, pdf, 10000000); - std::cout << "This number should be close to 1.0: " << integral << std::endl; + std::cout << "This number should be close to 1.0: " << integral + << std::endl; } /** diff --git a/range_queries/segtree.cpp b/range_queries/segtree.cpp index bc03b5428..71e6890fb 100644 --- a/range_queries/segtree.cpp +++ b/range_queries/segtree.cpp @@ -144,7 +144,7 @@ void update(std::vector *segtree, std::vector *lazy, * @returns void */ static void test() { - int64_t max = static_cast(2 * pow(2, ceil(log2(7))) - 1); + auto max = static_cast(2 * pow(2, ceil(log2(7))) - 1); assert(max == 15); std::vector arr{1, 2, 3, 4, 5, 6, 7}, lazy(max), segtree(max); @@ -172,7 +172,7 @@ int main() { uint64_t n = 0; std::cin >> n; - uint64_t max = static_cast(2 * pow(2, ceil(log2(n))) - 1); + auto max = static_cast(2 * pow(2, ceil(log2(n))) - 1); std::vector arr(n), lazy(max), segtree(max); int choice = 0;