Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
landerrosette committed Oct 13, 2024
1 parent 49bd09e commit 14dfa74
Show file tree
Hide file tree
Showing 72 changed files with 436 additions and 410 deletions.
4 changes: 2 additions & 2 deletions AcyclicSP.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion AcyclicSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class AcyclicSP : public SP {
public:
AcyclicSP(const EdgeWeightedDigraph& G, int s);
AcyclicSP(const EdgeWeightedDigraph &G, int s);
};


Expand Down
85 changes: 43 additions & 42 deletions BST.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "OrderedST.h"
#include <memory>

template <typename Key, typename Value>
template<typename Key, typename Value>
class BST : public OrderedST<Key, Value> {
protected:
struct Node {
Expand All @@ -14,57 +14,58 @@ class BST : public OrderedST<Key, Value> {
std::shared_ptr<Node> 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<Node> root;

std::optional<Value> get(std::shared_ptr<Node> x, const Key& key) const;
std::optional<Value> get(std::shared_ptr<Node> x, const Key &key) const;

std::shared_ptr<Node> put(std::shared_ptr<Node> x, const Key& key, const Value& val);
std::shared_ptr<Node> put(std::shared_ptr<Node> x, const Key &key, const Value &val);

std::shared_ptr<Node> remove(std::shared_ptr<Node> x, const Key& key);
std::shared_ptr<Node> remove(std::shared_ptr<Node> x, const Key &key);

int size(std::shared_ptr<Node> x) const;

std::shared_ptr<Node> min(std::shared_ptr<Node> x) const;

std::shared_ptr<Node> max(std::shared_ptr<Node> x) const;

std::shared_ptr<Node> floor(std::shared_ptr<Node> x, const Key& key) const;
std::shared_ptr<Node> floor(std::shared_ptr<Node> x, const Key &key) const;

std::shared_ptr<Node> ceiling(std::shared_ptr<Node> x, const Key& key) const;
std::shared_ptr<Node> ceiling(std::shared_ptr<Node> x, const Key &key) const;

int rank(std::shared_ptr<Node> x, const Key& key) const;
int rank(std::shared_ptr<Node> x, const Key &key) const;

std::shared_ptr<Node> select(std::shared_ptr<Node> x, int k) const;

std::shared_ptr<Node> removeMin(std::shared_ptr<Node> x);

std::shared_ptr<Node> removeMax(std::shared_ptr<Node> x);

void keys(std::shared_ptr<Node> x, std::list<Key>& queue, const Key& lo, const Key& hi) const;
void keys(std::shared_ptr<Node> x, std::list<Key> &queue, const Key &lo, const Key &hi) const;

public:
std::optional<Value> get(const Key& key) const override { return get(root, key); }
std::optional<Value> 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); }

std::optional<Key> min() const override { return min(root)->key; }

std::optional<Key> max() const override { return max(root)->key; }

std::optional<Key> floor(const Key& key) const override;
std::optional<Key> floor(const Key &key) const override;

std::optional<Key> ceiling(const Key& key) const override;
std::optional<Key> 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<Key> select(int k) const override { return select(root, k)->key; }

Expand All @@ -74,20 +75,20 @@ class BST : public OrderedST<Key, Value> {

using OrderedST<Key, Value>::keys;

std::list<Key> keys(const Key& lo, const Key& hi) const override;
std::list<Key> keys(const Key &lo, const Key &hi) const override;
};

template <typename Key, typename Value>
std::optional<Value> BST<Key, Value>::get(std::shared_ptr<Node> x, const Key& key) const {
template<typename Key, typename Value>
std::optional<Value> BST<Key, Value>::get(std::shared_ptr<Node> 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 <typename Key, typename Value>
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node>
BST<Key, Value>::put(std::shared_ptr<Node> x, const Key& key, const Value& val) {
BST<Key, Value>::put(std::shared_ptr<Node> x, const Key &key, const Value &val) {
if (!x) return std::make_shared<Node>(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);
Expand All @@ -96,8 +97,8 @@ BST<Key, Value>::put(std::shared_ptr<Node> x, const Key& key, const Value& val)
return x;
}

template <typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::remove(std::shared_ptr<Node> x, const Key& key) {
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::remove(std::shared_ptr<Node> 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);
Expand All @@ -113,26 +114,26 @@ std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::remove(std::sha
return x;
}

template <typename Key, typename Value>
template<typename Key, typename Value>
int BST<Key, Value>::size(std::shared_ptr<Node> x) const {
if (!x) return 0;
return x->N;
}

template <typename Key, typename Value>
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::min(std::shared_ptr<Node> x) const {
if (!x->left) return x;
return min(x->left);
}

template <typename Key, typename Value>
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::max(std::shared_ptr<Node> x) const {
if (!x->right) return x;
return max(x->right);
}

template <typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::floor(std::shared_ptr<Node> x, const Key& key) const {
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::floor(std::shared_ptr<Node> 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;
Expand All @@ -143,9 +144,9 @@ std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::floor(std::shar
}
}

template <typename Key, typename Value>
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node>
BST<Key, Value>::ceiling(std::shared_ptr<Node> x, const Key& key) const {
BST<Key, Value>::ceiling(std::shared_ptr<Node> 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;
Expand All @@ -156,16 +157,16 @@ BST<Key, Value>::ceiling(std::shared_ptr<Node> x, const Key& key) const {
}
}

template <typename Key, typename Value>
int BST<Key, Value>::rank(std::shared_ptr<Node> x, const Key& key) const {
template<typename Key, typename Value>
int BST<Key, Value>::rank(std::shared_ptr<Node> x, const Key &key) const {
if (!x) return 0;
if (key < x->key) return rank(x->left, key);
else if (key > x->key)
return size(x->left) /* 左子树的结点总数 */ + 1 /* 根结点 */ + rank(x->right, key);
else return size(x->left);
}

template <typename Key, typename Value>
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::select(std::shared_ptr<Node> x, int k) const {
if (!x) return nullptr;
int t = size(x->left);
Expand All @@ -174,46 +175,46 @@ std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::select(std::sha
else return x;
}

template <typename Key, typename Value>
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::removeMin(std::shared_ptr<Node> x) {
if (!x->left) return x->right;
x->left = removeMin(x->left);
x->N = size(x->left) + size(x->right) + 1;
return x;
}

template <typename Key, typename Value>
template<typename Key, typename Value>
std::shared_ptr<typename BST<Key, Value>::Node> BST<Key, Value>::removeMax(std::shared_ptr<Node> x) {
if (!x->right) return x->left;
x->right = removeMax(x->right);
x->N = size(x->left) + size(x->right) + 1;
return x;
}

template <typename Key, typename Value>
void BST<Key, Value>::keys(std::shared_ptr<Node> x, std::list<Key>& queue, const Key& lo, const Key& hi) const {
template<typename Key, typename Value>
void BST<Key, Value>::keys(std::shared_ptr<Node> x, std::list<Key> &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 <typename Key, typename Value>
std::optional<Key> BST<Key, Value>::floor(const Key& key) const {
template<typename Key, typename Value>
std::optional<Key> BST<Key, Value>::floor(const Key &key) const {
auto x = floor(root, key);
if (!x) return std::nullopt;
return x->key;
}

template <typename Key, typename Value>
std::optional<Key> BST<Key, Value>::ceiling(const Key& key) const {
template<typename Key, typename Value>
std::optional<Key> BST<Key, Value>::ceiling(const Key &key) const {
auto x = ceiling(root, key);
if (!x) return std::nullopt;
return x->key;
}

template <typename Key, typename Value>
std::list<Key> BST<Key, Value>::keys(const Key& lo, const Key& hi) const {
template<typename Key, typename Value>
std::list<Key> BST<Key, Value>::keys(const Key &lo, const Key &hi) const {
std::list<Key> queue;
keys(root, queue, lo, hi);
return queue;
Expand Down
53 changes: 27 additions & 26 deletions BinarySearchST.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,50 @@
#include "OrderedST.h"
#include <vector>

template <typename Key, typename Value>
template<typename Key, typename Value>
class BinarySearchST : public OrderedST<Key, Value> {
private:
std::vector<std::optional<Key>> keys_;
std::vector<std::optional<Value>> vals;
std::vector<std::optional<Key> > keys_;
std::vector<std::optional<Value> > vals;
int N = 0;

public:
BinarySearchST(int capacity) : keys_(capacity), vals(capacity) {}
BinarySearchST(int capacity) : keys_(capacity), vals(capacity) {
}

std::optional<Value> get(const Key& key) const override;
std::optional<Value> 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::optional<Key> min() const override { return keys_[0]; }

std::optional<Key> max() const override { return keys_[N - 1]; }

std::optional<Key> floor(const Key& key) const override;
std::optional<Key> floor(const Key &key) const override;

std::optional<Key> ceiling(const Key& key) const override;
std::optional<Key> ceiling(const Key &key) const override;

int rank(const Key& key) const override;
int rank(const Key &key) const override;

std::optional<Key> select(int k) const override { return keys_[k]; }

std::list<Key> keys(const Key& lo, const Key& hi) const override;
std::list<Key> keys(const Key &lo, const Key &hi) const override;
};

template <typename Key, typename Value>
std::optional<Value> BinarySearchST<Key, Value>::get(const Key& key) const {
template<typename Key, typename Value>
std::optional<Value> BinarySearchST<Key, Value>::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 <typename Key, typename Value>
void BinarySearchST<Key, Value>::put(const Key& key, const Value& val) {
template<typename Key, typename Value>
void BinarySearchST<Key, Value>::put(const Key &key, const Value &val) {
int i = rank(key);
if (i < N && keys_[i] == key) {
vals[i] = val;
Expand All @@ -62,8 +63,8 @@ void BinarySearchST<Key, Value>::put(const Key& key, const Value& val) {
++N;
}

template <typename Key, typename Value>
void BinarySearchST<Key, Value>::remove(const Key& key) {
template<typename Key, typename Value>
void BinarySearchST<Key, Value>::remove(const Key &key) {
if (this->isEmpty()) return;
int i = rank(key);
if (i < N && keys_[i] == key) {
Expand All @@ -74,24 +75,24 @@ void BinarySearchST<Key, Value>::remove(const Key& key) {
}
--N;
keys_[N] = std::nullopt; // 置空
vals[N] = std::nullopt; // 置空
vals[N] = std::nullopt; // 置空
}

template <typename Key, typename Value>
std::optional<Key> BinarySearchST<Key, Value>::floor(const Key& key) const {
template<typename Key, typename Value>
std::optional<Key> BinarySearchST<Key, Value>::floor(const Key &key) const {
int i = rank(key);
if (i < N && keys_[i] == key) return keys_[i];
else return keys_[i - 1];
}

template <typename Key, typename Value>
std::optional<Key> BinarySearchST<Key, Value>::ceiling(const Key& key) const {
template<typename Key, typename Value>
std::optional<Key> BinarySearchST<Key, Value>::ceiling(const Key &key) const {
int i = rank(key);
return keys_[i];
}

template <typename Key, typename Value>
int BinarySearchST<Key, Value>::rank(const Key& key) const {
template<typename Key, typename Value>
int BinarySearchST<Key, Value>::rank(const Key &key) const {
int lo = 0, hi = N - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
Expand All @@ -102,8 +103,8 @@ int BinarySearchST<Key, Value>::rank(const Key& key) const {
return lo;
}

template <typename Key, typename Value>
std::list<Key> BinarySearchST<Key, Value>::keys(const Key& lo, const Key& hi) const {
template<typename Key, typename Value>
std::list<Key> BinarySearchST<Key, Value>::keys(const Key &lo, const Key &hi) const {
std::list<Key> queue;
if (hi < lo) return queue;
for (int i = rank(lo); i < rank(hi); ++i) queue.push_back(*keys_[i]);
Expand Down
4 changes: 2 additions & 2 deletions BreadthFirstPaths.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "BreadthFirstPaths.h"
#include <queue>

void BreadthFirstPaths::bfs(const Graph& G, int s) {
void BreadthFirstPaths::bfs(const Graph &G, int s) {
std::queue<int> 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;
Expand Down
Loading

0 comments on commit 14dfa74

Please sign in to comment.