Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RandyGaul/ckit.h

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ckit.h

A tiny, single-header kit of high-performance C essentials.

  • ~400 LoC
  • Dynamic arrays (stretchy buffers)
  • Map (hashtable): uint64_t → uint64_t
  • String interning (stable pointer per unique string)

Usage

A small program that exercises all three parts:

#define CKIT_IMPLEMENTATION
#include "ckit.h"

int main(void)
{
	// Dynamic array.
	int* a = NULL;
	for (int i = 0; i < 10; ++i) {
		apush(a, i);
	}
	for (int i = 0; i < 10; ++i) {
		printf("%d\n", a[i]);
	}
	printf("len=%d cap=%d\n", acount(a), acap(a));
	afree(a);

	// Map.
	Map m = (Map){ 0 };
	for (int i = 0; i < 10; ++i)
		map_add(m, i, i * 10);
	for (int i = 0; i < 10; ++i)
		printf("k : %d, v %d\n", (int)i, (int)map_get(m, i));
	map_free(m);

	// Sort keys as strings.
	m = (Map){ 0 };
	const char* keys[] = { "Banana", "apple", "carrot", "Apple", "banana" };

	for (int i = 0; i < sizeof(keys)/sizeof(keys[0]); ++i) {
		const char* s = sintern(keys[i]);
		map_add(m, s, i);
	}

	// Sort case sensitive.
	map_ssort(m, 0);

	for (int i = 0; i < m.size; ++i) {
		printf("key %s : val %d\n", (const char*)m.keys[i], (int)m.vals[i]);
	}

	// String interning.
	const char *sa = sintern("hello");
	const char *sb = sintern("he" "llo");
	assert(sa == sb);

	return 0;
}

See example.c for demonstration and unit tests.

License

The code is in public domain. See LICENSE for more info.

About

Tiny kit of essentials for C projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages