Skip to content

Instantly share code, notes, and snippets.

View leok7v's full-sized avatar

Leo Kuznetsov leok7v

View GitHub Profile
@leok7v
leok7v / map_test.c
Created November 29, 2025 07:42
map_test.c (see map.h/map.c)
#include "map.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#define TEST_INT_PAIR(name, T, v1, v2) \
static void map_test_##name(void) { \
struct name map_of(T, T); \
struct name m = {}; \
bool ok = map_alloc(&m, 0); assert(ok); \
@leok7v
leok7v / map.c
Last active November 29, 2025 07:43
Simple (almost trivial) map. Subset of C++ std::map good enough to limited usage for str::map<std::string,something> replacement
#include "map.h"
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#undef MAP_DUMP_CONVERT
@leok7v
leok7v / proquint.c
Created November 21, 2025 19:14
Identifiers that are Readable, Spellable, and Pronounceable
/* This file is part of proquint: http://github.com/dsw/proquint .
See License.txt for copyright and terms of use. */
/*
Convert between proquint, hex, and decimal strings.
Please see the article on proquints: http://arXiv.org/html/0901.4016
Daniel S. Wilkerson
*/
#include "proquint.h"
@leok7v
leok7v / PlatformView.swift
Created October 2, 2025 01:05
Platform Independent unifying Platform[Web]View templates using ViewRepresentable to minimize/reduce #if #else #endif in SwiftUI code
import SwiftUI
/// Platform Independent unifying ViewRepresentable template to minimize #if #else #endif in SwiftUI code
#if canImport(UIKit)
import UIKit
public typealias _BaseView = UIView
public typealias PlatformViewRepresentable = UIViewRepresentable
#else
import AppKit
@leok7v
leok7v / UIWebView.swift
Created September 30, 2025 03:42
Swift.WebView polyfill
import SwiftUI
import WebKit
import Foundation
import SwiftUI
//import Foundation
#if os(iOS)
import UIKit
typealias OSWindow = UIWindow
typealias OSView = UIView
@leok7v
leok7v / arrays.c
Created September 26, 2025 04:08
growing arrays on heap and stack with bounds checks
#include <alloca.h>
#include <assert.h>
#include <limits.h> // INT_MAX
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h> // malloc/calloc/realloc/free
#include <string.h> // memcpy
@leok7v
leok7v / gpu.c
Last active September 14, 2025 19:18
Metal/Vulkan GPU count and VRAM detection (gpu.h/gpu.c)
/* File: gpu.h */
#pragma once
#ifndef GPU_H
#define GPU_H
#ifdef __cplusplus
extern "C" {
#endif
#include "etc.h"
@leok7v
leok7v / array.c
Last active September 12, 2025 00:44
Simple std::vector replacement in C
struct _array {
void* data;
int count;
int capacity;
};
#define array_of(type) { \
type* data; \
int count; \
int capacity; \
@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;