Skip to content

Instantly share code, notes, and snippets.

Avatar

Derrick Lyndon Pallas pallas

View GitHub Profile
@pallas
pallas / intrusive_queue.hh
Last active Aug 29, 2015
C++ intrusive queue
View intrusive_queue.hh
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef INTRUSIVE_QUEUE_H
#define INTRUSIVE_QUEUE_H
#include <cassert>
#include <cstddef>
@pallas
pallas / countable.hh
Last active Aug 29, 2015
C++ reference counter base, with hook
View countable.hh
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef COUNTABLE_H
#define COUNTABLE_H
#include <cassert>
template <class T>
@pallas
pallas / intrusive_order.hh
Last active Aug 29, 2015
C++ intrusive ordered queue + fast tail inserts
View intrusive_order.hh
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef INTRUSIVE_ORDER_H
#define INTRUSIVE_ORDER_H
#include <cassert>
#include <cstddef>
#include <algorithm>
@pallas
pallas / magic_mask.h
Created Jul 19, 2015
C++ template to generate a magic mask with alternating runs of 0s & 1s
View magic_mask.h
#include <cstdint>
template <typename T>
inline T magic_mask(int_fast8_t n) {
T base = T(1)<<(int_fast16_t(1)<<n);
return (~T(0))/(base*base-1)*(base-1);
}
@pallas
pallas / minkowski_question_mark.cc
Last active Dec 17, 2015
Minkowski question mark function
View minkowski_question_mark.cc
// All rights reserved,
// Derrick Pallas
// License: zlib
#include <cmath>
#include <cstdlib>
template <typename fp_type>
fp_type minkowski_question_mark(const fp_type fp) {
float a0 = floor(fp);
@pallas
pallas / try.hh
Created Nov 21, 2016
C++ macros for trying libc calls, throwing std::runtime_error on errno
View try.hh
#ifndef TRY_H
#define TRY_H
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <errno.h>
#define TRY(f, ...) ({ \
@pallas
pallas / container_of.hh
Last active Nov 22, 2016
C++ member-to-object for classes with standard layout
View container_of.hh
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef CONTAINER_OF
#define CONTAINER_OF
#include <cstddef>
template <class T, typename M>
@pallas
pallas / courier-pem.mk
Created Jan 14, 2017
Will regenerate courier.pem & restart mail daemons when Let's Encrypt certificate renews.
View courier-pem.mk
BASE?=/etc/letsencrypt
DOMAIN?=mx.example.com
DHBITS:=4096
LIVE:=$(BASE)/live/$(DOMAIN)
PRIVKEY:=$(LIVE)/privkey.pem
FULLCHAIN:=$(LIVE)/fullchain.pem
.PHONY: default
@pallas
pallas / Makefile
Last active Aug 23, 2017
Basic Makefile template
View Makefile
default: all
DEBUG ?= -g -ggdb -DDEBUG
ifeq ($(DEBUG),)
override DEBUG := -DNDEBUG
endif
override LDFLAGS += -lstdc++
override CFLAGS += $(DEBUG) -MD -MP
override CXXFLAGS += $(DEBUG) -MD -MP
@pallas
pallas / minkowski_distance.cc
Last active Sep 22, 2018
Minkowski distance function
View minkowski_distance.cc
// All rights reserved,
// Derrick Pallas
// License: zlib
#include <cmath>
#include <cstdlib>
#include <limits>
#include <vector>
template <typename fp_type>