Skip to content

Instantly share code, notes, and snippets.

View PurityLake's full-sized avatar

Robert O'Shea PurityLake

View GitHub Profile
@PurityLake
PurityLake / uniquerptr.cpp
Last active May 9, 2023 20:12
A sample of using Empty Base Object Optimisation using a custom simplified unique_ptr implementation
#pragma once
#include <iostream>
// T1 should be a stateless class that has only one member function
// T2 can be any noexcept constructible class
template <class T1, class T2>
// We inherit from T1 to provide the functionality of T1 without
// incurring any additional memory
class SimpleCompressedPair : T1
@PurityLake
PurityLake / stack.c
Created January 27, 2021 16:04
Generic Stack in C
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
typedef struct stack(int) IntStack;
int main(int argc, char *argv[]) {
createStack(IntStack, stack, int, 5);
@PurityLake
PurityLake / CustomABS.py
Last active September 27, 2019 16:41
An implementation of an Abstract Base Class in python
def makeFun(name, *args):
exec("""def {}({}): raise NotImplementedError("'{}' is not implemented!")""".format(name, ', '.join(args), name))
return locals()[name]
class ABC(type):
def __init__(cls, name, bases, dct):
for k, v in cls.__dict__.items():
if not k.startswith("__") and callable(v):
setattr(cls, k, makeFun(k, *v.__code__.co_varnames))