This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Diagnostics; | |
| public static class SpanExtensions { | |
| /// <summary> | |
| /// Partitions a span. Useful if you want the smallest/largest k elements of a list without sorting the whole list. | |
| /// This is a generalization of the partition step of quickselect. | |
| /// </summary> | |
| /// <param name="array">source array</param> | |
| /// <param name="k">desired split point</param> | |
| /// <param name="comparer">optional comparer to use instead of the default for the type</param> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyClass : MyDerivedClass { | |
| public override void Dispose() { | |
| base.Dispose(); | |
| GC.SuppressFinalize(this); // here we improperly call GC.SuppressFinalize(this) | |
| } | |
| } | |
| public class MyDerivedClass : MyBaseClass { | |
| public override void Dispose() { | |
| ; // oops, we forgot to call base.Dispose(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Example: | |
| // E: y^2 = x^3 + 2x + 2 (mod 17) - a=2, b=2 | |
| // G: (5, 1) | |
| using Point = (int x, int y); | |
| int a = 2, b = 2, mod = 17, Z = 19; // y^2 = x^3 + 2x + 2 (mod 17) | |
| Point g = (5, 1); // G: (5,1) | |
| Console.WriteLine($"G:{g}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Diagnostics; | |
| int p = 23, q = 29; // two private primes | |
| int pubMod = p * q; // 667, the neighbor of the beast | |
| // In practice, the primes used are crazy big (1024-bit each), making it infeasible to factor pubMod (2048-bit). | |
| // Even if you could try 4B factors a second, on 4B machines, each with 4B CPUs, for 4B seconds (~126 years), you | |
| // could only explore 128-bits worth of possibilities! And that doesn't mean you're an eighth of the way there. It | |
| // would be if the number were 131-bits in length. Each extra bit doubles the effort required. | |
| int pubExp = 257; // third Fermat prime (2^2^n+1, where n=3) | |
| // The real RSA uses the fourth, and believed to be the last, Fermat prime, 2^2^4+1=65537. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DisposableList<T> : List<T>, IDisposable where T : IDisposable { | |
| public void Dispose() { | |
| foreach (var i in this) i.Dispose(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
NewerOlder