Skip to content

Instantly share code, notes, and snippets.

View newlawrence's full-sized avatar

Alberto Lorenzo Márquez newlawrence

View GitHub Profile
@newlawrence
newlawrence / ctstr.cpp
Created April 15, 2022 10:02
Portable C++17 compile time string implementation
#include <ostream>
#include <string>
#include <string_view>
#include <cstdint>
#define CTSTR_DEFINE_LITERAL_OPERATOR(NAMESPACE, CHARSET) \
namespace NAMESPACE { \
using ctstr_t = basic_ctstr_t<charset::CHARSET>; \
namespace literals { \
@newlawrence
newlawrence / isa.cpp
Last active May 23, 2020 17:52
Compile Time International Standard Atmosphere
#include <array>
#include <limits>
#include <type_traits>
#include <cmath>
namespace isa {
template<typename Params, int Offset=0>
class ISA : public Params {
@newlawrence
newlawrence / enums.cpp
Last active April 15, 2022 09:14
Enhanced enums
#define COUNT_( \
X64, X63, X62, X61, X60, X59, X58, X57, X56, X55, X54, \
X53, X52, X51, X50, X49, X48, X47, X46, X45, X44, X43, \
X42, X41, X40, X39, X38, X37, X36, X35, X34, X33, X32, \
X31, X30, X29, X28, X27, X26, X25, X24, X23, X22, X21, \
X20, X19, X18, X17, X16, X15, X14, X13, X12, X11, X10, \
X9, X8, X7, X6, X5, X4, X3, X2, X1, N, ... \
) N
#define COUNT(...) COUNT_( __VA_ARGS__, \
64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, \
@newlawrence
newlawrence / contiguous.cpp
Created November 20, 2018 19:22
Contiguous memory resource
#include <memory_resource>
template<typename T>
class contiguous_memory_resource : public std::pmr::memory_resource {
std::pmr::memory_resource* _upstream;
std::size_t _size;
void* _buffer;
void* _offset;
void* do_allocate(size_t bytes, size_t alignment) override {
@newlawrence
newlawrence / reduceop.cpp
Last active July 17, 2018 12:19
C++17 generator of Reduce-able Operators
#include <functional>
#include <tuple>
#include <type_traits>
template<typename T, typename U, typename V>
class ReduceOperator {
T _init;
U _apply;
V _chain;
@newlawrence
newlawrence / join.cpp
Last active July 12, 2018 21:57
Performant C++17 implementation of Python's join function
#include <string_view>
#include <string>
#include <tuple>
class Joint {
std::string _j;
auto& _concat(std::string& s, std::string_view sv) { return s += sv; }
template<typename... Args>
@newlawrence
newlawrence / .vimrc
Last active January 3, 2018 10:57
My vimrc settings
syntax on
filetype indent off
set backspace=indent,eol,start
set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
@newlawrence
newlawrence / macos_tricks.sh
Last active January 3, 2018 13:02
Some macOS tricks - just add to .bash_profile
# Customize prompt
export PROMPT_COMMAND="BRANCH=\
\"\$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/')\""
export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]"\
"\h:\[\033[33;1m\]\w\[\033[m\]\[\033[0;36m\]\${BRANCH}\[\033[0m\]\$ "
# Customize command line
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls="ls -GFh"
@newlawrence
newlawrence / vagenc.c
Last active January 26, 2024 16:40
A "not too much elegant" (and the only) way to get a templated, variadic and type-safe function in pure C
#include <stdio.h>
#define COUNT(...) (sizeof((int[]){ __VA_ARGS__ })/sizeof(int))
#define __eval(x0, x1, x2, x3, ...) _Generic((x0), \
void*: _Generic((x1), int: _evali, double: _evald), \
int: _evali, \
double: _evald \
)( \
COUNT(__VA_ARGS__) - _Generic((x0), void *: 1, default: 0), \
@newlawrence
newlawrence / wallis.hs
Last active February 19, 2017 23:32
Computes the product of the first n elements of the Wallis succession
import Data.Ratio
import Text.Read
import System.Environment
wallis_succession :: [Rational]
wallis_succession = merge odds evens
where
odds = [n % (n - 1) | n <- [2, 4..]]
evens = [n % (n + 1) | n <- [2, 4..]]
merge (x:xs) (y:ys) = x : y : merge xs ys