Skip to content

Instantly share code, notes, and snippets.

package com.hafthor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(final String[] args) {
package com.hafthor;
public class Main {
public static void main(final String[] args) {
Nonogram n = new Nonogram("1 3 9 122 42 42 122 9 3 1", "33 22 8 121 121 6 22 11 6 4"); // 9
while(n.solve()) n.print();
n.print();
}
public static class Nonogram {
package com.hafthor;
public class Main {
public static void main(final String[] args) {
final String input = "" +
"89. .5. ..." +
"2.. 7.9 ..." +
"..4 ..3 ..." +
"..9 6.2 ..3" +
"... ..1 897" +
@Hafthor
Hafthor / slow_inflate.c
Last active November 11, 2021 21:33
Slow implementation of inflate in C (zlib ~2x faster)
const static unsigned char LENGTH_CODE_EXTRA_BITS[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
const static unsigned char DISTANCE_CODE_EXTRA_BITS[] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
const static unsigned char CODE_LENGTHS_ORDER[] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
static unsigned short LENGTH_CODE[sizeof(LENGTH_CODE_EXTRA_BITS)];
static unsigned short DISTANCE_CODE[sizeof(DISTANCE_CODE_EXTRA_BITS)];
static unsigned char BIT_REVERSE[256];
const static unsigned int BLOCK_END = 256;
const static unsigned char READ_BITS_FOR_CODE[]={2,3,7}, ADD_COUNT_FOR_CODE[]={3,3,11};
static short HUFFTREE_LITLEN[(1 << 24)+1];
static short HUFFTREE_DIST[(1 << 24)+1];
@Hafthor
Hafthor / slow_inflate.rs
Last active November 11, 2021 21:33
Slow implementation of inflate in Rust (zlib ~2.5x faster)
pub struct Inflatey {
input: Vec<u8>,
output: Vec<u8>,
input_bit_pos: usize,
output_pos: usize,
length_code: [u16; 29],
distance_code: [u16; 30],
bit_reverse: [u8; 256],
}
@Hafthor
Hafthor / SlowInflate_Inflatey.cs
Last active November 11, 2021 21:34
Slow implementation of Inflate in C# (zlib ~2.8x faster)
using System;
namespace SlowInflate {
#pragma warning disable IDE0044 // Add readonly modifier
public class Inflatey {
private byte[] input, output;
public uint inputBitPos, outputPos;
private static byte[] LENGTH_CODE_EXTRA_BITS = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
private static byte[] DISTANCE_CODE_EXTRA_BITS = new byte[] { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
@Hafthor
Hafthor / slow_inflate.go
Last active November 11, 2021 21:34
Slow implementation of inflate in Go (zlib ~2.5x faster)
package main
import (
"errors"
"fmt"
"io"
"os"
"time"
)
@Hafthor
Hafthor / slowInflate.js
Last active November 11, 2021 21:35
Slow implementation of inflate in pure JavaScript with no dependencies (zlib ~3.4x faster)
// to use
// const inflatey = require('./inflatey'), fs = require('fs');
// const buf = fs.readFileSync('somefile.gz');
// const size = buf[buf.length-1]*16777216+buf[buf.length-2]*65536+buf[buf.length-3]*256+buf[buf.length-4];
// const out = new Uint8Array(size);
// const infl = new inflatey(buf, out);
// infl.inputBitPos(80); // skip gz header bits
// infl.inflate();
// fs.writeFileSync('somefile', out);
@Hafthor
Hafthor / SlowInflate.java
Last active November 11, 2021 21:35
Slow implementation of Inflate decompression with examples and explanations (zlib ~3x faster)
package com.hafthor;
import java.util.Arrays;
public class SlowInflate {
public static void main(final String[] args) {
fixed();
fixedBetter();
dynamic();
stored();
// instead of
source.parallelStream().forEachOrdered(v -> ...);
// which is ordered, but not parallel
// or
source.parallelStream().forEach(v -> ...);
// which is parallel, but unordered
// do this
final var sourceQueue = new ConcurrentQueue<>(source);
source.parallelStream().forEach(dummy -> { final var v = sourceQueue.poll(); ... }