Skip to content

Instantly share code, notes, and snippets.

View leok7v's full-sized avatar

Leo Kuznetsov leok7v

View GitHub Profile
@leok7v
leok7v / maps34.c
Created November 3, 2024 20:55
3 & 4 bytes map for lz77
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
struct map { // single threaded use only
const void** p; // p[n]
size_t n;
uint32_t m; // mask 0xFFFFFF for 3 bytes and 0xFFFFFFFFu for 4 bytes
};
@leok7v
leok7v / random.c
Created October 7, 2024 05:23
random64 Linear Congruential Generator (LCG) and an XOR-based generator (XBG)
// Linear Congruential Generator (LCG) and an XOR-based generator (XBG)
// like xoroshiro or xoshiro
static uint64_t random64(uint64_t* state) {
// Linear Congruential Generator with inline mixing
thread_local static bool initialized;
if (!initialized) { initialized = true; *state |= 1; };
*state = (*state * 0xD1342543DE82EF95uLL) + 1;
uint64_t z = *state;
z = (z ^ (z >> 32)) * 0xDABA0B6EB09322E3uLL;
@leok7v
leok7v / ft.h
Last active October 7, 2024 05:14
Fenwick tree implementation
#ifndef ft_header_included
#define ft_header_included
// Copyright (c) 2024, Leo Kuznetsov
// This code and the accompanying materials are made available under the
// terms of the BSD-3 license, which accompanies this distribution. The full
// text of the license may be found at https://opensource.org/license/bsd-3-clause
#include <assert.h>
#include <stdbool.h>
@leok7v
leok7v / range_coder.h
Last active October 10, 2024 00:34
Range coder with simple adaptive frequency model
#ifndef rc_header_included
#define rc_header_included
// Copyright (c) 2024, "Leo" Dmitry Kuznetsov
// https://github.com/leok7v/rc
// This code and the accompanying materials are made available under the terms
// of BSD-3 license, which accompanies this distribution. The full text of the
// license may be found at https://opensource.org/license/bsd-3-clause
#include <stdint.h>
@leok7v
leok7v / bin_heap.c
Created August 9, 2024 20:24
bin heap for Huffman encode/decode
// https://chatgpt.com/share/696284b8-02bd-4651-a094-40feff0157ab
// https://en.wikipedia.org/wiki/Binary_heap
// J. W. J. Williams 1964
enum { alphabet = 32 }; // > 2 and power of two
enum { max_nodes = alphabet * 2 };
static_assert(alphabet > 2 && (alphabet & (alphabet - 1)) == 0,
"alphabet must be 2^n");
@leok7v
leok7v / functor.h
Created August 6, 2024 22:30
C99-C23 functors
typedef struct functor_s functor_t;
typedef struct functor_s {
void (*callback)(functor_t* f);
// other fields here
} functor_t;
typedef struct functor_ex_s {
union {
functor_t base;
struct functor_s; // open unnamed struct to push field names up
@leok7v
leok7v / vigil.h
Last active June 3, 2024 18:42
vigil.h exports: trace_line_va() trace_line() traceln() swear() wrong() fatal_if_error() for Win32 API debugging
#ifndef vigil_h
#define vigil_h // https://github.com/munificent/vigil
// exports: trace_line_va() trace_line() traceln() swear() wrong() fatal_if_error()
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winternl.h>
#include <ntstatus.h>
@leok7v
leok7v / named_dark_colors.c
Created April 30, 2024 21:55
named dark colors
typedef enum colors_t {
/* Main Panel Backgrounds */
charcoal = 0x36454F,
onyx = 0x353839,
gunmetal = 0x2A3439,
jet_black = 0x343434,
outer_space = 0x414A4C,
eerie_black = 0x1B1B1B,
oil = 0x3B3C36,
@leok7v
leok7v / lorem_ipsum_generator.c
Created January 6, 2023 08:22
lorem ipsum generator in C
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct {
char* text;
@leok7v
leok7v / polyfit.c
Last active June 26, 2022 01:13
Weekend 1 hour project - needed simplest possible "least squares fitting of a polynomial"
// polyfit
// Created by leo on 6/25/22.
// Copyright © 2022 leok7v.github.io. All rights reserved.
#include <stdbool.h>
#include <assert.h>
#include <math.h>
#include <float.h>
// by Legendre in 1805: