Skip to content

Instantly share code, notes, and snippets.

@bnm3k
bnm3k / join_strs.c
Last active April 23, 2020 08:14
Procedure demonstrating joining of strings in C using snprintf
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Takes an array of c-strings and joins them into a single string placed in buf
* If the size of buf, i.e. buf_len, suffices, join_strs returns a non_negative
* integer that's equal to the total chars written to the buf, minus the
* null terminating character '\0'. The caller can also supply a sep argument
@bnm3k
bnm3k / khash_strs.c
Created April 25, 2020 14:57
A quick demonstration on using khash to store string values allocated on the heap, and eventually freeing them after use
#include "khash.h"
#include <stdbool.h>
#include <stdio.h>
void gen_rand_string(char *buf, size_t buf_len, bool should_randomize_str_len) {
static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
size_t str_len = buf_len - 1;
if (should_randomize_str_len)
str_len = (rand() % str_len) + 1;
@bnm3k
bnm3k / google_btree_data_race_detect.go
Last active July 21, 2020 14:16
Single writer, multiple readers google/btree data race
package main
import (
"math/rand"
"sync"
"time"
"github.com/google/btree"
)
@bnm3k
bnm3k / promises.go
Last active July 3, 2020 12:20
JS-style promises in Golang
package promises
import (
"fmt"
"time"
)
/*
Credits to github.com/Workiva/go-datastructures/blob/master/futures/futures.go
whose interface/API and tests for Futures I generously borrowed, but I restricted my version
@bnm3k
bnm3k / mqtt_generate_topics.go
Created July 21, 2020 11:27
A means of generating all the possible combination of MQTT wildcards and token arrangements for a given topic while minimizing allocations as much as possible. The topic should not contain any wildcard to begin with plus as per the MQTT specification, it should have at least 1 character
package main
import (
"fmt"
"strings"
)
type node struct {
val string
isIntermediary bool
@bnm3k
bnm3k / mqtt_generate_topics_V2.go
Created July 21, 2020 14:03
An even more space-efficient means of generating all the possible combination of MQTT wildcards and token arrangements for a given. Uses structural sharing of nodes wherever possible. Compare with first version
package main
import (
"fmt"
"strings"
)
type node struct {
val string
exact *node
@bnm3k
bnm3k / mqtt_generate_topics_V3.go
Created July 21, 2020 14:14
Most space-efficient MQTT topic generation. This one skips the tree building step entirely and goes straight to emitting. Compare with V1 and V2 gists
package main
import (
"fmt"
"strings"
)
func generateTopicStrings(tokens []string) (generatedTopics []string) {
// the expected no. of generated topics. See either of the previous
// gists for explanation as to why this formula is used
@bnm3k
bnm3k / radix.go
Created June 28, 2021 23:53
radix sort adopted from questdb
// RadixSort ...
// credits QuestDB impl for radix sort (7104a57)
// core/src/main/c/share/ooo.cpp
func RadixSort(array []uint64) {
if len(array) <= 100 {
insertionSort(array)
return
}
// counts
type rscounts struct {
@bnm3k
bnm3k / ex3.sql
Created January 9, 2022 20:01
SQL Recursive queries: solution for exercise 3 (https://habr.com/en/company/postgrespro/blog/490228/)
with recursive p(hops, flights, last_stop, last_arrival, total_duration, closest, found) as (
select *
from (
select
array[departure_airport, arrival_airport] as hops,
array[flight_no] as flights,
arrival_airport as last_stop,
scheduled_arrival as last_arrival,
scheduled_arrival - scheduled_departure as total_duration,
rank() over(
@bnm3k
bnm3k / ts_indexing.md
Last active January 11, 2022 14:24
Postgres indexing for overlaps operator doesn't work