Skip to content

Instantly share code, notes, and snippets.

View markusl's full-sized avatar

Markus Lindqvist markusl

  • Supercell
  • Finland
View GitHub Profile
#include <array>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <functional>
#include <cassert>
#include "memory_content.hpp"
// opcode | instruction | operands (mod 0) | operands (mod 1)
// -------+-------------+------------------+-----------------
struct handle_deleter_t
{
void operator()(HANDLE handle) {
if(handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
}
};
void foo()
{
@markusl
markusl / Boost Optional usage.cpp
Last active December 30, 2015 05:39
Purpose of this document is to define how boost::optional is used in unified way throughout the code.
/**
* RULES
* 1. Prefer using is_initialized() for checking the contents if the
* value is not assigned to a boost::optional right before
* 2. Prefer get() over 'operator ->', to make visible what's happening
* 3. Use get_value_or() if applicable, this makes the control flow less complex
* 4. Optionally prefix variable with 'opt' or postfix with 'Optional'
*
* DOCS http://www.boost.org/doc/libs/1_55_0/libs/optional/doc/html/index.html
*
@markusl
markusl / LongestCommonSubstring.fs
Created September 5, 2013 10:07
Longest common substring, in F#.
module LongestCommonSubstring
let get (strings:seq<string>) =
let first' = strings |> Seq.tryFind (fun _ -> true)
let isWhiteSpace = System.String.IsNullOrWhiteSpace
let mapper substringLength (first:string) currentStrings offset =
if substringLength + offset < first.Length then
let currentSubstring = first.Substring(offset, substringLength)
if not(isWhiteSpace(currentSubstring)) &&
@markusl
markusl / CohenSutherland.fs
Created July 31, 2012 17:04
FSharp translation of the Cohen–Sutherland 2D line clipping algorithm
/// FSharp translation of the Cohen–Sutherland 2D line clipping algorithm
/// http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
///
/// Implemented in purely functional form which means there are no mutable variables.
/// No guarantees are given on the performance or reliability, it's not
/// thoroughly tested so feel free to submit unit tests. :-)
///
/// Markus Lindqvist 07/2012
module CohenSutherland
@markusl
markusl / GCHQ_vm.fs
Last active September 28, 2015 20:17
GCHQ Challenge Stage 2
/// Implementation for GCHQ Challenge stage 2 virtual machine, in F#
/// From: http://canyoucrackit.co.uk/codeexplained.asp?v=1
/// This is a JavaScript programming challenge, with a cyber security angle. To solve
/// this stage an implementation of a simple virtual processor is required. Some notes
/// on the architecture are provided along with a block of data that can be analysed.
/// Solving this stage will reveal the final stage of the challenge.
/// The challenge originally suggested writing a virtual machine for the given instructions in JavaScript, but I decided to go with F#
/// See original 15b436de1f9107f3778aad525e5d0b20.js at http://pastebin.com/p5AHwPra
///
@markusl
markusl / eoq.fs
Created October 28, 2011 12:26
Economic order quantity calculator in F#
/// Read more about EOQ from http://en.wikipedia.org/wiki/Economic_order_quantity
/// Using functional approach to handle user interface interaction using reactive programming style.
/// Any comments and improvements welcome :-)
/// -Markus L/2011
#if INTERACTIVE
#r "PresentationCore"
#r "PresentationFramework"
#r "System.Xaml"
@markusl
markusl / castvalue.fs
Created August 24, 2011 18:25
Implementing the Fast inverse square root (InvSqrt) in F#
#load "FsharpChart.fsx"
open MSDN.FSharp.Charting
let castValue (value : 'TSource) :'TResult =
let an = new System.Reflection.AssemblyName("CastUtil")
let ab = System.AppDomain.CurrentDomain.DefineDynamicAssembly(an, System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave)
let mb = ab.DefineDynamicModule(an.Name)
let tb = mb.DefineType("CastUtil", System.Reflection.TypeAttributes.Public ||| System.Reflection.TypeAttributes.Class)
let fb = tb.DefineMethod("Read", System.Reflection.MethodAttributes.Public ||| System.Reflection.MethodAttributes.Static, typeof<'TResult>, ([|typeof<'TSource>|]))
let ig = fb.GetILGenerator()
@markusl
markusl / brainfuck.fs
Created August 11, 2011 17:31
Brainfuck interpreter written in F#
// FSharp (F#) interpreter for brainfuck programming language
// Read more from
// - http://www.muppetlabs.com/~breadbox/bf/
// - http://fsharp.net
// (c) 2011 Markus Lindqvist
module brainfuck
open System
let brainfuckMemorySize = 30000
@markusl
markusl / IpToCountry.cpp
Created July 17, 2011 13:45
Faster C++ class to map IP addresses to countries using database from http://software77.net/geo-ip/
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <array>
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {