Skip to content

Commit

Permalink
Format source code (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 authored Jun 13, 2020
1 parent 6792012 commit 8a9447b
Show file tree
Hide file tree
Showing 269 changed files with 2,123 additions and 2,065 deletions.
14 changes: 14 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 120

---
Language: Cpp
AllowShortBlocksOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline

---
Language: Java
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: clang-format

on: [push]

jobs:
check-clang-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: run clang-format
run: |
source_files=$(find . -type f -name "*.cpp" -o -name "*.h" -o -name "*.java")
diff -u <(cat $source_files) <(clang-format $source_files)
6 changes: 4 additions & 2 deletions cpp/backtrack/mis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ using namespace std;
#ifdef _MSC_VER
int __builtin_ctzll(unsigned long long x) {
int bit = 0;
while (bit < 64 && (x & (1LL << bit)) == 0) ++bit;
while (bit < 64 && (x & (1LL << bit)) == 0)
++bit;
return bit;
}
int __builtin_popcountll(unsigned long long x) {
int bits = 0;
for (; x; x &= x - 1, ++bits);
for (; x; x &= x - 1, ++bits)
;
return bits;
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion cpp/combinatorics/enumerating_combinations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ bool next_combination(vector<int> &comb, int n) {
int main() {
vector<int> comb{0, 1, 2};
do {
for (int v : comb) cout << v + 1 << " ";
for (int v : comb)
cout << v + 1 << " ";
cout << endl;
} while (next_combination(comb, 5));
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ll double_signed_area(const vector<int> &x, const vector<int> &y) {
int n = x.size();
ll area = 0;
for (int i = 0, j = n - 1; i < n; j = i++) {
area += (ll) (x[i] - x[j]) * (y[i] + y[j]); // area += (long) x[i] * y[j] - (long) x[j] * y[i];
area += (ll)(x[i] - x[j]) * (y[i] + y[j]); // area += (long) x[i] * y[j] - (long) x[j] * y[i];
}
return area;
}
Expand Down Expand Up @@ -50,10 +50,12 @@ struct Point {
bool operator<(const Point &o) const {
bool up1 = y > 0 || (y == 0 && x >= 0);
bool up2 = o.y > 0 || (o.y == 0 && o.x >= 0);
if (up1 != up2) return up1;
ll cmp = (ll) o.x * y - (ll) o.y * x;
if (cmp != 0) return cmp < 0;
return (ll) x * x + (ll) y * y < (ll) o.x * o.x + (ll) o.y * o.y;
if (up1 != up2)
return up1;
ll cmp = (ll)o.x * y - (ll)o.y * x;
if (cmp != 0)
return cmp < 0;
return (ll)x * x + (ll)y * y < (ll)o.x * o.x + (ll)o.y * o.y;
// return atan2(y, x) < atan2(o.y, o.x);
}
};
Expand All @@ -71,5 +73,4 @@ Line perpendicular(Line line, ll x, ll y) {
}

// usage example
int main() {
}
int main() {}
12 changes: 4 additions & 8 deletions cpp/geometry/convex_hull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct point {
};

bool isNotRightTurn(const point &a, const point &b, const point &c) {
long long cross = (long long) (a.x - b.x) * (c.y - b.y) - (long long) (a.y - b.y) * (c.x - b.x);
long long dot = (long long) (a.x - b.x) * (c.x - b.x) + (long long) (a.y - b.y) * (c.y - b.y);
long long cross = (long long)(a.x - b.x) * (c.y - b.y) - (long long)(a.y - b.y) * (c.x - b.x);
long long dot = (long long)(a.x - b.x) * (c.x - b.x) + (long long)(a.y - b.y) * (c.y - b.y);
return cross < 0 || (cross == 0 && dot <= 0);
}

Expand All @@ -30,13 +30,9 @@ vector<point> convex_hull(vector<point> points) {

// usage example
int main() {
vector<point> hull1 = convex_hull({{0, 0},
{3, 0},
{0, 3},
{1, 1}});
vector<point> hull1 = convex_hull({{0, 0}, {3, 0}, {0, 3}, {1, 1}});
cout << (3 == hull1.size()) << endl;

vector<point> hull2 = convex_hull({{0, 0},
{0, 0}});
vector<point> hull2 = convex_hull({{0, 0}, {0, 0}});
cout << (1 == hull2.size()) << endl;
}
5 changes: 1 addition & 4 deletions cpp/geometry/diameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ double diameter(const vector<point> &p) {

// usage example
int main() {
double d = diameter({{0, 0},
{3, 0},
{0, 3},
{1, 1}});
double d = diameter({{0, 0}, {3, 0}, {0, 3}, {1, 1}});
cout << d << endl;
}
14 changes: 9 additions & 5 deletions cpp/geometry/dynamic_upper_envelope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
const ll inf = numeric_limits<ll>::max();

ll div(ll a, ll b) { // floored division
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b);
}

Expand All @@ -31,15 +31,19 @@ struct LineContainer : multiset<Line, less<>> {
x->p = inf;
return false;
}
if (x->a == y->a) x->p = x->b > y->b ? inf : -inf;
else x->p = div(y->b - x->b, x->a - y->a);
if (x->a == y->a)
x->p = x->b > y->b ? inf : -inf;
else
x->p = div(y->b - x->b, x->a - y->a);
return x->p >= y->p;
}

void add_line(ll a, ll b) {
auto z = insert({a, b, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, erase(y));
while (isect(y, z))
z = erase(z);
if (x != begin() && isect(--x, y))
isect(x, erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
Expand Down
4 changes: 1 addition & 3 deletions cpp/geometry/find_segments_intersection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ struct segment {
pii a, b;
int id;

segment(pii a, pii b, int id) :
a(std::move(a)), b(std::move(b)), id(id) {
}
segment(pii a, pii b, int id) : a(std::move(a)), b(std::move(b)), id(id) {}

bool operator<(const segment &o) const {
if (a.first < o.a.first) {
Expand Down
16 changes: 4 additions & 12 deletions cpp/geometry/li_chao_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ using T = long long;
struct Line {
T a, d;

T eval(T x) {
return a * x + d;
}
T eval(T x) { return a * x + d; }
};

struct Node {
Expand Down Expand Up @@ -63,17 +61,11 @@ struct LiChaoTree {
T maxx;
Node *root;

LiChaoTree(T minx, T maxx) : minx(minx), maxx(maxx) {
root = new Node({0, numeric_limits<T>::max() / 2});
}
LiChaoTree(T minx, T maxx) : minx(minx), maxx(maxx) { root = new Node({0, numeric_limits<T>::max() / 2}); }

void add_line(Line line) {
root->add_line(line, minx, maxx + 1);
}
void add_line(Line line) { root->add_line(line, minx, maxx + 1); }

T get_min(T x) {
return root->get_min(x, minx, maxx + 1);
}
T get_min(T x) { return root->get_min(x, minx, maxx + 1); }
};

// usage example
Expand Down
7 changes: 2 additions & 5 deletions cpp/geometry/point_classification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ using namespace std;

using ll = long long;

enum class Position {
Left, Right, Behind, Beyond, Origin, Destionation, Between
};
enum class Position { Left, Right, Behind, Beyond, Origin, Destionation, Between };

// Classifies position of point p against vector a
Position classify(ll px, ll py, ll ax, ll ay) {
Expand All @@ -33,5 +31,4 @@ Position classify(ll px, ll py, ll ax, ll ay) {
}

// usage example
int main() {
}
int main() {}
7 changes: 3 additions & 4 deletions cpp/geometry/point_in_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ int pointInPolygon(int qx, int qy, const vector<int> &x, const vector<int> &y) {
int cnt = 0;
for (int i = 0, j = n - 1; i < n; j = i++) {
if (y[i] == qy && (x[i] == qx || (y[j] == qy && (x[i] <= qx || x[j] <= qx) && (x[i] >= qx || x[j] >= qx))))
return 0; // boundary
return 0; // boundary
if ((y[i] > qy) != (y[j] > qy)) {
ll det = ((ll) x[i] - qx) * ((ll) y[j] - qy) - ((ll) x[j] - qx) * ((ll) y[i] - qy);
ll det = ((ll)x[i] - qx) * ((ll)y[j] - qy) - ((ll)x[j] - qx) * ((ll)y[i] - qy);
if (det == 0)
return 0; // boundary
return 0; // boundary
if ((det > 0) != (y[j] > y[i]))
++cnt;
}
}
return cnt % 2 == 0 ? -1 /* exterior */ : 1 /* interior */;
}


// usage example
int main() {
vector<int> x{0, 0, 2, 2};
Expand Down
3 changes: 1 addition & 2 deletions cpp/geometry/point_to_segment_distance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ double point_to_segment_distance(int x, int y, int x1, int y1, int x2, int y2) {
return fastHypot(px, py);
if (dotProduct >= squaredLength)
return fastHypot(px - dx, py - dy);
double q = (double) dotProduct / squaredLength;
double q = (double)dotProduct / squaredLength;
return fastHypot(px - q * dx, py - q * dy);
}

double point_to_line_distance(ll x, ll y, ll a, ll b, ll c) {
return abs(a * x + b * y + c) / fastHypot(a, b);
}


// usage example
int main() {
cout << fixed << setprecision(10);
Expand Down
13 changes: 7 additions & 6 deletions cpp/geometry/segments_intersection.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <bits/stdc++.h>

#include <optional>

using namespace std;
Expand All @@ -14,8 +15,8 @@ bool is_cross_intersect(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3, ll x4, ll y4)
}

bool is_cross_or_touch_intersect(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3, ll x4, ll y4) {
if (max(x1, x2) < min(x3, x4) || max(x3, x4) < min(x1, x2)
|| max(y1, y2) < min(y3, y4) || max(y3, y4) < min(y1, y2))
if (max(x1, x2) < min(x3, x4) || max(x3, x4) < min(x1, x2) || max(y1, y2) < min(y3, y4) ||
max(y3, y4) < min(y1, y2))
return false;
ll z1 = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
ll z2 = (x2 - x1) * (y4 - y1) - (y2 - y1) * (x4 - x1);
Expand All @@ -34,17 +35,17 @@ optional<pair<double, double>> get_lines_intersection(ll x1, ll y1, ll x2, ll y2
ll det = a1 * b2 - a2 * b1;
if (det == 0)
return {};
double x = -(c1 * b2 - c2 * b1) / (double) det;
double y = -(a1 * c2 - a2 * c1) / (double) det;
double x = -(c1 * b2 - c2 * b1) / (double)det;
double y = -(a1 * c2 - a2 * c1) / (double)det;
return optional{make_pair(x, y)};
}

// usage example
int main() {
optional<pair<double, double>> intersection = get_lines_intersection(0, 0, 4, 2, 2, -1, 2, 5);
cout << (bool) intersection << endl;
cout << (bool)intersection << endl;
cout << intersection->first << " " << intersection->second << endl;

optional<pair<double, double>> no_intersection = get_lines_intersection(0, 0, 0, 1, 1, 0, 1, 1);
cout << (bool) no_intersection << endl;
cout << (bool)no_intersection << endl;
}
14 changes: 6 additions & 8 deletions cpp/graphs/cycle_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ vector<int> find_cycle(const vector<vector<int>> &graph) {
vector<int> color(n);
vector<int> next(n);
for (int u = 0; u < n; u++) {
if (color[u] != 0) continue;
if (color[u] != 0)
continue;
int cycleStart = dfs(graph, u, color, next);
if (cycleStart != -1) {
vector<int> cycle;
Expand All @@ -41,17 +42,14 @@ vector<int> find_cycle(const vector<vector<int>> &graph) {

// usage example
int main() {
vector<vector<int>> graph{{1},
{2},
{0}};
vector<vector<int>> graph{{1}, {2}, {0}};
auto cycle = find_cycle(graph);
cout << cycle.size() << endl;
for (int x:cycle) cout << x << " ";
for (int x : cycle)
cout << x << " ";
cout << endl;

graph = {{1},
{2},
{}};
graph = {{1}, {2}, {}};
cycle = find_cycle(graph);
cout << cycle.size() << endl;
}
16 changes: 8 additions & 8 deletions cpp/graphs/dfs/strongly_connected_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ vector<vector<int>> scc_graph(const vector<vector<int>> &graph, const vector<vec
unordered_set<long long> edges;
for (int u = 0; u < graph.size(); u++)
for (int v : graph[u])
if (comp[u] != comp[v] && edges.insert(((long long) comp[u] << 32) + comp[v]).second)
if (comp[u] != comp[v] && edges.insert(((long long)comp[u] << 32) + comp[v]).second)
g[comp[u]].push_back(comp[v]);
return g;
}

// usage example
int main() {
vector<vector<int>> graph = {{1},
{0},
{0, 1}};
vector<vector<int>> graph = {{1}, {0}, {0, 1}};

vector<vector<int>> components = scc(graph);
for (auto &component: components) {
for (int v:component) cout << v << " ";
for (auto &component : components) {
for (int v : component)
cout << v << " ";
cout << endl;
}

vector<vector<int>> sg = scc_graph(graph, components);
for (auto &a: sg) {
for (int v : a) cout << v << " ";
for (auto &a : sg) {
for (int v : a)
cout << v << " ";
cout << endl;
}
}
7 changes: 3 additions & 4 deletions cpp/graphs/dfs/topological_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ vector<int> topological_sort(const vector<vector<int>> &graph) {

// usage example
int main() {
vector<vector<int>> g = {{0},
{},
{0, 1}};
vector<vector<int>> g = {{0}, {}, {0, 1}};

vector<int> order = topological_sort(g);

for (int v : order) cout << v << " ";
for (int v : order)
cout << v << " ";
cout << endl;
}
10 changes: 4 additions & 6 deletions cpp/graphs/flows/global_min_cut_stoer_wagner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ pair<int, vector<int>> min_cut(vector<vector<int>> &cap) {

// usage example
int main() {
vector<vector<int>> capacity{{0, 1, 1, 0},
{1, 0, 1, 1},
{1, 1, 0, 1},
{0, 1, 1, 0}};
auto[cap, cut] = min_cut(capacity);
vector<vector<int>> capacity{{0, 1, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 0}};
auto [cap, cut] = min_cut(capacity);
cout << cap << endl;
for (int v:cut) cout << v << " ";
for (int v : cut)
cout << v << " ";
cout << endl;
}
Loading

1 comment on commit 8a9447b

@Asbaharoon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect project and it has amazing open source

Please sign in to comment.