Skip to content

Instantly share code, notes, and snippets.

@krackers
krackers / 3dhull.cpp
Created September 14, 2017 06:39 — forked from msg555/3dhull.cpp
3D Convex Hull
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cassert>
using namespace std;
@krackers
krackers / range2D.cpp
Created September 14, 2017 06:39 — forked from msg555/range2D.cpp
2D Range Tree Query Example
/*
LANG: C++
*/
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXN 512
@krackers
krackers / suffix.cpp
Created September 14, 2017 06:39 — forked from msg555/suffix.cpp
Suffix Array Implementation
struct suffix_array {
suffix_array(const char* S) : N(strlen(S)) {
vector<int> V;
for(int i = 0; i < N; i++) V.push_back(S[i]);
init(V);
}
suffix_array(const vector<int>& VV) : N(VV.size()) {
vector<int> V(VV);
init(V);
@krackers
krackers / geo.cpp
Created September 14, 2017 06:41 — forked from msg555/geo.cpp
Mark's geometry routines
#include <algorithm>
#include <vector>
#include <complex>
#include <cmath>
using namespace std;
/* A flag used by some geometry routines to indicate exceptional circumstances.
*/
static bool geoerror;
@krackers
krackers / splay.cpp
Created September 14, 2017 06:41 — forked from msg555/splay.cpp
Optimized splay tree implementation
template<class T> struct splnode {
typedef splnode<T> node_t;
splnode() : P(NULL) {
C[0] = C[1] = NULL;
pull();
}
/* Add extra state here. */
@krackers
krackers / plot-gp.py
Created February 9, 2018 02:22 — forked from neubig/plot-gp.py
A simple program to sample functions from a Gaussian process and plot them
#!/usr/bin/python
from math import exp
import numpy as np
import matplotlib.pyplot as plt
def rbf_kernel(x1, x2, variance = 1):
return exp(-1 * ((x1-x2) ** 2) / (2*variance))
def gram_matrix(xs):
@krackers
krackers / Proof of Reservoir Sampling
Created May 6, 2018 05:32 — forked from Nan-Zhang/Proof of Reservoir Sampling
How to prove reservoir sampling?
Say we want to generate a set of s elements and that we have already seen n>s elements.
Let's assume that our current s elements have already each been chosen with probability s/n.
By the definition of the algorithm, we choose element n+1 with probability s/(n+1).
Each element already part of our result set has a probability 1/s of being replaced.
The probability that an element from the n-seen result set is replaced in the n+1-seen result set is therefore (1/s)*s/(n+1)=1/(n+1). Conversely, the probability that an element is not replaced is 1-1/(n+1)=n/(n+1).
# basic pfctl control
# ==
# Related: http://www.OpenBSD.org
# Last update: Tue Dec 28, 2004
# ==
# Note:
# this document is only provided as a basic overview
# for some common pfctl commands and is by no means
# a replacement for the pfctl and pf manual pages.
@krackers
krackers / howto_nat_traversal.md
Created December 22, 2018 08:07 — forked from mildred/howto_nat_traversal.md
How To TCP NAT Traversal using Node.js and a STUN Server

How To TCP NAT Traversal using Node.js and a STUN Server

With the scarecity of IPv4 addresses, and IPv6 still not available at large, NAT traversal is becoming a necessity. Especially with the generalisation of Carrier-grade NATs that you can find on mobile connections. Even with IPv6 you may suffer NAT66. Imagine your mobile device that gets only a single Ipv6 address, and you want to share it on your computer.

The solution might be in a decentralized protocol for address attribution such

@krackers
krackers / lmutracker.mm
Created October 17, 2019 07:02 — forked from Glavin001/lmutracker.mm
Read lux measurement using MBP ambient light sensor.
// lmutracker.mm -- Provides lux measurement using MacBook Ambient Light Sensor
//
// clang -o lmutracker lmutracker.mm -framework IOKit -framework CoreFoundation
//
// Adaptation of code originally posted at https://bugzilla.mozilla.org/show_bug.cgi?id=793728
// by Reuben Morais. Modified by Ken Keiter <ken@kenkeiter.com> to output a single *lux* value
// and exit, rather than repeating measurements on the sensor's arbitrary scale.
#include <mach/mach.h>
#include <math.h>