Skip to content

Instantly share code, notes, and snippets.

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

saxbophone

🏳️‍⚧️
View GitHub Profile
@saxbophone
saxbophone / packed_utf32.cpp
Last active March 17, 2024 02:13
Taking advantage of the fact that Unicode only uses 21 bits to pack 3 UTF-32 code units into one 64-bit word (63 bits with one wasted/spare).
#include <string>
#pragma pack(push, 1)
struct Chunk {
char32_t first : 21;
char32_t second : 21;
char32_t third : 21;
operator std::u32string() const {
return {first, second, third};
}
@saxbophone
saxbophone / build-clang.sh
Last active February 24, 2024 11:39
my clang build command-line
time CC=gcc-10.2 CXX=g++-10.2 cmake -S ../llvm-project/llvm -B build -DCMAKE_BUILD_TYPE=Release
-DLLVM_PARALLEL_COMPILE_JOBS=4 -DLLVM_PARALLEL_LINK_JOBS=1 -DLLVM_ENABLE_RUNTIMES="" -DLLVM_ENABLE_PROJECTS=clang
-DLLVM_CCACHE_BUILD=ON -DLLVM_ENABLE_EH=ON -DLLVM_ENABLE_LTO=On -DLLVM_ENABLE_RTTI=ON -DLLVM_INCLUDE_TESTS=OFF
-DLLVM_LINK_LLVM_DYLIB=ON -DCMAKE_C_FLAGS_RELEASE="-O3 -march=native" -DCMAKE_CXX_FLAGS_RELEASE="-O3 -march=native"
-DLLVM_ENABLE_LIBXML2=OFF -DCMAKE_CXX_STANDARD=20
@saxbophone
saxbophone / autotools-to-cmake-header.c++
Created February 19, 2024 18:36
For transforming autoheader defines into CMake configure_file() defines
#include <iostream>
#include <regex>
#include <string>
int main() {
std::regex find_undefs{"(?:#undef\\s+)([_A-z][_0-9A-z]*)"};
std::cout << std::regex_replace("#undef _1FOO_BAR_BAZ1234_", find_undefs, "#cmakedefine $1 1") << std::endl;
}
@saxbophone
saxbophone / seizable.c++
Last active February 14, 2024 21:25
Seizable --a thread-safe convenience wrapper around std::shared_mutex that allows read-write lock semantics via getters and setters, as well as the ability to "seize" the protected value in order to operate on it more efficiently using reference semantics
#include <mutex>
#include <shared_mutex>
#include <type_traits>
// TOOD: reïmplement all methods non-inline so the prototype is nice and readable
template <typename T>
class Seizable {
public:
// TODO: change to std::shared_timed_mutex and add additional time-based locking methods
@saxbophone
saxbophone / convert.sh
Created February 1, 2024 22:42
Converts git refs to base-91 for brevity
git ls-remote --tags --refs --sort='v:refname' https://github.com/llvm/llvm-project.git | while read -r line; do
sha=$(echo $line | awk '{print substr($1, 1, 8)}');
ref=$(echo $line | awk '{print $2}');
b91=$(echo $sha | xxd -r -p | base91);
echo "$sha = $b91 ($ref)";
done
@saxbophone
saxbophone / lotto.c++
Created October 7, 2023 14:20
C++ Lottery Machine implementation
#include <cstddef>
#include <numeric>
#include <random>
#include <stdexcept>
#include <vector>
class LotteryMachine {
public:
LotteryMachine(std::size_t number_of_balls)
@saxbophone
saxbophone / span_babysitter.c++
Created September 16, 2023 13:31
using std::span to babysit array-new-allocated data
#include <cstddef>
#include <span>
template <typename T>
constexpr std::span<T> allocate(std::size_t size) {
return {new T[size], size};
}
@saxbophone
saxbophone / unpack_constexpr.c++
Last active September 7, 2023 21:26
Unpacking containers with variable-size at compile-time, without needing two duplicate functions to do so.
#include <cstddef>
#include <span>
constexpr std::size_t populate(std::span<int> data) {
std::size_t w = 0;
for (; w < 1000; ++w) {
// this should maybe be a bounds-check instead, but in theory
// this should only be called on containers that are exactly
@saxbophone
saxbophone / stringview.py
Last active August 3, 2023 03:33
Python StringView implementation. Supports slicing, iteration and creating sub-views of existing StringViews. No copying, only reference semantics.
class StringView:
"""
StringView implementation using minimal copying with maximum use of
reference semantics. Creating a sub-view of an existing StringView using
either object slicing or constructing one from another will reüse the same
source string object, using a reference rather than a copy.
The contents() method can similarly be used to get an iterator (Generator)
to access the view contents sequentially without putting it all in memory
at once.
A brand new string object is only created if the StringView is cast to str.
@saxbophone
saxbophone / rwlock.py
Created July 30, 2023 23:49 — forked from tylerneylon/rwlock.py
A simple read-write lock implementation in Python.
# -*- coding: utf-8 -*-
""" rwlock.py
A class to implement read-write locks on top of the standard threading
library.
This is implemented with two mutexes (threading.Lock instances) as per this
wikipedia pseudocode:
https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Using_two_mutexes