Skip to content

Instantly share code, notes, and snippets.

@buybackoff
buybackoff / OrderedDictionary<TKey, TValue>
Last active December 14, 2015 08:49
Generic OrderedDictionary<TKey, TValue> See http://stackoverflow.com/a/15146898/801189 for details.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Data
{
[Serializable]
public class OrderedList<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary
{
@buybackoff
buybackoff / C# test via PInvoke
Last active January 2, 2020 17:58
LE query micro benchmark
[Test]
public void CursorShouldGetLessOrEqual() {
_db = _txn.OpenDatabase(flags: DbFlags.None);
var db2 = _txn.OpenDatabase("test_le",
DbFlags.Create | DbFlags.IntegerKey);
using (var cur = _txn.CreateCursor(db2)) {
int[] keys = new int[10000000];
@buybackoff
buybackoff / gist:e7ceb018e983df1b1573
Last active August 29, 2015 14:23
F# Fast Events
namespace Test
open System
open System.Linq
open System.Linq.Expressions
//thanks to http://v2matveev.blogspot.ru/2010/06/f-performance-of-events.html
// helper type that will perform invocation
type internal Invoker<'D, 'A> = delegate of 'D * obj * 'A -> unit
@buybackoff
buybackoff / RLSEF.R
Created December 9, 2016 16:46
Recursive Least Squares with Exponential Forgetting
RLSF <- function(y,x,alpha=0.95,ist=30,xpxi=NULL,xpy0=NULL)
{
# http://queue.acm.org/detail.cfm?id=2534976
if(!is.matrix(x))x=as.matrix(x)
nT=dim(x)[1]
k=dim(x)[2]
xpx0 = NULL
#
if(is.null(xpxi)){
if(ist <= k)ist=k+1
@buybackoff
buybackoff / GDAXWebSocketsConnection.cs
Last active December 7, 2017 15:57
GDAX WebSockets connection
using System.Net.WebSockets;
var wsc = new ClientWebSocket();
await wsc.ConnectAsync(new Uri("wss://ws-feed.gdax.com"), CancellationToken.None);
Console.WriteLine("Connected...");
var msg = @"{""type"": ""subscribe"", ""product_ids"": [""BTC-USD"", ""ETH-USD"", ""ETH-BTC""]}";
Console.WriteLine(msg);
await wsc.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg)), WebSocketMessageType.Text, true, CancellationToken.None);
var buffer = new ArraySegment<byte>(new byte[4096]);
while (true)
{
@buybackoff
buybackoff / sum.ts
Last active April 11, 2018 21:41
sum.ts
let sum = (x?: number) => {
// the trick is to access inner from inside itself as `any` with `.sum` initialized to 0
// we know that the reference will be initialized
// we could certainly achieve the same effect by using `this.`
// but scoping with `this` is not intuitive
let inner;
inner = (x1?: number) => {
// need additional check for 0, e.g. calling sum(0)(1)(0)() is valid, same below
if (x1 || x1 === 0) {
@buybackoff
buybackoff / typescript.json
Created May 5, 2018 06:21
TypeScript snippets to shorten typing of const vs let by default
{
// Place your snippets for typescript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
@buybackoff
buybackoff / Program.cs
Last active September 21, 2018 12:31
Create memory pressure on private working set. Exit after 30 seconds of inactivity (if on RDP and server hangs)
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MemoryPressure
{
internal class Program
{
private static List<byte[]> _buffers = new List<byte[]>();
@buybackoff
buybackoff / Test.cs
Created February 22, 2019 13:00
Fixed-size struct without unsafe keyword, using "safe" S.R.CS.Unsafe
[StructLayout(LayoutKind.Sequential, Size = Size)]
public struct Test
{
public const int Size = 32;
public byte this[int idx] => Unsafe.AddByteOffset(ref Unsafe.As<Test, byte>(ref Unsafe.AsRef(in this)), (IntPtr)idx);
public override string ToString()
{
Span<byte> bytes = stackalloc byte[Size];
for (int i = 0; i < Size; i++)
@buybackoff
buybackoff / ManualResetValueTaskSourceCore.cs
Created May 7, 2019 15:31
ManualResetValueTaskSourceCore for net461
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
namespace System.Threading.Tasks.Sources