From fb27d4d304cb6e8d2e3d6cbd36a4dfc1db3c00d4 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Fri, 15 Aug 2025 05:52:28 +0200 Subject: [PATCH 1/5] style: remove unused params of `main` (#2948) Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- CONTRIBUTING.md | 4 +--- data_structures/sparse_table.cpp | 4 +--- data_structures/tree_234.cpp | 4 ++-- dynamic_programming/fibonacci_bottom_up.cpp | 2 +- dynamic_programming/longest_increasing_subsequence.cpp | 4 +--- dynamic_programming/longest_increasing_subsequence_nlogn.cpp | 2 +- dynamic_programming/maximum_circular_subarray.cpp | 4 +--- dynamic_programming/minimum_edit_distance.cpp | 4 +--- graph/hamiltons_cycle.cpp | 4 +--- machine_learning/k_nearest_neighbors.cpp | 4 +--- machine_learning/kohonen_som_topology.cpp | 2 +- machine_learning/kohonen_som_trace.cpp | 2 +- math/eulers_totient_function.cpp | 4 +--- math/modular_division.cpp | 4 +--- math/n_choose_r.cpp | 4 +--- math/realtime_stats.cpp | 2 +- numerical_methods/babylonian_method.cpp | 4 +--- numerical_methods/composite_simpson_rule.cpp | 4 ++-- numerical_methods/fast_fourier_transform.cpp | 4 +--- numerical_methods/inverse_fast_fourier_transform.cpp | 4 +--- numerical_methods/lu_decompose.cpp | 2 +- numerical_methods/midpoint_integral_method.cpp | 4 ++-- operations_on_datastructures/inorder_successor_of_bst.cpp | 4 +--- operations_on_datastructures/trie_multiple_search.cpp | 4 +--- probability/windowed_median.cpp | 4 +--- search/sublist_search.cpp | 4 +--- sorting/non_recursive_merge_sort.cpp | 2 +- sorting/radix_sort.cpp | 2 +- sorting/random_pivot_quick_sort.cpp | 4 +--- sorting/stooge_sort.cpp | 2 -- 30 files changed, 32 insertions(+), 70 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 13202ce4e76..13dea32342a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -204,11 +204,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // run self-test implementations // code here return 0; diff --git a/data_structures/sparse_table.cpp b/data_structures/sparse_table.cpp index 7b3d74b90c3..7e3faacaa50 100644 --- a/data_structures/sparse_table.cpp +++ b/data_structures/sparse_table.cpp @@ -155,11 +155,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // run self-test implementations return 0; } diff --git a/data_structures/tree_234.cpp b/data_structures/tree_234.cpp index 46de03bfaef..5d79e32f9b0 100644 --- a/data_structures/tree_234.cpp +++ b/data_structures/tree_234.cpp @@ -1291,8 +1291,8 @@ static void test2(int64_t n) { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) + * @param argc commandline argument count + * @param argv commandline array of arguments * @returns 0 on exit */ int main(int argc, char *argv[]) { diff --git a/dynamic_programming/fibonacci_bottom_up.cpp b/dynamic_programming/fibonacci_bottom_up.cpp index 555f15282f4..dcc2b9a7232 100644 --- a/dynamic_programming/fibonacci_bottom_up.cpp +++ b/dynamic_programming/fibonacci_bottom_up.cpp @@ -11,7 +11,7 @@ int fib(int n) { } return res[1]; } -int main(int argc, char const *argv[]) { +int main() { int n; cout << "Enter n: "; cin >> n; diff --git a/dynamic_programming/longest_increasing_subsequence.cpp b/dynamic_programming/longest_increasing_subsequence.cpp index 8c8f97c438e..dc20ad44926 100644 --- a/dynamic_programming/longest_increasing_subsequence.cpp +++ b/dynamic_programming/longest_increasing_subsequence.cpp @@ -74,11 +74,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char const *argv[]) { +int main() { uint32_t n = 0; std::cout << "Enter size of array: "; diff --git a/dynamic_programming/longest_increasing_subsequence_nlogn.cpp b/dynamic_programming/longest_increasing_subsequence_nlogn.cpp index b0a49d2dab0..5cf516baf3d 100644 --- a/dynamic_programming/longest_increasing_subsequence_nlogn.cpp +++ b/dynamic_programming/longest_increasing_subsequence_nlogn.cpp @@ -29,7 +29,7 @@ int LIS(const std::vector& arr, int n) { } return active.size(); // size of the LIS. } -int main(int argc, char const* argv[]) { +int main() { int n; cout << "Enter size of array: "; cin >> n; diff --git a/dynamic_programming/maximum_circular_subarray.cpp b/dynamic_programming/maximum_circular_subarray.cpp index 466fc317702..ab62cfa01ac 100644 --- a/dynamic_programming/maximum_circular_subarray.cpp +++ b/dynamic_programming/maximum_circular_subarray.cpp @@ -79,11 +79,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // run self-test implementations return 0; } diff --git a/dynamic_programming/minimum_edit_distance.cpp b/dynamic_programming/minimum_edit_distance.cpp index 8664ccb4513..4ba2c6f63ed 100644 --- a/dynamic_programming/minimum_edit_distance.cpp +++ b/dynamic_programming/minimum_edit_distance.cpp @@ -166,11 +166,9 @@ static void test() { /** * @brief main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // run self-test implementations return 0; } diff --git a/graph/hamiltons_cycle.cpp b/graph/hamiltons_cycle.cpp index 1506b78d2fc..290e54c8fb6 100644 --- a/graph/hamiltons_cycle.cpp +++ b/graph/hamiltons_cycle.cpp @@ -136,10 +136,8 @@ static void test3() { /** * Main function * - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) */ -int main(int argc, char **argv) { +int main() { test1(); test2(); test3(); diff --git a/machine_learning/k_nearest_neighbors.cpp b/machine_learning/k_nearest_neighbors.cpp index 92621862ccf..4c855c891b0 100644 --- a/machine_learning/k_nearest_neighbors.cpp +++ b/machine_learning/k_nearest_neighbors.cpp @@ -186,11 +186,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @return int 0 on exit */ -int main(int argc, char* argv[]) { +int main() { test(); // run self-test implementations return 0; } diff --git a/machine_learning/kohonen_som_topology.cpp b/machine_learning/kohonen_som_topology.cpp index cccc9faa372..40b3e6414b5 100644 --- a/machine_learning/kohonen_som_topology.cpp +++ b/machine_learning/kohonen_som_topology.cpp @@ -579,7 +579,7 @@ double get_clock_diff(clock_t start_t, clock_t end_t) { } /** Main function */ -int main(int argc, char **argv) { +int main() { #ifdef _OPENMP std::cout << "Using OpenMP based parallelization\n"; #else diff --git a/machine_learning/kohonen_som_trace.cpp b/machine_learning/kohonen_som_trace.cpp index 224591269b8..e1299403a4c 100644 --- a/machine_learning/kohonen_som_trace.cpp +++ b/machine_learning/kohonen_som_trace.cpp @@ -454,7 +454,7 @@ double get_clock_diff(clock_t start_t, clock_t end_t) { } /** Main function */ -int main(int argc, char **argv) { +int main() { #ifdef _OPENMP std::cout << "Using OpenMP based parallelization\n"; #else diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index 768034327da..9105d99f2e9 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -72,11 +72,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); return 0; } diff --git a/math/modular_division.cpp b/math/modular_division.cpp index d37341b4a47..6a95f4fa91f 100644 --- a/math/modular_division.cpp +++ b/math/modular_division.cpp @@ -106,11 +106,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // execute the tests return 0; } diff --git a/math/n_choose_r.cpp b/math/n_choose_r.cpp index 5e151f39a84..2031cf2002b 100644 --- a/math/n_choose_r.cpp +++ b/math/n_choose_r.cpp @@ -73,11 +73,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // executing tests return 0; } diff --git a/math/realtime_stats.cpp b/math/realtime_stats.cpp index 672acfa03b9..6fe7d1bde68 100644 --- a/math/realtime_stats.cpp +++ b/math/realtime_stats.cpp @@ -155,7 +155,7 @@ void test_function(const float *test_data, const int number_of_samples) { } /** Main function */ -int main(int argc, char **argv) { +int main() { const float test_data1[] = {3, 4, 5, -1.4, -3.6, 1.9, 1.}; test_function(test_data1, sizeof(test_data1) / sizeof(test_data1[0])); diff --git a/numerical_methods/babylonian_method.cpp b/numerical_methods/babylonian_method.cpp index a70fa993e01..18a8d42e258 100644 --- a/numerical_methods/babylonian_method.cpp +++ b/numerical_methods/babylonian_method.cpp @@ -87,13 +87,11 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * calls automated test function to test the working of fast fourier transform. * @returns 0 on exit */ -int main(int argc, char const *argv[]) { +int main() { test(); // run self-test implementations // with 2 defined test cases return 0; diff --git a/numerical_methods/composite_simpson_rule.cpp b/numerical_methods/composite_simpson_rule.cpp index 2ca58cbe4c0..f1ec3cd166c 100644 --- a/numerical_methods/composite_simpson_rule.cpp +++ b/numerical_methods/composite_simpson_rule.cpp @@ -163,8 +163,8 @@ static void test(std::int32_t N, double h, double a, double b, /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) + * @param argc commandline argument count + * @param argv commandline array of arguments * @returns 0 on exit */ int main(int argc, char** argv) { diff --git a/numerical_methods/fast_fourier_transform.cpp b/numerical_methods/fast_fourier_transform.cpp index 392789ea710..ce01069a648 100644 --- a/numerical_methods/fast_fourier_transform.cpp +++ b/numerical_methods/fast_fourier_transform.cpp @@ -154,13 +154,11 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * calls automated test function to test the working of fast fourier transform. * @returns 0 on exit */ -int main(int argc, char const *argv[]) { +int main() { test(); // run self-test implementations // with 2 defined test cases return 0; diff --git a/numerical_methods/inverse_fast_fourier_transform.cpp b/numerical_methods/inverse_fast_fourier_transform.cpp index 928a20da802..7ab806b228d 100644 --- a/numerical_methods/inverse_fast_fourier_transform.cpp +++ b/numerical_methods/inverse_fast_fourier_transform.cpp @@ -148,13 +148,11 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * calls automated test function to test the working of fast fourier transform. * @returns 0 on exit */ -int main(int argc, char const *argv[]) { +int main() { test(); // run self-test implementations // with 2 defined test cases return 0; diff --git a/numerical_methods/lu_decompose.cpp b/numerical_methods/lu_decompose.cpp index 66f1a855139..19d29aad255 100644 --- a/numerical_methods/lu_decompose.cpp +++ b/numerical_methods/lu_decompose.cpp @@ -81,7 +81,7 @@ void test2() { } /** Main function */ -int main(int argc, char **argv) { +int main() { std::srand(std::time(NULL)); // random number initializer test1(); diff --git a/numerical_methods/midpoint_integral_method.cpp b/numerical_methods/midpoint_integral_method.cpp index 3eab8fd1e2d..76ab6408268 100644 --- a/numerical_methods/midpoint_integral_method.cpp +++ b/numerical_methods/midpoint_integral_method.cpp @@ -155,8 +155,8 @@ static void test(std::int32_t N, double h, double a, double b, /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) + * @param argc commandline argument count + * @param argv commandline array of arguments * @returns 0 on exit */ int main(int argc, char** argv) { diff --git a/operations_on_datastructures/inorder_successor_of_bst.cpp b/operations_on_datastructures/inorder_successor_of_bst.cpp index a91c0d71528..8da46cb92ca 100644 --- a/operations_on_datastructures/inorder_successor_of_bst.cpp +++ b/operations_on_datastructures/inorder_successor_of_bst.cpp @@ -391,11 +391,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // run self-test implementations operations_on_datastructures::inorder_traversal_of_bst::Node *root = diff --git a/operations_on_datastructures/trie_multiple_search.cpp b/operations_on_datastructures/trie_multiple_search.cpp index 6dbb9d338d6..54211daee8c 100644 --- a/operations_on_datastructures/trie_multiple_search.cpp +++ b/operations_on_datastructures/trie_multiple_search.cpp @@ -459,11 +459,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char const *argv[]) { +int main() { test(); // run self-test implementations return 0; } diff --git a/probability/windowed_median.cpp b/probability/windowed_median.cpp index 52c70ae2487..95ce8eb3fb5 100644 --- a/probability/windowed_median.cpp +++ b/probability/windowed_median.cpp @@ -191,11 +191,9 @@ static void test(const std::vector &vals, int windowSize) { /** * @brief Main function - * @param argc command line argument count (ignored) - * @param argv command line array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, const char *argv[]) { +int main() { /// A few fixed test cases test({1, 2, 3, 4, 5, 6, 7, 8, 9}, 3); /// Array of sorted values; odd window size diff --git a/search/sublist_search.cpp b/search/sublist_search.cpp index bb63cac0fa5..bb4e485a018 100644 --- a/search/sublist_search.cpp +++ b/search/sublist_search.cpp @@ -353,11 +353,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // run self-test implementations std::vector mainlistData = { diff --git a/sorting/non_recursive_merge_sort.cpp b/sorting/non_recursive_merge_sort.cpp index b99b931080b..47bf97e9d3d 100644 --- a/sorting/non_recursive_merge_sort.cpp +++ b/sorting/non_recursive_merge_sort.cpp @@ -91,7 +91,7 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last) { using sorting::non_recursive_merge_sort; -int main(int argc, char** argv) { +int main() { int size; std::cout << "Enter the number of elements : "; std::cin >> size; diff --git a/sorting/radix_sort.cpp b/sorting/radix_sort.cpp index a0fbfe99ee5..01f3aed9c04 100644 --- a/sorting/radix_sort.cpp +++ b/sorting/radix_sort.cpp @@ -49,7 +49,7 @@ void print(int a[], int n) { std::cout << std::endl; } -int main(int argc, char const* argv[]) { +int main() { int a[] = {170, 45, 75, 90, 802, 24, 2, 66}; int n = sizeof(a) / sizeof(a[0]); radixsort(a, n); diff --git a/sorting/random_pivot_quick_sort.cpp b/sorting/random_pivot_quick_sort.cpp index a6ed19f1296..e514cbb0f9d 100644 --- a/sorting/random_pivot_quick_sort.cpp +++ b/sorting/random_pivot_quick_sort.cpp @@ -316,11 +316,9 @@ static void test() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ -int main(int argc, char *argv[]) { +int main() { test(); // Executes various test cases. const int64_t inputSize = 10; diff --git a/sorting/stooge_sort.cpp b/sorting/stooge_sort.cpp index b1f01321ae2..735cbb31222 100644 --- a/sorting/stooge_sort.cpp +++ b/sorting/stooge_sort.cpp @@ -72,8 +72,6 @@ void test3() { /** * @brief Main function - * @param argc commandline argument count (ignored) - * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ int main() { From 13306be79f852e103fca650f4baeff05821498b6 Mon Sep 17 00:00:00 2001 From: Jonas Vautherin Date: Fri, 15 Aug 2025 06:05:18 +0200 Subject: [PATCH 2/5] Fixed name space problem by removing using namespace std (#2966) Co-authored-by: FuelTheBurn Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- data_structures/queue_using_array2.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/data_structures/queue_using_array2.cpp b/data_structures/queue_using_array2.cpp index 13f7d8e17f8..cda95946ba2 100644 --- a/data_structures/queue_using_array2.cpp +++ b/data_structures/queue_using_array2.cpp @@ -1,5 +1,4 @@ #include -using namespace std; int queue[10]; int front = 0; @@ -7,7 +6,7 @@ int rear = 0; void Enque(int x) { if (rear == 10) { - cout << "\nOverflow"; + std::cout << "\nOverflow"; } else { queue[rear++] = x; } @@ -15,11 +14,11 @@ void Enque(int x) { void Deque() { if (front == rear) { - cout << "\nUnderflow"; + std::cout << "\nUnderflow"; } else { - cout << "\n" << queue[front++] << " deleted"; + std::cout << "\n" << queue[front++] << " deleted"; for (int i = front; i < rear; i++) { queue[i - front] = queue[i]; } @@ -30,21 +29,21 @@ void Deque() { void show() { for (int i = front; i < rear; i++) { - cout << queue[i] << "\t"; + std::cout << queue[i] << "\t"; } } int main() { int ch, x; do { - cout << "\n1. Enque"; - cout << "\n2. Deque"; - cout << "\n3. Print"; - cout << "\nEnter Your Choice : "; - cin >> ch; + std::cout << "\n1. Enque"; + std::cout << "\n2. Deque"; + std::cout << "\n3. Print"; + std::cout << "\nEnter Your Choice : "; + std::cin >> ch; if (ch == 1) { - cout << "\nInsert : "; - cin >> x; + std::cout << "\nInsert : "; + std::cin >> x; Enque(x); } else if (ch == 2) { Deque(); From 6568ab983d37245972161bb9bfce112f2737cb2c Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Fri, 15 Aug 2025 06:40:28 +0200 Subject: [PATCH 3/5] style: resolve `-Wreorder` (#2950) Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- ciphers/uint256_t.hpp | 2 +- data_structures/stack_using_array.cpp | 2 +- data_structures/tree_234.cpp | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ciphers/uint256_t.hpp b/ciphers/uint256_t.hpp index a776d4cb0e4..aef52f10e2d 100644 --- a/ciphers/uint256_t.hpp +++ b/ciphers/uint256_t.hpp @@ -72,7 +72,7 @@ class uint256_t { */ template ::value, T>::type> - explicit uint256_t(T low) : s(low), f(0) {} + explicit uint256_t(T low) : f(0), s(low) {} /** * @brief Parameterized constructor diff --git a/data_structures/stack_using_array.cpp b/data_structures/stack_using_array.cpp index 73feaf445cc..ab67a9172a6 100644 --- a/data_structures/stack_using_array.cpp +++ b/data_structures/stack_using_array.cpp @@ -25,7 +25,7 @@ class Stack { * * @param size Maximum size of the stack */ - Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {} + Stack(int size) : stack(new T[size]), stackSize(size), stackIndex(-1) {} /** * @brief Checks if the stack is full diff --git a/data_structures/tree_234.cpp b/data_structures/tree_234.cpp index 5d79e32f9b0..0971727eca2 100644 --- a/data_structures/tree_234.cpp +++ b/data_structures/tree_234.cpp @@ -39,9 +39,9 @@ class Node { * @param item the first value we insert to the node */ explicit Node(int64_t item) - : count(1), - items({{item, 0, 0}}), - children({{nullptr, nullptr, nullptr, nullptr}}) {} + : items({{item, 0, 0}}), + children({{nullptr, nullptr, nullptr, nullptr}}), + count(1) {} /** * @brief Get the item count that current saved in the node From e9caade55e16f2744ed12b859cce5b4ff8e0370a Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Fri, 15 Aug 2025 06:45:42 +0200 Subject: [PATCH 4/5] chore: sort subdirectories (#2949) Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- CMakeLists.txt | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba3ec4b7e24..1fb339e475f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,30 +27,30 @@ if(USE_OPENMP) endif() endif() -add_subdirectory(math) -add_subdirectory(others) -add_subdirectory(search) -add_subdirectory(ciphers) -add_subdirectory(hashing) -add_subdirectory(strings) -add_subdirectory(sorting) -add_subdirectory(geometry) -add_subdirectory(graphics) -add_subdirectory(probability) add_subdirectory(backtracking) add_subdirectory(bit_manipulation) +add_subdirectory(ciphers) +add_subdirectory(cpu_scheduling_algorithms) +add_subdirectory(data_structures) +add_subdirectory(divide_and_conquer) add_subdirectory(dynamic_programming) +add_subdirectory(games) +add_subdirectory(geometry) +add_subdirectory(graph) +add_subdirectory(graphics) add_subdirectory(greedy_algorithms) -add_subdirectory(range_queries) -add_subdirectory(operations_on_datastructures) -add_subdirectory(data_structures) +add_subdirectory(hashing) add_subdirectory(machine_learning) +add_subdirectory(math) add_subdirectory(numerical_methods) -add_subdirectory(graph) -add_subdirectory(divide_and_conquer) -add_subdirectory(games) -add_subdirectory(cpu_scheduling_algorithms) +add_subdirectory(operations_on_datastructures) +add_subdirectory(others) add_subdirectory(physics) +add_subdirectory(probability) +add_subdirectory(range_queries) +add_subdirectory(search) +add_subdirectory(sorting) +add_subdirectory(strings) cmake_policy(SET CMP0054 NEW) cmake_policy(SET CMP0057 NEW) From 79aeaa9b5278542d49c83368003887e46a9cf7d5 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Fri, 15 Aug 2025 05:24:35 +0000 Subject: [PATCH 5/5] chore: try to comply to C++20 (#2978) * chroe: add cstdint * fix: remove <> from explicit function --- data_structures/trie_tree.cpp | 1 + machine_learning/a_star_search.cpp | 2 + range_queries/heavy_light_decomposition.cpp | 1003 +++++++++-------- .../persistent_seg_tree_lazy_prop.cpp | 1 + 4 files changed, 509 insertions(+), 498 deletions(-) diff --git a/data_structures/trie_tree.cpp b/data_structures/trie_tree.cpp index c0b6e4fad43..b7b4ce5fd5e 100644 --- a/data_structures/trie_tree.cpp +++ b/data_structures/trie_tree.cpp @@ -9,6 +9,7 @@ */ #include #include +#include #include #include #include diff --git a/machine_learning/a_star_search.cpp b/machine_learning/a_star_search.cpp index 9f713883a1d..3f093cf903b 100644 --- a/machine_learning/a_star_search.cpp +++ b/machine_learning/a_star_search.cpp @@ -22,12 +22,14 @@ #include /// for `std::reverse` function #include /// for `std::array`, representing `EightPuzzle` board #include /// for `assert` +#include /// for `std::uint32_t` #include /// for `std::function` STL #include /// for IO operations #include /// for `std::map` STL #include /// for `std::shared_ptr` #include /// for `std::set` STL #include /// for `std::vector` STL + /** * @namespace machine_learning * @brief Machine learning algorithms diff --git a/range_queries/heavy_light_decomposition.cpp b/range_queries/heavy_light_decomposition.cpp index b535a980848..1c57eadbeb9 100644 --- a/range_queries/heavy_light_decomposition.cpp +++ b/range_queries/heavy_light_decomposition.cpp @@ -75,471 +75,477 @@ namespace heavy_light_decomposition { * kth_ancestor :- returns the kth ancestor * lca :- returns the least common ancestor */ -template class Tree { - // - -private: - std::vector> - t_adj; ///< an adjacency list to stores the tree edges - const int t_nodes, ///< number of nodes - t_maxlift; ///< maximum possible height of the tree - std::vector> - t_par; ///< a matrix to store every node's 2^kth parent - std::vector t_depth, ///< a vector to store the depth of a node, - t_size; ///< a vector to store the subtree size rooted at node - - int t_root; ///< the root of the tree - std::vector t_val; ///< values of nodes - template friend class HLD; - - /** - * @brief Utility function to compute sub-tree sizes - * @param u current dfs node - * @param p the parent of node @param u - * @returns void - */ - void dfs_size(int u, int p = -1) { - for (const int &v : t_adj[u]) { - if (v ^ p) { - dfs_size(v, u); - t_size[u] += t_size[v]; - } +template +class Tree { + // + + private: + std::vector> + t_adj; ///< an adjacency list to stores the tree edges + const int t_nodes, ///< number of nodes + t_maxlift; ///< maximum possible height of the tree + std::vector> + t_par; ///< a matrix to store every node's 2^kth parent + std::vector t_depth, ///< a vector to store the depth of a node, + t_size; ///< a vector to store the subtree size rooted at node + + int t_root; ///< the root of the tree + std::vector t_val; ///< values of nodes + template + friend class HLD; + + /** + * @brief Utility function to compute sub-tree sizes + * @param u current dfs node + * @param p the parent of node @param u + * @returns void + */ + void dfs_size(int u, int p = -1) { + for (const int &v : t_adj[u]) { + if (v ^ p) { + dfs_size(v, u); + t_size[u] += t_size[v]; + } + } } - } - - /** - * @brief Utility function to populate the t_par vector - * @param u current dfs node - * @param p the parent of node u - * @returns void - */ - void dfs_lca(int u, int p = -1) { - t_par[u][0] = p; - if (p != -1) { - t_depth[u] = 1 + t_depth[p]; + + /** + * @brief Utility function to populate the t_par vector + * @param u current dfs node + * @param p the parent of node u + * @returns void + */ + void dfs_lca(int u, int p = -1) { + t_par[u][0] = p; + if (p != -1) { + t_depth[u] = 1 + t_depth[p]; + } + for (int k = 1; k < t_maxlift; k++) { + if (t_par[u][k - 1] != -1) { + t_par[u][k] = t_par[t_par[u][k - 1]][k - 1]; + } + } + + for (const int &v : t_adj[u]) { + if (v ^ p) { + dfs_lca(v, u); + } + } } - for (int k = 1; k < t_maxlift; k++) { - if (t_par[u][k - 1] != -1) { - t_par[u][k] = t_par[t_par[u][k - 1]][k - 1]; - } + + public: + /** + * @brief Class parameterized constructor, resizes the and initializes the + * data members + * @param nodes the total number of nodes in the tree + */ + explicit Tree(int nodes) + : t_nodes(nodes), t_maxlift(static_cast(floor(log2(nodes))) + 1) { + /* Initialize and resize all the vectors */ + t_root = 0; /* Default */ + t_adj.resize(t_nodes); + t_par.assign(t_nodes, std::vector(t_maxlift, -1)); + t_depth.assign(t_nodes, 0); + t_size.assign(t_nodes, 1); + t_val.resize(t_nodes); } - for (const int &v : t_adj[u]) { - if (v ^ p) { - dfs_lca(v, u); - } + /** + * @brief Adds an undirected edge from node u to node v in the tree + * @param u the node where the edge is from + * @param v the node where the edge is to + * @returns void + */ + void add_edge(const int u, const int v) { + t_adj[u].push_back(v); + t_adj[v].push_back(u); } - } - -public: - /** - * @brief Class parameterized constructor, resizes the and initializes the - * data members - * @param nodes the total number of nodes in the tree - */ - explicit Tree(int nodes) - : t_nodes(nodes), t_maxlift(static_cast(floor(log2(nodes))) + 1) { - /* Initialize and resize all the vectors */ - t_root = 0; /* Default */ - t_adj.resize(t_nodes); - t_par.assign(t_nodes, std::vector(t_maxlift, -1)); - t_depth.assign(t_nodes, 0); - t_size.assign(t_nodes, 1); - t_val.resize(t_nodes); - } - - /** - * @brief Adds an undirected edge from node u to node v in the tree - * @param u the node where the edge is from - * @param v the node where the edge is to - * @returns void - */ - void add_edge(const int u, const int v) { - t_adj[u].push_back(v); - t_adj[v].push_back(u); - } - - /** - * @brief Set the root for the tree - * @param new_root the new root - * @returns void - */ - void change_root(int new_root) { t_root = new_root; } - - /** - * @brief Set the values for all the nodes - * @param node_val a vector of size n, with all the node values where, n is - * the number of nodes - * @returns void - */ - void set_node_val(const std::vector &node_val) { - assert(static_cast(node_val.size()) == t_nodes); - t_val = node_val; - } - - /** - * @brief This function must be called after the tree adjacency list and node - * values are populated The function initializes the required parameters, and - * populates the segment tree - * @returns void - */ - void init() { - assert(t_nodes > 0); - dfs_size(t_root); - dfs_lca(t_root); - } - - /** - * @brief The function lifts a node, k units up the tree. - * The lifting is done in place, and the result is stored in the address - * pointed by p. - * @param p a pointer to the variable that stores the node id - * @param dist the distance to move up the tree - * @returns void - */ - void lift(int *const p, int dist) { - for (int k = 0; k < t_maxlift; k++) { - if (*p == -1) { - return; - } - if (dist & 1) { - *p = t_par[*p][k]; - } - dist >>= 1; + + /** + * @brief Set the root for the tree + * @param new_root the new root + * @returns void + */ + void change_root(int new_root) { t_root = new_root; } + + /** + * @brief Set the values for all the nodes + * @param node_val a vector of size n, with all the node values where, n is + * the number of nodes + * @returns void + */ + void set_node_val(const std::vector &node_val) { + assert(static_cast(node_val.size()) == t_nodes); + t_val = node_val; } - } - - /** - * @brief The function returns the kth ancestor of a node - * @param p the node id whose kth ancestor is to be found - * @param dist the distance to move up the tree - * @returns the kth ancestor of node - */ - int kth_ancestor(int p, const int &dist) { - lift(&p, dist); - return p; - } - - /** - * @brief The function returns the least common ancestor of two nodes - * @param a node id_1 - * @param b node id_2 - * @returns the least common ancestor of node a, and node b - */ - int lca(int a, int b) { - assert(a >= 0 and b >= 0 and a < t_nodes and b < t_nodes); - if (t_depth[a] > t_depth[b]) { - lift(&a, t_depth[a] - t_depth[b]); + + /** + * @brief This function must be called after the tree adjacency list and + * node values are populated The function initializes the required + * parameters, and populates the segment tree + * @returns void + */ + void init() { + assert(t_nodes > 0); + dfs_size(t_root); + dfs_lca(t_root); } - if (t_depth[b] > t_depth[a]) { - lift(&b, t_depth[b] - t_depth[a]); + + /** + * @brief The function lifts a node, k units up the tree. + * The lifting is done in place, and the result is stored in the address + * pointed by p. + * @param p a pointer to the variable that stores the node id + * @param dist the distance to move up the tree + * @returns void + */ + void lift(int *const p, int dist) { + for (int k = 0; k < t_maxlift; k++) { + if (*p == -1) { + return; + } + if (dist & 1) { + *p = t_par[*p][k]; + } + dist >>= 1; + } } - if (a == b) { - return a; + + /** + * @brief The function returns the kth ancestor of a node + * @param p the node id whose kth ancestor is to be found + * @param dist the distance to move up the tree + * @returns the kth ancestor of node + */ + int kth_ancestor(int p, const int &dist) { + lift(&p, dist); + return p; } - for (int k = t_maxlift - 1; k >= 0; k--) { - if (t_par[a][k] != t_par[b][k]) { - a = t_par[a][k]; - b = t_par[b][k]; - } + + /** + * @brief The function returns the least common ancestor of two nodes + * @param a node id_1 + * @param b node id_2 + * @returns the least common ancestor of node a, and node b + */ + int lca(int a, int b) { + assert(a >= 0 and b >= 0 and a < t_nodes and b < t_nodes); + if (t_depth[a] > t_depth[b]) { + lift(&a, t_depth[a] - t_depth[b]); + } + if (t_depth[b] > t_depth[a]) { + lift(&b, t_depth[b] - t_depth[a]); + } + if (a == b) { + return a; + } + for (int k = t_maxlift - 1; k >= 0; k--) { + if (t_par[a][k] != t_par[b][k]) { + a = t_par[a][k]; + b = t_par[b][k]; + } + } + return t_par[a][0]; } - return t_par[a][0]; - } }; /** * @brief Segment Tree, to store heavy chains * @tparam the data type of the values stored in the tree nodes */ -template class SG { -private: - /** - * @brief Everything here is private, - * and can only be accessed through the methods, - * in the derived class (HLD) - */ - - std::vector s_tree; ///< the segment tree, stored as a vector - int s_size; ///< number of leaves in the segment tree - X sret_init = 0; ///< inital query return value - template friend class HLD; - - /** - * @brief Function that specifies the type of operation involved when segments - * are combined - * @param lhs the left segment - * @param rhs the right segment - * @returns the combined result - */ - X combine(X lhs, X rhs) { return lhs + rhs; } - - /** - * @brief Class parameterized constructor. Resizes the and initilizes the data - * members. - * @param nodes the total number of nodes in the tree - * @returns void - */ - explicit SG(int size) { - s_size = size; - s_tree.assign(2 * s_size, 0ll); - } - - /** - * @brief Update the value at a node - * @param p the node to be udpated - * @param v the update value - * @returns void - */ - void update(int p, X v) { - for (p += s_size; p > 0; p >>= 1) { - s_tree[p] += v; +template +class SG { + private: + /** + * @brief Everything here is private, + * and can only be accessed through the methods, + * in the derived class (HLD) + */ + + std::vector s_tree; ///< the segment tree, stored as a vector + int s_size; ///< number of leaves in the segment tree + X sret_init = 0; ///< inital query return value + template + friend class HLD; + + /** + * @brief Function that specifies the type of operation involved when + * segments are combined + * @param lhs the left segment + * @param rhs the right segment + * @returns the combined result + */ + X combine(X lhs, X rhs) { return lhs + rhs; } + + /** + * @brief Class parameterized constructor. Resizes the and initilizes the + * data members. + * @param nodes the total number of nodes in the tree + * @returns void + */ + explicit SG(int size) { + s_size = size; + s_tree.assign(2 * s_size, 0ll); + } + + /** + * @brief Update the value at a node + * @param p the node to be udpated + * @param v the update value + * @returns void + */ + void update(int p, X v) { + for (p += s_size; p > 0; p >>= 1) { + s_tree[p] += v; + } } - } - - /** - * @brief Make a range query from node label l to node label r - * @param l node label where the path starts - * @param r node label where the path ends - * @returns void - */ - X query(int l, int r) { - X lhs = sret_init, rhs = sret_init; - for (l += s_size, r += s_size + 1; l < r; l >>= 1, r >>= 1) { - if (l & 1) { - lhs = combine(lhs, s_tree[l++]); - } - if (r & 1) { - rhs = combine(s_tree[--r], rhs); - } + + /** + * @brief Make a range query from node label l to node label r + * @param l node label where the path starts + * @param r node label where the path ends + * @returns void + */ + X query(int l, int r) { + X lhs = sret_init, rhs = sret_init; + for (l += s_size, r += s_size + 1; l < r; l >>= 1, r >>= 1) { + if (l & 1) { + lhs = combine(lhs, s_tree[l++]); + } + if (r & 1) { + rhs = combine(s_tree[--r], rhs); + } + } + return combine(lhs, rhs); } - return combine(lhs, rhs); - } - - /** - * @brief Set the initialization for the query data type, based on requirement - * - * @details - * Change the sret_init, based on requirement: - * * Sum Query: 0 (Default) - * * XOR Query: 0 (Default) - * * Min Query: Infinity - * * Max Query: -Infinity - * @param new_sret_init the new init - */ - void set_sret_init(X new_sret_init) { sret_init = new_sret_init; } + + /** + * @brief Set the initialization for the query data type, based on + * requirement + * + * @details + * Change the sret_init, based on requirement: + * * Sum Query: 0 (Default) + * * XOR Query: 0 (Default) + * * Min Query: Infinity + * * Max Query: -Infinity + * @param new_sret_init the new init + */ + void set_sret_init(X new_sret_init) { sret_init = new_sret_init; } }; /** * @brief The Heavy-Light Decomposition class * @tparam the data type of the values stored in the tree nodes */ -template class HLD : public Tree, public SG { -private: - int label; ///< utility member to assign labels in dfs_labels() - std::vector h_label, ///< stores the label of a node - h_heavychlid, ///< stores the heavy child of a node - h_parent; ///< stores the top of the heavy chain from a node - - /** - * @brief Utility function to assign heavy child to each node (-1 for a leaf - * node) - * @param u current dfs node - * @param p the parent of node u - * @returns void - */ - void dfs_hc(int u, int p = -1) { - int hc_size = -1, hc_id = -1; - for (const int &v : Tree::t_adj[u]) { - if (v ^ p) { - dfs_hc(v, u); - if (Tree::t_size[v] > hc_size) { - hc_size = Tree::t_size[v]; - hc_id = v; +template +class HLD : public Tree, public SG { + private: + int label; ///< utility member to assign labels in dfs_labels() + std::vector h_label, ///< stores the label of a node + h_heavychlid, ///< stores the heavy child of a node + h_parent; ///< stores the top of the heavy chain from a node + + /** + * @brief Utility function to assign heavy child to each node (-1 for a leaf + * node) + * @param u current dfs node + * @param p the parent of node u + * @returns void + */ + void dfs_hc(int u, int p = -1) { + int hc_size = -1, hc_id = -1; + for (const int &v : Tree::t_adj[u]) { + if (v ^ p) { + dfs_hc(v, u); + if (Tree::t_size[v] > hc_size) { + hc_size = Tree::t_size[v]; + hc_id = v; + } + } } - } - } - h_heavychlid[u] = hc_id; - } - - /** - * @brief Utility function to assign highest parent that can be reached though - * heavy chains - * @param u current dfs node - * @param p the parent of node u - * @returns void - */ - void dfs_par(int u, int p = -1) { - if (h_heavychlid[u] != -1) { - h_parent[h_heavychlid[u]] = h_parent[u]; - dfs_par(h_heavychlid[u], u); + h_heavychlid[u] = hc_id; } - for (const int &v : Tree::t_adj[u]) { - if (v ^ p and v ^ h_heavychlid[u]) { - dfs_par(v, u); - } + + /** + * @brief Utility function to assign highest parent that can be reached + * though heavy chains + * @param u current dfs node + * @param p the parent of node u + * @returns void + */ + void dfs_par(int u, int p = -1) { + if (h_heavychlid[u] != -1) { + h_parent[h_heavychlid[u]] = h_parent[u]; + dfs_par(h_heavychlid[u], u); + } + for (const int &v : Tree::t_adj[u]) { + if (v ^ p and v ^ h_heavychlid[u]) { + dfs_par(v, u); + } + } } - } - - /** - * @brief Utility function to lable the nodes so that heavy chains have a - * contigous lable - * @param u current dfs node - * @param p the parent of node u - * @returns void - */ - void dfs_labels(int u, int p = -1) { - h_label[u] = label++; - if (h_heavychlid[u] != -1) { - dfs_labels(h_heavychlid[u], u); + + /** + * @brief Utility function to lable the nodes so that heavy chains have a + * contigous lable + * @param u current dfs node + * @param p the parent of node u + * @returns void + */ + void dfs_labels(int u, int p = -1) { + h_label[u] = label++; + if (h_heavychlid[u] != -1) { + dfs_labels(h_heavychlid[u], u); + } + for (const int &v : Tree::t_adj[u]) { + if (v ^ p and v ^ h_heavychlid[u]) { + dfs_labels(v, u); + } + } } - for (const int &v : Tree::t_adj[u]) { - if (v ^ p and v ^ h_heavychlid[u]) { - dfs_labels(v, u); - } + + /** + * @brief Utility function to break down a path query into two chain queries + * @param a node where the path starts + * @param b node where the path ends + * a and b must belong to a single root to leaf chain + * @returns the sum of ndoe values in the simple path from a to b + */ + X chain_query(int a, int b) { + X ret = SG::sret_init; + if (Tree::t_depth[a] < Tree::t_depth[b]) { + std::swap(a, b); + } + while (Tree::t_depth[a] >= Tree::t_depth[b]) { + int l = h_label[h_parent[a]]; + int r = h_label[a]; + if (Tree::t_depth[h_parent[a]] < Tree::t_depth[b]) { + l += Tree::t_depth[b] - Tree::t_depth[h_parent[a]]; + } + ret = SG::combine(ret, SG::query(l, r)); + a = Tree::t_par[h_parent[a]][0]; + if (a == -1) { + break; + } + } + return ret; } - } - - /** - * @brief Utility function to break down a path query into two chain queries - * @param a node where the path starts - * @param b node where the path ends - * a and b must belong to a single root to leaf chain - * @returns the sum of ndoe values in the simple path from a to b - */ - X chain_query(int a, int b) { - X ret = SG::sret_init; - if (Tree::t_depth[a] < Tree::t_depth[b]) { - std::swap(a, b); + + public: + /** + * @brief Class parameterized constructor. Resizes the and initilizes the + * data members. + * @param nodes the total number of nodes in the tree + */ + explicit HLD(int nodes) : Tree(nodes), SG(nodes) { + /* Initialization and resize vectors */ + label = 0; + h_label.assign(Tree::t_nodes, -1); + h_heavychlid.assign(Tree::t_nodes, -1); + h_parent.resize(Tree::t_nodes); + iota(h_parent.begin(), h_parent.end(), 0); } - while (Tree::t_depth[a] >= Tree::t_depth[b]) { - int l = h_label[h_parent[a]]; - int r = h_label[a]; - if (Tree::t_depth[h_parent[a]] < Tree::t_depth[b]) { - l += Tree::t_depth[b] - Tree::t_depth[h_parent[a]]; - } - ret = SG::combine(ret, SG::query(l, r)); - a = Tree::t_par[h_parent[a]][0]; - if (a == -1) { - break; - } + + /** + * @brief This function must be called after the tree adjacency list and + * node values are populated The function initializes the required + * parametes, and populates the segment tree + * @returns void + */ + void init() { + Tree::init(); + + // Fill the heavy child, greatest parent, and labels + label = 0; + dfs_hc(Tree::t_root); + dfs_par(Tree::t_root); + dfs_labels(Tree::t_root); + + // Segment Tree Initialization + for (int i = 0; i < Tree::t_nodes; i++) { + SG::s_tree[h_label[i] + Tree::t_nodes] = Tree::t_val[i]; + } + for (int i = Tree::t_nodes - 1; i > 0; i--) { + SG::s_tree[i] = SG::combine(SG::s_tree[i << 1], + SG::s_tree[i << 1 | 1]); + } } - return ret; - } - -public: - /** - * @brief Class parameterized constructor. Resizes the and initilizes the data - * members. - * @param nodes the total number of nodes in the tree - */ - explicit HLD(int nodes) : Tree(nodes), SG(nodes) { - /* Initialization and resize vectors */ - label = 0; - h_label.assign(Tree::t_nodes, -1); - h_heavychlid.assign(Tree::t_nodes, -1); - h_parent.resize(Tree::t_nodes); - iota(h_parent.begin(), h_parent.end(), 0); - } - - /** - * @brief This function must be called after the tree adjacency list and node - * values are populated The function initializes the required parametes, and - * populates the segment tree - * @returns void - */ - void init() { - Tree::init(); - - // Fill the heavy child, greatest parent, and labels - label = 0; - dfs_hc(Tree::t_root); - dfs_par(Tree::t_root); - dfs_labels(Tree::t_root); - - // Segment Tree Initialization - for (int i = 0; i < Tree::t_nodes; i++) { - SG::s_tree[h_label[i] + Tree::t_nodes] = Tree::t_val[i]; + + /** + * @brief This function updates the value at node with val + * @param node the node where the update is done + * @param val the value that is being updated + * @returns void + */ + void update(int node, X val) { + X diff = val - Tree::t_val[node]; + SG::update(h_label[node], diff); + Tree::t_val[node] = val; } - for (int i = Tree::t_nodes - 1; i > 0; i--) { - SG::s_tree[i] = - SG::combine(SG::s_tree[i << 1], SG::s_tree[i << 1 | 1]); + + /** + * @brief This function returns the sum of node values in the simple path + * from from node_1 to node_2 + * @param a the node where the simple path starts + * @param b the node where the simple path ends + * (parameters are interchangeable, i.e., the function is commutative) + * @returns the sum of node values in the simple path from a to b + */ + X query(int a, int b) { + int lc = Tree::lca(a, b); + X ret = SG::sret_init; + assert(lc != -1); + ret += chain_query(a, lc); + ret += chain_query(b, lc); + return ret - Tree::t_val[lc]; } - } - - /** - * @brief This function updates the value at node with val - * @param node the node where the update is done - * @param val the value that is being updated - * @returns void - */ - void update(int node, X val) { - X diff = val - Tree::t_val[node]; - SG::update(h_label[node], diff); - Tree::t_val[node] = val; - } - - /** - * @brief This function returns the sum of node values in the simple path from - * from node_1 to node_2 - * @param a the node where the simple path starts - * @param b the node where the simple path ends - * (parameters are interchangeable, i.e., the function is commutative) - * @returns the sum of node values in the simple path from a to b - */ - X query(int a, int b) { - int lc = Tree::lca(a, b); - X ret = SG::sret_init; - assert(lc != -1); - ret += chain_query(a, lc); - ret += chain_query(b, lc); - return ret - Tree::t_val[lc]; - } }; -} // namespace heavy_light_decomposition -} // namespace range_queries +} // namespace heavy_light_decomposition +} // namespace range_queries /** * Test implementations * @returns none */ static void test_1() { - std::cout << "Test 1:\n"; - - // Test details - int n = 5; - std::vector node_values = {4, 2, 5, 2, 1}; - std::vector> edges = {{1, 2}, {1, 3}, {3, 4}, {3, 5}}; - std::vector> queries = { - {2, 1, 4}, - {1, 3, 2}, - {2, 1, 4}, - }; - std::vector expected_result = {11, 8}; - std::vector code_result; - - range_queries::heavy_light_decomposition::HLD hld(n); - hld.set_node_val(node_values); - for (int i = 0; i < n - 1; i++) { - int u = edges[i][0], v = edges[i][1]; - hld.add_edge(u - 1, v - 1); - } - hld.init(); - for (const auto &q : queries) { - int type = q[0]; - if (type == 1) { - int p = q[1], x = q[2]; - hld.update(p - 1, x); - } else if (type == 2) { - int a = q[1], b = q[2]; - code_result.push_back(hld.query(a - 1, b - 1)); - } else { - continue; + std::cout << "Test 1:\n"; + + // Test details + int n = 5; + std::vector node_values = {4, 2, 5, 2, 1}; + std::vector> edges = {{1, 2}, {1, 3}, {3, 4}, {3, 5}}; + std::vector> queries = { + {2, 1, 4}, + {1, 3, 2}, + {2, 1, 4}, + }; + std::vector expected_result = {11, 8}; + std::vector code_result; + + range_queries::heavy_light_decomposition::HLD hld(n); + hld.set_node_val(node_values); + for (int i = 0; i < n - 1; i++) { + int u = edges[i][0], v = edges[i][1]; + hld.add_edge(u - 1, v - 1); + } + hld.init(); + for (const auto &q : queries) { + int type = q[0]; + if (type == 1) { + int p = q[1], x = q[2]; + hld.update(p - 1, x); + } else if (type == 2) { + int a = q[1], b = q[2]; + code_result.push_back(hld.query(a - 1, b - 1)); + } else { + continue; + } + } + for (int i = 0; i < static_cast(expected_result.size()); i++) { + assert(expected_result[i] == code_result[i]); } - } - for (int i = 0; i < static_cast(expected_result.size()); i++) { - assert(expected_result[i] == code_result[i]); - } - std::cout << "\nTest 1 passed!\n"; + std::cout << "\nTest 1 passed!\n"; } /** @@ -547,42 +553,43 @@ static void test_1() { * @returns void */ static void test_2() { - std::cout << "Test 2:\n"; - - // Test details (Bamboo) - int n = 10; - std::vector node_values = {1, 8, 6, 8, 6, 2, 9, 2, 3, 2}; - std::vector> edges = { - {10, 5}, {6, 2}, {10, 7}, {5, 2}, {3, 9}, {8, 3}, {1, 4}, {6, 4}, {8, 7}}; - std::vector> queries = { - {2, 1, 10}, {2, 1, 6}, {1, 3, 4}, {2, 1, 9}, {1, 5, 3}, - {1, 7, 8}, {2, 1, 4}, {2, 1, 8}, {1, 1, 4}, {1, 2, 7}}; - std::vector expected_result = {27, 11, 45, 9, 34}; - std::vector code_result; - - range_queries::heavy_light_decomposition::HLD hld(n); - hld.set_node_val(node_values); - for (int i = 0; i < n - 1; i++) { - int u = edges[i][0], v = edges[i][1]; - hld.add_edge(u - 1, v - 1); - } - hld.init(); - for (const auto &q : queries) { - int type = q[0]; - if (type == 1) { - int p = q[1], x = q[2]; - hld.update(p - 1, x); - } else if (type == 2) { - int a = q[1], b = q[2]; - code_result.push_back(hld.query(a - 1, b - 1)); - } else { - continue; + std::cout << "Test 2:\n"; + + // Test details (Bamboo) + int n = 10; + std::vector node_values = {1, 8, 6, 8, 6, 2, 9, 2, 3, 2}; + std::vector> edges = {{10, 5}, {6, 2}, {10, 7}, + {5, 2}, {3, 9}, {8, 3}, + {1, 4}, {6, 4}, {8, 7}}; + std::vector> queries = { + {2, 1, 10}, {2, 1, 6}, {1, 3, 4}, {2, 1, 9}, {1, 5, 3}, + {1, 7, 8}, {2, 1, 4}, {2, 1, 8}, {1, 1, 4}, {1, 2, 7}}; + std::vector expected_result = {27, 11, 45, 9, 34}; + std::vector code_result; + + range_queries::heavy_light_decomposition::HLD hld(n); + hld.set_node_val(node_values); + for (int i = 0; i < n - 1; i++) { + int u = edges[i][0], v = edges[i][1]; + hld.add_edge(u - 1, v - 1); } - } - for (int i = 0; i < static_cast(expected_result.size()); i++) { - assert(expected_result[i] == code_result[i]); - } - std::cout << "\nTest2 passed!\n"; + hld.init(); + for (const auto &q : queries) { + int type = q[0]; + if (type == 1) { + int p = q[1], x = q[2]; + hld.update(p - 1, x); + } else if (type == 2) { + int a = q[1], b = q[2]; + code_result.push_back(hld.query(a - 1, b - 1)); + } else { + continue; + } + } + for (int i = 0; i < static_cast(expected_result.size()); i++) { + assert(expected_result[i] == code_result[i]); + } + std::cout << "\nTest2 passed!\n"; } /** @@ -590,50 +597,50 @@ static void test_2() { * @returns void */ static void test_3() { - std::cout << "Test 3:\n"; - - // Test details - int n = 8; - std::vector node_values = {1, 8, 6, 8, 6, 2, 9, 2}; - std::vector> edges = {{1, 2}, {2, 3}, {3, 4}, {1, 5}, - {6, 3}, {7, 5}, {8, 7}}; - std::vector> queries = { - {2, 6, 8}, {2, 3, 6}, {1, 3, 4}, {2, 7, 1}, {1, 5, 3}, - {1, 7, 8}, {2, 6, 4}, {2, 7, 8}, {1, 1, 4}, {1, 2, 7}}; - std::vector expected_result = {34, 8, 16, 14, 10}; - std::vector code_result; - - range_queries::heavy_light_decomposition::HLD hld(n); - hld.set_node_val(node_values); - for (int i = 0; i < n - 1; i++) { - int u = edges[i][0], v = edges[i][1]; - hld.add_edge(u - 1, v - 1); - } - hld.init(); - for (const auto &q : queries) { - int type = q[0]; - if (type == 1) { - int p = q[1], x = q[2]; - hld.update(p - 1, x); - } else if (type == 2) { - int a = q[1], b = q[2]; - code_result.push_back(hld.query(a - 1, b - 1)); - } else { - continue; + std::cout << "Test 3:\n"; + + // Test details + int n = 8; + std::vector node_values = {1, 8, 6, 8, 6, 2, 9, 2}; + std::vector> edges = {{1, 2}, {2, 3}, {3, 4}, {1, 5}, + {6, 3}, {7, 5}, {8, 7}}; + std::vector> queries = { + {2, 6, 8}, {2, 3, 6}, {1, 3, 4}, {2, 7, 1}, {1, 5, 3}, + {1, 7, 8}, {2, 6, 4}, {2, 7, 8}, {1, 1, 4}, {1, 2, 7}}; + std::vector expected_result = {34, 8, 16, 14, 10}; + std::vector code_result; + + range_queries::heavy_light_decomposition::HLD hld(n); + hld.set_node_val(node_values); + for (int i = 0; i < n - 1; i++) { + int u = edges[i][0], v = edges[i][1]; + hld.add_edge(u - 1, v - 1); + } + hld.init(); + for (const auto &q : queries) { + int type = q[0]; + if (type == 1) { + int p = q[1], x = q[2]; + hld.update(p - 1, x); + } else if (type == 2) { + int a = q[1], b = q[2]; + code_result.push_back(hld.query(a - 1, b - 1)); + } else { + continue; + } + } + for (int i = 0; i < static_cast(expected_result.size()); i++) { + assert(expected_result[i] == code_result[i]); } - } - for (int i = 0; i < static_cast(expected_result.size()); i++) { - assert(expected_result[i] == code_result[i]); - } - std::cout << "\nTest3 passed!\n"; + std::cout << "\nTest3 passed!\n"; } /** * Main function */ int main() { - test_1(); - test_2(); - test_3(); - return 0; + test_1(); + test_2(); + test_3(); + return 0; } diff --git a/range_queries/persistent_seg_tree_lazy_prop.cpp b/range_queries/persistent_seg_tree_lazy_prop.cpp index 9c84e2c4d39..638f889776d 100644 --- a/range_queries/persistent_seg_tree_lazy_prop.cpp +++ b/range_queries/persistent_seg_tree_lazy_prop.cpp @@ -22,6 +22,7 @@ * * @author [Magdy Sedra](https://github.com/MSedra) */ +#include /// for std::uint32_t #include /// for IO operations #include /// to manage dynamic memory #include /// for std::vector