Skip to content

Instantly share code, notes, and snippets.

View baioc's full-sized avatar
🌑
Does the Black Moon howl?

Gabriel B. Sant'Anna baioc

🌑
Does the Black Moon howl?
View GitHub Profile
@baioc
baioc / readtcpcap.sh
Created November 6, 2023 14:12
Extract payload data from captured TCP stream
#!/bin/sh
#
# Example usage:
# cat tcpdump.pcap | ./readtcpcap.sh 1 > stream1
#
if [ -z "$1" ]; then
echo 'You have to specify a TCP stream!'
exit 1
@baioc
baioc / main.d
Last active July 11, 2023 17:32
D vs C++ set micro-benchmarks
module main;
import core.stdc.string : strcmp;
import core.stdc.time : clock, clock_t, CLOCKS_PER_SEC;
import core.stdc.stdlib : rand, srand;
import eris.math : Accumulator;
// ^ tracks a stream of values and computes some statistics (min, max, avg, var, std) in constant space
alias stringz = char*;
@baioc
baioc / gyre_example.d
Created July 6, 2022 16:28
GyreD API example
import gyre.mnemonics;
Graph graph;
graph.initialize(MacroNode.ID(42));
scope(exit) graph.dispose();
assert(graph.exported == 0);
assert(graph.length == 0);
// imagine we're compiling something like
@baioc
baioc / hornify.scm
Created February 19, 2021 09:55
Polynomial definition and conversion to Horner form
(import (scheme base))
(define-syntax hornify
(syntax-rules ()
((_ x an) an)
((_ x a pn ...)
(+ a (* x (hornify x pn ...))))))
(define-syntax poly
(syntax-rules ()
@baioc
baioc / Combinatorial.hs
Last active December 3, 2019 21:18
Lazy Permutations in Haskell
module Combinatorial (
combine,
permutations,
matrices,
) where
-- inserts a value in a list at every possible position
combine :: t -> [t] -> [[t]]
combine x [] = [[x]]
@baioc
baioc / fibonacci.scm
Last active November 7, 2019 01:57
Recursive Fibonacci Schemes
;;; Gabriel B. Sant'Anna <baiocchi.gabriel@gmail.com>
;;; Explained in pt-br at <https://baioc.github.io/scheme/>
;; binary-recursive aka dumb fibonacci
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
@baioc
baioc / unique_ptr.hpp
Last active September 14, 2019 21:10
A simple smart pointer
#ifndef UTIL_UNIQUE_PTR_HPP
#define UTIL_UNIQUE_PTR_HPP
#include <utility>
namespace util {
template <typename T>
class unique_ptr {
public:
@baioc
baioc / chkxml.cpp
Last active October 16, 2019 22:33
XML tag-balance-checking algorithm
/*
* See the full version at <https://gitlab.com/snippets/1849449>
* and a pure C implementation at <https://gitlab.com/snippets/1837083>
*/
#include <stack>
#include <string>
bool balanced(const std::string& xml)
@baioc
baioc / atomic.asm
Last active September 30, 2022 14:56
Spin-Lock Mutex in MIPS-32
.text
# void lock(mutex)
lock:
ll $t0, 0($a0) # read the mutex (and set LLbit)
bne $t0, $zero, lock # if occupied (1), start over
addi $t1, $zero, 1
sc $t1, 0($a0) # try grabing the mutex
beq $t1, $zero, lock # if wasn't atomic (0), start over
@baioc
baioc / memoize.scm
Last active February 19, 2021 09:59
Automatic Memoization with Closures
;; make a function that caches its past results
(define (memoize proc)
(let ((cache (make-table)))
(define (delegate . args)
(let ((hit (lookup cache args)))
(or hit ; or miss
(let ((result (apply proc args)))
(insert! cache args result)
result))))
delegate))