Skip to content

Commit

Permalink
Merge pull request #83 from alesiong/master
Browse files Browse the repository at this point in the history
Format and fix
  • Loading branch information
alesiong authored May 21, 2017
2 parents 50ded19 + db1f700 commit 4ad122d
Show file tree
Hide file tree
Showing 34 changed files with 1,730 additions and 1,832 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
**Under Development**

**Pull requests other from the maintainers will be closed**

# CUHKSZ C++ Library

[![Build Status](https://travis-ci.org/cuhkshenzhen/CUHKSZLib.svg?branch=master)](https://travis-ci.org/cuhkshenzhen/CUHKSZLib)
Expand Down Expand Up @@ -86,3 +82,5 @@ You can also download our prebuilt library in the Release section on Github. The
If you are using IDEs other than Visual Studio, you can find the project file in `ide-project-files` directory. Currently we support CLion and Qt Creator. You need to copy the `ide-project-files/src` to your project directory.

For example, you are using Qt Creator with MinGW32 on Windows. You copy the contents of `ide-project-files/qt` directory to anywhere you like, say `my-project`. Then you also need to copy `ide-project-files/src` to `myproject` directory, so that `my-project` now has a `.pro` file and a `src` directory. Now you can download the prebuilt library: `windows-x86-mingw.zip` and unzip the contents to `my-project`.

### [FAQ](https://github.com/cuhkshenzhen/CUHKSZLib/wiki/FAQ)
45 changes: 24 additions & 21 deletions src/geometry/Line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,50 @@
namespace {

int dcmp(double x) {
if (std::fabs(x) < EPS) return 0;
return x > 0 ? 1 : -1;
if (std::fabs(x) < EPS) return 0;
return x > 0 ? 1 : -1;
}

}
} // namespace

namespace cuhksz {

Line::Line(Point<2> A, GVector<2> v) : A(A), v(v) { }
Line::Line(Point<2> A, GVector<2> v) : A(A), v(v) {}

Line::Line(const Line& src) {
A = src.A;
v = src.v;
A = src.A;
v = src.v;
}

Line::~Line() { }
Line::~Line() {}

Line& Line::operator= (const Line& src) {
A = src.A;
v = src.v;
return *this;
Line& Line::operator=(const Line& src) {
A = src.A;
v = src.v;
return *this;
}

Point<2> getIntersection(const Line& l1, const Line& l2) {
GVector<2> u = l1.A - l2.A;
double t = cross(l2.v, u) / cross(l1.v, l2.v);
return l1.A + l1.v * t;
GVector<2> u = l1.A - l2.A;
double t = cross(l2.v, u) / cross(l1.v, l2.v);
return l1.A + l1.v * t;
}

double disToLine(Point<2> P, Line l) {
GVector<2> v1 = l.v, v2 = P - l.A;
return std::fabs(cross(v1, v2) / v1.len());
GVector<2> v1 = l.v, v2 = P - l.A;
return std::fabs(cross(v1, v2) / v1.len());
}

double disToSegment(Point<2> P, Line l) {
GVector<2> v1 = l.v, v2 = P - l.A, v3 = P - l.A - l.v;
if (dcmp(v1 * v2) < 0) return v2.len();
else if (dcmp(v1 * v3) > 0) return v3.len();
else return std::fabs(cross(v1, v2) / v1.len());
GVector<2> v1 = l.v, v2 = P - l.A, v3 = P - l.A - l.v;
if (dcmp(v1 * v2) < 0)
return v2.len();
else if (dcmp(v1 * v3) > 0)
return v3.len();
else
return std::fabs(cross(v1, v2) / v1.len());
}

}
} // namespace cuhksz

#undef EPS
171 changes: 88 additions & 83 deletions src/graph/BST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,114 @@
namespace cuhksz {

void BST::rotate(BSTNode* x, int c) {
BSTNode* y = x->fa;
y->ch[!c] = x->ch[c];
if (x->ch[c] != nullptr) x->ch[c]->fa = y;
x->fa = y->fa;
if (y->fa != nullptr) {
if (y->fa->ch[0] == y) y->fa->ch[0] = x;
else y->fa->ch[1] = x;
}
y->fa = x, x->ch[c] = y;
if (y == root) root = x;
BSTNode* y = x->fa;
y->ch[!c] = x->ch[c];
if (x->ch[c] != nullptr) x->ch[c]->fa = y;
x->fa = y->fa;
if (y->fa != nullptr) {
if (y->fa->ch[0] == y)
y->fa->ch[0] = x;
else
y->fa->ch[1] = x;
}
y->fa = x, x->ch[c] = y;
if (y == root) root = x;
}

void BST::splay(BSTNode* x) {
while (x->fa != nullptr) {
if (x->fa->fa == nullptr) {
if (x->fa->ch[0] == x) rotate(x, 1);
else rotate(x, 0);
} else {
BSTNode* y = x->fa;
BSTNode* z = y->fa;
if (z->ch[0] == y) {
if (y->ch[0] == x)
rotate(y, 1), rotate(x, 1);
else rotate(x, 0), rotate(x, 1);
} else {
if (y->ch[1] == x)
rotate(y, 0), rotate(x, 0);
else rotate(x, 1), rotate(x, 0);
}
}
while (x->fa != nullptr) {
if (x->fa->fa == nullptr) {
if (x->fa->ch[0] == x)
rotate(x, 1);
else
rotate(x, 0);
} else {
BSTNode* y = x->fa;
BSTNode* z = y->fa;
if (z->ch[0] == y) {
if (y->ch[0] == x)
rotate(y, 1), rotate(x, 1);
else
rotate(x, 0), rotate(x, 1);
} else {
if (y->ch[1] == x)
rotate(y, 0), rotate(x, 0);
else
rotate(x, 1), rotate(x, 0);
}
}
}
}

BSTNode* BST::pre(BSTNode* x) {
BSTNode* cur = x->ch[0];
if (cur == nullptr) return nullptr;
while(cur->ch[1] != nullptr)
cur = cur->ch[1];
return cur;
BSTNode* cur = x->ch[0];
if (cur == nullptr) return nullptr;
while (cur->ch[1] != nullptr) cur = cur->ch[1];
return cur;
}

BSTNode* BST::suf(BSTNode* x) {
BSTNode* cur = x->ch[1];
if (cur == nullptr) return nullptr;
while(cur->ch[0] != nullptr)
cur = cur->ch[0];
return cur;
BSTNode* cur = x->ch[1];
if (cur == nullptr) return nullptr;
while (cur->ch[0] != nullptr) cur = cur->ch[0];
return cur;
}

void BST::insert(BSTNode* x) {
if (root == nullptr) {
root = x;
return;
}
if (root == nullptr) {
root = x;
return;
}

BSTNode* cur = root;
BSTNode* next;
while (true) {
if (x->rank < cur->rank) {
next = cur->ch[0];
} else {
next = cur->ch[1];
}
if (next == nullptr) break;
cur = next;
BSTNode* cur = root;
BSTNode* next;
while (true) {
if (x->rank < cur->rank) {
next = cur->ch[0];
} else {
next = cur->ch[1];
}
x->fa = cur;
cur->ch[x->rank >= cur->rank] = x;
splay(x);
if (next == nullptr) break;
cur = next;
}
x->fa = cur;
cur->ch[x->rank >= cur->rank] = x;
splay(x);
}

void BST::erase(BSTNode* x) {
splay(x);
if (x->ch[0] == nullptr && x->ch[1] == nullptr) {
root = nullptr;
} else if (x->ch[0] == nullptr && x->ch[1] != nullptr) {
root = x->ch[1];
root->fa = nullptr;
} else if (x->ch[0] != nullptr && x->ch[1] == nullptr) {
root = x->ch[1];
root->fa = nullptr;
} else {
root = pre(x);
if (root->fa->ch[0] == root)
root->fa->ch[0] = root->ch[0];
else root->fa->ch[1] = root->ch[0];
if (root->ch[0] != nullptr) root->ch[0]->fa = root->fa;
root->fa = nullptr;
root->ch[0] = x->ch[0];
root->ch[1] = x->ch[1];
}
splay(x);
if (x->ch[0] == nullptr && x->ch[1] == nullptr) {
root = nullptr;
} else if (x->ch[0] == nullptr && x->ch[1] != nullptr) {
root = x->ch[1];
root->fa = nullptr;
} else if (x->ch[0] != nullptr && x->ch[1] == nullptr) {
root = x->ch[1];
root->fa = nullptr;
} else {
root = pre(x);
if (root->fa->ch[0] == root)
root->fa->ch[0] = root->ch[0];
else
root->fa->ch[1] = root->ch[0];
if (root->ch[0] != nullptr) root->ch[0]->fa = root->fa;
root->fa = nullptr;
root->ch[0] = x->ch[0];
root->ch[1] = x->ch[1];
}
}

BSTNode* BST::find(int k) {
BSTNode* cur = root;
while (cur != nullptr) {
if (cur->rank == k) return cur;
if (cur->rank < k) cur = cur->ch[1];
else cur = cur->ch[0];
}
return nullptr;
BSTNode* cur = root;
while (cur != nullptr) {
if (cur->rank == k) return cur;
if (cur->rank < k)
cur = cur->ch[1];
else
cur = cur->ch[0];
}
return nullptr;
}



}
} // namespace cuhksz
42 changes: 18 additions & 24 deletions src/graph/Edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,32 @@ namespace cuhksz {
int Edge::nextEdgeID = 0;

Edge::Edge(Vertex* from, Vertex* to, int val) : from(from), to(to), val(val) {
use = true;
id = Edge::nextEdgeID ++;
use = true;
id = Edge::nextEdgeID++;
}

Edge::Edge(const Edge& src) {
from = src.from;
to = src.to;
val = src.val;
use = src.use;
id = Edge::nextEdgeID ++;
from = src.from;
to = src.to;
val = src.val;
use = src.use;
id = Edge::nextEdgeID++;
}

Edge::~Edge() { }
Edge::~Edge() {}

Edge& Edge::operator= (const Edge & src) {
from = src.from;
to = src.to;
val = src.val;
use = src.use;
return *this;
Edge& Edge::operator=(const Edge& src) {
from = src.from;
to = src.to;
val = src.val;
use = src.use;
return *this;
}

bool Edge::operator== (const Edge& other) const {
return id == other.id;
}
bool Edge::operator==(const Edge& other) const { return id == other.id; }

bool Edge::operator!= (const Edge& other) const {
return !(*this == other);
}
bool Edge::operator!=(const Edge& other) const { return !(*this == other); }

bool Edge::operator< (const Edge& other) const {
return val < other.val;
}
bool Edge::operator<(const Edge& other) const { return val < other.val; }

} // namespace cuhksz
} // namespace cuhksz
Loading

0 comments on commit 4ad122d

Please sign in to comment.