Skip to content

Instantly share code, notes, and snippets.

@vurtun
vurtun / kth.c
Last active April 23, 2024 10:11
print K largest(or smallest) elements in an array
// https://cs.stackexchange.com/questions/68125/finding-kth-smallest-element-from-a-given-sequence-only-with-ok-memory-on-t
// https://en.wikipedia.org/wiki/Floyd%E2%80%93Rivest_algorithm
// https://core.ac.uk/download/pdf/82672439.pdf
#include <math.h>
#define heap_parent(i) (((i)-1) >> 1)
#define heap_right(i) (((i)+1) << 1)
#define heap_left(i) (heap_right(i)-1)
#define sgn(val) ((0 < val) - (val < 0))
@vurtun
vurtun / _GJK.md
Last active April 16, 2024 17:52
3D Gilbert–Johnson–Keerthi (GJK) distance algorithm

Gilbert–Johnson–Keerthi (GJK) 3D distance algorithm

The Gilbert–Johnson–Keerthi (GJK) distance algorithm is a method of determining the minimum distance between two convex sets. The algorithm's stability, speed which operates in near-constant time, and small storage footprint make it popular for realtime collision detection.

Unlike many other distance algorithms, it has no requirments on geometry data to be stored in any specific format, but instead relies solely on a support function to iteratively generate closer simplices to the correct answer using the Minkowski sum (CSO) of two convex shapes.

@vurtun
vurtun / fold.c
Last active April 15, 2024 16:41
unicode case folding
// Selection algorithm
// Partial sorting
// http://www.unicode.org/Public/UCD/latest/ucd/CaseFolding.txt
switch(rune) {
case 0x0041: return 0x0061; // LATIN CAPITAL LETTER A
case 0x0042: return 0x0062; // LATIN CAPITAL LETTER B
case 0x0043: return 0x0063; // LATIN CAPITAL LETTER C
case 0x0044: return 0x0064; // LATIN CAPITAL LETTER D
case 0x0045: return 0x0065; // LATIN CAPITAL LETTER E
@vurtun
vurtun / burg.c
Last active March 13, 2024 08:22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define szof(a) ((int)sizeof(a))
#define cntof(a) ((int)(sizeof(a) / sizeof((a)[0])))
static void
@vurtun
vurtun / utf.c
Last active December 26, 2023 11:58
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define UTF_INVALID 0xFFFD
static const char*
utf_dec(unsigned *dst, const char *p, const char *e)
{

API Design: Coroutines APIs (Janurary-2017)

I am currently dealing with a lot of libraries at work. Both third party as well as libraries written or being currently in process of being written by me. I absolutely love writing and working with libraries. Especially if they present or bring me to either a new or different approach to solve a problem. Or at least provide a different view.

Over time I noticed however that quite regulary we had to decide that we cannot use a third party library. Often it is the usual reason.

@vurtun
vurtun / _readme_quarks.md
Last active December 9, 2023 12:03
Quarks: Graphical user interface

gui

@vurtun
vurtun / aoc.c
Last active December 8, 2023 09:34
AoC 2023
#include <stdio.h>
#include <limits.h>
#define byte4_dup(c) (((c)<<24u)|((c)<<16u)|((c)<<8u)|(c))
#define byte4_cmp_lt(x,n) (((x)-~0UL/255u*(n))&~(x)&~0UL/255u*128u) // x>=0; 0<=n<=128
#define byte4_cmp_gt(x,n) (((x)+~0UL/255*(127-(n))|(x))&~0UL/255*128) // x>=0; 0<=n<=127
#ifdef _MSC_VER
static int bit_ffs32(unsigned int u) {_BitScanForward(&u, u); return (int)(u);}
static int bit_fls32(unsigned int u) {_BitScanBackward(&u, u); return 31-(int)(u);}

API Design: Builder APIs (October-2020)

Some time has past (three years!) since I last wrote about API specifically about coroutines style APIs so I thought why not write another one about a different API type I encounter relatively often. The builder API.

Now first let me take a step back and put this into 20,000 feet view on where builder APIs are located in the grant scheme. In general everything in computing is separated into input, processing and finally output. In its most basic form I am currently typing on my keyboard. All pressed keys are processed from the OS up to the browser I am writing this in and finally rendered and displayed on the screen as output. Of course this example is very user centric