Skip to content

Commit

Permalink
Improve comment positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
landerrosette committed Dec 21, 2024
1 parent bd7c4cd commit 2b30eb6
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 39 deletions.
9 changes: 3 additions & 6 deletions BinaryStdIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
void BinaryStdIO::writeBit(std::byte bit) {
outBuffer = (outBuffer << 1) | bit; // Add bit to buffer.
if (++outN == 8) {
// If buffer is full (8 bits), write out as a single byte.
std::cout.put(std::to_integer<char>(outBuffer));
outBuffer = std::byte();
outN = 0;
}
} // If buffer is full (8 bits), write out as a single byte.
}

void BinaryStdIO::writeByte(std::byte byte) {
if (outN == 0) {
// optimized if byte-aligned
std::cout.put(std::to_integer<char>(byte));
return;
}
} // optimized if byte-aligned
for (int i = 0; i < 8; ++i) writeBit(byte >> (8 - i - 1) & std::byte{1}); // Otherwise write one bit at a time.
}

Expand Down Expand Up @@ -56,11 +54,10 @@ char BinaryStdIO::readChar() {
auto x = inBuffer;
if (inN == 8) fillInBuffer(); // special case when aligned byte
else {
// Combine last n bits of current buffer with first 8-n bits of new buffer.
x <<= 8 - inN;
fillInBuffer();
x |= inBuffer >> inN;
}
} // Combine last n bits of current buffer with first 8-n bits of new buffer.
return std::to_integer<char>(x);
}

Expand Down
4 changes: 2 additions & 2 deletions BoyerMoore.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "BoyerMoore.h"
#include <utility>

// Compute skip table.
BoyerMoore::BoyerMoore(std::string pat) : pat(std::move(pat)) {
// Compute skip table.
int M = this->pat.length(), R = 256;
right = std::vector(R, -1);
for (int j = 0; j < M; ++j) right[this->pat[j]] = j;
}

int BoyerMoore::search(std::string_view txt) const {
int N = txt.length(), M = pat.length();
// Does the pattern match the text at position i?
for (int skip, i = 0; i <= N - M; i += skip) {
// Does the pattern match the text at position i?
skip = 0;
for (int j = M - 1; j >= 0; --j) {
if (pat[j] != txt[i + j]) {
Expand Down
10 changes: 5 additions & 5 deletions Huffman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include <iostream>
#include <vector>

// Make a lookup table from trie.
std::vector<std::string> Huffman::buildCode(const std::shared_ptr<Node> &root) {
// Make a lookup table from trie.
std::vector<std::string> st(R);
buildCode(st, root, "");
return st;
}

void Huffman::buildCode(std::vector<std::string> &st, const std::shared_ptr<Node> &x, const std::string &s) {
// Make a lookup table from trie (recursive).
void Huffman::buildCode(std::vector<std::string> &st, const std::shared_ptr<Node> &x, const std::string &s) {
if (x->isLeaf()) {
st[x->ch] = s;
return;
Expand All @@ -21,24 +21,24 @@ void Huffman::buildCode(std::vector<std::string> &st, const std::shared_ptr<Node
buildCode(st, x->right, s + '1');
}

// Initialize priority queue with singleton trees.
std::shared_ptr<Huffman::Node> Huffman::buildTrie(const std::vector<int> &freq) {
// Initialize priority queue with singleton trees.
MinPQ<NodePtr> pq(R);
for (int c = 0; c < R; ++c) {
if (freq[c] > 0) pq.insert(std::make_shared<Node>(c, freq[c], nullptr, nullptr));
}

while (pq.size() > 1) {
// Merge two smallest trees.
while (pq.size() > 1) {
auto x = *pq.delMin();
auto y = *pq.delMin();
pq.insert(std::make_shared<Node>('\0', x->freq + y->freq, x, y));
}
return *pq.delMin();
}

void Huffman::writeTrie(const std::shared_ptr<Node> &x) {
// Write bitstring-encoded trie.
void Huffman::writeTrie(const std::shared_ptr<Node> &x) {
if (x->isLeaf()) {
BinaryStdIO::write(true);
BinaryStdIO::write(x->ch);
Expand Down
2 changes: 1 addition & 1 deletion Huffman.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Huffman {
static std::shared_ptr<Node> readTrie();

public:
// comparable std::shared_ptr<Node> wrapper
class NodePtr {
// comparable std::shared_ptr<Node> wrapper
private:
std::shared_ptr<Node> nodePtr;

Expand Down
2 changes: 1 addition & 1 deletion Insertion.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Insertion : public Sorting {
template<typename T>
void Insertion::sort(std::vector<T> &a) {
int N = a.size();
// Insert a[i] among a[i-1], a[i-2], a[i-3]...
for (int i = 1; i < N; ++i) {
// Insert a[i] among a[i-1], a[i-2], a[i-3]...
for (int j = i; j > 0 && less(a[j], a[j - 1]); --j) {
exch(a, j, j - 1);
}
Expand Down
4 changes: 2 additions & 2 deletions KMP.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "KMP.h"
#include <utility>

// Build DFA from pattern.
KMP::KMP(std::string pat) : pat(std::move(pat)) {
// Build DFA from pattern.
int M = this->pat.length(), R = 256;
dfa = std::vector(R, std::vector<int>(M));
dfa[this->pat[0]][0] = 1;
// Compute dfa[][j].
for (int X = 0, j = 1; j < M; ++j) {
// Compute dfa[][j].
for (int c = 0; c < R; ++c) dfa[c][j] = dfa[c][X]; // Copy mismatch cases.
dfa[this->pat[j]][j] = j + 1; // Set match case.
X = dfa[this->pat[j]][X]; // Update restart state.
Expand Down
3 changes: 1 addition & 2 deletions MSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ void MSD::sort(std::vector<std::string> &a, int lo, int hi, int d) {
for (int i = lo; i <= hi; ++i) aux[count[charAt(a[i], d) + 1]++] = a[i];
for (int i = lo; i <= hi; ++i) a[i] = aux[i - lo];
for (int r = 0; r < R; ++r) {
// Recursively sort for each character value.
sort(a, lo + count[r], lo + count[r + 1] - 1, d + 1);
sort(a, lo + count[r], lo + count[r + 1] - 1, d + 1); // Recursively sort for each character value.
}
}

Expand Down
4 changes: 2 additions & 2 deletions MergeBU.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ template<typename T>
void MergeBU::sort(std::vector<T> &a) {
int N = a.size();
aux<T> = std::vector<T>(N);
// sz: subarray size
for (int sz = 1; sz < N; sz *= 2) {
// sz: subarray size
// lo: subarray index
for (int lo = 0; lo < N - sz; lo += 2 * sz) {
// lo: subarray index
merge(a, lo, lo + sz - 1, std::min(lo + 2 * sz - 1, N - 1));
}
}
Expand Down
5 changes: 2 additions & 3 deletions NFA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ NFA::NFA(std::string_view regexp) : re(regexp), M(re.length()), G(M + 1) {
} else lp = orPos;
}
if (i < M - 1 && re[i + 1] == '*') {
// lookahead
G.addEdge(lp, i + 1);
G.addEdge(i + 1, lp);
}
} // lookahead
if (re[i] == '(' || re[i] == '*' || re[i] == ')') G.addEdge(i, i + 1);
}
}
Expand All @@ -32,8 +31,8 @@ bool NFA::recognizes(std::string_view txt) const {
for (int v = 0; v < G.V(); ++v)
if (dfs.marked(v)) pc.push_front(v);

// Compute possible NFA states for txt[i+1].
for (int i = 0; i < txt.length(); ++i) {
// Compute possible NFA states for txt[i+1].
std::list<int> match;
for (int v: pc) {
if (v < M) {
Expand Down
3 changes: 1 addition & 2 deletions PrimMST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ void PrimMST::visit(const EdgeWeightedGraph &G, int v) {
int w = e.other(v);
if (marked[w]) continue; // v-w is ineligible
if (e.weight() < distTo[w]) {
// Edge e is new best connection from tree to w.
edgeTo[w] = e;
distTo[w] = e.weight();
if (pq.contains(w)) pq.change(w, distTo[w]);
else pq.insert(w, distTo[w]);
}
} // Edge e is new best connection from tree to w.
}
}

Expand Down
4 changes: 2 additions & 2 deletions Quick.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ template<typename T>
int Quick::partition(std::vector<T> &a, int lo, int hi) {
int i = lo, j = hi + 1;
T v = a[lo];
// Scan right, scan left, check for scan complete, and exchange.
while (true) {
// Scan right, scan left, check for scan complete, and exchange.
while (less(a[++i], v)) {
if (i == hi) break;
}
Expand All @@ -35,7 +35,7 @@ int Quick::partition(std::vector<T> &a, int lo, int hi) {
exch(a, i, j);
}
exch(a, lo, j); // Put v = a[j] into position
return j; // with a[lo..j-1] <= a[j] <= a[j+1..hi].
return j; // with a[lo..j-1] <= a[j] <= a[j+1..hi].
}

template<typename T>
Expand Down
4 changes: 2 additions & 2 deletions RabinKarp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ long long RabinKarp::hash(std::string_view key, int M) const {
return h;
}

// a random 31-bit prime
long long RabinKarp::longRandomPrime() {
// a random 31-bit prime
std::default_random_engine e(std::random_device{}());
std::uniform_int_distribution u(1LL << 30, (1LL << 31) - 1);
while (true) {
Expand All @@ -34,8 +34,8 @@ int RabinKarp::search(std::string_view txt) const {
int N = txt.length();
long long txtHash = hash(txt, M);
if (patHash == txtHash && check(0)) return 0; // Match at beginning.
// Remove leading digit, add trailing digit, check for match.
for (int i = M; i < N; ++i) {
// Remove leading digit, add trailing digit, check for match.
txtHash = (txtHash + Q - RM * txt[i - M] % Q) % Q;
txtHash = (txtHash * R + txt[i]) % Q;
if (patHash == txtHash && check(i - M + 1)) return i - M + 1; // match
Expand Down
2 changes: 1 addition & 1 deletion Selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Selection : public Sorting {
template<typename T>
void Selection::sort(std::vector<T> &a) {
int N = a.size();
// Exchange a[i] with the smallest entry in a[i+1..N).
for (int i = 0; i < N; ++i) {
// Exchange a[i] with smallest entry in a[i+1..N).
int min = i;
for (int j = i + 1; j < N; ++j) {
if (less(a[j], a[min])) min = j;
Expand Down
2 changes: 1 addition & 1 deletion Shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void Shell::sort(std::vector<T> &a) {
int h = 1;
while (h < N / 3) h = h * 3 + 1;
while (h >= 1) {
// Insert a[i] among a[i-h], a[i-2*h], a[i-3*h]...
for (int i = h; i < N; ++i) {
// Insert a[i] among a[i-h], a[i-2*h], a[i-3*h]...
for (int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
exch(a, j, j - h);
}
Expand Down
4 changes: 2 additions & 2 deletions SymbolDigraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

SymbolDigraph::SymbolDigraph(const std::filesystem::path &stream, char sp) {
std::ifstream in(stream);
// First pass builds the index by reading strings to associate each distinct string with an index.
for (std::string line; std::getline(in, line);) {
// First pass builds the index by reading strings to associate each distinct string with an index.
std::string name;
for (std::istringstream iss(line); std::getline(iss, name, sp);) {
if (!st.contains(name)) st.put(name, st.size());
Expand All @@ -17,8 +17,8 @@ SymbolDigraph::SymbolDigraph(const std::filesystem::path &stream, char sp) {

G_ = std::make_unique<Digraph>(st.size());
in = std::ifstream(stream);
// Second pass builds the graph by connecting the first vertex on each line to all the others.
for (std::string line; std::getline(in, line);) {
// Second pass builds the graph by connecting the first vertex on each line to all the others.
std::string name;
std::istringstream iss(line);
std::getline(iss, name, sp);
Expand Down
4 changes: 2 additions & 2 deletions TestOrderedST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace TestST {
os << "max = " << st.max().value_or(INVALID_KEY) << std::endl;
}

// Remove the smallest keys.
void removeSome(OrderedST<std::string, int> &st, std::ostream &os) {
// Remove the smallest keys.
for (int i = 0; i < st.size() / 2; ++i) {
st.removeMin();
}
Expand All @@ -19,8 +19,8 @@ namespace TestST {
listAll(st, os);
}

// Remove all the remaining keys.
void removeAll(OrderedST<std::string, int> &st, std::ostream &os) {
// Remove all the remaining keys.
while (!st.isEmpty()) {
st.remove(st.select(st.size() / 2).value_or(INVALID_KEY));
}
Expand Down
6 changes: 3 additions & 3 deletions TestST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace TestST {
os << "size = " << st.size() << std::endl;
}

// Print keys using keys().
void listAll(const ST<std::string, int> &st, std::ostream &os) {
// Print keys using keys().
for (const auto &s: st.keys()) {
os << s << " " << st.get(s).value_or(INVALID_VALUE) << std::endl;
}
}

// Remove some randomly selected keys.
void removeSome(ST<std::string, int> &st, std::ostream &os) {
// Remove some randomly selected keys.
std::default_random_engine e(std::random_device{}());
std::bernoulli_distribution b;
for (const auto &s: st.keys()) {
Expand All @@ -27,8 +27,8 @@ namespace TestST {
listAll(st, os);
}

// Remove all the remaining keys.
void removeAll(ST<std::string, int> &st, std::ostream &os) {
// Remove all the remaining keys.
for (const auto &s: st.keys()) {
st.remove(s);
}
Expand Down

0 comments on commit 2b30eb6

Please sign in to comment.