-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from 37790928/master
work
- Loading branch information
Showing
31 changed files
with
2,315 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule gtest
updated
2 files
+4 −0 | googletest/include/gtest/internal/custom/gtest.h | |
+3 −1 | googletest/src/gtest.cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.