Skip to content

Instantly share code, notes, and snippets.

View bbarry's full-sized avatar

Bill Barry bbarry

  • Ren Inc
  • Pennsylvania
View GitHub Profile
@bbarry
bbarry / PrimeTests.cs
Created February 21, 2017 23:44
Miller–Rabin primality test in C# for 32 and 64 bit numbers; 32bit version is approximately 35 times faster than NaiveIsPrime on average; 64 bit ver is untested
public static class PrimeTests {
public static bool IsPrime(uint n) {
if (n < 2) return false;
if (n == 2 || n == 3 || n == 5 || n == 7) return true;
if (n % 2 == 0) return false;
var n1 = n - 1;
var r = 1;
var d = n1;
@bbarry
bbarry / shapez.ps1
Created November 30, 2021 01:07
powershell script to determine if a shape is buildable
function rotate1($shape) {
$low = $shape -band 0x7777
$high = $shape -band 0x8888
($low -shl 1) + ($high -shr 3)
}
function mirror($shape) {
$a = $shape -band 0x1111
$b = $shape -band 0x2222
$c = $shape -band 0x4444
@bbarry
bbarry / JwtValidationService.cs
Created August 19, 2020 19:02
validate a token according to the asp.net core application's configured options
public class JwtValidationService : ITokenService
{
private readonly JwtBearerOptions _options;
public JwtTokenService(IOptions<JwtBearerOptions> options)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
}
@bbarry
bbarry / typescript-better-enum.ts
Created July 15, 2020 13:28
typescript building a better enum
// https://github.com/krzkaczor/ts-essentials
import { Opaque } from 'ts-essentials';
export const DIRECTIONS = {
UP: 'Up' as Opaque<'Up', 'DIRECTIONS'>,
DOWN: 'Down' as Opaque<'Down', 'DIRECTIONS'>,
} as const;
export type DIRECTIONS = typeof DIRECTIONS[keyof typeof DIRECTIONS];
module.exports = {
plugins: [
"@typescript-eslint"
],
rules: {
"no-restricted-syntax": [
"error",
{
"selector": "TSEnumDeclaration[const=true]",
"message": "Don't declare const enums"
using System;
using Microsoft.Extensions.DependencyInjection;
namespace ServiceFactory
{
public interface IServiceFactory<T>
{
T GetService();
}
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ConsoleApp2 {
public class Benchmarking {
private static int[] gArray;
private static List<int> gList;
@bbarry
bbarry / example 2
Last active October 20, 2018 01:18
0eNrtvVtuXUmWJDoXfus0/P2Ij/7rGdRfISFQEiOCKAUlUFTgBgoxgB5IT6xHcg+pEM8RtW27mR027m2gfjJTSdL28uVvX7Zs/efVu49fbz7f3949XP3yn1e37z/dfbn65d//8+rL7W931x8f/7+Hvz7fXP1y9evHr7cfrt5c3V3/8fjP9/dfP9wcPt1+vPr7zdXt3Yeb/+fql/j3v95c3dw93D7c3nxDefrHX2/vvv7x7ub++AvPf/7l4f769rffHw7H//p4RP386cvxrz7dPX7wiHQY4c3VX8f/7u3vv9/8hJOecR7//PCPrT+hjP9Wv6P8t7qFk1l74q45hYUpaQ+msjC97ME0vlFtD6fTOHEXZ9A4ebddk8bpu7312JfqKCybQFEePts4SR4/2zhZHkDbOIWbXr0+z698nF9vrj7c3t+8//YLZQu36iNz28CmD81toK6PzW2goQ/ObaDJ+T72qDk/nUb9r9dfHg5HK+++fP50/3B4d/PxYWPsx3b6Qnr5hbb1BWN1T5ure5Ln1TZOlufVNk6R59U2zmn8P8LcHb48fPq8AdKeQd5cHffjh/tPH9++u/n9+s/bT/ePv3N/c/3h7a/3n/54+wRz9cvD/debN98w3z5ifr758Pan/fvP2/uHr09D6Xtbnn7j8G9XR1uPDbv+ZsLV1Zblxp6y7QJjU9kGMnaVbSBjW9kEyoHq3djT/++6N0d5bSjPa0MAR6ukrwZhE0g/pG3j6Ke0bRz9mLaNY8ypbSBjTm0DGXNqG8iYU5tART+qtbEJJB/VAI58VAM48lEN4JwP6pubj4f3v998edjaU77P19af9vJ314/9tIVYf1wLbu++3Nw/HH+yc/z7B/PsfJC2kJu2yvSB8bfOH6XTlnfR8qFZPoJo+RQ9MzX8GuSlZnu41SgvNQAoyUsNAMqa62IW+74W9QND/EBVPzDFD4jzLpYgfqCrH4jiB4b6gSR+YKofyNoHWlA/UMQPRPUD4urdkvqBJn5AnclFnMlNnclFnMl
@bbarry
bbarry / hg.ArgumentCompleters.ps1
Created August 16, 2018 21:54
mercurial argument completion, works in powershell 5.1, should work with TabExpansionPlusPlus; faster (and more limited) than https://github.com/JeremySkinner/posh-hg
# http://www.wtfpl.net/txt/copying/
function isHgDirectory() {
if(test-path ".hg") {
return (Get-Item .).fullname
}
$d = (Get-Item .).parent
while ($d -ne $NULL) {
$p = $d.fullname + "\.hg"
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)