Skip to content

Instantly share code, notes, and snippets.

View jeremy-rifkin's full-sized avatar
🐉

Jeremy Rifkin jeremy-rifkin

🐉
View GitHub Profile
@jeremy-rifkin
jeremy-rifkin / ftime-trace-aggregator.py
Last active March 13, 2024 04:49
very hacky -ftime-trace aggreggator, aggregating by instantiation time
import json
import copy
path = "..."
# hacky but whatever
def filter_templates(s: str):
out = ""
depth = 0
for c in s:
@jeremy-rifkin
jeremy-rifkin / 1d_kmeans.js
Created June 4, 2020 03:18
simple javascript implementation of the k-means algorithm for 1 dimentional data and the silhouette method for calculating the optimal number of clusters
// Couple quick utilities
// compute the average of an array
function average(data) {
let sum = 0;
for(let i = 0, l = data.length; i < l; i++)
sum += data[i];
return sum / data.length;
}
// compute the variation of an array
function variation(data) {
@jeremy-rifkin
jeremy-rifkin / optional.hpp
Created September 16, 2023 18:31
C++11 optional implementation
#ifndef OPTIONAL_HPP
#define OPTIONAL_HPP
#include <memory>
#include <new>
#include <stdexcept>
#include <type_traits>
#include <utility>
struct nullopt_t {};
@jeremy-rifkin
jeremy-rifkin / ifunny_filter.py
Last active June 28, 2023 11:58
A script to detect photos with "ifunny" watermarks and filter them from other photos.
# This is a program to filter photos with ifunny watermarks.
# I wrote this script for personal use to separate saved ifunny memes from other photos on my phone.
# The script is fully parallelized and is able to make good use of technologies like Intel
# hyperthreading because substantial program time is spent in IO. Bottleneck will be IO.
#
# Watermark false positive rate appears to be < 1/500
# Watermark false negative rate appears to be ~ 1/500
# False positive / negative rate checked with manual spot check sample size = 500.
#
# Rifkin 2020
@jeremy-rifkin
jeremy-rifkin / discord-ansi.py
Created September 16, 2022 15:12
Turns a copypasta into something ridiculous for discord
import re
import random
import math
text = """
What the fuck did you just fucking say about me, you little bitch? I'll have you know I graduated top of my class in the Navy Seals, and I've been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and I'm the top sniper in the entire US armed forces. You are nothing to me but just another target. I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth, mark my fucking words. You think you can get away with saying that shit to me over the Internet? Think again, fucker. As we speak I am contacting my secret network of spies across the USA and your IP is being traced right now so you better prepare for the storm, maggot. The storm that wipes out the pathetic little thing you call your life. You're fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways, and that's just with my bare hands. Not only am I extensively t
@jeremy-rifkin
jeremy-rifkin / malloc.hpp
Last active April 6, 2022 12:37
High performance malloc implementation
uintptr_t base;
const uintptr_t height = 100000000;
std::mt19937 rng;
[[gnu::constructor]] void init_malloc() {
base = (uintptr_t) sbrk(height);
}
void* malloc(size_t) { // parameter ignored, we don't need it
return (void*)(base + rng() % height); // odds of any collision is like, low
}
void free(void*) {} // no-op
@jeremy-rifkin
jeremy-rifkin / stringf.cpp
Last active September 11, 2021 06:20
c++ string formatting
#include <assert.h>
#include <string>
template<typename... T> std::string stringf(T... args) {
int length = snprintf(0, 0, args...);
if(length < 0) assert(("invalid arguments to stringf", false));
std::string str(length, 0);
snprintf(str.data(), length + 1, args...);
return str;
}
@jeremy-rifkin
jeremy-rifkin / typename.hpp
Created September 8, 2021 03:05
Short utility to get the name of a type in C++
template<class T> constexpr std::string_view type_name() {
// clang: std::string_view ns::type_name() [T = int]
// gcc: constexpr std::string_view ns::type_name() [with T = int; std::string_view = std::basic_string_view<char>]
// msvc: const char *__cdecl ns::type_name<int>(void)
auto substring_bounded_by = [](std::string_view sig, std::string_view l, std::string_view r) {
assert(sig.find(l) != std::string_view::npos);
assert(sig.rfind(r) != std::string_view::npos);
assert(sig.find(l) < sig.rfind(r));
auto i = sig.find(l) + l.length();
return sig.substr(i, sig.rfind(r) - i);
@jeremy-rifkin
jeremy-rifkin / c++numbers.re
Created August 27, 2021 03:33
regular expressions for c++ numeric literals
integer: (?:(?:0[Xx][0-9a-fA-F](?:'?[0-9a-fA-F])*)|(?:0[Bb][01](?:'?[01])*)|(?:0(?:'?[0-7])*)|(?:[1-9](?:'?\d)*))(?:[Uu](?:LL?|ll?|Z|z)?|(?:LL?|ll?|Z|z)[Uu]?)?
floating-point: (?:((?:\d(?:'?\d)*)?\.\d(?:'?\d)*|\d(?:'?\d)*\.)(?:[Ee][\+-]?\d(?:'?\d)*)?[FfLl]?|\d(?:'?\d)*[Ee][\+-]?\d(?:'?\d)*[FfLl]?|0[Xx](?:(?:[0-9a-fA-F](?:'?[0-9a-fA-F])*)?\.[0-9a-fA-F](?:'?[0-9a-fA-F])*|[0-9a-fA-F](?:'?[0-9a-fA-F])*\.)[Pp][\+-]?\d(?:'?\d)*[FfLl]?|0[Xx][0-9a-fA-F](?:'?[0-9a-fA-F])*[Pp][\+-]?\d(?:'?\d)*[FfLl]?)
@jeremy-rifkin
jeremy-rifkin / add.cpp
Last active August 8, 2021 02:33
Very simple idiomatic C++ program to add two numbers together
#include <cstdint>
#include <functional>
#include <memory>
#include <tuple>
#include <utility>
[[nodiscard]] decltype(std) add(const std::unique_ptr<std::tuple<std::int32_t and, std::int32_t and>> and t) noexcept {
return std::plus{}(std::min(std::forward<std::int32_t>(std::get<0>(*t)), std::forward<std::int32_t>(std::get<1>(*t))),
std::max(std::forward<std::int32_t>(std::get<0>(*t)), std::forward<std::int32_t>(std::get<1>(*t))));
}