Skip to content

Instantly share code, notes, and snippets.

@dno89
dno89 / tree_to_dlist.cpp
Created April 12, 2022 16:03
convert a binary tree to a double linked list in in-order order
#include <iostream>
using namespace std;
struct Node {
int value = 0;
Node* left = nullptr;
Node* right = nullptr;
};
@dno89
dno89 / k_means.cpp
Created March 29, 2022 00:20
Naive implementation of K-means for 2D points
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <Eigen/Dense>
#define T(X) cerr << #X": " << X << endl;
@dno89
dno89 / sparse_matrix_multiplication.cpp
Created March 27, 2022 00:42
Sparse matrix multiplication
#include <iostream>
#include <map>
#include <utility>
#include <cassert>
class SparseMatrix {
public:
SparseMatrix(size_t rows, size_t cols) :
rows_(rows), cols_(cols)
{}
@dno89
dno89 / convex_polygon_intersection.cc
Created March 5, 2022 17:26
intersection test for convex polygon (variant of SAT)
#include <iostream>
#include <vector>
#include <Eigen/Dense>
using namespace std;
using Point2 = Eigen::Vector2d;
using Polygon = vector<Point2>;
Point2 EdgeNormal(const Point2& p1, const Point2& p2) {
@dno89
dno89 / graph_copy.cpp
Created February 26, 2022 02:08
Copy of graph
#include <iostream>
#include <vector>
#include <map>
#include <functional>
using namespace std;
template<typename T>
class Node;
@dno89
dno89 / static_polymorphism.cpp
Created July 20, 2021 01:09
Static Polymorphism With Some Metaprogramming Fun
#include <type_traits>
#include <variant>
#include <string>
#include <iostream>
#include <vector>
namespace detail {
// create a variant with Ts... and add NewT iff Add == true.
template<bool Add, typename NewT, typename... Ts>
struct add_if_unique_helper {
@dno89
dno89 / sparse_matrix_sum.cpp
Created July 20, 2021 01:08
Sparse Matrix Sum
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>
class SparseMatrix {
public:
void setElement(int i, int j, double value) {
auto element_iter = findElement(i,j);
bool is_zero = (value==0.0);
@dno89
dno89 / transpose_inplace.cpp
Created July 20, 2021 01:07
Transpose inplace
#include <vector>
#include <cassert>
#include <cmath>
#include <iostream>
#define P(X)\
std::cerr << #X": " << X << std::endl;
class Matrix {
@dno89
dno89 / compute_pi.cpp
Created July 20, 2021 01:07
Compute PI
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int, char**) {
long long inside = 0;
for(long long ii = 0; ii < 1e9; ++ii) {
double x = rand()/double(RAND_MAX);