Skip to content

Instantly share code, notes, and snippets.

View Hafthor's full-sized avatar

Hafthor Stefansson Hafthor

View GitHub Profile
@Hafthor
Hafthor / gcf.cs
Last active November 19, 2022 19:45
Greatest Common Factor using Euclid's algorithm and, gasp, gotos to be fast (about 50% faster)
static long gcf(long m, long n)
{
if (m < n) goto l2;
l1: if ((m %= n) == 0) return n;
l2: if ((n %= m) == 0) return m;
goto l1;
}
@Hafthor
Hafthor / ForwardCopy.cs
Last active August 21, 2022 00:22
Works like Array.Copy or Buffer.BlockCopy but with memcpy forward only operation vs memmove
public static void ForwardCopy(byte[] s, uint si, byte[] d, uint di, uint cx) {
unchecked {
// pre copy to qword align to destination
uint cxpre = (8 - (di & 7)) & 7;
if (cx < cxpre) {
// early out if we cannot even align to the first qword
for (int i = 0; i < cx; i++)
d[di++] = s[si++];
return;
}
@Hafthor
Hafthor / DisposableList.cs
Created August 7, 2022 04:53
For making a list of disposable items. Useful since you can using wrap the list create and add disposable items to it, like FileStreams.
public class DisposableList<T> : List<T>, IDisposable where T : IDisposable {
public void Dispose() {
foreach (var i in this) i.Dispose();
}
}
// Copyright (c) 2022 Hafthor Stefansson
// Distributed under the MIT/X11 software license
// Ref: http://www.opensource.org/licenses/mit-license.php.
static unsafe void UnsafeSet(byte[] a, uint d, uint c, byte v) {
unchecked {
ushort v2 = (ushort)(v << 8 | v);
uint v4 = (uint)v2 << 16 | v2;
ulong v8 = (ulong)v4 << 32 | v4;
fixed (byte* p = a) {
byte* di = p + d;
// Copyright (c) 2022 Hafthor Stefansson
// Distributed under the MIT/X11 software license
// Ref: http://www.opensource.org/licenses/mit-license.php.
static unsafe void UnsafeCopy(byte[] sa, uint s, byte[] da, uint d, uint c) {
unchecked {
fixed (byte* sp = sa, dp = da) {
byte* si = sp + s, di = dp + d;
if (c >= 1 && (d & 1) != 0) { // word align
*((byte*)di) = *((byte*)si);
si++; di++; c--; d++;
@Hafthor
Hafthor / jab.cs
Created June 17, 2022 01:00
minimal dependency injector
public class Jab : Attribute {
public T Resolve<T>() where T : class {
return (T)Resolve(typeof(T));
}
public object Resolve(Type typ) {
var typeToInstantiate = (from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsDefined(typeof(Jab), false) && t.IsClass && t.IsPublic && t.GetInterfaces().Contains(typ)
select t).Single();
var defaultConstructor = (from c in typeToInstantiate.GetConstructors().ToList()
@Hafthor
Hafthor / RubiksCube.java
Created January 17, 2022 22:35
command line rubik's cube
package com.hafthor;
import java.util.Scanner;
public class Main {
public static void main(final String[] args) {
final RubiksCube rc = new RubiksCube();
rc.print();
Scanner scanner = new Scanner(System.in);
for (; ; ) {
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" +