Skip to content

Instantly share code, notes, and snippets.

@kaja47
kaja47 / XMLSW.scala
Created August 27, 2018 03:19
nicer javax.xml.stream.XMLStreamWriter
object XMLSW {
private val f = XMLOutputFactory.newInstance
def document(encoding: String = "utf-8", version: String = "1.0")(body: XMLSW => Unit) = {
val out = new StringWriter()
val w = f.createXMLStreamWriter(out)
val ww = new XMLSW(w)
ww.document(encoding, version)(body)
w.flush()
w.close()
@kaja47
kaja47 / gist:f1804142577495beae13041c9d9094d8
Last active February 15, 2018 18:44
reasonable replaceAll
import scala.util.matching.Regex
import java.util.regex.Matcher
def replaceAllByString(r: Regex, s: String, f: Matcher => String) = {
val m = r.pattern.matcher(s)
m.reset()
var lastAppendPos = 0
var result = m.find()
if (result) {
def parseLine(l: String) = {
def march(l: String, pos: Int, ch: Char): Int = {
var i = pos; while (i < l.length) {
if (l.charAt(i) == ch) return i
i += 1
}
sys.error("")
}
def marchUntilStringEnds(l: String, pos: Int): Int = {
@kaja47
kaja47 / regex-api-is-really-intuitive.scala
Last active June 28, 2017 19:46
scala.util.matching.Regex.replaceAll
val matchAll = new Regex(".*")
matchAll.replaceAllIn("I have no money", m => m.matched)
// I have no money
matchAll.replaceAllIn("I have $0", m => m.matched)
// I have I have $0
matchAll.replaceAllIn("I have $1", m => m.matched)
// java.lang.IndexOutOfBoundsException: No group 1
@kaja47
kaja47 / parallelRadixsort.d
Last active June 6, 2017 21:10
parallel radix sort
import std.range;
import std.parallelism;
import core.atomic;
import gcc.builtins;
T[] parallelRadixsort(T)(T[] arr, T[] tmparr) {
assert(arr.length == tmparr.length, "lengths");
alias Histo = int[256];
alias Histo4 = int[256][T.sizeof];
@kaja47
kaja47 / radix-sort.d
Last active May 27, 2017 14:08
radix sort in D
T[] radixsort(T)(T[] arr, T[] tmparr) {
assert(arr.length == tmparr.length);
int[256][T.sizeof] counts;
foreach (v; arr) {
foreach (b; 0 .. T.sizeof) {
uint c = (v >> (b * 8)) & 0xff;
counts[b][c] += 1;
}
@kaja47
kaja47 / mm.c
Last active December 1, 2016 02:34
matrix multiplication
__attribute__((always_inline)) inline float hadd(__m256 x) {
x = _mm256_hadd_ps(x, x);
x = _mm256_hadd_ps(x, x);
return ((float*)&x)[0] + ((float*)&x)[4];
}
#define PAD 16
void square_mat_mul_tiered(float *a, float *b, const int len, float *res) {
#include <immintrin.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int len = 16;
char* arr = malloc(1024*1024*16);
int stride = 1024*4;
// warm TLB
case class Segment(hash: Int, str: String)
sealed trait DiffGroup
case class NewLine(str: String) extends DiffGroup
case class Diffs(diffs: Seq[Diff]) extends DiffGroup
sealed trait Diff { def pos: Int }
case class Addition(pos: Int, str: String) extends Diff
case class Deletion(pos: Int, len: Int) extends Diff
@kaja47
kaja47 / alias.c
Created April 12, 2016 15:01
Another hardware mystery
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// STEPS = 4 830ms
// STEPS = 16 4850ms
int main(int argc, char *argv[]) {
int STEPS = atoi(argv[1]);