Skip to content

Instantly share code, notes, and snippets.

@Masstronaut
Masstronaut / parallel_task_resolution.cpp
Created January 15, 2024 20:21
A c++ implementation for resolving a non-cyclical graph of parallel task execution
#include <utility>
#include <tuple>
#include <future>
#include <mutex>
#include <thread>
#include <iostream>
// This code is used to unpack tuple elements into function arguments.
// apply(f, tuple<int,bool,char>) would be unpacked as f(int,bool,char)
// This implementation has been specialized for tuples of futures.
@Masstronaut
Masstronaut / hash_ptr.h
Created September 29, 2017 00:44
trivial implementation of a basic hash pointer
template<typename T, typename Hasher = ::std::hash<T>>
class hash_ptr {
public:
using value_type = T;
using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = const value_type &;
using pointer = T*;
using hash_type = typename Hasher::result_type;
//using hash_type = typename decltype( std::declval<Hasher>( )( std::declval<value_type>( ) ) );
@Masstronaut
Masstronaut / hash.hpp
Last active March 23, 2017 07:48
Compile time implementation of the MurmurHash2A 64bit version for 64bit architecture.
// All content copyright (C) Allan Deutsch 2017. All rights reserved.
#pragma once
#include <cstdint>
// According to the top post here: http://softwareengineering.stackexchange.com/a/145633
// murmur2 is the best hash for speed and random distribution.
// This is a constexpr implementation of Murmur2A, a hash algorithm designed by Austin Appleby.
// This version is optimized for 64bit architectures.
constexpr uint64_t murmur2a_64_ct( const char *str, unsigned long size, uint64_t seed ) {
constexpr uint64_t prime{ 0xc6a4a7935bd1e995ull };
@Masstronaut
Masstronaut / task.hpp
Created February 26, 2017 01:39
Enables programmers to break a large, complex task into smaller dependencies which can be resolved and executed concurrently.
#include <utility>
#include <tuple>
#include <future>
#include <mutex>
#include <thread>
#include <iostream>
// This code is used to unpack tuple elements into function arguments.
// apply(f, tuple<int,bool,char>) would be unpacked as f(int,bool,char)
// This implementation has been specialized for tuples of futures.
#include <iostream>
template<typename Ret, typename... Args>
constexpr int arg_count(Ret(*)(Args...)){
return sizeof...(Args);
}
void foo(int, float, double, char){}
int foo2(float, int, char*){return 0;}
@Masstronaut
Masstronaut / generic_first_class_function_composition
Created May 12, 2016 22:49
Generic first-class function composition in c++
// This is my implementation submission for this page: http://www.programming-idioms.org/idiom/36/first-class-function-generic-composition
template<typename Pred1, typename Pred2>
auto compose(Pred1 f, Pred2 g){
return [f,g](auto x){ return g(f(x)); };
}
@Masstronaut
Masstronaut / RVO_Test.cpp
Last active February 29, 2016 07:37
Test code for determining the behavior of Return Value Optimization in various situations
// This file is to test the behavior of various compilers' return value optimizations (RVO) in different scenarios
#include <iostream>
#include <cstdlib> // std::rand
#include <array>
#include <utility> // std::move
// This class is small enough to fit in a register, and should be a simple target for return value optimization
class SmallObject {
public:
static unsigned instance_count;
@Masstronaut
Masstronaut / DefineSFINAECheck.hpp
Created October 21, 2015 16:01
Easy to use macro for creating SFINAE checks.
#include <type_traits>
// Create our interface
// usage: T1 is the typename, x1 is the variable name.
#define DEFINE_SFINAE_CHECK( NAME, EXPR ) \
template <typename U1> \
struct NAME \
{ \
using yes = char; \
using no = char[2]; \
template <typename T1>\
@Masstronaut
Masstronaut / VariadicLambdaOverloads.hpp
Last active January 25, 2016 03:26
Variadic "overloading" of lambdas.
#include <iostream>
#include <string>
#include <ostream>
template<typename... Predicates>
struct overload;
template<typename Pred, typename... Predicates>
struct overload<Pred, Predicates...> : Pred, overload<Predicates...>
{
@Masstronaut
Masstronaut / lambdagenerators.hpp
Created October 20, 2015 13:56
Using lambdas to generate lambdas.
#include <iostream>
#include <cassert>
#include <cmath>
int main()
{
auto add = [](auto x) { return [x](auto y) { return x + y; }; };
auto pyth = [](auto x) { return [x](auto y) { return std::sqrt(x * x + y * y); }; };
// are lambdas really fucking dumb?!?!
auto add4 = add(4);