Skip to content

Instantly share code, notes, and snippets.

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

saxbophone

🏳️‍⚧️
View GitHub Profile
@saxbophone
saxbophone / floatrep.cpp
Last active July 27, 2023 12:31
Using memcpy to easily and portably get the underlying representation of an IEEE-754 float
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
std::uint32_t floatrep(float x) {
std::uint32_t n;
std::memcpy(&n, &x, sizeof(x));
return n;
@saxbophone
saxbophone / multivariant.cpp
Last active July 17, 2022 01:16
Multivariant --imagine a tagged union that can hold N number of its members at once
#include <cassert>
#include <cstddef>
#include <initializer_list>
#include <tuple>
#include <variant>
template <std::size_t ORDER, typename... Ts>
class multivariant {
std::variant<Ts...> members[ORDER];
public:
@saxbophone
saxbophone / union_watcher.cpp
Created June 11, 2022 21:50
Automatically tracks which member of the union was last set
template <typename T, typename E, E SYMBOL>
class Proxy {
public:
Proxy(T& value, E& which) : ref(value), which(which) {}
Proxy& operator=(const T& value) {
ref = value;
which = SYMBOL;
}
operator T() { return ref; }
protected:
@saxbophone
saxbophone / span_babysitter.cpp
Last active June 11, 2022 18:35
Using std::span<> to "babysit" a heap-allocated array
#include <cstddef>
#include <span>
std::span<int> allocate(std::size_t size) {
int* storage = new int[size];
return std::span<int>{storage, size};
}
void deallocate(std::span<int>& items) {
@saxbophone
saxbophone / lcb.py
Last active May 22, 2022 15:22
Least common base function --will probably infinite-loop for inputs without an ideal answer
from itertools import combinations
from math import log, ceil
def common_bases(x: int, y: int) -> int:
"""
Returns a list of all the bases common to x and y
That is to say, all bases n such that n can be raised by an integer power to
produce x or y
The list returned is sorted in ascending order, and may be empty if there
@saxbophone
saxbophone / constexpr_stack_list.cpp
Last active May 16, 2022 21:26
A constexpr stack-like list to prove dynamic things can be done at compile-time as long as it's all cleaned up before returning from the function
class List {
public:
constexpr ~List() {
if (next != nullptr) {
delete next;
next = nullptr;
}
}
constexpr void push(int n) {
get_end().next = new List;
@saxbophone
saxbophone / negative_literal.cpp
Created May 10, 2022 16:59
Testing with negative literals, which technically don't actually exist
#include <cstdlib>
#include <iostream>
signed operator "" _signed(const char* literal) {
std::cout << "actual literal value: " << literal << std::endl;
return std::strtol(literal, nullptr, 0);
}
int main() {
@saxbophone
saxbophone / repetend.cpp
Last active May 4, 2022 19:29
Display repeating decimals with overbar
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
struct RepeatingDecimal {
std::vector<bool> integer;
std::vector<bool> decimal;
@saxbophone
saxbophone / variadic_xor.cpp
Last active April 19, 2022 06:08
Variadic xor template function --takes any types convertible to bool
#include <cstddef>
namespace {
/*
* Helper function, variadic xor
* It works by counting the number of true values and returning true only
* if this number is 1.
*/
template <typename T>
@saxbophone
saxbophone / ContainerAdaptor.cpp
Last active April 8, 2022 17:00
Wrapper type around vector that increases the size of the vector on attempts to access out-of-bounds
#include <cstddef>
#include <vector>
template <typename T>
class ContainerAdaptor {
public:
constexpr ContainerAdaptor(std::vector<T>& container) : _container(container) {}
// no const version of operator[] is provided, because we always want to resize the vector on out-of-bounds