chore: try to comply to C++20 (#2978)

* chroe: add cstdint

* fix: remove <> from explicit function
This commit is contained in:
realstealthninja
2025-08-15 05:24:35 +00:00
committed by GitHub
parent e9caade55e
commit 79aeaa9b52
4 changed files with 489 additions and 478 deletions

View File

@@ -9,6 +9,7 @@
*/
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>

View File

@@ -22,12 +22,14 @@
#include <algorithm> /// for `std::reverse` function
#include <array> /// for `std::array`, representing `EightPuzzle` board
#include <cassert> /// for `assert`
#include <cstdint> /// for `std::uint32_t`
#include <functional> /// for `std::function` STL
#include <iostream> /// for IO operations
#include <map> /// for `std::map` STL
#include <memory> /// for `std::shared_ptr`
#include <set> /// for `std::set` STL
#include <vector> /// for `std::vector` STL
/**
* @namespace machine_learning
* @brief Machine learning algorithms

View File

@@ -75,10 +75,11 @@ namespace heavy_light_decomposition {
* kth_ancestor :- returns the kth ancestor
* lca :- returns the least common ancestor
*/
template <typename X> class Tree {
template <typename X>
class Tree {
//
private:
private:
std::vector<std::list<int>>
t_adj; ///< an adjacency list to stores the tree edges
const int t_nodes, ///< number of nodes
@@ -90,7 +91,8 @@ private:
int t_root; ///< the root of the tree
std::vector<X> t_val; ///< values of nodes
template <typename T> friend class HLD;
template <typename T>
friend class HLD;
/**
* @brief Utility function to compute sub-tree sizes
@@ -131,7 +133,7 @@ private:
}
}
public:
public:
/**
* @brief Class parameterized constructor, resizes the and initializes the
* data members
@@ -178,9 +180,9 @@ public:
}
/**
* @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
* @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() {
@@ -251,8 +253,9 @@ public:
* @brief Segment Tree, to store heavy chains
* @tparam the data type of the values stored in the tree nodes
*/
template <typename X> class SG {
private:
template <typename X>
class SG {
private:
/**
* @brief Everything here is private,
* and can only be accessed through the methods,
@@ -262,11 +265,12 @@ private:
std::vector<X> 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 <typename T> friend class HLD;
template <typename T>
friend class HLD;
/**
* @brief Function that specifies the type of operation involved when segments
* are combined
* @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
@@ -274,8 +278,8 @@ private:
X combine(X lhs, X rhs) { return lhs + rhs; }
/**
* @brief Class parameterized constructor. Resizes the and initilizes the data
* members.
* @brief Class parameterized constructor. Resizes the and initilizes the
* data members.
* @param nodes the total number of nodes in the tree
* @returns void
*/
@@ -316,7 +320,8 @@ private:
}
/**
* @brief Set the initialization for the query data type, based on requirement
* @brief Set the initialization for the query data type, based on
* requirement
*
* @details
* Change the sret_init, based on requirement:
@@ -333,8 +338,9 @@ private:
* @brief The Heavy-Light Decomposition class
* @tparam the data type of the values stored in the tree nodes
*/
template <typename X> class HLD : public Tree<X>, public SG<X> {
private:
template <typename X>
class HLD : public Tree<X>, public SG<X> {
private:
int label; ///< utility member to assign labels in dfs_labels()
std::vector<int> h_label, ///< stores the label of a node
h_heavychlid, ///< stores the heavy child of a node
@@ -362,8 +368,8 @@ private:
}
/**
* @brief Utility function to assign highest parent that can be reached though
* heavy chains
* @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
@@ -426,13 +432,13 @@ private:
return ret;
}
public:
public:
/**
* @brief Class parameterized constructor. Resizes the and initilizes the data
* members.
* @brief Class parameterized constructor. Resizes the and initilizes the
* data members.
* @param nodes the total number of nodes in the tree
*/
explicit HLD<X>(int nodes) : Tree<X>(nodes), SG<X>(nodes) {
explicit HLD(int nodes) : Tree<X>(nodes), SG<X>(nodes) {
/* Initialization and resize vectors */
label = 0;
h_label.assign(Tree<X>::t_nodes, -1);
@@ -442,9 +448,9 @@ public:
}
/**
* @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
* @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() {
@@ -461,8 +467,8 @@ public:
SG<X>::s_tree[h_label[i] + Tree<X>::t_nodes] = Tree<X>::t_val[i];
}
for (int i = Tree<X>::t_nodes - 1; i > 0; i--) {
SG<X>::s_tree[i] =
SG<X>::combine(SG<X>::s_tree[i << 1], SG<X>::s_tree[i << 1 | 1]);
SG<X>::s_tree[i] = SG<X>::combine(SG<X>::s_tree[i << 1],
SG<X>::s_tree[i << 1 | 1]);
}
}
@@ -479,8 +485,8 @@ public:
}
/**
* @brief This function returns the sum of node values in the simple path from
* from node_1 to node_2
* @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)
@@ -552,8 +558,9 @@ static void test_2() {
// Test details (Bamboo)
int n = 10;
std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2, 3, 2};
std::vector<std::vector<int>> edges = {
{10, 5}, {6, 2}, {10, 7}, {5, 2}, {3, 9}, {8, 3}, {1, 4}, {6, 4}, {8, 7}};
std::vector<std::vector<int>> edges = {{10, 5}, {6, 2}, {10, 7},
{5, 2}, {3, 9}, {8, 3},
{1, 4}, {6, 4}, {8, 7}};
std::vector<std::vector<int>> 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}};

View File

@@ -22,6 +22,7 @@
*
* @author [Magdy Sedra](https://github.com/MSedra)
*/
#include <cstdint> /// for std::uint32_t
#include <iostream> /// for IO operations
#include <memory> /// to manage dynamic memory
#include <vector> /// for std::vector