Skip to content

Instantly share code, notes, and snippets.

View rmartinho's full-sized avatar

R. Martinho Fernandes rmartinho

View GitHub Profile
@rmartinho
rmartinho / expand.h++
Last active April 3, 2024 06:32
Some lousy simple logger
// Internal Combustion Engine
//
// Written in 2012 by Martinho Fernandes <martinho.fernandes@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
@rmartinho
rmartinho / 01broken_vector.cpp
Last active December 15, 2015 07:09
Never pass vectors by reference. Wait, never pass ints by reference. Better, never pass anything by reference.
struct Gadget
{
std::vector<Widget> w;
};
void thread1()
{
Gadget g;
launch_async(unsafe(g.w));
@rmartinho
rmartinho / flavoured_strings.cpp
Created March 23, 2013 13:50
Using char_traits to get strong string aliases.
#include <string>
#include <iostream>
namespace dessert {
template <typename Tag>
struct not_quite_the_same_traits : std::char_traits<char> {};
template <typename Tag>
using strong_string_alias = std::basic_string<char, not_quite_the_same_traits<Tag>>;
using vanilla_string = std::string;
@rmartinho
rmartinho / future.c++
Created March 24, 2013 11:42
Sample implementation of std::future
#include <wheels/concurrency/locker_box.h++>
#include <chrono>
#include <exception>
#include <future>
#include <memory>
#include <mutex>
#include <type_traits>
#include <utility>
$ locale
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
@rmartinho
rmartinho / fast.c++
Last active December 16, 2015 02:39
“I want it as fast as possible”
int main(){}
@rmartinho
rmartinho / stack_alloc.h++
Created April 17, 2013 15:42
Just a sample of what kind of magic I think could make a C++ alloca().
// Usage:
// int f(int n) {
// auto buf = stack_alloc<int>(n);
// std::iota(buf.begin(), buf.end());
// return std::sum(buf.begin(), buf.end());
// }
// allocation function
template <typename T>
stack_array_ref<T> stack_alloc(size_t size) __magic_works_like_a_macro_to_use_the_right_frame__;
@rmartinho
rmartinho / bit_ops.cpp
Created April 24, 2013 22:44 — forked from bananu7/bit_ops.cpp
Bit twiddling tools. I got tired of primitive stuff like & and |
template <int Size>
struct uint_least : uint_least<Size+1> {};
template <>
struct uint_least<8> : identity<u8> {};
template <>
struct uint_least<16> : identity<u16> {};
template <>
struct uint_least<32> : identity<u32> {};
template <>
struct uint_least<64> : identity<u64> {};
BOOST_DEFINE_MATH_CONSTANT(half, 5.000000000000000000000000000000000000e-01, "5.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-01")
@rmartinho
rmartinho / gist:9485770
Last active April 9, 2019 01:19
Program that swaps the values of variables x and y, with the usual algorithm that uses a temporary variable in a weird nonexistent language.
temporary_ACC creation_V.
temporary_DAT assignment_V x_ACC.
y_ACC x_DAT assignment_V.
assignment_V temporary_ACC y_DAT.