Skip to content

Instantly share code, notes, and snippets.

View cloudwu's full-sized avatar

云风 cloudwu

View GitHub Profile
@cloudwu
cloudwu / handle.c
Last active November 14, 2024 04:21
Unique handle
#include "handle.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define HANDLE_USED 0x80000000
size_t
handle_size(int bits) {
int n = 1 << bits;
@cloudwu
cloudwu / cachefunc.lua
Last active July 30, 2024 06:35
cache function result with key
-- https://weibo.com/2050899237/OpPODpfBK
local function cache_func(func, cap)
cap = cap or 128
local current_cache = {}
local cache_meta = { __index = current_cache }
local func_meta = { __index = nil }
local prev_meta = { __index = nil }
local n = 0
@cloudwu
cloudwu / build.c
Last active July 24, 2024 11:00
scene
#include "scene.h"
#include <assert.h>
struct build_context {
int shift;
int x;
int y;
int id;
slot_t *grid;
};
@cloudwu
cloudwu / print_r.lua
Created May 11, 2024 11:17
A function in Lua similar to PHP's print_r
local function keys(o)
local len = #o
local skeys = {}
local ukeys = {}
local n = 1
for k,v in pairs(o) do
local nk = math.tointeger(k)
if nk == nil or nk <= 0 or nk > len then
local sk = tostring(k)
if type(k) == "string" then
@cloudwu
cloudwu / rainwater.c
Last active May 26, 2024 13:15
Trapping rain water
#include <stdio.h>
static void
minmax(int height[], int n, int *min, int *max) {
int i;
*min = *max = height[0];
for (i=1;i<n;i++) {
if (height[i] < *min)
*min = height[i];
else if (height[i] > *max)
@cloudwu
cloudwu / clog.c
Created February 5, 2024 04:49
concurrence log
#include "clog.h"
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#define LOGMETA_BUFFER 4096
#define LOGDATA_BUFFER (64 * 1024)
// [..........................]
// ^
// interface
struct object {
object * create();
void release();
int getState();
};
// implementation
@cloudwu
cloudwu / sortnumber.c
Created August 1, 2023 05:38
sort telephone number
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#define N (100000063/64)
struct bitset {
uint64_t bits[N];
@cloudwu
cloudwu / entityid2.c
Created June 21, 2023 06:15
Compress monotonic 48bits id array, version 2
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define CHUNK_N 64
struct eid_chunk {
uint64_t id[2];
@cloudwu
cloudwu / entityid.c
Created June 21, 2023 02:16
Compress monotonic 48bits id array
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
// .size is size of sec[]
// .size * 8 >= .section_n * 8 + .n