Skip to content

Commit

Permalink
Merge pull request #81 from 37790928/master
Browse files Browse the repository at this point in the history
work
  • Loading branch information
alesiong authored May 21, 2017
2 parents a50053e + f289f32 commit 50ded19
Show file tree
Hide file tree
Showing 31 changed files with 2,315 additions and 263 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ option(SAMPLES "Compile samples" ON)
option(COVERAGE "Enable coverage test for codecov" OFF)

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W4 -wd4244 -wd4267 -wd4800 -wd4068 -WX -MP")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W4 -wd4244 -wd4267 -wd4800 -wd4068 -wd4458 -WX -MP")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Zi -Od")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ox")
add_definitions(-DUNICODE -D_UNICODE)
Expand Down
2 changes: 1 addition & 1 deletion external/gtest
53 changes: 53 additions & 0 deletions src/geometry/Line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <cmath>

#include "geometry/Line.h"

#define EPS 1e-6

namespace {

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

}

namespace cuhksz {

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

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

Line::~Line() { }

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;
}

double disToLine(Point<2> P, Line l) {
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());
}

}

#undef EPS
111 changes: 111 additions & 0 deletions src/graph/BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "graph/BST.h"

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;
}

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);
}
}
}
}

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* 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;
}

void BST::insert(BSTNode* x) {
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;
}
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];
}
}

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;
}



}
42 changes: 42 additions & 0 deletions src/graph/Edge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "graph/Edge.h"

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 ++;
}

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

Edge::~Edge() { }

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 !(*this == other);
}

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

} // namespace cuhksz
57 changes: 57 additions & 0 deletions src/graph/Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "graph/Tree.h"

namespace cuhksz {

Node::Node(int id, int val) : id(id), val(val) {
fa = nullptr;
son.clear();
}

Node::~Node() { }

Node* Node::getAncestor(int level) {
Node* cur = this;
for (int i = 0; level >= (1 << i); i ++)
if (level & (1 << i))
cur = cur->ancestor[i];

return cur;
}

void Tree::setRoot(Node* newRoot) {
root = newRoot;
root->setFa(nullptr);
root->setHeight(0);
}

void Tree::addNode(Node* node, Node* fa) {
fa->addSon(node);
node->setFa(fa);
node->setHeight(fa->getHeight() + 1);
node->ancestor.push_back(fa);
for (int i = 0; ; i ++) {
Node* nextNode = node->ancestor[i];
if ((int)nextNode->ancestor.size() <= i) break;
node->ancestor.push_back(nextNode->ancestor[i]);
}
}

Node* Tree::LCA(Node* x, Node* y) {
int hx = x->getHeight(), hy = y->getHeight();
if (hx < hy) y = y->getAncestor(hy - hx);
else x = x->getAncestor(hx - hy);
if (x == y) return x;
for (int i = x->ancestor.size() - 1; i >= 0; i --) {
if (x->ancestor[i] != y->ancestor[i]) {
x = x->ancestor[i], y = y->ancestor[i];
}
}
return x->getFa();
}

int Tree::getDistance(Node* x, Node* y) {
return x->getHeight() + y->getHeight() - 2 * LCA(x, y)->getHeight();
}


}
75 changes: 75 additions & 0 deletions src/graph/Vertex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "graph/Vertex.h"
#include "graph/Edge.h"

namespace cuhksz {

int Vertex::nextVertexID = 0;

Vertex::Vertex() {
val = 0;
use = true;
id = Vertex::nextVertexID ++;
inEdges.clear();
outEdges.clear();
}

Vertex::Vertex(int val) : val(val) {
use = true;
id = Vertex::nextVertexID ++;
inEdges.clear();
outEdges.clear();
}

Vertex::Vertex(const Vertex& src) {
val = src.val;
use = src.use;
id = Vertex::nextVertexID ++;
inEdges.clear();
outEdges.clear();
for (std::list<Edge*>::const_iterator it = src.inEdges.begin(); it != src.inEdges.end(); ++ it)
inEdges.push_back(*it);
for (std::list<Edge*>::const_iterator it = src.outEdges.begin(); it != src.outEdges.end(); ++ it)
outEdges.push_back(*it);
}

Vertex::~Vertex() {
for (std::list<Edge*>::iterator it = outEdges.begin(); it != outEdges.end(); ++ it)
if (*it != nullptr)
delete *it;
inEdges.clear();
outEdges.clear();
}

Vertex& Vertex::operator= (const Vertex& src) {
val = src.val;
use = src.use;
inEdges.clear();
outEdges.clear();
for (std::list<Edge*>::const_iterator it = src.inEdges.begin(); it != src.inEdges.end(); ++ it)
inEdges.push_back(*it);
for (std::list<Edge*>::const_iterator it = src.outEdges.begin(); it != src.outEdges.end(); ++ it)
outEdges.push_back(*it);
return *this;
}

bool Vertex::operator== (const Vertex& src) const {
return id == src.id;
}

bool Vertex::operator!= (const Vertex& src) const {
return !(*this == src);
}

bool Vertex::operator< (const Vertex& src) const {
return val < src.val;
}

void Vertex::addEdge(Vertex& to, int val) {
Edge* e = new Edge(this, &to, val);
outEdges.push_back(e);
to.inEdges.push_back(e);
}



} // namespace cuhksz
30 changes: 29 additions & 1 deletion src/include/geometry/Circle.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
#ifndef CUHKSZ_GEOMETRY_CIRCLE
#define CUHKSZ_GEOMETRY_CIRCLE

#include "geometry/Point.h"
#include <cmath>

namespace cuhksz {

#define PI 3.1415926

class Circle {
private:
public:
Circle() : O(Point<2>(0, 0)), r(1.0) { }
explicit Circle(double r) : O(Point<2>(0, 0)), r(r) { }
explicit Circle(Point<2> O) : O(O), r(1.0) { }
Circle(Point<2> O, double r) : O(O), r(r) { }
~Circle() { }

double getArea() {
return PI * r * r;
}

double getPerimeter() {
return 2.0 * PI * r;
}

Point<2> getPoint(double rad) {
return O + GVector<2>(r * std::cos(rad), r * std::sin(rad));
}

private:
Point<2> O;
double r;
};

#undef PI

} // namespace cuhksz

#endif // CUHKSZ_GEOMETRY_CIRCLE
Loading

0 comments on commit 50ded19

Please sign in to comment.