Skip to content

Instantly share code, notes, and snippets.

View NolanDeveloper's full-sized avatar
🇺🇦
Support Ukraine

Denis M. NolanDeveloper

🇺🇦
Support Ukraine
View GitHub Profile
@NolanDeveloper
NolanDeveloper / timeout.c
Last active March 8, 2024 19:13
Small demo utility functionally similar to timeout(1). It shows how to start child process, pass execv() error to the parent and how to wait child process with timeout.
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@NolanDeveloper
NolanDeveloper / map_benchmark.cpp
Last active December 20, 2023 00:04
Comparison of performance between std::map and std::unordered_map
/* My result:
* $ c++ --std=c++20 -O3 map_benchmark.cpp 2>&1 && ./a.out
* tiny map, a lot of queries
* omap) hits: 27271969, setup: 0ms, query: 916ms
* umap) hits: 27271969, setup: 0ms, query: 637ms
* small map, a lot of queries
* omap) hits: 4004139, setup: 0ms, query: 378ms
* umap) hits: 4004139, setup: 0ms, query: 167ms
* big map, a few queries
* omap) hits: 63229, setup: 577ms, query: 60ms
@NolanDeveloper
NolanDeveloper / segment_tree.h
Last active March 1, 2021 20:36
Segment tree in pure c
#include <stdlib.h>
#include <stdio.h>
#ifndef PREFIX
#define PREFIX st_
#endif
#ifndef TYPE
#error "Define TYPE for which segment tree should be created. You may also want "
"to define PREFIX for function names which defaults to 'st_'"
@NolanDeveloper
NolanDeveloper / project-euler-utils.hs
Last active July 24, 2019 20:48
Project euler utility functinos
-- Convert number to list of digits
tods n = if n < 10 then [n] else reverse $ unfoldr (\i -> if i == 0 then Nothing else Just (i `mod` 10, i `div` 10)) n
-- Convert list of digits to number
ton = foldl' (\a b -> a * 10 + b) 0
-- Make list of numbers from digits
numbers ns = concat $ iterate (\xs -> concatMap (\n -> map (\xs' -> n : xs') xs) ns) (map (\c -> [c]) ns)
-- Primality test
@NolanDeveloper
NolanDeveloper / haskell-token.re
Last active July 17, 2019 18:08
Regular expression to match haskell lexemes. Try it out: https://regex101.com/!
\G((@)|(\\)|(case\b)|(class\b)|(,)|(data\b)|(default\b)|(deriving\b)|(do\b)|(else\b)|(foreign\b)|(`)|(if\b)|(import\b)|(infixl\b)|(infixr\b)|(infix\b)|(instance\b)|(in\b)|(<-)|((?&NCOMMENT))|(\{)|(\[)|(\()|(let\b)|(module\b)|(newtype\b)|(of\b)|(->)|(\})|(\])|(=>)|(=)|(\))|(;)|(then\b)|(~)|(::)|(\.\.)|(type\b)|(_)|(\|)|(where\b)|((?&WHITECHAR))|((?&CHAR))|((?&STRING))|((?&VARID))|((?&CONID))|((?&COMMENT))|((?&VARSYM))|((?&CONSYM))|(:)|((?&FLOAT))|((?&INTEGER))|$)
(?(DEFINE)
(?<UNI_SMALL>\p{Ll})
(?<ASC_SMALL>[a-z])
(?<SMALL>((?&ASC_SMALL)|(?&UNI_SMALL)|_))
(?<UNI_LARGE>\p{Lu})
(?<ASC_LARGE>[A-Z])
(?<LARGE>((?&ASC_LARGE)|(?&UNI_LARGE)))
(?<UNI_DIGIT>\p{Nd})
(?<ASC_DIGIT>[0-9])
@NolanDeveloper
NolanDeveloper / lambda.c
Created February 17, 2019 21:02
Parser of lambda terms
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_NAME_LEN 128
#define MAX_FILE_LEN 1024
@NolanDeveloper
NolanDeveloper / oint.c
Last active February 7, 2019 13:38
Implementation of observer pattern written in c
#if 0
/* Usage: */
int avg(int a, int b) { return (a + b) / 2; }
struct OInt * a;
struct OInt * b;
struct OInt * c;
void show_updates(void * extra) {
struct OInt * oint;