Skip to content

Instantly share code, notes, and snippets.

View komakai's full-sized avatar

Giles Payne komakai

View GitHub Profile
typedef string::size_type ssize;
typedef pair<ssize, ssize> valLenPair;
ssize get(uint64_t x) {
double v = (sqrt((8. * x) + 1.) - 1)/2;
double c = ceil(v);
double dummy;
double frac = modf(v, &dummy);
double res = floor((c+1) * frac);
return (ssize)res;
// Red-black tree with subtree sizes for O(log(N)) calculation of order within the set
template <typename T>
class RedBlackTree {
public:
RedBlackTree() {
root = NIL = new Node();
NIL->parent = NIL->left = NIL->right = NIL;
}
~RedBlackTree() {
@komakai
komakai / Shuffles.cpp
Created February 8, 2025 03:46
Shuffles
#include <iostream>
#include <vector>
int getAt(long i, int n, int m) {
long long tmpI = i;
for (int tmpN = n; tmpN > 0; tmpN--) {
long long half = m * /*pow(2, tmpN - 1)*/ (1LL << (tmpN - 1));
long long quarter = (half + 1) / 2;
long long adjustedI = tmpI % half;
@komakai
komakai / add.metal
Created December 13, 2024 03:43
Metal compute shader swift
kernel void add_arrays(device const float* inA [[buffer(0)]],
device const float* inB [[buffer(1)]],
device float* result [[buffer(2)]],
uint index [[thread_position_in_grid]])
{
result[index] = inA[index] + inB[index];
}
@komakai
komakai / compare.cpp
Created September 17, 2023 08:43
Compare OpenCV Mat
void compareMats(const Mat& a, const Mat& b) {
Mat diff = abs(a - b);
double maxVal;
Point maxLoc;
minMaxLoc( diff, nullptr, &maxVal, nullptr, &maxLoc );
if (maxVal > DBL_EPSILON) {
std::cout << "Max " << maxVal;
}
}
@komakai
komakai / readmat.cpp
Created September 17, 2023 08:39
Read OpenCV Mat from file
// refer to dumpMat gist regarding how to generate a file readable by this function
void readMatFromFile(std::string fileName, Mat& m) {
std::ifstream file(fileName, std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
if (size == 0) {
std::cout << "File is empty or does not exist\n";
}
file.seekg(0, std::ios::beg);
file.read((char*)m.data, MIN(size, m.total()*m.elemSize()));
file.close();
@komakai
komakai / dump.cpp
Last active September 17, 2023 08:41
Dump OpenCV Mat to file
static std::string type2str(int type) {
std::string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch ( depth ) {
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
@komakai
komakai / MatDataExt.h
Created October 5, 2020 13:07
Access Mat.data from Swift
//
// MatDataExt.h
//
// Created by Giles Payne on 2020/10/05.
//
#pragma once
#ifdef __cplusplus
#import <opencv2/opencv2.h>