Skip to content

Instantly share code, notes, and snippets.

@kccqzy
kccqzy / bus_schedule.cc
Last active October 9, 2022 00:16
The Bus Schedule Problem (Problem B)
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
@kccqzy
kccqzy / make_tree_for_range.py
Created April 5, 2021 19:32
Make a binary tree based on binary numbers
import sys
import itertools
def next_power_of_two(v):
# From Stanford bit-twiddling hacks: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
v -= 1
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
#include <algorithm>
#include <array>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
static const size_t STACK_SIZE = 16777216;
static const size_t DEFAULT_THREADS = 4;
struct Runtime;
@kccqzy
kccqzy / pext_utf8_decode.cpp
Created February 1, 2019 08:15
Test UTF-8 decoder using PEXT instruction
#include <stdlib.h>
unsigned pext_utf8_decode(unsigned char*& buf) {
unsigned next4;
__builtin_memcpy(&next4, buf, 4);
next4 = __builtin_bswap32(next4);
if (__builtin_expect(!!(next4 >> 31), 0)) {
// multi-byte handling
unsigned r;
if (((next4 >> 16) & 0b11100000'11000000) == 0b11000000'10000000) {
@kccqzy
kccqzy / cumulative1.hs
Last active July 6, 2018 23:31
Cumulative monoidal append
module Cumulative
( makeCumulative
, makeCumulativeReverse
) where
import Data.Traversable
import Data.Monoid
-- | Make a traversable data structure cumulative by performing a left-biased
-- partial sum (actually a monoidal append). Linear time.
@kccqzy
kccqzy / ieee754_explain.cpp
Created May 14, 2017 04:55
Explain an IEEE754 single-/double-precision floating point number
#include <bitset>
#include <iomanip>
#include <iostream>
#include <string.h>
namespace {
template <typename Floating, typename Integral, int expBits>
void explain(Floating x) {
static_assert(sizeof(Floating) == sizeof(Integral),
"types not the same size");
@kccqzy
kccqzy / FRM.hs
Last active April 26, 2016 17:13
Quick implementation of a segment tree with fast range mconcat
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NoMonoLocalBinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
module FRM
( FastRangeMconcat
, fromVector
, toVector
, (!), (!?)
@kccqzy
kccqzy / clean_trash.js
Last active September 12, 2023 06:01
This gist cleans your OS X trash by permanently removing items trashed more than X days ago
#!/usr/bin/env osascript -l JavaScript
var numDaysToKeep = 20;
var now = $.NSDate.date;
console.log("Now:", now.description.js);
var deleteThreshold = now.dateByAddingTimeInterval(-86400 * numDaysToKeep);
console.log("Will delete items whose date added is earlier than", deleteThreshold.description.js);
var urlPath = $.NSURL.alloc.initFileURLWithPathIsDirectory($('~/.Trash').stringByExpandingTildeInPath, true);
var fm = $.NSFileManager.defaultManager;
var urlContents = fm.contentsOfDirectoryAtURLIncludingPropertiesForKeysOptionsError(urlPath, ["NSURLAddedToDirectoryDateKey"], $.NSDirectoryEnumerationSkipsHiddenFiles, null).js;
@kccqzy
kccqzy / RNCryptor.hs
Created December 13, 2014 13:12
Quick Implementation of RNCryptor
{-# LANGUAGE RecordWildCards #-}
module RNCryptor (Credentials(..), encrypt, decrypt) where
import Control.Applicative ((<$>), (<*>), pure)
import Control.Monad
import Control.Monad.Trans (liftIO)
import Control.Error (hoistEither, note, hush, runEitherT, EitherT)
import qualified Data.ByteString as B
import qualified Data.Attoparsec.ByteString as PB