Skip to content

Instantly share code, notes, and snippets.

@ahancock1
ahancock1 / ViewportControl.cs
Last active August 14, 2018 18:12
ViewportControl in WPF
// -----------------------------------------------------------------------
// Copyright (C) 2017 Adam Hancock
//
// Viewer.cs can not be copied and/or distributed without the express
// permission of Adam Hancock
// -----------------------------------------------------------------------
namespace Controls
{
using System;
@ahancock1
ahancock1 / DouglasPeuckerReduction.cs
Last active June 1, 2023 09:00
Ramer–Douglas–Peucker algorithm is a line simplification algorithm for reducing the number of points used to define its shape. Implemented in C# 7
namespace Interpolation
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
/// <summary>
/// Douglas Peucker Reduction algorithm.
/// </summary>
@ahancock1
ahancock1 / MonotoneCubicInterpolation.cs
Created March 26, 2019 09:44
MonotoneCubicInterpolation
public class MonotoneCubicSpline
{
private readonly double[] _x;
private readonly double[] _y;
private readonly int _n;
private readonly double[] _m;
public MonotoneCubicSpline(IList<Point> points)
{
_x = points.OrderBy(p => p.X).Select(p => p.X).ToArray();
@ahancock1
ahancock1 / RateLimiter.cs
Created June 15, 2019 23:45
A weighted and timed rate limiter for c# that limits access to resources
public abstract class Disposable : IDisposable
{
private bool _disposed;
public void Dispose()
{
if (_disposed)
{
return;
@ahancock1
ahancock1 / FluentValidator.cs
Created April 13, 2020 21:21
Simple Blazor Fluent Validation Validator Component
public class FluentValidator : ComponentBase
{
private readonly IDictionary<Type, IValidator> _validators =
new Dictionary<Type, IValidator>();
[CascadingParameter]
public EditContext EditContext { get; set; }
[Inject]
@ahancock1
ahancock1 / Socket.cs
Last active May 1, 2020 20:41
Simple ClientWebSocket implementation C#
public interface ISocket : IDisposable
{
event Action<Exception> OnError;
event Action<dynamic> OnData;
event Action OnDisconnected;
event Action OnConnected;
@ahancock1
ahancock1 / Disposable.cs
Created May 4, 2020 14:53
Singleton disposable factory wrapper C# - Maintains reference count and disposes of singleton instance on last reference dispose or instance disposed invoked
namespace DisposableInstance
{
using System;
using System.Threading;
// Usage
internal class Program
{
private static void TestDisposeInstance()
{
@ahancock1
ahancock1 / Series.cs
Created September 27, 2020 11:12
Observable extensible generic trading indicator series
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public interface ISeries
{
void Attach(ISeries series);
@ahancock1
ahancock1 / Classifier.cs
Created October 10, 2020 14:05
Half Space Trees HST classifier .Net C# implemetation
public class ClassifierSettings
{
public int WindowSize { get; set; } = 250;
public int Estimators { get; set; } = 25;
public int MaxDepth { get; set; } = 15;
public int Features { get; set; } = 0;
@ahancock1
ahancock1 / RateLimiter.cs
Created February 22, 2021 22:14
Reactive Rate Gate using observables to limit access to resources async
using System;
using System.Collections.Concurrent;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
public class RateLimiter
{