diff --git a/AcyclicSP.cpp b/AcyclicSP.cpp index 8aa3cd5..b59ca6b 100644 --- a/AcyclicSP.cpp +++ b/AcyclicSP.cpp @@ -1,7 +1,7 @@ #include "AcyclicSP.h" #include "Topological.h" -AcyclicSP::AcyclicSP(const EdgeWeightedDigraph& G, int s) : SP(G, s) { +AcyclicSP::AcyclicSP(const EdgeWeightedDigraph &G, int s) : SP(G, s) { Topological top(G); - for (int v : top.order()) relax(G, v); + for (int v: top.order()) relax(G, v); } diff --git a/AcyclicSP.h b/AcyclicSP.h index f76db11..abf6b30 100644 --- a/AcyclicSP.h +++ b/AcyclicSP.h @@ -6,7 +6,7 @@ class AcyclicSP : public SP { public: - AcyclicSP(const EdgeWeightedDigraph& G, int s); + AcyclicSP(const EdgeWeightedDigraph &G, int s); }; diff --git a/BST.h b/BST.h index b1b7b3d..4562d5b 100644 --- a/BST.h +++ b/BST.h @@ -5,7 +5,7 @@ #include "OrderedST.h" #include -template +template class BST : public OrderedST { protected: struct Node { @@ -14,18 +14,19 @@ class BST : public OrderedST { std::shared_ptr left, right; int N; // 以该结点为根的子树中的结点总数 - Node(Key key, Value val, int N) : key(key), val(val), N(N) {} + Node(Key key, Value val, int N) : key(key), val(val), N(N) { + } virtual ~Node() = default; }; std::shared_ptr root; - std::optional get(std::shared_ptr x, const Key& key) const; + std::optional get(std::shared_ptr x, const Key &key) const; - std::shared_ptr put(std::shared_ptr x, const Key& key, const Value& val); + std::shared_ptr put(std::shared_ptr x, const Key &key, const Value &val); - std::shared_ptr remove(std::shared_ptr x, const Key& key); + std::shared_ptr remove(std::shared_ptr x, const Key &key); int size(std::shared_ptr x) const; @@ -33,11 +34,11 @@ class BST : public OrderedST { std::shared_ptr max(std::shared_ptr x) const; - std::shared_ptr floor(std::shared_ptr x, const Key& key) const; + std::shared_ptr floor(std::shared_ptr x, const Key &key) const; - std::shared_ptr ceiling(std::shared_ptr x, const Key& key) const; + std::shared_ptr ceiling(std::shared_ptr x, const Key &key) const; - int rank(std::shared_ptr x, const Key& key) const; + int rank(std::shared_ptr x, const Key &key) const; std::shared_ptr select(std::shared_ptr x, int k) const; @@ -45,14 +46,14 @@ class BST : public OrderedST { std::shared_ptr removeMax(std::shared_ptr x); - void keys(std::shared_ptr x, std::list& queue, const Key& lo, const Key& hi) const; + void keys(std::shared_ptr x, std::list &queue, const Key &lo, const Key &hi) const; public: - std::optional get(const Key& key) const override { return get(root, key); } + std::optional get(const Key &key) const override { return get(root, key); } - void put(const Key& key, const Value& val) override { root = put(root, key, val); } + void put(const Key &key, const Value &val) override { root = put(root, key, val); } - void remove(const Key& key) override { root = remove(root, key); } + void remove(const Key &key) override { root = remove(root, key); } int size() const override { return size(root); } @@ -60,11 +61,11 @@ class BST : public OrderedST { std::optional max() const override { return max(root)->key; } - std::optional floor(const Key& key) const override; + std::optional floor(const Key &key) const override; - std::optional ceiling(const Key& key) const override; + std::optional ceiling(const Key &key) const override; - int rank(const Key& key) const override { return rank(root, key); } + int rank(const Key &key) const override { return rank(root, key); } std::optional select(int k) const override { return select(root, k)->key; } @@ -74,20 +75,20 @@ class BST : public OrderedST { using OrderedST::keys; - std::list keys(const Key& lo, const Key& hi) const override; + std::list keys(const Key &lo, const Key &hi) const override; }; -template -std::optional BST::get(std::shared_ptr x, const Key& key) const { +template +std::optional BST::get(std::shared_ptr x, const Key &key) const { if (!x) return std::nullopt; if (key < x->key) return get(x->left, key); else if (key > x->key) return get(x->right, key); else return x->val; } -template +template std::shared_ptr::Node> -BST::put(std::shared_ptr x, const Key& key, const Value& val) { +BST::put(std::shared_ptr x, const Key &key, const Value &val) { if (!x) return std::make_shared(key, val, 1); if (key < x->key) x->left = put(x->left, key, val); else if (key > x->key) x->right = put(x->right, key, val); @@ -96,8 +97,8 @@ BST::put(std::shared_ptr x, const Key& key, const Value& val) return x; } -template -std::shared_ptr::Node> BST::remove(std::shared_ptr x, const Key& key) { +template +std::shared_ptr::Node> BST::remove(std::shared_ptr x, const Key &key) { if (!x) return nullptr; if (key < x->key) x->left = remove(x->left, key); else if (key > x->key) x->right = remove(x->right, key); @@ -113,26 +114,26 @@ std::shared_ptr::Node> BST::remove(std::sha return x; } -template +template int BST::size(std::shared_ptr x) const { if (!x) return 0; return x->N; } -template +template std::shared_ptr::Node> BST::min(std::shared_ptr x) const { if (!x->left) return x; return min(x->left); } -template +template std::shared_ptr::Node> BST::max(std::shared_ptr x) const { if (!x->right) return x; return max(x->right); } -template -std::shared_ptr::Node> BST::floor(std::shared_ptr x, const Key& key) const { +template +std::shared_ptr::Node> BST::floor(std::shared_ptr x, const Key &key) const { if (!x) return nullptr; if (key < x->key) return floor(x->left, key); else if (key == x->key) return x; @@ -143,9 +144,9 @@ std::shared_ptr::Node> BST::floor(std::shar } } -template +template std::shared_ptr::Node> -BST::ceiling(std::shared_ptr x, const Key& key) const { +BST::ceiling(std::shared_ptr x, const Key &key) const { if (!x) return nullptr; if (key > x->key) return ceiling(x->right, key); else if (key == x->key) return x; @@ -156,8 +157,8 @@ BST::ceiling(std::shared_ptr x, const Key& key) const { } } -template -int BST::rank(std::shared_ptr x, const Key& key) const { +template +int BST::rank(std::shared_ptr x, const Key &key) const { if (!x) return 0; if (key < x->key) return rank(x->left, key); else if (key > x->key) @@ -165,7 +166,7 @@ int BST::rank(std::shared_ptr x, const Key& key) const { else return size(x->left); } -template +template std::shared_ptr::Node> BST::select(std::shared_ptr x, int k) const { if (!x) return nullptr; int t = size(x->left); @@ -174,7 +175,7 @@ std::shared_ptr::Node> BST::select(std::sha else return x; } -template +template std::shared_ptr::Node> BST::removeMin(std::shared_ptr x) { if (!x->left) return x->right; x->left = removeMin(x->left); @@ -182,7 +183,7 @@ std::shared_ptr::Node> BST::removeMin(std:: return x; } -template +template std::shared_ptr::Node> BST::removeMax(std::shared_ptr x) { if (!x->right) return x->left; x->right = removeMax(x->right); @@ -190,30 +191,30 @@ std::shared_ptr::Node> BST::removeMax(std:: return x; } -template -void BST::keys(std::shared_ptr x, std::list& queue, const Key& lo, const Key& hi) const { +template +void BST::keys(std::shared_ptr x, std::list &queue, const Key &lo, const Key &hi) const { if (!x) return; if (lo < x->key) keys(x->left, queue, lo, hi); if (lo <= x->key && hi >= x->key) queue.push_back(x->key); if (hi > x->key) keys(x->right, queue, lo, hi); } -template -std::optional BST::floor(const Key& key) const { +template +std::optional BST::floor(const Key &key) const { auto x = floor(root, key); if (!x) return std::nullopt; return x->key; } -template -std::optional BST::ceiling(const Key& key) const { +template +std::optional BST::ceiling(const Key &key) const { auto x = ceiling(root, key); if (!x) return std::nullopt; return x->key; } -template -std::list BST::keys(const Key& lo, const Key& hi) const { +template +std::list BST::keys(const Key &lo, const Key &hi) const { std::list queue; keys(root, queue, lo, hi); return queue; diff --git a/BinarySearchST.h b/BinarySearchST.h index d48af94..a163ad5 100644 --- a/BinarySearchST.h +++ b/BinarySearchST.h @@ -5,21 +5,22 @@ #include "OrderedST.h" #include -template +template class BinarySearchST : public OrderedST { private: - std::vector> keys_; - std::vector> vals; + std::vector > keys_; + std::vector > vals; int N = 0; public: - BinarySearchST(int capacity) : keys_(capacity), vals(capacity) {} + BinarySearchST(int capacity) : keys_(capacity), vals(capacity) { + } - std::optional get(const Key& key) const override; + std::optional get(const Key &key) const override; - void put(const Key& key, const Value& val) override; + void put(const Key &key, const Value &val) override; - void remove(const Key& key) override; + void remove(const Key &key) override; int size() const override { return N; } @@ -27,27 +28,27 @@ class BinarySearchST : public OrderedST { std::optional max() const override { return keys_[N - 1]; } - std::optional floor(const Key& key) const override; + std::optional floor(const Key &key) const override; - std::optional ceiling(const Key& key) const override; + std::optional ceiling(const Key &key) const override; - int rank(const Key& key) const override; + int rank(const Key &key) const override; std::optional select(int k) const override { return keys_[k]; } - std::list keys(const Key& lo, const Key& hi) const override; + std::list keys(const Key &lo, const Key &hi) const override; }; -template -std::optional BinarySearchST::get(const Key& key) const { +template +std::optional BinarySearchST::get(const Key &key) const { if (this->isEmpty()) return std::nullopt; int i = rank(key); if (i < N && keys_[i] == key) return vals[i]; else return std::nullopt; } -template -void BinarySearchST::put(const Key& key, const Value& val) { +template +void BinarySearchST::put(const Key &key, const Value &val) { int i = rank(key); if (i < N && keys_[i] == key) { vals[i] = val; @@ -62,8 +63,8 @@ void BinarySearchST::put(const Key& key, const Value& val) { ++N; } -template -void BinarySearchST::remove(const Key& key) { +template +void BinarySearchST::remove(const Key &key) { if (this->isEmpty()) return; int i = rank(key); if (i < N && keys_[i] == key) { @@ -74,24 +75,24 @@ void BinarySearchST::remove(const Key& key) { } --N; keys_[N] = std::nullopt; // 置空 - vals[N] = std::nullopt; // 置空 + vals[N] = std::nullopt; // 置空 } -template -std::optional BinarySearchST::floor(const Key& key) const { +template +std::optional BinarySearchST::floor(const Key &key) const { int i = rank(key); if (i < N && keys_[i] == key) return keys_[i]; else return keys_[i - 1]; } -template -std::optional BinarySearchST::ceiling(const Key& key) const { +template +std::optional BinarySearchST::ceiling(const Key &key) const { int i = rank(key); return keys_[i]; } -template -int BinarySearchST::rank(const Key& key) const { +template +int BinarySearchST::rank(const Key &key) const { int lo = 0, hi = N - 1; while (lo <= hi) { int mid = (lo + hi) / 2; @@ -102,8 +103,8 @@ int BinarySearchST::rank(const Key& key) const { return lo; } -template -std::list BinarySearchST::keys(const Key& lo, const Key& hi) const { +template +std::list BinarySearchST::keys(const Key &lo, const Key &hi) const { std::list queue; if (hi < lo) return queue; for (int i = rank(lo); i < rank(hi); ++i) queue.push_back(*keys_[i]); diff --git a/BreadthFirstPaths.cpp b/BreadthFirstPaths.cpp index a33cb7d..66fc3a2 100644 --- a/BreadthFirstPaths.cpp +++ b/BreadthFirstPaths.cpp @@ -1,14 +1,14 @@ #include "BreadthFirstPaths.h" #include -void BreadthFirstPaths::bfs(const Graph& G, int s) { +void BreadthFirstPaths::bfs(const Graph &G, int s) { std::queue queue; marked[s] = true; queue.push(s); while (!queue.empty()) { int v = queue.front(); queue.pop(); - for (int w : G.adj(v)) { + for (int w: G.adj(v)) { if (!marked[w]) { edgeTo[w] = v; marked[w] = true; diff --git a/BreadthFirstPaths.h b/BreadthFirstPaths.h index fcf6f6e..4e90ddf 100644 --- a/BreadthFirstPaths.h +++ b/BreadthFirstPaths.h @@ -6,10 +6,10 @@ class BreadthFirstPaths : public Paths { private: - void bfs(const Graph& G, int s); + void bfs(const Graph &G, int s); public: - BreadthFirstPaths(const Graph& G, int s) : Paths(G, s) { bfs(G, s); } + BreadthFirstPaths(const Graph &G, int s) : Paths(G, s) { bfs(G, s); } }; diff --git a/CC.cpp b/CC.cpp index 6f551a1..6c8720b 100644 --- a/CC.cpp +++ b/CC.cpp @@ -1,6 +1,6 @@ #include "CC.h" -CC::CC(const Graph& G) : CCBase(G) { +CC::CC(const Graph &G) : CCBase(G) { for (int s = 0; s < G.V(); ++s) { if (!marked[s]) { dfs(G, s); diff --git a/CC.h b/CC.h index d6c3d21..1853622 100644 --- a/CC.h +++ b/CC.h @@ -7,7 +7,7 @@ class CC : public CCBase { public: - CC(const Graph& G); + CC(const Graph &G); }; diff --git a/CCBase.cpp b/CCBase.cpp index 8e766cc..eaa5aee 100644 --- a/CCBase.cpp +++ b/CCBase.cpp @@ -1,9 +1,9 @@ #include "CCBase.h" -void CCBase::dfs(const GraphBase& G, int v) { +void CCBase::dfs(const GraphBase &G, int v) { marked[v] = true; id_[v] = count_; - for (int w : G.adj(v)) { + for (int w: G.adj(v)) { if (!marked[w]) dfs(G, w); } } diff --git a/CCBase.h b/CCBase.h index 90489ac..4ecba75 100644 --- a/CCBase.h +++ b/CCBase.h @@ -11,10 +11,11 @@ class CCBase { std::vector id_; int count_ = 0; - void dfs(const GraphBase& G, int v); + void dfs(const GraphBase &G, int v); public: - CCBase(const GraphBase& G) : marked(G.V()), id_(G.V()) {} + CCBase(const GraphBase &G) : marked(G.V()), id_(G.V()) { + } virtual ~CCBase() = default; diff --git a/DepthFirstOrder.h b/DepthFirstOrder.h index 589cca6..07b5c0f 100644 --- a/DepthFirstOrder.h +++ b/DepthFirstOrder.h @@ -13,12 +13,12 @@ class DepthFirstOrder { std::vector marked; std::list pre_, post_, reversePost_; - template - void dfs(const GraphBase& G, int v); + template + void dfs(const GraphBase &G, int v); public: - template - DepthFirstOrder(const GraphBase& G); + template + DepthFirstOrder(const GraphBase &G); std::list pre() const { return pre_; } @@ -27,11 +27,11 @@ class DepthFirstOrder { std::list reversePost() const { return reversePost_; } }; -template -void DepthFirstOrder::dfs(const GraphBase& G, int v) { +template +void DepthFirstOrder::dfs(const GraphBase &G, int v) { pre_.push_back(v); marked[v] = true; - for (const auto& e : G.adj(v)) { + for (const auto &e: G.adj(v)) { int w; if constexpr (std::is_same_v, DirectedEdge>) w = e.to(); else w = e; @@ -41,8 +41,8 @@ void DepthFirstOrder::dfs(const GraphBase& G, int v) { reversePost_.push_front(v); } -template -DepthFirstOrder::DepthFirstOrder(const GraphBase& G) : marked(G.V()) { +template +DepthFirstOrder::DepthFirstOrder(const GraphBase &G) : marked(G.V()) { for (int v = 0; v < G.V(); ++v) { if (!marked[v]) dfs(G, v); } diff --git a/DepthFirstPaths.cpp b/DepthFirstPaths.cpp index 0b4c616..0dbcb42 100644 --- a/DepthFirstPaths.cpp +++ b/DepthFirstPaths.cpp @@ -1,8 +1,8 @@ #include "DepthFirstPaths.h" -void DepthFirstPaths::dfs(const Graph& G, int v) { +void DepthFirstPaths::dfs(const Graph &G, int v) { marked[v] = true; - for (int w : G.adj(v)) { + for (int w: G.adj(v)) { if (!marked[w]) { edgeTo[w] = v; dfs(G, w); diff --git a/DepthFirstPaths.h b/DepthFirstPaths.h index 559645e..a64ec39 100644 --- a/DepthFirstPaths.h +++ b/DepthFirstPaths.h @@ -6,10 +6,10 @@ class DepthFirstPaths : public Paths { private: - void dfs(const Graph& G, int v); + void dfs(const Graph &G, int v); public: - DepthFirstPaths(const Graph& G, int s) : Paths(G, s) { dfs(G, s); } + DepthFirstPaths(const Graph &G, int s) : Paths(G, s) { dfs(G, s); } }; diff --git a/Digraph.cpp b/Digraph.cpp index 5f3c1a9..7e035ca 100644 --- a/Digraph.cpp +++ b/Digraph.cpp @@ -1,6 +1,6 @@ #include "Digraph.h" -Digraph::Digraph(std::istream&& in) : GraphBase(in) { +Digraph::Digraph(std::istream &&in) : GraphBase(in) { int E; in >> E; for (int i = 0; i < E; ++i) { @@ -18,7 +18,7 @@ void Digraph::addEdge(int v, int w) { Digraph Digraph::reverse() const { Digraph R(V_); for (int v = 0; v < V_; ++v) { - for (int w : adj(v)) R.addEdge(w, v); + for (int w: adj(v)) R.addEdge(w, v); } return R; } diff --git a/Digraph.h b/Digraph.h index 0a65c8f..685b4d5 100644 --- a/Digraph.h +++ b/Digraph.h @@ -7,9 +7,10 @@ class Digraph : public GraphBase { public: - Digraph(int V) : GraphBase(V) {} + Digraph(int V) : GraphBase(V) { + } - Digraph(std::istream&& in); + Digraph(std::istream &&in); void addEdge(int v, int w); diff --git a/DijkstraSP.cpp b/DijkstraSP.cpp index 9df96f6..65f6d14 100644 --- a/DijkstraSP.cpp +++ b/DijkstraSP.cpp @@ -1,11 +1,11 @@ #include "DijkstraSP.h" -void DijkstraSP::onRelax(const DirectedEdge& e, int w) { +void DijkstraSP::onRelax(const DirectedEdge &e, int w) { if (pq.contains(w)) pq.change(w, distTo_[w]); else pq.insert(w, distTo_[w]); } -DijkstraSP::DijkstraSP(const EdgeWeightedDigraph& G, int s) : SP(G, s), pq(G.V()) { +DijkstraSP::DijkstraSP(const EdgeWeightedDigraph &G, int s) : SP(G, s), pq(G.V()) { pq.insert(s, 0.0); while (!pq.isEmpty()) relax(G, *pq.delMin()); } diff --git a/DijkstraSP.h b/DijkstraSP.h index e64adcb..70a4e0a 100644 --- a/DijkstraSP.h +++ b/DijkstraSP.h @@ -9,10 +9,10 @@ class DijkstraSP : public SP { private: IndexMinPQ pq; - void onRelax(const DirectedEdge& e, int w) override; + void onRelax(const DirectedEdge &e, int w) override; public: - DijkstraSP(const EdgeWeightedDigraph& G, int s); + DijkstraSP(const EdgeWeightedDigraph &G, int s); }; diff --git a/DirectedCycle.h b/DirectedCycle.h index d473e13..50d0d82 100644 --- a/DirectedCycle.h +++ b/DirectedCycle.h @@ -15,23 +15,23 @@ class DirectedCycle { std::list cycle_; std::vector onStack; // 栈上的所有顶点 - template - void dfs(const GraphBase& G, int v); + template + void dfs(const GraphBase &G, int v); public: - template - DirectedCycle(const GraphBase& G); + template + DirectedCycle(const GraphBase &G); bool hasCycle() const { return !cycle_.empty(); } std::list cycle() const { return cycle_; } }; -template -void DirectedCycle::dfs(const GraphBase& G, int v) { +template +void DirectedCycle::dfs(const GraphBase &G, int v) { onStack[v] = true; marked[v] = true; - for (const auto& e : G.adj(v)) { + for (const auto &e: G.adj(v)) { int w; if constexpr (std::is_same_v, DirectedEdge>) w = e.to(); else w = e; @@ -49,8 +49,8 @@ void DirectedCycle::dfs(const GraphBase& G, int v) { onStack[v] = false; } -template -DirectedCycle::DirectedCycle(const GraphBase& G) : marked(G.V()), edgeTo(G.V()), onStack(G.V()) { +template +DirectedCycle::DirectedCycle(const GraphBase &G) : marked(G.V()), edgeTo(G.V()), onStack(G.V()) { for (int v = 0; v < G.V(); ++v) { if (!marked[v]) dfs(G, v); } diff --git a/DirectedDFS.cpp b/DirectedDFS.cpp index 9133b59..a9c139d 100644 --- a/DirectedDFS.cpp +++ b/DirectedDFS.cpp @@ -1,14 +1,14 @@ #include "DirectedDFS.h" -void DirectedDFS::dfs(const Digraph& G, int v) { +void DirectedDFS::dfs(const Digraph &G, int v) { marked_[v] = true; - for (int w : G.adj(v)) { + for (int w: G.adj(v)) { if (!marked_[w]) dfs(G, w); } } -DirectedDFS::DirectedDFS(const Digraph& G, const std::list& sources) : marked_(G.V()) { - for (int s : sources) { +DirectedDFS::DirectedDFS(const Digraph &G, const std::list &sources) : marked_(G.V()) { + for (int s: sources) { if (!marked_[s]) dfs(G, s); } } diff --git a/DirectedDFS.h b/DirectedDFS.h index d4f312c..632dce4 100644 --- a/DirectedDFS.h +++ b/DirectedDFS.h @@ -10,12 +10,12 @@ class DirectedDFS { private: std::vector marked_; - void dfs(const Digraph& G, int v); + void dfs(const Digraph &G, int v); public: - DirectedDFS(const Digraph& G, int s) : marked_(G.V()) { dfs(G, s); } + DirectedDFS(const Digraph &G, int s) : marked_(G.V()) { dfs(G, s); } - DirectedDFS(const Digraph& G, const std::list& sources); + DirectedDFS(const Digraph &G, const std::list &sources); bool marked(int v) const { return marked_[v]; } }; diff --git a/DirectedEdge.cpp b/DirectedEdge.cpp index 4d19ee7..2c88d4c 100644 --- a/DirectedEdge.cpp +++ b/DirectedEdge.cpp @@ -1,7 +1,7 @@ #include "DirectedEdge.h" #include -std::ostream& operator<<(std::ostream& os, const DirectedEdge& e) { +std::ostream &operator<<(std::ostream &os, const DirectedEdge &e) { return os << e.edge_->v << "->" << e.edge_->w << " " << std::fixed << std::setprecision(2) << e.edge_->weight << - " "; + " "; } diff --git a/DirectedEdge.h b/DirectedEdge.h index 017debe..dd29284 100644 --- a/DirectedEdge.h +++ b/DirectedEdge.h @@ -15,10 +15,10 @@ class DirectedEdge : private Edge { using Edge::operator bool; - friend std::ostream& operator<<(std::ostream& os, const DirectedEdge& e); + friend std::ostream &operator<<(std::ostream &os, const DirectedEdge &e); }; -std::ostream& operator<<(std::ostream& os, const DirectedEdge& e); +std::ostream &operator<<(std::ostream &os, const DirectedEdge &e); #endif //DIRECTEDEDGE_H diff --git a/Edge.cpp b/Edge.cpp index 9ce8d6a..42e15ba 100644 --- a/Edge.cpp +++ b/Edge.cpp @@ -8,10 +8,10 @@ int Edge::other(int vertex) const { else throw std::runtime_error("Inconsistent edge"); } -bool operator<(const Edge& l, const Edge& r) { return l.weight() < r.weight(); } +bool operator<(const Edge &l, const Edge &r) { return l.weight() < r.weight(); } -bool operator>(const Edge& l, const Edge& r) { return l.weight() > r.weight(); } +bool operator>(const Edge &l, const Edge &r) { return l.weight() > r.weight(); } -std::ostream& operator<<(std::ostream& os, const Edge& e) { +std::ostream &operator<<(std::ostream &os, const Edge &e) { return os << e.edge_->v << "-" << e.edge_->w << " " << std::fixed << std::setprecision(2) << e.edge_->weight << " "; } diff --git a/Edge.h b/Edge.h index 7f27670..a737b75 100644 --- a/Edge.h +++ b/Edge.h @@ -8,10 +8,11 @@ class Edge { protected: struct Edge_ { - const int v, w; // 顶点 + const int v, w; // 顶点 const double weight; // 边的权重 - Edge_(int v, int w, double weight) : v(v), w(w), weight(weight) {} + Edge_(int v, int w, double weight) : v(v), w(w), weight(weight) { + } }; std::shared_ptr edge_; @@ -19,7 +20,8 @@ class Edge { public: Edge() = default; - Edge(int v, int w, double weight) : edge_(std::make_shared(v, w, weight)) {} + Edge(int v, int w, double weight) : edge_(std::make_shared(v, w, weight)) { + } double weight() const { return edge_->weight; } @@ -32,11 +34,11 @@ class Edge { friend std::ostream &operator<<(std::ostream &os, const Edge &e); }; -bool operator<(const Edge& l, const Edge& r); +bool operator<(const Edge &l, const Edge &r); -bool operator>(const Edge& l, const Edge& r); +bool operator>(const Edge &l, const Edge &r); -std::ostream& operator<<(std::ostream& os, const Edge& e); +std::ostream &operator<<(std::ostream &os, const Edge &e); #endif //ALGS4_EDGE_H diff --git a/EdgeWeightedDigraph.cpp b/EdgeWeightedDigraph.cpp index bd143e7..52da3ab 100644 --- a/EdgeWeightedDigraph.cpp +++ b/EdgeWeightedDigraph.cpp @@ -1,6 +1,6 @@ #include "EdgeWeightedDigraph.h" -EdgeWeightedDigraph::EdgeWeightedDigraph(std::istream&& in) : GraphBase(in) { +EdgeWeightedDigraph::EdgeWeightedDigraph(std::istream &&in) : GraphBase(in) { int E; in >> E; for (int i = 0; i < E; ++i) { @@ -11,7 +11,7 @@ EdgeWeightedDigraph::EdgeWeightedDigraph(std::istream&& in) : GraphBase(in) { } } -void EdgeWeightedDigraph::addEdge(const DirectedEdge& e) { +void EdgeWeightedDigraph::addEdge(const DirectedEdge &e) { adj_[e.from()].push_front(e); ++E_; } @@ -19,7 +19,7 @@ void EdgeWeightedDigraph::addEdge(const DirectedEdge& e) { std::list EdgeWeightedDigraph::edges() const { std::list bag; for (int v = 0; v < V_; ++v) { - for (const auto& e : adj_[v]) { + for (const auto &e: adj_[v]) { bag.push_front(e); } } diff --git a/EdgeWeightedDigraph.h b/EdgeWeightedDigraph.h index c2ef00a..9a6193c 100644 --- a/EdgeWeightedDigraph.h +++ b/EdgeWeightedDigraph.h @@ -9,11 +9,12 @@ class EdgeWeightedDigraph : public GraphBase { public: - EdgeWeightedDigraph(int V) : GraphBase(V) {} + EdgeWeightedDigraph(int V) : GraphBase(V) { + } - EdgeWeightedDigraph(std::istream&& in); + EdgeWeightedDigraph(std::istream &&in); - void addEdge(const DirectedEdge& e); + void addEdge(const DirectedEdge &e); std::list edges() const; }; diff --git a/EdgeWeightedGraph.cpp b/EdgeWeightedGraph.cpp index a3f10f3..dcbbb7e 100644 --- a/EdgeWeightedGraph.cpp +++ b/EdgeWeightedGraph.cpp @@ -1,6 +1,6 @@ #include "EdgeWeightedGraph.h" -EdgeWeightedGraph::EdgeWeightedGraph(std::istream&& in) : GraphBase(in) { +EdgeWeightedGraph::EdgeWeightedGraph(std::istream &&in) : GraphBase(in) { int E; in >> E; for (int i = 0; i < E; ++i) { @@ -11,7 +11,7 @@ EdgeWeightedGraph::EdgeWeightedGraph(std::istream&& in) : GraphBase(in) { } } -void EdgeWeightedGraph::addEdge(const Edge& e) { +void EdgeWeightedGraph::addEdge(const Edge &e) { int v = e.either(), w = e.other(v); adj_[v].push_front(e); adj_[w].push_front(e); @@ -21,7 +21,7 @@ void EdgeWeightedGraph::addEdge(const Edge& e) { std::list EdgeWeightedGraph::edges() const { std::list bag; for (int v = 0; v < V_; ++v) { - for (const auto& e : adj_[v]) { + for (const auto &e: adj_[v]) { if (e.other(v) > v) bag.push_front(e); } } diff --git a/EdgeWeightedGraph.h b/EdgeWeightedGraph.h index be969fc..e1957d8 100644 --- a/EdgeWeightedGraph.h +++ b/EdgeWeightedGraph.h @@ -9,11 +9,12 @@ class EdgeWeightedGraph : public GraphBase { public: - EdgeWeightedGraph(int V) : GraphBase(V) {} + EdgeWeightedGraph(int V) : GraphBase(V) { + } - EdgeWeightedGraph(std::istream&& in); + EdgeWeightedGraph(std::istream &&in); - void addEdge(const Edge& e); + void addEdge(const Edge &e); std::list edges() const; }; diff --git a/Graph.cpp b/Graph.cpp index 4f2db1e..48cd1d8 100644 --- a/Graph.cpp +++ b/Graph.cpp @@ -1,6 +1,6 @@ #include "Graph.h" -Graph::Graph(std::istream&& in) : GraphBase(in) { +Graph::Graph(std::istream &&in) : GraphBase(in) { int E; in >> E; for (int i = 0; i < E; ++i) { diff --git a/Graph.h b/Graph.h index 321e9f9..084f30f 100644 --- a/Graph.h +++ b/Graph.h @@ -7,9 +7,10 @@ class Graph : public GraphBase { public: - Graph(int V) : GraphBase(V) {} + Graph(int V) : GraphBase(V) { + } - Graph(std::istream&& in); + Graph(std::istream &&in); void addEdge(int v, int w); }; diff --git a/GraphBase.h b/GraphBase.h index 396bd0f..bede11a 100644 --- a/GraphBase.h +++ b/GraphBase.h @@ -6,17 +6,18 @@ #include #include -template +template class GraphBase { protected: - const int V_; // 顶点数目 - int E_; // 边的数目 - std::vector> adj_; // 邻接表 + const int V_; // 顶点数目 + int E_; // 边的数目 + std::vector > adj_; // 邻接表 public: - GraphBase(int V) : V_(V), E_(0), adj_(V) {} + GraphBase(int V) : V_(V), E_(0), adj_(V) { + } - GraphBase(std::istream& in); + GraphBase(std::istream &in); virtual ~GraphBase() = default; @@ -27,18 +28,19 @@ class GraphBase { std::list adj(int v) const { return adj_[v]; } }; -template -GraphBase::GraphBase(std::istream& in) : GraphBase([&in] { +template +GraphBase::GraphBase(std::istream &in) : GraphBase([&in] { int i; return in >> i, i; -}()) {} +}()) { +} -template -std::ostream& operator<<(std::ostream& os, const GraphBase& G) { +template +std::ostream &operator<<(std::ostream &os, const GraphBase &G) { os << G.V() << " vertices, " << G.E() << " edges" << "\n"; for (int v = 0; v < G.V(); ++v) { os << v << ": "; - for (T w : G.adj(v)) os << w << " "; + for (T w: G.adj(v)) os << w << " "; os << "\n"; } return os; diff --git a/Heap.h b/Heap.h index f66e9f1..dbadaba 100644 --- a/Heap.h +++ b/Heap.h @@ -6,22 +6,22 @@ class Heap : public Sorting { private: - template - static void sink(std::vector& a, int k, int N); + template + static void sink(std::vector &a, int k, int N); - template - static bool less(const std::vector& a, int i, int j) { return a[i - 1] < a[j - 1]; } + template + static bool less(const std::vector &a, int i, int j) { return a[i - 1] < a[j - 1]; } - template - static void exch(std::vector& a, int i, int j); + template + static void exch(std::vector &a, int i, int j); public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -void Heap::sink(std::vector& a, int k, int N) { +template +void Heap::sink(std::vector &a, int k, int N) { while (2 * k <= N) { int j = 2 * k; if (j < N && less(a, j, j + 1)) ++j; @@ -31,15 +31,15 @@ void Heap::sink(std::vector& a, int k, int N) { } } -template -void Heap::exch(std::vector& a, int i, int j) { +template +void Heap::exch(std::vector &a, int i, int j) { T t = a[i - 1]; a[i - 1] = a[j - 1]; a[j - 1] = t; } -template -void Heap::sort(std::vector& a) { +template +void Heap::sort(std::vector &a) { int N = a.size(); for (int k = N / 2; k >= 1; --k) sink(a, k, N); while (N > 1) { diff --git a/IndexMinPQ.h b/IndexMinPQ.h index d452589..59a7f99 100644 --- a/IndexMinPQ.h +++ b/IndexMinPQ.h @@ -6,52 +6,53 @@ #include #include -template +template class IndexMinPQ : private MinPQ { private: - std::vector> qp; // 逆序:qp[pq[i]] = pq[qp[i]] = i - std::vector> keys; // 元素 + std::vector > qp; // 逆序:qp[pq[i]] = pq[qp[i]] = i + std::vector > keys; // 元素 bool lower(int i, int j) const override { return keys[*pq[i]] > keys[*pq[j]]; } void exch(int i, int j) override; public: - IndexMinPQ(int maxN) : MinPQ(maxN), qp(maxN + 1), keys(maxN + 1) {} + IndexMinPQ(int maxN) : MinPQ(maxN), qp(maxN + 1), keys(maxN + 1) { + } using MinPQ::isEmpty, MinPQ::size; bool contains(int k) const { return qp[k].has_value(); } - void insert(int k, const Key& key); + void insert(int k, const Key &key); - void change(int k, const Key& key); + void change(int k, const Key &key); std::optional delMin(); }; -template +template void IndexMinPQ::exch(int i, int j) { MinPQ::exch(i, j); qp[*pq[i]] = i; qp[*pq[j]] = j; } -template -void IndexMinPQ::insert(int k, const Key& key) { +template +void IndexMinPQ::insert(int k, const Key &key) { qp[k] = N + 1; keys[k] = key; MinPQ::insert(k); } -template -void IndexMinPQ::change(int k, const Key& key) { +template +void IndexMinPQ::change(int k, const Key &key) { keys[k] = key; swim(*qp[k]); sink(*qp[k]); } -template +template std::optional IndexMinPQ::delMin() { int indexOfLast = *pq[N + 1]; int indexOfMin = *MinPQ::delMin(); diff --git a/Insertion.h b/Insertion.h index 478a2ff..a993881 100644 --- a/Insertion.h +++ b/Insertion.h @@ -6,12 +6,12 @@ class Insertion : public Sorting { public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -void Insertion::sort(std::vector& a) { +template +void Insertion::sort(std::vector &a) { int N = a.size(); for (int i = 1; i < N; ++i) { // 将a[i]插入到a[i-1]、a[i-2]、a[i-3]...之中 diff --git a/KosarajuSCC.cpp b/KosarajuSCC.cpp index ce8e9ca..9908bf1 100644 --- a/KosarajuSCC.cpp +++ b/KosarajuSCC.cpp @@ -1,9 +1,9 @@ #include "KosarajuSCC.h" #include "DepthFirstOrder.h" -KosarajuSCC::KosarajuSCC(const Digraph& G) : CCBase(G) { +KosarajuSCC::KosarajuSCC(const Digraph &G) : CCBase(G) { DepthFirstOrder order(G.reverse()); - for (int s : order.reversePost()) { + for (int s: order.reversePost()) { if (!marked[s]) { dfs(G, s); ++count_; diff --git a/KosarajuSCC.h b/KosarajuSCC.h index 75867e5..5c7dbe1 100644 --- a/KosarajuSCC.h +++ b/KosarajuSCC.h @@ -7,7 +7,7 @@ class KosarajuSCC : public CCBase { public: - KosarajuSCC(const Digraph& G); + KosarajuSCC(const Digraph &G); bool stronglyConnected(int v, int w) const { return connected(v, w); } }; diff --git a/KruskalMST.cpp b/KruskalMST.cpp index d009c74..aca4642 100644 --- a/KruskalMST.cpp +++ b/KruskalMST.cpp @@ -2,9 +2,9 @@ #include "MinPQ.h" #include "UF.h" -KruskalMST::KruskalMST(const EdgeWeightedGraph& G) { +KruskalMST::KruskalMST(const EdgeWeightedGraph &G) { MinPQ pq(G.E()); - for (const auto& e : G.edges()) pq.insert(e); + for (const auto &e: G.edges()) pq.insert(e); UF uf(G.V()); while (!pq.isEmpty() && mst.size() < G.V() - 1) { auto e = pq.delMin(); diff --git a/KruskalMST.h b/KruskalMST.h index 55a8c54..21611d5 100644 --- a/KruskalMST.h +++ b/KruskalMST.h @@ -11,7 +11,7 @@ class KruskalMST : public MST { std::list mst; public: - KruskalMST(const EdgeWeightedGraph& G); + KruskalMST(const EdgeWeightedGraph &G); std::list edges() const override; }; diff --git a/LinearProbingHashST.h b/LinearProbingHashST.h index 0add14b..48f3f39 100644 --- a/LinearProbingHashST.h +++ b/LinearProbingHashST.h @@ -6,41 +6,43 @@ #include #include -template +template class LinearProbingHashST : public ST { private: - int N = 0; // 符号表中键值对总数 + int N = 0; // 符号表中键值对总数 int M = 16; // 线性探测表的大小 - std::vector> keys_; - std::vector> vals; + std::vector > keys_; + std::vector > vals; - int hash(const Key& key) const; + int hash(const Key &key) const; - LinearProbingHashST(int M) : M(M), keys_(M), vals(M) {} + LinearProbingHashST(int M) : M(M), keys_(M), vals(M) { + } void resize(int cap); public: - LinearProbingHashST() : keys_(M), vals(M) {} + LinearProbingHashST() : keys_(M), vals(M) { + } - std::optional get(const Key& key) const override; + std::optional get(const Key &key) const override; - void put(const Key& key, const Value& val) override; + void put(const Key &key, const Value &val) override; - void remove(const Key& key) override; + void remove(const Key &key) override; int size() const override { return N; } std::list keys() const override; }; -template -int LinearProbingHashST::hash(const Key& key) const { +template +int LinearProbingHashST::hash(const Key &key) const { std::hash hasher; return hasher(key) % M; } -template +template void LinearProbingHashST::resize(int cap) { LinearProbingHashST t(cap); for (int i = 0; i < M; ++i) { @@ -51,16 +53,16 @@ void LinearProbingHashST::resize(int cap) { M = t.M; } -template -std::optional LinearProbingHashST::get(const Key& key) const { +template +std::optional LinearProbingHashST::get(const Key &key) const { for (int i = hash(key); keys_[i]; i = (i + 1) % M) { if (keys_[i] == key) return vals[i]; } return std::nullopt; } -template -void LinearProbingHashST::put(const Key& key, const Value& val) { +template +void LinearProbingHashST::put(const Key &key, const Value &val) { if (N >= M / 2) resize(2 * M); int i; @@ -75,8 +77,8 @@ void LinearProbingHashST::put(const Key& key, const Value& val) { ++N; } -template -void LinearProbingHashST::remove(const Key& key) { +template +void LinearProbingHashST::remove(const Key &key) { if (!this->contains(key)) return; int i = hash(key); while (keys_[i] != key) i = (i + 1) % M; @@ -96,7 +98,7 @@ void LinearProbingHashST::remove(const Key& key) { if (N > 0 && N == M / 8) resize(M / 2); } -template +template std::list LinearProbingHashST::keys() const { std::list queue; for (int i = 0; i < M; ++i) { diff --git a/MST.cpp b/MST.cpp index c362901..872a30a 100644 --- a/MST.cpp +++ b/MST.cpp @@ -2,6 +2,6 @@ double MST::weight() const { double weight = 0.0; - for (const auto& e : edges()) weight += e.weight(); + for (const auto &e: edges()) weight += e.weight(); return weight; } diff --git a/MaxPQ.h b/MaxPQ.h index 9174f9b..49d957b 100644 --- a/MaxPQ.h +++ b/MaxPQ.h @@ -5,7 +5,7 @@ #include "PQ.h" #include -template +template class MaxPQ : public PQ { private: bool lower(int i, int j) const override { return this->pq[i] < this->pq[j]; } diff --git a/Merge.h b/Merge.h index e4d146e..a1bce72 100644 --- a/Merge.h +++ b/Merge.h @@ -6,23 +6,23 @@ class Merge : public Sorting { private: - template - static void sort(std::vector& a, int lo, int hi); + template + static void sort(std::vector &a, int lo, int hi); protected: - template + template static std::vector aux; - template - static void merge(std::vector& a, int lo, int mid, int hi); + template + static void merge(std::vector &a, int lo, int mid, int hi); public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -void Merge::sort(std::vector& a, int lo, int hi) { +template +void Merge::sort(std::vector &a, int lo, int hi) { if (hi <= lo) return; int mid = (lo + hi) / 2; sort(a, lo, mid); @@ -30,11 +30,11 @@ void Merge::sort(std::vector& a, int lo, int hi) { merge(a, lo, mid, hi); } -template +template std::vector Merge::aux; -template -void Merge::merge(std::vector& a, int lo, int mid, int hi) { +template +void Merge::merge(std::vector &a, int lo, int mid, int hi) { int i = lo, j = mid + 1; for (int k = lo; k <= hi; ++k) aux[k] = a[k]; for (int k = lo; k <= hi; ++k) { @@ -45,8 +45,8 @@ void Merge::merge(std::vector& a, int lo, int mid, int hi) { } } -template -void Merge::sort(std::vector& a) { +template +void Merge::sort(std::vector &a) { aux = std::vector(a.size()); sort(a, 0, a.size() - 1); } diff --git a/MergeBU.h b/MergeBU.h index 3028770..795d032 100644 --- a/MergeBU.h +++ b/MergeBU.h @@ -7,16 +7,18 @@ class MergeBU : public Merge { public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -void MergeBU::sort(std::vector& a) { +template +void MergeBU::sort(std::vector &a) { int N = a.size(); aux = std::vector(a.size()); - for (int sz = 1; sz < N; sz *= 2) { // 子数组大小sz - for (int lo = 0; lo < N - sz; lo += 2 * sz) { // 子数组索引lo + for (int sz = 1; sz < N; sz *= 2) { + // 子数组大小sz + for (int lo = 0; lo < N - sz; lo += 2 * sz) { + // 子数组索引lo merge(a, lo, lo + sz - 1, std::min(lo + 2 * sz - 1, N - 1)); } } diff --git a/MinPQ.h b/MinPQ.h index 363977b..f21666e 100644 --- a/MinPQ.h +++ b/MinPQ.h @@ -5,7 +5,7 @@ #include "PQ.h" #include -template +template class MinPQ : public PQ { protected: bool lower(int i, int j) const override { return this->pq[i] > this->pq[j]; } diff --git a/OrderedST.h b/OrderedST.h index cc19325..9cb42dc 100644 --- a/OrderedST.h +++ b/OrderedST.h @@ -6,18 +6,18 @@ #include #include -template +template class OrderedST : public ST { public: virtual std::optional min() const = 0; virtual std::optional max() const = 0; - virtual std::optional floor(const Key& key) const = 0; + virtual std::optional floor(const Key &key) const = 0; - virtual std::optional ceiling(const Key& key) const = 0; + virtual std::optional ceiling(const Key &key) const = 0; - virtual int rank(const Key& key) const = 0; + virtual int rank(const Key &key) const = 0; virtual std::optional select(int k) const = 0; @@ -27,15 +27,15 @@ class OrderedST : public ST { using ST::size; - int size(const Key& lo, const Key& hi) const; + int size(const Key &lo, const Key &hi) const; std::list keys() const override { return keys(*min(), *max()); } - virtual std::list keys(const Key& lo, const Key& hi) const = 0; + virtual std::list keys(const Key &lo, const Key &hi) const = 0; }; -template -int OrderedST::size(const Key& lo, const Key& hi) const { +template +int OrderedST::size(const Key &lo, const Key &hi) const { if (hi < lo) return 0; else if (this->contains(hi)) return rank(hi) - rank(lo) + 1; else return rank(hi) - rank(lo); diff --git a/PQ.h b/PQ.h index ba9cf84..0ffe2e8 100644 --- a/PQ.h +++ b/PQ.h @@ -5,11 +5,11 @@ #include #include -template +template class PQ { protected: - std::vector> pq; // 基于堆的完全二叉树 - int N = 0; // 存储于pq[1..N]中,pq[0]没有使用 + std::vector > pq; // 基于堆的完全二叉树 + int N = 0; // 存储于pq[1..N]中,pq[0]没有使用 virtual bool lower(int i, int j) const = 0; @@ -22,7 +22,8 @@ class PQ { std::optional delTop(); public: - PQ(int maxN) : pq(maxN + 1) {} + PQ(int maxN) : pq(maxN + 1) { + } virtual ~PQ() = default; @@ -30,17 +31,17 @@ class PQ { int size() const { return N; } - void insert(const Key& v); + void insert(const Key &v); }; -template +template void PQ::exch(int i, int j) { auto t = pq[i]; pq[i] = pq[j]; pq[j] = t; } -template +template void PQ::swim(int k) { while (k > 1 && lower(k / 2, k)) { exch(k / 2, k); @@ -48,7 +49,7 @@ void PQ::swim(int k) { } } -template +template void PQ::sink(int k) { while (2 * k <= N) { int j = 2 * k; @@ -59,7 +60,7 @@ void PQ::sink(int k) { } } -template +template std::optional PQ::delTop() { auto top = pq[1]; exch(1, N--); @@ -68,8 +69,8 @@ std::optional PQ::delTop() { return top; } -template -void PQ::insert(const Key& v) { +template +void PQ::insert(const Key &v) { pq[++N] = v; swim(N); } diff --git a/Paths.h b/Paths.h index c15b44f..1c94b7e 100644 --- a/Paths.h +++ b/Paths.h @@ -9,11 +9,12 @@ class Paths { protected: std::vector marked; // 这个顶点上调用过dfs()了吗? - std::vector edgeTo; // 从起点到一个顶点的已知路径上的最后一个顶点 - const int s; // 起点 + std::vector edgeTo; // 从起点到一个顶点的已知路径上的最后一个顶点 + const int s; // 起点 public: - Paths(const Graph& G, int s) : marked(G.V()), edgeTo(G.V()), s(s) {} + Paths(const Graph &G, int s) : marked(G.V()), edgeTo(G.V()), s(s) { + } bool hasPathTo(int v) const { return marked[v]; } diff --git a/PrimMST.cpp b/PrimMST.cpp index 21f4679..8cd3175 100644 --- a/PrimMST.cpp +++ b/PrimMST.cpp @@ -1,9 +1,9 @@ #include "PrimMST.h" #include -void PrimMST::visit(const EdgeWeightedGraph& G, int v) { +void PrimMST::visit(const EdgeWeightedGraph &G, int v) { marked[v] = true; - for (const auto& e : G.adj(v)) { + for (const auto &e: G.adj(v)) { int w = e.other(v); if (marked[w]) continue; // v-w失效 if (e.weight() < distTo[w]) { @@ -16,7 +16,7 @@ void PrimMST::visit(const EdgeWeightedGraph& G, int v) { } } -PrimMST::PrimMST(const EdgeWeightedGraph& G) : edgeTo(G.V()), distTo(G.V(), std::numeric_limits::infinity()), +PrimMST::PrimMST(const EdgeWeightedGraph &G) : edgeTo(G.V()), distTo(G.V(), std::numeric_limits::infinity()), marked(G.V()), pq(G.V()) { distTo[0] = 0.0; pq.insert(0, 0.0); diff --git a/PrimMST.h b/PrimMST.h index 38fc3b7..55e7aca 100644 --- a/PrimMST.h +++ b/PrimMST.h @@ -9,15 +9,15 @@ class PrimMST : public MST { private: - std::vector edgeTo; // 距离树最近的边 + std::vector edgeTo; // 距离树最近的边 std::vector distTo; // distTo[w] = edgeTo[w].weight() - std::vector marked; // 如果v在树中则为true - IndexMinPQ pq; // 有效的横切边 + std::vector marked; // 如果v在树中则为true + IndexMinPQ pq; // 有效的横切边 - void visit(const EdgeWeightedGraph& G, int v); + void visit(const EdgeWeightedGraph &G, int v); public: - PrimMST(const EdgeWeightedGraph& G); + PrimMST(const EdgeWeightedGraph &G); std::list edges() const override; }; diff --git a/Quick.h b/Quick.h index f920431..ac32317 100644 --- a/Quick.h +++ b/Quick.h @@ -8,19 +8,19 @@ class Quick : public Sorting { private: - template - static int partition(std::vector& a, int lo, int hi); + template + static int partition(std::vector &a, int lo, int hi); - template - static void sort(std::vector& a, int lo, int hi); + template + static void sort(std::vector &a, int lo, int hi); public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -int Quick::partition(std::vector& a, int lo, int hi) { +template +int Quick::partition(std::vector &a, int lo, int hi) { int i = lo, j = hi + 1; T v = a[lo]; while (true) { @@ -38,16 +38,16 @@ int Quick::partition(std::vector& a, int lo, int hi) { return j; // a[lo..j-1] <= a[j] <= a[j+1..hi] 达成 } -template -void Quick::sort(std::vector& a, int lo, int hi) { +template +void Quick::sort(std::vector &a, int lo, int hi) { if (lo >= hi) return; int j = partition(a, lo, hi); sort(a, lo, j - 1); sort(a, j + 1, hi); } -template -void Quick::sort(std::vector& a) { +template +void Quick::sort(std::vector &a) { std::random_device rd; std::mt19937 g(rd()); std::shuffle(a.begin(), a.end(), g); diff --git a/Quick3way.h b/Quick3way.h index 192ca48..b3cf657 100644 --- a/Quick3way.h +++ b/Quick3way.h @@ -8,16 +8,16 @@ class Quick3way : public Sorting { private: - template - static void sort(std::vector& a, int lo, int hi); + template + static void sort(std::vector &a, int lo, int hi); public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -void Quick3way::sort(std::vector& a, int lo, int hi) { +template +void Quick3way::sort(std::vector &a, int lo, int hi) { if (lo >= hi) return; int lt = lo, i = lo + 1, gt = hi; T v = a[lo]; @@ -30,8 +30,8 @@ void Quick3way::sort(std::vector& a, int lo, int hi) { sort(a, gt + 1, hi); } -template -void Quick3way::sort(std::vector& a) { +template +void Quick3way::sort(std::vector &a) { std::random_device rd; std::mt19937 g(rd()); std::shuffle(a.begin(), a.end(), g); diff --git a/RedBlackBST.h b/RedBlackBST.h index d1772e5..9367ffc 100644 --- a/RedBlackBST.h +++ b/RedBlackBST.h @@ -5,7 +5,7 @@ #include "BST.h" #include -template +template class RedBlackBST : public BST { private: static constexpr bool RED = true, BLACK = false; @@ -13,7 +13,8 @@ class RedBlackBST : public BST { struct Node : public BST::Node { bool color; // 由其父结点指向它的链接的颜色 - Node(Key key, Value val, int N, bool color) : BST::Node(key, val, N), color(color) {} + Node(Key key, Value val, int N, bool color) : BST::Node(key, val, N), color(color) { + } }; bool isRed(std::shared_ptr x) const; @@ -24,19 +25,19 @@ class RedBlackBST : public BST { void flipColors(std::shared_ptr h); - std::shared_ptr put(std::shared_ptr h, const Key& key, const Value& val); + std::shared_ptr put(std::shared_ptr h, const Key &key, const Value &val); public: - void put(const Key& key, const Value& val) override; + void put(const Key &key, const Value &val) override; }; -template +template bool RedBlackBST::isRed(std::shared_ptr x) const { if (!x) return false; return x->color == RED; } -template +template std::shared_ptr::Node> RedBlackBST::rotateLeft(std::shared_ptr h) { std::shared_ptr x = std::dynamic_pointer_cast(h->right); h->right = x->left; @@ -48,7 +49,7 @@ std::shared_ptr::Node> RedBlackBST: return x; } -template +template std::shared_ptr::Node> RedBlackBST::rotateRight(std::shared_ptr h) { std::shared_ptr x = std::dynamic_pointer_cast(h->left); h->left = x->right; @@ -60,16 +61,16 @@ std::shared_ptr::Node> RedBlackBST: return x; } -template +template void RedBlackBST::flipColors(std::shared_ptr h) { h->color = RED; std::dynamic_pointer_cast(h->left)->color = BLACK; std::dynamic_pointer_cast(h->right)->color = BLACK; } -template +template std::shared_ptr::Node> -RedBlackBST::put(std::shared_ptr h, const Key& key, const Value& val) { +RedBlackBST::put(std::shared_ptr h, const Key &key, const Value &val) { if (!h) return std::make_shared(key, val, 1, RED); if (key < h->key) h->left = put(std::dynamic_pointer_cast(h->left), key, val); else if (key > h->key) h->right = put(std::dynamic_pointer_cast(h->right), key, val); @@ -89,8 +90,8 @@ RedBlackBST::put(std::shared_ptr h, const Key& key, const Valu return h; } -template -void RedBlackBST::put(const Key& key, const Value& val) { +template +void RedBlackBST::put(const Key &key, const Value &val) { this->root = put(std::dynamic_pointer_cast(this->root), key, val); std::dynamic_pointer_cast(this->root)->color = BLACK; } diff --git a/SP.cpp b/SP.cpp index 30f6af1..1f9a90c 100644 --- a/SP.cpp +++ b/SP.cpp @@ -1,7 +1,7 @@ #include "SP.h" -void SP::relax(const EdgeWeightedDigraph& G, int v) { - for (const auto& e : G.adj(v)) { +void SP::relax(const EdgeWeightedDigraph &G, int v) { + for (const auto &e: G.adj(v)) { int w = e.to(); if (distTo_[w] > distTo_[v] + e.weight()) { distTo_[w] = distTo_[v] + e.weight(); @@ -11,7 +11,7 @@ void SP::relax(const EdgeWeightedDigraph& G, int v) { } } -SP::SP(const EdgeWeightedDigraph& G, int s) : edgeTo(G.V()), distTo_(G.V(), std::numeric_limits::infinity()) { +SP::SP(const EdgeWeightedDigraph &G, int s) : edgeTo(G.V()), distTo_(G.V(), std::numeric_limits::infinity()) { distTo_[s] = 0.0; } diff --git a/SP.h b/SP.h index e7b0f57..bda80e7 100644 --- a/SP.h +++ b/SP.h @@ -13,12 +13,13 @@ class SP { std::vector edgeTo; std::vector distTo_; - void relax(const EdgeWeightedDigraph& G, int v); + void relax(const EdgeWeightedDigraph &G, int v); - virtual void onRelax(const DirectedEdge& e, int w) {} + virtual void onRelax(const DirectedEdge &e, int w) { + } public: - SP(const EdgeWeightedDigraph& G, int s); + SP(const EdgeWeightedDigraph &G, int s); virtual ~SP() = default; diff --git a/ST.h b/ST.h index 9ab623b..6e5daab 100644 --- a/ST.h +++ b/ST.h @@ -5,18 +5,18 @@ #include #include -template +template class ST { public: virtual ~ST() = default; - virtual std::optional get(const Key& key) const = 0; + virtual std::optional get(const Key &key) const = 0; - virtual void put(const Key& key, const Value& val) = 0; + virtual void put(const Key &key, const Value &val) = 0; - virtual void remove(const Key& key) = 0; + virtual void remove(const Key &key) = 0; - bool contains(const Key& key) const { return get(key).has_value(); } + bool contains(const Key &key) const { return get(key).has_value(); } bool isEmpty() const { return size() == 0; } diff --git a/Selection.h b/Selection.h index 207a3e2..cfcabce 100644 --- a/Selection.h +++ b/Selection.h @@ -6,12 +6,12 @@ class Selection : public Sorting { public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -void Selection::sort(std::vector& a) { +template +void Selection::sort(std::vector &a) { int N = a.size(); for (int i = 0; i < N; ++i) { // 将a[i]和a[i+1..N]中最小的元素交换 diff --git a/SeparateChainingHashST.h b/SeparateChainingHashST.h index 872b394..4223edf 100644 --- a/SeparateChainingHashST.h +++ b/SeparateChainingHashST.h @@ -7,46 +7,47 @@ #include #include -template +template class SeparateChainingHashST : public ST { private: int M; // 散列表的大小 - std::vector> st; + std::vector > st; - int hash(const Key& key) const; + int hash(const Key &key) const; public: - SeparateChainingHashST(int M) : M(M), st(M) {} + SeparateChainingHashST(int M) : M(M), st(M) { + } - std::optional get(const Key& key) const override { return st[hash(key)].get(key); } + std::optional get(const Key &key) const override { return st[hash(key)].get(key); } - void put(const Key& key, const Value& val) override { st[hash(key)].put(key, val); } + void put(const Key &key, const Value &val) override { st[hash(key)].put(key, val); } - void remove(const Key& key) override { st[hash(key)].remove(key); } + void remove(const Key &key) override { st[hash(key)].remove(key); } int size() const override; std::list keys() const override; }; -template -int SeparateChainingHashST::hash(const Key& key) const { +template +int SeparateChainingHashST::hash(const Key &key) const { std::hash hasher; return hasher(key) % M; } -template +template int SeparateChainingHashST::size() const { int N = 0; for (int i = 0; i < M; ++i) N += st[i].size(); return N; } -template +template std::list SeparateChainingHashST::keys() const { std::list queue; for (int i = 0; i < M; ++i) { - for (const auto& key : st[i].keys()) { + for (const auto &key: st[i].keys()) { queue.push_back(key); } } diff --git a/SequentialSearchST.h b/SequentialSearchST.h index 4ed0398..a887852 100644 --- a/SequentialSearchST.h +++ b/SequentialSearchST.h @@ -5,7 +5,7 @@ #include "ST.h" #include -template +template class SequentialSearchST : public ST { private: struct Node { @@ -13,7 +13,8 @@ class SequentialSearchST : public ST { Value val; std::shared_ptr next; - Node(Key key, Value val, std::shared_ptr next) : key(key), val(val), next(next) {} + Node(Key key, Value val, std::shared_ptr next) : key(key), val(val), next(next) { + } }; int N = 0; @@ -22,18 +23,18 @@ class SequentialSearchST : public ST { std::shared_ptr remove(std::shared_ptr x, Key key); public: - std::optional get(const Key& key) const override; + std::optional get(const Key &key) const override; - void put(const Key& key, const Value& val) override; + void put(const Key &key, const Value &val) override; - void remove(const Key& key) override { first = remove(first, key); } + void remove(const Key &key) override { first = remove(first, key); } int size() const override { return N; } std::list keys() const override; }; -template +template std::shared_ptr::Node> SequentialSearchST::remove(std::shared_ptr x, Key key) { if (!x) return nullptr; // 基准情况:如果当前节点为空,返回 nullptr @@ -42,19 +43,19 @@ SequentialSearchST::remove(std::shared_ptr x, Key key) { return x->next; } x->next = remove(x->next, key); // 递归调用,在下一个节点中继续删除指定的键 - return x; // 返回当前节点,当前节点没有被删除 + return x; // 返回当前节点,当前节点没有被删除 } -template -std::optional SequentialSearchST::get(const Key& key) const { +template +std::optional SequentialSearchST::get(const Key &key) const { for (auto x = first; x; x = x->next) { if (key == x->key) return x->val; } return std::nullopt; } -template -void SequentialSearchST::put(const Key& key, const Value& val) { +template +void SequentialSearchST::put(const Key &key, const Value &val) { for (auto x = first; x; x = x->next) { if (key == x->key) { x->val = val; @@ -65,7 +66,7 @@ void SequentialSearchST::put(const Key& key, const Value& val) { ++N; } -template +template std::list SequentialSearchST::keys() const { std::list queue; for (auto x = first; x; x = x->next) queue.push_back(x->key); diff --git a/Shell.h b/Shell.h index 92ff3ee..6980569 100644 --- a/Shell.h +++ b/Shell.h @@ -6,12 +6,12 @@ class Shell : public Sorting { public: - template - static void sort(std::vector& a); + template + static void sort(std::vector &a); }; -template -void Shell::sort(std::vector& a) { +template +void Shell::sort(std::vector &a) { int N = a.size(); int h = 1; while (h < N / 3) h = h * 3 + 1; diff --git a/Sorting.h b/Sorting.h index ad7a9a3..016c66f 100644 --- a/Sorting.h +++ b/Sorting.h @@ -7,35 +7,35 @@ class Sorting { protected: - template - static bool less(const T& v, const T& w) { return v < w; } + template + static bool less(const T &v, const T &w) { return v < w; } - template - static void exch(std::vector& a, int i, int j); + template + static void exch(std::vector &a, int i, int j); public: - template - static void show(const std::vector& a); + template + static void show(const std::vector &a); - template - static bool isSorted(const std::vector& a); + template + static bool isSorted(const std::vector &a); }; -template -void Sorting::exch(std::vector& a, int i, int j) { +template +void Sorting::exch(std::vector &a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; } -template -void Sorting::show(const std::vector& a) { - for (const auto& item : a) std::cout << item << " "; +template +void Sorting::show(const std::vector &a) { + for (const auto &item: a) std::cout << item << " "; std::cout << "\n"; } -template -bool Sorting::isSorted(const std::vector& a) { +template +bool Sorting::isSorted(const std::vector &a) { for (int i = 1; i < a.size(); ++i) { if (less(a[i], a[i - 1])) return false; } diff --git a/SymbolDigraph.cpp b/SymbolDigraph.cpp index ff791a4..fa5038d 100644 --- a/SymbolDigraph.cpp +++ b/SymbolDigraph.cpp @@ -3,27 +3,31 @@ #include #include -SymbolDigraph::SymbolDigraph(const std::filesystem::path& dataFilePath, char sp) { +SymbolDigraph::SymbolDigraph(const std::filesystem::path &dataFilePath, char sp) { std::ifstream dataFile(dataFilePath); // 第一遍 std::string line; - while (std::getline(dataFile, line)) { // 构造索引 + while (std::getline(dataFile, line)) { + // 构造索引 std::string name; std::istringstream iss(line); - while (std::getline(iss, name, sp)) { // 为每个不同的字符串关联一个索引 + while (std::getline(iss, name, sp)) { + // 为每个不同的字符串关联一个索引 if (!st.contains(name)) st.put(name, st.size()); } } keys = std::vector(st.size()); // 用来获得顶点名的反向索引是一个数组 - for (const auto& name : st.keys()) keys[*st.get(name)] = name; + for (const auto &name: st.keys()) keys[*st.get(name)] = name; G_ = std::make_unique(st.size()); dataFile = std::ifstream(dataFilePath); // 第二遍 - while (std::getline(dataFile, line)) { // 构造图 + while (std::getline(dataFile, line)) { + // 构造图 std::string name; std::istringstream iss(line); std::getline(iss, name, sp); int v = *st.get(name); - while (std::getline(iss, name, sp)) { // 将每一行的第一个顶点和该行的其他顶点相连 + while (std::getline(iss, name, sp)) { + // 将每一行的第一个顶点和该行的其他顶点相连 G_->addEdge(v, *st.get(name)); } } diff --git a/SymbolDigraph.h b/SymbolDigraph.h index bdecf82..28f2a7e 100644 --- a/SymbolDigraph.h +++ b/SymbolDigraph.h @@ -12,15 +12,15 @@ class SymbolDigraph { private: RedBlackBST st; // 符号名 -> 索引 - std::vector keys; // 索引 -> 符号名 - std::unique_ptr G_; // 图 + std::vector keys; // 索引 -> 符号名 + std::unique_ptr G_; // 图 public: - SymbolDigraph(const std::filesystem::path& dataFilePath, char sp); + SymbolDigraph(const std::filesystem::path &dataFilePath, char sp); - bool contains(const std::string& s) const { return st.contains(s); } + bool contains(const std::string &s) const { return st.contains(s); } - std::optional index(const std::string& s) const { return st.get(s); } + std::optional index(const std::string &s) const { return st.get(s); } std::string name(int v) const { return keys[v]; } diff --git a/Topological.h b/Topological.h index e30f788..0663f2f 100644 --- a/Topological.h +++ b/Topological.h @@ -12,16 +12,16 @@ class Topological { std::list order_; // 顶点的拓扑顺序 public: - template - Topological(const GraphBase& G); + template + Topological(const GraphBase &G); std::list order() const { return order_; } bool isDAG() const { return !order_.empty(); } }; -template -Topological::Topological(const GraphBase& G) { +template +Topological::Topological(const GraphBase &G) { DirectedCycle cyclefinder(G); if (!cyclefinder.hasCycle()) { DepthFirstOrder dfs(G); diff --git a/UF.h b/UF.h index 92485ca..eb658ed 100644 --- a/UF.h +++ b/UF.h @@ -8,7 +8,7 @@ class UF { private: std::vector id; // 父链接数组(以触点为索引) std::vector sz; // 各个根节点对应的分量的大小(以触点为索引) - int count_; // 分量数量 + int count_; // 分量数量 public: UF(int N); diff --git a/main.cpp b/main.cpp index eed5335..88e841c 100644 --- a/main.cpp +++ b/main.cpp @@ -39,7 +39,7 @@ #include "tests/testST.h" #include "tests/testGraph.h" -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { if (argc < 2) { // 测试union-find // Example: ./algs4 < ../data/tinyUF.txt diff --git a/tests/testGraph.cpp b/tests/testGraph.cpp index 795c4bc..b6aef0d 100644 --- a/tests/testGraph.cpp +++ b/tests/testGraph.cpp @@ -3,11 +3,11 @@ #include #include -void testPaths(const Graph& G, int s, const Paths& search) { +void testPaths(const Graph &G, int s, const Paths &search) { for (int v = 0; v < G.V(); ++v) { std::cout << s << " to " << v << ": "; if (search.hasPathTo(v)) { - for (int x : search.pathTo(v)) { + for (int x: search.pathTo(v)) { if (x == s) std::cout << x; else std::cout << "-" << x; } @@ -16,44 +16,44 @@ void testPaths(const Graph& G, int s, const Paths& search) { } } -void testCC(const GraphBase& G, const CCBase& cc) { +void testCC(const GraphBase &G, const CCBase &cc) { int M = cc.count(); std::cout << M << " components" << "\n"; std::list components[M]; for (int v = 0; v < G.V(); ++v) components[cc.id(v)].push_front(v); for (int i = 0; i < M; ++i) { - for (int v : components[i]) std::cout << v << " "; + for (int v: components[i]) std::cout << v << " "; std::cout << "\n"; } } -void testSearch(const Digraph& G, const DirectedDFS& reachable) { +void testSearch(const Digraph &G, const DirectedDFS &reachable) { for (int v = 0; v < G.V(); ++v) { if (reachable.marked(v)) std::cout << v << " "; } std::cout << "\n"; } -void testTopological(const SymbolDigraph& G, const Topological& top) { - for (int v : top.order()) { +void testTopological(const SymbolDigraph &G, const Topological &top) { + for (int v: top.order()) { std::cout << G.name(v) << "\n"; } } -void testMST(const EdgeWeightedGraph& G, const MST& mst) { - for (const auto& e : mst.edges()) { +void testMST(const EdgeWeightedGraph &G, const MST &mst) { + for (const auto &e: mst.edges()) { std::cout << e << "\n"; } std::cout << mst.weight() << "\n"; } -void testSP(const EdgeWeightedDigraph& G, int s, const SP& sp) { +void testSP(const EdgeWeightedDigraph &G, int s, const SP &sp) { for (int t = 0; t < G.V(); ++t) { std::cout << s << " to " << t; std::cout << " (" << std::fixed << std::setprecision(2) << std::setw(4) << sp.distTo(t) << "): "; if (sp.hasPathTo(t)) { - for (const auto& e : sp.pathTo(t)) { + for (const auto &e: sp.pathTo(t)) { std::cout << e << " "; } } diff --git a/tests/testGraph.h b/tests/testGraph.h index 6a2bd09..e09b63e 100644 --- a/tests/testGraph.h +++ b/tests/testGraph.h @@ -15,17 +15,17 @@ #include "MST.h" #include "SP.h" -void testPaths(const Graph& G, int s, const Paths& search); +void testPaths(const Graph &G, int s, const Paths &search); -void testCC(const GraphBase& G, const CCBase& cc); +void testCC(const GraphBase &G, const CCBase &cc); -void testSearch(const Digraph& G, const DirectedDFS& reachable); +void testSearch(const Digraph &G, const DirectedDFS &reachable); -void testTopological(const SymbolDigraph& G, const Topological& top); +void testTopological(const SymbolDigraph &G, const Topological &top); -void testMST(const EdgeWeightedGraph& G, const MST& mst); +void testMST(const EdgeWeightedGraph &G, const MST &mst); -void testSP(const EdgeWeightedDigraph& G, int s, const SP& sp); +void testSP(const EdgeWeightedDigraph &G, int s, const SP &sp); #endif //ALGS4_TESTGRAPH_H diff --git a/tests/testPQ.cpp b/tests/testPQ.cpp index 7370a69..e6991f0 100644 --- a/tests/testPQ.cpp +++ b/tests/testPQ.cpp @@ -1,7 +1,7 @@ #include "testPQ.h" #include -void testPQ(MaxPQ&& pq, std::istream&& data) { +void testPQ(MaxPQ &&pq, std::istream &&data) { std::string word; while (data >> word) { if (word != "-") pq.insert(word); diff --git a/tests/testPQ.h b/tests/testPQ.h index d9985de..716df23 100644 --- a/tests/testPQ.h +++ b/tests/testPQ.h @@ -6,7 +6,7 @@ #include #include -void testPQ(MaxPQ&& pq, std::istream&& data); +void testPQ(MaxPQ &&pq, std::istream &&data); #endif //ALGS4_TESTPQ_H diff --git a/tests/testST.cpp b/tests/testST.cpp index 8518b63..cf641ee 100644 --- a/tests/testST.cpp +++ b/tests/testST.cpp @@ -6,7 +6,7 @@ static constexpr char INVALID_KEY[] = ""; static constexpr int INVALID_VALUE = -1; -void testBasicST(ST&& st, std::istream&& data) { +void testBasicST(ST &&st, std::istream &&data) { std::string word; int i = 0; while (data >> word) { @@ -19,13 +19,13 @@ void testBasicST(ST&& st, std::istream&& data) { // print keys using keys() std::cout << "Testing keys()" << "\n"; std::cout << "––––––––––––––––––––––––––––––––––––––––––––––––" << "\n"; - for (const auto& s : st.keys()) { + for (const auto &s: st.keys()) { std::cout << s << " " << st.get(s).value_or(INVALID_VALUE) << "\n"; } std::cout << "––––––––––––––––––––––––––––––––––––––––––––––––" << "\n" << "\n"; // remove all keys - for (const auto& s : st.keys()) { + for (const auto &s: st.keys()) { std::cout << "Removing " << s << "\n"; st.remove(s); } @@ -36,7 +36,7 @@ void testBasicST(ST&& st, std::istream&& data) { std::cout << "––––––––––––––––––––––––––––––––––––––––––––––––" << "\n"; } -void testOrderedST(OrderedST&& st, std::istream&& data) { +void testOrderedST(OrderedST &&st, std::istream &&data) { std::string word; int i = 0; while (data >> word) { @@ -51,7 +51,7 @@ void testOrderedST(OrderedST&& st, std::istream&& data) { // print keys in order using keys() std::cout << "Testing keys()" << "\n"; std::cout << "––––––––––––––––––––––––––––––––––––––––––––––––" << "\n"; - for (const auto& s : st.keys()) { + for (const auto &s: st.keys()) { std::cout << s << " " << st.get(s).value_or(INVALID_VALUE) << "\n"; } std::cout << "––––––––––––––––––––––––––––––––––––––––––––––––" << "\n" << "\n"; @@ -70,9 +70,9 @@ void testOrderedST(OrderedST&& st, std::istream&& data) { for (char i = 'A'; i <= 'Z'; ++i) { std::string s(1, i); std::cout << std::setw(2) << s << " " - << std::setw(4) << st.rank(s) << " " - << std::setw(4) << st.floor(s).value_or(INVALID_KEY) << " " - << std::setw(4) << st.ceiling(s).value_or(INVALID_KEY) << "\n"; + << std::setw(4) << st.rank(s) << " " + << std::setw(4) << st.floor(s).value_or(INVALID_KEY) << " " + << std::setw(4) << st.ceiling(s).value_or(INVALID_KEY) << "\n"; } std::cout << "–––––––––––––––––––" << "\n" << "\n"; @@ -83,8 +83,8 @@ void testOrderedST(OrderedST&& st, std::istream&& data) { std::cout << "–––––––––––––––––––" << "\n"; for (int i = 0; i < from.size(); ++i) { std::cout << from[i] << "-" << to[i] << " (" - << std::setw(2) << st.size(from[i], to[i]) << ") : "; - for (const auto& s : st.keys(from[i], to[i])) { + << std::setw(2) << st.size(from[i], to[i]) << ") : "; + for (const auto &s: st.keys(from[i], to[i])) { std::cout << s << " "; } std::cout << "\n"; @@ -97,7 +97,7 @@ void testOrderedST(OrderedST&& st, std::istream&& data) { } std::cout << "After removing the smallest " << st.size() / 2 << " keys" << "\n"; std::cout << "––––––––––––––––––––––––––––––––––––––––––––––––" << "\n"; - for (const auto& s : st.keys()) { + for (const auto &s: st.keys()) { std::cout << s << " " << st.get(s).value_or(INVALID_VALUE) << "\n"; } std::cout << "––––––––––––––––––––––––––––––––––––––––––––––––" << "\n" << "\n"; diff --git a/tests/testST.h b/tests/testST.h index c71fae7..1f271ae 100644 --- a/tests/testST.h +++ b/tests/testST.h @@ -7,9 +7,9 @@ #include #include -void testBasicST(ST&& st, std::istream&& data); +void testBasicST(ST &&st, std::istream &&data); -void testOrderedST(OrderedST&& st, std::istream&& data); +void testOrderedST(OrderedST &&st, std::istream &&data); #endif //ALGS4_TESTST_H diff --git a/tests/testSort.h b/tests/testSort.h index 448351a..0fd90c3 100644 --- a/tests/testSort.h +++ b/tests/testSort.h @@ -7,11 +7,11 @@ #include #include -template -void testSort(std::istream&& data); +template +void testSort(std::istream &&data); -template -void testSort(std::istream&& data) { +template +void testSort(std::istream &&data) { std::vector a; std::string word; while (data >> word) a.push_back(word);