Skip to content

Instantly share code, notes, and snippets.

@sjolsen
sjolsen / gist:5817795
Last active December 18, 2015 17:19
Lisp in C++ templates: proof-of-concept
// NUM
template <typename T, T Val>
struct _NUM_IMPL
{
static const T value = Val;
};
#define NUM(val) _NUM_IMPL <decltype (val), val>
@sjolsen
sjolsen / lazy.cc
Last active December 19, 2015 14:49
Simple lazy evaluator for C++
#include <functional>
#include <optional>
template <typename T>
class lazy
{
std::optional <T> result;
std::function <T ()> generator;
public:
@sjolsen
sjolsen / cudamap.cc
Last active July 3, 2021 13:43
Combining memory-mapped I/O and CUDA mapped memory
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <cuda_runtime.h>
#include <cerrno>
#include <cstring>
#include <memory>
#include <stdexcept>
@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
@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 / 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 / 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 / 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>
#include <stack>
#include <unordered_map>
#include <typeindex>
class MultiStack
{
class MultiStackBase
{
public:
virtual ~MultiStackBase () = default;
@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)