Skip to content

Instantly share code, notes, and snippets.

View mortehu's full-sized avatar

Morten Hustveit mortehu

  • Jump Trading
  • New York, New York
View GitHub Profile
@mortehu
mortehu / prime-triangle.py
Last active May 18, 2018 22:50
Prime Triangle
#!/usr/bin/env python3
import fractions
import sys
def is_prime(n):
for d in range(2, int(n**0.5)):
if n % d == 0:
return False
@mortehu
mortehu / .vimrc
Created March 20, 2017 23:17
vimrc
let loaded_matchparen = 1
syntax on
map <Down> gj
map <Up> gk
filetype indent on
set cryptmethod=blowfish2
set tags=tags;/
set bg=dark
@mortehu
mortehu / utf8.cc
Last active September 22, 2016 00:22
UTF-8 case folding in C++14
#include <codecvt>
#include <iomanip>
#include <iostream>
#include <locale>
#include <string>
#include "zip.h" // See https://gist.github.com/mortehu/373069390c75b02f98b655e3f7dbef9a
const auto low = u8"abcæøåαβγ";
const auto upp = u8"ABCÆØÅΑΒΓ";
@mortehu
mortehu / smooth-histogram.py
Created July 15, 2016 14:41
Smooth histograms
#!/usr/bin/env python
# Generates a curve for drawing a smooth histogram for a set of real values
# provided on the standard input.
# Copyright (C) 2016 Morten Hustveit
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
@mortehu
mortehu / zip-test.cc
Last active May 23, 2022 12:00
C++ zip
#include <cstdio>
#include <list>
#include <vector>
#include "zip.h"
int main() {
std::vector<int> one{{1, 11}};
auto two = [] { return std::vector<short>{{2, 22}}; };
const std::list<float> three{{3, 33}};
@mortehu
mortehu / autoskeleton.sh
Created June 1, 2015 20:58
Script to generate minimal Makefile.am and configure.ac for a C++ project
#!/bin/bash
echo "Press Ctrl-C at any time to abort Makefile generation"
echo
echo "The project name should start with a letter and contain nothing but "
echo "letters (A-z), digits (0-9) and dashes ('-'). Do not include the "
echo "version number."
echo
echo "Example: hello-world"
@mortehu
mortehu / reboot.md
Last active August 29, 2015 14:20
Checklist for resilient reboot of Debian and Ubuntu servers

/etc/default/rcS

DELAYLOGIN=no
FSCKFIX=yes

/etc/fstab

Add the nofail option to as many mount points as possible. This will allow the boot process to continue even if a file system is corrupted.

Dropbear

@mortehu
mortehu / google-cloud-storage-list.py
Created April 16, 2015 15:18
Minimal Google Cloud Storage API Python example
#!/usr/bin/env python
"""Sample Google Cloud Storage API client.
Based on <https://cloud.google.com/storage/docs/json_api/v1/json-api-python-samples>,
but removed parts that are not relevant to the Cloud Storage API.
Assumes the use of a service account, whose secrets are stored in
$HOME/google-api-secrets.json"""
@mortehu
mortehu / error.c
Created December 11, 2014 22:26
Error reporting
static __thread char* last_error;
const char* last_error(void) {
return last_error ? last_error : strerror(errno);
}
void clear_error(void) {
free(last_error);
last_error = NULL;
}
@mortehu
mortehu / gist:d9f9671dd4972a4c0c57
Last active August 29, 2015 14:10
libstdc++ hash function extracted
uint64_t Hash(const StringRef& key) {
static const uint64_t mul = (0xc6a4a793UL << 32UL) + 0x5bd1e995UL;
auto shift_mix = [](uint64_t v) -> uint64_t { return v ^ (v >> 47); };
auto len = key.size();
auto buf = key.data();
const auto len_aligned = len & ~7;
const auto end = buf + len_aligned;