Skip to content

Instantly share code, notes, and snippets.

@sjolsen
sjolsen / BNat2.agda
Created January 30, 2015 03:40
Agda Binary
{-# OPTIONS --sized-types #-}
module BNat2 where
import Level
open import Data.Nat
open import Data.Sum hiding (map)
open import Data.Product
open import Function
open import Data.Empty
open import Relation.Nullary
open import Relation.Nullary.Decidable hiding (map)
@sjolsen
sjolsen / format.cc
Last active August 29, 2015 14:10
Parsing CL:FORMAT-like control strings at compile time
#include <iostream>
#include <type_traits>
#include <utility>
#include "odds-and-ends/dynamic-bind/dynamic_bind.hh"
extern dynamic_variable <std::ostream&> _standard_output_;
dynamic_variable <std::ostream&> _standard_output_ (std::cout);
@sjolsen
sjolsen / dynamic_bind.cc
Created September 24, 2014 23:47
Dynamic scope in C++
#include <utility>
#include <cassert>
template <typename T>
class dynamic_binding;
template <typename T>
class dynamic_variable
{
T _initial_object;
@sjolsen
sjolsen / gist:9778748
Created March 26, 2014 08:16
A first stab at parsing wayland.xml
(defpackage parse
(:use :common-lisp)
(:export :parse-protocol
:bad-form
:bad-header
:bad-type
:form
:name))
(in-package :parse)
#include <stack>
#include <unordered_map>
#include <typeindex>
class MultiStack
{
class MultiStackBase
{
public:
virtual ~MultiStackBase () = default;
@sjolsen
sjolsen / xor_testcase.cc
Created January 10, 2014 21:39
Program to generate input for xor_key.cc
#include <random>
using integer = int;
using distribution = std::uniform_int_distribution <integer>;
std::mt19937 engine {};
#include <iostream>
@sjolsen
sjolsen / xor_key.cc
Last active January 2, 2016 19:19
A solution to https://www.hackerrank.com/challenges/xor-key, with input validation thrown in for kicks.
#include <vector>
#include <iterator>
#ifndef NDEBUG
static int nth_input = 0;
#endif
using integer = int;
using key_type = std::vector <integer>;
using key_iter = typename key_type::const_iterator;
@sjolsen
sjolsen / uniform_int.h
Last active December 31, 2015 08:19
Basic uniform int distribution for C
#ifndef UNIFORM_INT_H
#define UNIFORM_INT_H
#ifndef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE 200112L
#elif _POSIX_C_SOURCE < 200112L
# undef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE 200112L
#endif
@sjolsen
sjolsen / binary_literal.cc
Last active December 23, 2015 00:49
Binary Literals (C++11)
#include <climits>
template <char c>
inline constexpr
unsigned long long int binary_digit ()
{
static_assert (c == '0' || c == '1', "bad bit in binary literal");
return (c - '0');
}
@sjolsen
sjolsen / unrestricted_union.hh
Last active May 19, 2023 15:28
Variadic, minimally type-checked unrestricted union in C++
/* Copyright 2021 Stuart Olsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in