Skip to content

Instantly share code, notes, and snippets.

View foonathan's full-sized avatar

Jonathan Müller foonathan

View GitHub Profile
@foonathan
foonathan / pdfcat.fish
Created March 3, 2022 10:58
Small fish script that cats pdf files while preserving bookmarks
#!/bin/fish
#
# Copyright (C) 2022 Jonathan Müller
# SPDX-License-Identifier: BSL-1.0
#
# Usage: pdfcat input1 input2 ... inputN output
function print_usage
echo "Usage:" (basename (status -f)) "input1 input2 ... inputN output"
end
@foonathan
foonathan / visitor.cpp
Last active April 5, 2021 01:10
My take on a modern implementation of the visitor pattern. - http://foonathan.net/blog/2017/12/21/visitors.html
//=== library ===//
#include <typeinfo>
#include <type_traits>
#include <utility>
enum class visit_event
{
container_begin,
container_end,
leaf,
@foonathan
foonathan / brainfuck.cpp
Created May 20, 2017 17:07
C++ brainfuck interpreter using only `a`, `u`, `t`, and `o` characters plus symbols
/***
* __________ .__ _____ __
* \______ \____________ |__| _____/ ____\_ __ ____ | | __ ___________ _______ ______ ___________
* | | _/\_ __ \__ \ | |/ \ __\ | \_/ ___\| |/ / \____ \__ \\_ __ \/ ___// __ \_ __ \
* | | \ | | \// __ \| | | \ | | | /\ \___| < | |_> > __ \| | \/\___ \\ ___/| | \/
* |______ / |__| (____ /__|___| /__| |____/ \___ >__|_ \ | __(____ /__| /____ >\___ >__|
* \/ \/ \/ \/ \/ |__| \/ \/ \/
*/
auto _ = +!!+[]{};
auto _o_ = _-_;
@foonathan
foonathan / borrow.cpp
Last active August 24, 2023 08:42
Quick'n'dirty implementation of Rust's borrow checker for a C++Now Lightning Talk - not supposed to be used
#include <iostream>
#include "borrow_checker.hpp"
int main()
{
auto i = 42;
// borrow `i` under name `ref`
borrow_var(ref, i)
@foonathan
foonathan / allocator_storage.md
Last active February 24, 2021 00:31
Sample documentation output for Standardese prototyp

Header file allocator_storage.hpp

#define FOONATHAN_MEMORY_ALLOCATOR_STORAGE_HPP_INCLUDED 

#include <new>

#include <type_traits>

#Node

##single

Size Heap New Small Node Bpool Array Bpool (ordered) Stack
256*1 9,133 9,571 2,670 696 696 1,738 1,148 437
256*4 9,007 9,578 2,675 696 695 1,726 1,150 399
256*8 8,899 9,636 2,668 747 697 1,737 1,146 400
256*256 10,499 11,175 2,676 692 922 1,742 1,344 1,793
@foonathan
foonathan / concept.cpp
Last active February 24, 2021 00:31
Prime_number concept
#include <type_traits>
//=== basic integer stuff ==//
template <typename T>
concept bool Integer = requires(T) {typename T::value_type; T::value;};
template <Integer A, typename A::value_type B>
concept bool Equal_to = A::value == B;
template <Integer A, Integer B>
@foonathan
foonathan / gist:3aa3114284863bf3141a
Created February 24, 2016 19:33
Profiling results of memory 0.5 (formatting wrecked)
Node
single
Heap New Small Node Array Stack Boost.Pool Boost.Pool (Ordered)
256* 1: 10 11 5 5 5 2 0 1
256* 4: 10 10 4 5 5 2 0 1
256* 8: 10 10 4 5 5 2 0 1
256*256: 11 13 3 4 4 2 0 1
512* 1: 20 21 9 10 10 5 1 2
512* 4: 20 21 9 10 11 5 1 2
@foonathan
foonathan / clz.cpp
Last active April 14, 2016 09:46
A clz() implementation wrapping __builtin_clz() - see blog post http://foonathan.github.io/blog/2016/02/11/implementation-challenge-2.html
#include <climits>
#include <cstdint>
#include <type_traits>
struct clzll_tag {};
struct clzl_tag : clzll_tag {};
struct clz_tag : clzl_tag {};
template <typename T, typename = typename std::enable_if<sizeof(T) <= sizeof(unsigned int)>::type>
unsigned clz_impl(clz_tag, T x)