Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Saxby saxbophone

View GitHub Profile
@saxbophone
saxbophone / union_watcher.cpp
Created Jun 11, 2022
Automatically tracks which member of the union was last set
View union_watcher.cpp
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 Jun 11, 2022
Using std::span<> to "babysit" a heap-allocated array
View span_babysitter.cpp
#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
Least common base function --will probably infinite-loop for inputs without an ideal answer
View lcb.py
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
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
View constexpr_stack_list.cpp
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
Testing with negative literals, which technically don't actually exist
View negative_literal.cpp
#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
Display repeating decimals with overbar
View repetend.cpp
#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 Apr 19, 2022
Variadic xor template function --takes any types convertible to bool
View variadic_xor.cpp
#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 Apr 8, 2022
Wrapper type around vector that increases the size of the vector on attempts to access out-of-bounds
View ContainerAdaptor.cpp
#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
@saxbophone
saxbophone / constexpr_vector.cpp
Last active Apr 7, 2022
Demo of constexpr vector usage and erroneous usage in C++20
View constexpr_vector.cpp
// https://godbolt.org/z/vsacWP36z
// Note: Only works with x86_64 GCC (trunk) on Godbolt.org --must have a C++ stdlib that supports constexpr vector
#include <cstddef>
#include <vector>
class Storage {
private:
std::vector<int> foo;
public:
@saxbophone
saxbophone / twelve_days.cpp
Created Dec 16, 2021
On the twelfth day of Christmas, my true love sent to me: A program written in CPP!
View twelve_days.cpp
#include <array>
#include <iostream>
#include <string>
#include <cstddef>
std::array<std::string, 12> ORDINAL_DAYS = {
"first",
"second",
"third",