Skip to content

Instantly share code, notes, and snippets.

View r-lyeh-archived's full-sized avatar

r-lyeh-archived

View GitHub Profile
@r-lyeh-archived
r-lyeh-archived / splaytree.cpp
Created June 26, 2014 09:20
splaytree from wikipedia
// [ref] http://en.wikipedia.org/wiki/Splay_tree
#include <functional>
#ifndef SPLAY_TREE
#define SPLAY_TREE
template< typename T, typename Comp = std::less< T > >
class splay_tree {
private:
// "License": Public Domain
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
#ifndef PORTABLE_ENDIAN_H__
#define PORTABLE_ENDIAN_H__
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
# define __WINDOWS__
// simple every() function
// - rlyeh. mit licensed
// @todo: during, in, when,
#pragma once
#include <map>
#ifdef EVERY_USE_OMP
#include <omp.h>
@r-lyeh-archived
r-lyeh-archived / when.cpp
Last active August 29, 2015 14:13
when/unless/go keywords
// when/unless go/unless keywords
// - rlyeh
#include <functional>
#include <iostream>
#define when(...) for( std::function<void()> fn; !fn && (__VA_ARGS__ +0); fn() ) fn = [&]
#define unless(...) , !(__VA_ARGS__) ? fn : fn = [&]
#define go when(true)
@r-lyeh-archived
r-lyeh-archived / lispy90.cpp
Created February 3, 2015 15:10
Scheme Interpreter in 90 lines of C++. Made by Anthony C. Hay in 2010.
// Scheme Interpreter in 90 lines of C++ (not counting lines after the first 90).
// Inspired by Peter Norvig's Lis.py.
// Made by Anthony C. Hay in 2010. See http://howtowriteaprogram.blogspot.co.uk/
// This is free and unencumbered public domain software, see http://unlicense.org/
// This code is known to have faults. E.g. it leaks memory. Use at your own risk.
#include <iostream>
#include <sstream>
#include <string>
@r-lyeh-archived
r-lyeh-archived / gist:f403dcb87f7b97883b62
Last active August 29, 2015 14:14
on compiling boost
rem 'cos I always forget how to do it
boostrap.bat
.\bjam2 runtime-link=static --with-serialization --toolset=msvc-11 && :: vs2012
.\bjam2 runtime-link=static --with-serialization --toolset=msvc-12.0 && :: vs2013, also: architecture=x86 address-model=64
rem also, extracting a lib:
bjam tools\bcp
dist\bin\bcp.exe algorithm/string.hpp [outdir]
// a few color space conversions in c++11, original code by mjijackson.com
// - rlyeh, public domain licensed.
#include <tuple>
#include <algorithm>
#include <stdint.h>
std::tuple<float,float,float> rgb2hsv(float r, float g, float b) { // ranges: input[0..255], output[0..1]
r = r/255, g = g/255, b = b/255;
float max = std::max(std::max(r, g), b), min = std::min(std::min(r, g), b), d = max - min;
float h, s = max > 0 ? d / max : 0, v = max;
if( max != min ) {
@r-lyeh-archived
r-lyeh-archived / downscale.cpp
Last active August 29, 2015 14:19
downscale texture
// downscale large textures
if (img.w > 512 || img.h > 512) {
// calculate factor shrink
float factor = std::max(img.w, img.h) / 512;
// downscale
for (unsigned y = 0, h = img.h / factor; y < h; ++y) {
for (unsigned x = 0, w = img.w / factor; x < w; ++x) {
img[x + y * w] = img[x * factor + y * factor * img.w];
}
}
// credits?
template<typename T>
T clamp(const T& value, const T& min, const T& max) {
return min < max
? (value < min ? min : value > max ? max : value)
: (value < max ? max : value > min ? min : value);
}
@r-lyeh-archived
r-lyeh-archived / reverse8.cpp
Last active August 29, 2015 14:20
reverse 8 bits
// credits?
static unsigned char reverse8(unsigned char v) {
return (v * 0x0202020202ULL & 0x010884422010ULL) % 1023;
}