Skip to content

Instantly share code, notes, and snippets.

View saxbophone's full-sized avatar
🏳️‍⚧️

saxbophone

🏳️‍⚧️
View GitHub Profile
@saxbophone
saxbophone / c̴̎̓u̴͌̂r̵̛̓s̸̎̽é̵̉̉d̷͑̚.cpp
Last active June 11, 2023 12:23
Some truly c̴̎̓u̴͌̂r̵̛̓s̸̎̽é̵̉̉d̷͑̚ C++ code
struct $ {
$ (*$)($*);
};
$ d($* x) {
return *x;
}
int main() {
$ d = {::d};
@saxbophone
saxbophone / generic-multidim.c++
Created May 28, 2023 19:08
Dimension-generic multidimensional array in C++23
#include <cstddef>
#include <concepts>
// version with static bounds on the dimensions
template <typename T, std::size_t... DIMS>
struct vec {
template <std::convertible_to<std::size_t>... Is>
requires (sizeof...(Is) == sizeof...(DIMS))
T operator[](Is... indices) { return {}; }
};
@saxbophone
saxbophone / thread-safe-memoiser.c++
Last active April 7, 2023 14:46
Thread-safe function memoisation in C++
#include <functional>
#include <mutex>
#include <shared_mutex>
#include <tuple>
#include <unordered_map>
#include <boost/container_hash/hash.hpp>
template <class>
@saxbophone
saxbophone / float_test.c++
Last active May 4, 2024 17:39
Generate and verify safe ranges of integers for to-and-from float-int conversion
/*
* Written by Joshua Saxby ("saxbophone") 2023-03-14 -- saxbophone.com
*
* This demonstration program for how to derive max/min "safe" integers for
* conversion to and from floating point is hereby released into the public domain.
*/
#include <limits> // numeric_limits
#include <type_traits> // make_signed
@saxbophone
saxbophone / xflow_detect.c++
Created February 3, 2023 15:45
Overflow/underflow detection without having to operate twice
#include <iostream>
#include <climits>
unsigned detect_overflow(unsigned x) {
auto old_x = x++;
if (old_x > x) {
std::cerr << "Overflow occurred: " << x - 1 << " -> " << x << std::endl;
}
return x;
}
@saxbophone
saxbophone / sfml-deps.sh
Created January 31, 2023 21:23
Installs for SFML on linux
sudo apt-get install libx11-dev libxrandr-dev libudev-dev libfreetype-dev libopengl-dev libflac-dev libogg-dev libvorbis-dev libvorbisenc2 libvorbisfile3 libopenal-dev
@saxbophone
saxbophone / compress_ratios.sh
Created November 24, 2022 08:46
Test XZ Compression ratios
echo "b" > file.txt;
while [[ true ]]; do
xz -9e -T 0 -k -v -c file.txt > /dev/null;
cat file.txt file.txt > new.txt;
mv new.txt file.txt;
done
@saxbophone
saxbophone / gitignore.sh
Created November 17, 2022 16:26
Generate .gitignore files for given languages straight from the comfort of the UNIX shell
# Created by Joshua Saxby 2022
# This BASH shell script function is placed into the Public Domain
# There are no restrictions on its use but it is provided with ABSOLUTELY no liability, warranty, etc, etc...
# !!don't sue me for using it at your own risk...!!
# uses the new version of the former gitignore.io API to generate gitignore files for given languages
# usage: $ gitignore <lang 1> <lang 2> ... <lang n>
#
# NOTE: overwrites local .gitignore file in-place
Context global_context;
while (I.am_operational()) {
Action current_course;
// 2) A robot must obey humans
while (I.have_pending_human_commands()) {
current_course.add_human_command(global_context, I.human_commands.pop());
}
// 3) A robot should protect itself
if (I.anticipate_harm_to_self(global_context)) {
@saxbophone
saxbophone / cheap_coroutine.cpp
Created August 8, 2022 03:08
Misusing the C preprocessor and function-local static variables to get cheap coroutines but at the cost of each coroutine being a unique new type
#include <cstddef>
template <std::size_t ID>
struct coroutine_imp {
enum class stage { FIRST, SECOND, THIRD, };
int run() {
static stage s = stage::FIRST;
switch (s) {
case stage::FIRST:
s = stage::SECOND;