Skip to content

Instantly share code, notes, and snippets.

View dermesser's full-sized avatar
🐢
a bit of time :-)

Lewin Bormann dermesser

🐢
a bit of time :-)
View GitHub Profile
@dermesser
dermesser / roman_converter.hs
Created July 6, 2014 18:36
A simple Roman2Decimal2Roman converter. Bugs: Dec2Rom converter uses four consecutive I (like in VIIII); romanToDecimal /may/ be simplified.
import Data.Char (toLower)
rdToDec :: Char -> Int
rdToDec 'i' = 1
rdToDec 'v' = 5
rdToDec 'x' = 10
rdToDec 'l' = 50
rdToDec 'c' = 100
rdToDec 'd' = 500
@dermesser
dermesser / rope.cpp
Last active November 5, 2023 00:10
A simple rope implementation in C++ – should work well enough.
// Licensed under MIT license.
// (c) Lewin Bormann 2014
# include <string>
# include <iostream>
# include <list>
# include <cstring>
# include <algorithm>
using std::string;
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>
_Bool find(const char* haystack, const char* needle)
{
size_t hlen = strlen(haystack);
size_t nlen = strlen(needle);
unsigned int found = 0;
@dermesser
dermesser / uint64encode.go
Created January 31, 2015 16:17
Encode and Decode uint64 values for sending them over the network or similar use cases. Fast (30 ns per number for both functions)
// Converts a number to a little-endian byte array
func lengthToBytes(l uint64) [8]byte {
var sizebuf [8]byte
var i int = 7
for ; i >= 0; i-- {
// The commented implementation is 5 times slower but equivalent
/*
var divisor uint64 = 1 << (uint(i) * 8)
sizebuf[i] = uint8(l / divisor)
@dermesser
dermesser / chan.hpp
Created October 7, 2015 19:16
Golang-like channels. Not *entirely* threadsafe yet, but almost (a.k.a. quick sketch)
# ifndef _CHAN_HPP
# define _CHAN_HPP
# include <deque>
# include <mutex>
# include <condition_variable>
# include <utility>
template<typename T>
class Chan {
@dermesser
dermesser / perpetual_calendar.rs
Last active February 10, 2023 11:21
A simple but working perpetual calendar implementation (don't expect fancy displaying abilities, it's essentially an Iterator<Item=Date>). Should also be fast enough for actual applications: Takes 220us per year including formatting on an "Intel(R) Xeon(R) CPU @ 2.50GHz". Supports forward and backward iteration from a given date.
extern crate time;
extern crate argparse;
use std::cmp::Ordering;
use std::cmp::PartialOrd;
use std::fmt::Write;
use std::marker::Copy;
use std::option::Option;
use std::ops::Add;
use std::ops::Sub;
@dermesser
dermesser / main.rs
Created April 16, 2016 14:12
Example for installed flow with yup-oauth2
fn read_client_secret(file: &str) -> ApplicationSecret {
use std::fs::{File, OpenOptions};
use std::io::Read;
let mut secret = String::new();
OpenOptions::new().read(true).open(file).unwrap().read_to_string(&mut secret);
let consappsec: ConsoleApplicationSecret = serde_json::from_str(secret.as_str()).unwrap();
consappsec.installed.unwrap()
}
@dermesser
dermesser / fun_with_heaps.rs
Last active April 29, 2016 20:08
Fun With Heaps.
use std::cmp::Ordering;
use std::fmt::Debug;
// min-heap, for now
#[derive(Debug)]
struct Heap<T: Ord> {
h: Vec<T>,
/// min heap if true, otherwise max heap
min: bool,
}
@dermesser
dermesser / arithmetic_parser.rs
Last active May 5, 2016 09:50
A simple arithmetic parser in Rust, based on the excellent `combine` crate. Use by giving an expression as argument: $ target/debug/test-combine "a+4*b/c"
// use with
// [dependencies]
// combine = "1.3.0"
extern crate combine;
use combine::*;
#[derive(Debug)]
enum Expr {
Scalar(f64),
@dermesser
dermesser / timeout_mutex.go
Last active May 12, 2016 15:13
A way to use Mutex-like locking in Go, but with timeouts.
package play1
import (
"errors"
"time"
)
type MyStruct struct {
m map[string]uint64
}