Skip to content

Instantly share code, notes, and snippets.

View leok7v's full-sized avatar

Leo Kuznetsov leok7v

View GitHub Profile
@leok7v
leok7v / arrays.c
Last active December 2, 2017 19:52
simplest C++ heap and stack array I can think of
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#define null ((void*)0)
#define mem_alloc malloc
#define mem_realloc realloc
#define mem_free free
#define assertion(e, fmt, ...) if (!(e)) { printf("assertion failed: %s ", #e); printf(fmt, __VA_ARGS__); __debugbreak(); }
#define assert(e) assertion(e, "");
@leok7v
leok7v / trace.h
Created January 2, 2018 22:25
trace.h
#define trace(format, ...) printf(__FILE__ "(%d) [%d] " format "\n", __LINE__, gettid(), ## __VA_ARGS__)
@leok7v
leok7v / system_np.c
Created February 1, 2018 02:00
Windows: system_np() - Creating a Child Process with Redirected Input and Output
#include <windows.h>
#include <stdio.h>
#include <malloc.h>
// Reworked MSDN sample starting with https://stackoverflow.com/questions/14147138/capture-output-of-spawned-process-to-string
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
// Implemented in ~C99 instead of C++
// Child Process window hidden (for use from WinMain() /SYSTEM:WINDOWS executable)
// Creates background thread for peeking the pipes to avoid deadlocks.
// Implements timeout in milliseconds and tames CPU utilization with WaitForMultipleObjects.
@leok7v
leok7v / hashtable.h
Last active June 1, 2021 13:19
Simple hash table in C
#ifndef HASHTABLE_DEFINITION // single file library cannot use pragma once
#define HASHTABLE_DEFINITION // https://en.wikipedia.org/wiki/Header-only
// https://github.com/nothings/single_file_libs
/*
License: "Unlicense" (public domain) see bottom of the file for details.
This is brain dead 4 hours implementation of #153 of absolutely non-universal,
simple, growing, lineral rehash, key and value retaining hashtable with open
read/write access to table entries.
@leok7v
leok7v / build-apk.bash
Last active August 11, 2024 03:29
Single file bash script to build Android Hello World apk (target API=21) on osx
#!/bin/bash
# https://medium.com/@authmane512/how-to-build-an-apk-from-command-line-without-ide-7260e1e22676
# dependencies:
# https://installvirtual.com/install-openjdk-8-on-mac-using-brew-adoptopenjdk/
# brew updata
# brew tap AdoptOpenJDK/openjdk
# brew cask install adoptopenjdk8
# https://developer.android.com/ndk/downloads
# https://dl.google.com/android/repository/sdk-tools-darwin-4333796.zip
# and use:
@leok7v
leok7v / README.md
Last active May 26, 2020 01:41
dcode4 - Delta Hex Coding ('Compression')

Delta Hex Coding ('Compression')

TLDR

Here's how to compress greyscale image bytes using dcode4:

    static void foo(byte* data, int bytes) {
        byte cenoded[bytes * 2];
        int k = encode4(data, bytes, coded, sizeof(encoded));    
        byte decoded[bytes];
 int n = decode4(encoded, k, decoded, sizeof(decoded));
@leok7v
leok7v / vigil.h
Created September 27, 2021 01:13
better assert() with optional format and varadic args
#pragma once /* Copyright (c) Dmitry "Leo" Kuznetsov 2021 see LICENSE for details */
#include <assert.h> // to prevent further redefinition of assert
// better assert(0 <= i && i < n, "index i=%d out of [0..%d( range", i, n);
// also available as 'swear' and 'implore' instead of assert()
// see: https://github.com/munificent/vigil
#undef assert
#if defined(_DEBUG) || defined(DEBUG) || defined(ASSERT_IN_NDEBUG)
@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:
@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;