Skip to content

Instantly share code, notes, and snippets.

View kspalaiologos's full-sized avatar
❤️‍🔥

Kamila Szewczyk kspalaiologos

❤️‍🔥
View GitHub Profile
// Feeling overwhelmed by "find the words in a grid" tasks for preschoolers?
// Look no further!
#include <leptonica/allheaders.h>
#include <tesseract/baseapi.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
@kspalaiologos
kspalaiologos / clos.cpp
Created November 15, 2024 00:44
kludge to convert a cpp-style closure into a c pointer, retains data in an external structure
namespace {
template <typename F, int I, typename L, typename R, typename ...A>
inline F convert(L&& l, R (*)(A...) noexcept(noexcept(::std::declval<F>()(::std::declval<A>()...)))) {
static thread_local L l_(::std::forward<L>(l));
static thread_local bool full;
if (full) {
l_.~L();
new (static_cast<void*>(&l_)) L(::std::forward<L>(l));
} else
@kspalaiologos
kspalaiologos / pgn.l
Created November 4, 2024 20:15
Flex scanner for PGN files.
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
enum {
TAG = 0x100, COMMENT, REGULAR_MOVE, PROMOTION_MOVE, SHORT_CASTLE, LONG_CASTLE,
R1_0, R0_1, R12_12, RSTAR, CONTINUATION_MOVE_NUMBER, MOVE_NUMBER
};
@kspalaiologos
kspalaiologos / ccg.c
Last active September 9, 2024 10:45
Compact Cyclic Generator - a fast non-cryptographic random number generator based on polynomial codes.
// Public Domain.
#include <stdint.h>
#include <stdio.h>
uint32_t s, b;
void ccg_seed(uint32_t seed) {
s = (b = seed ^ 0xAAAAAAAA) | 1;
}
uint32_t ccg_next() {
uint32_t b = s >> 31;
s <<= 1, s ^= b * 0xEDB88320;
@kspalaiologos
kspalaiologos / ppm90.c
Last active June 4, 2024 17:13
PPM in 90 lines of code.
#define COPY(dst, src, n) for (int i = 0; i < n; i++) dst = src;
typedef struct {
uint32_t range, FF, cache; uint64_t low; FILE * source;
} rc_t;
void rc_init(rc_t * rc, FILE * source) {
rc->range = 0xFFFFFFFF;
rc->FF = rc->cache = 0; rc->low = 0;
rc->source = source;
}
void rc_init_dec(rc_t * rc, FILE * source) {
@kspalaiologos
kspalaiologos / gist:8845b3e65d82440b37635c6603ae436b
Created May 13, 2024 12:45
the most popular languages of cgcc
106 : ES6
107 : Nim
110 : Keg
114 : Fig
115 : Cubix
116 : Attache
117 : k
122 : Ly
123 : tinylisp
124 : Oracle
#include <book.h>
#define CODE_MAX 65534
#define DICT (1 << 17)
#define LZWEOF 256
struct { int32_t parent; int16_t c, code; } dict[DICT];
void encode(FILE * in, FILE * out) {
int nc = LZWEOF + 1, c, sc, i;
COPY(dict[i].code, -1, DICT);
if ((sc = fgetc(in)) == EOF) sc = LZWEOF;
while ((c = fgetc(in)) != EOF) {
@kspalaiologos
kspalaiologos / 2048.hs
Last active August 31, 2023 22:05
funniest 2048 on the planet
import Control.Monad
import System.Random
import Control.Monad.Trans.State
import Control.Lens hiding(under)
import Control.Conditional hiding(unless, when)
import Data.List
import Data.Functor
import Data.Random.Lift
import Control.Monad.IO.Class
import Control.Monad.Fix
@kspalaiologos
kspalaiologos / magic.c
Created August 25, 2023 17:08
practical o(n) {⍵[⍋⍵]} if {645=⎕dr⍵}
void magic(float * x, size_t n) {
uint32_t * result = malloc(n * sizeof(uint32_t)),
* buckets = malloc(0x100*sizeof(uint32_t)),
* si = malloc(0x100*sizeof(uint32_t)),
* y = (uint32_t *) x, key, temp;
for (size_t i = 0; i < n; i++) y[i] ^= 0x80000000;
for (int i = 0; i < n; ++i) ++buckets[y[i] & 0xFF];
si[0] = 0;
for (int j = 1; j < 0x100; ++j) si[j] = si[j - 1] + buckets[j - 1];
for (int i = n-1; i >= 0; --i) key = y[i] & 0xFF, result[si[key] + --buckets[key]] = y[i];
@kspalaiologos
kspalaiologos / ph1.apl
Created August 3, 2023 10:14
Phase 1 of the Dyalog APL competition.
e1←{⍵/⍨⍵≥⌈\⍵}
e2←⌽⍷⍥⌽
e3←{(⍺∘⌽⌷⍨∘⊂⍵⍳⍨⊢)' ',⎕A}
e4←×2⊥>-<
e5←{n,l-n←(l↑⍺)+.≤⍵↑⍨l←⍺⌊⍥≢⍵}
e6←(⊢⍴⍨2,⍨2÷⍨⍴)~⍤∊⍨⊆⊢
e7←(,÷∨)∘1
e8←{3↑⊃¯2⎕dt(1⎕dt⊂⍵)+⌊/644 924 759|⍺-⍥(1⎕dt⊆)⍵}
e9←{↑,/⌽¨⍵⊂⍨1 0⍴⍨≢⍵}
e0←{(⊢,(⊂'')↑⍨⍺-≢)1↓¨(' ',⍵)⊂⍨(⊢∧⍺≥+\)1,' '=⍵}