Skip to content

Instantly share code, notes, and snippets.

View StephenCleary's full-sized avatar

Stephen Cleary StephenCleary

View GitHub Profile
@StephenCleary
StephenCleary / ObservableProgress.cs
Last active April 16, 2024 11:22
ObservableProgress
using System;
using System.Reactive.Linq;
using System.Threading;
/// <summary>
/// Helper methods for using observable <see cref="IProgress{T}"/> implementations. These are hot observables.
/// </summary>
public static class ObservableProgress
{
/// <summary>
@StephenCleary
StephenCleary / AsyncCache.cs
Last active April 15, 2024 10:22
Asynchronous cache
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Nito.Logging;
@StephenCleary
StephenCleary / Queue.cs
Last active April 13, 2024 18:37
Some types from Purely Functional Data Structures
public readonly record struct Queue<T>(int FrontCount, Stream<T> Front, int RearCount, Stream<T> Rear)
{
// val empty = Queue (0, $Nil, 0, $Nil)
public static Queue<T> Empty { get; } = new(0, Stream<T>.Empty, 0, Stream<T>.Empty);
// fun isEmpty (lenf, _, _, _) = (lenf = 0)
public bool IsEmpty => FrontCount == 0;
// fun snoc ((lenf, f, lenr, r), x) = check (lenf, f, lenr+1, $Cons (x, r))
public Queue<T> Snoc(T item) => (this with { RearCount = RearCount + 1, Rear = new(item, Rear) }).Check();
@StephenCleary
StephenCleary / FnvHash.cs
Created October 6, 2019 14:15
FNV-1a hash in C#
/// <summary>
/// A hash combiner that is implemented with the Fowler/Noll/Vo algorithm (FNV-1a). This is a mutable struct for performance reasons.
/// </summary>
public struct FnvHash
{
/// <summary>
/// The starting point of the FNV hash.
/// </summary>
public const int Offset = unchecked((int)2166136261);
@StephenCleary
StephenCleary / UnitTests.cs
Last active February 4, 2024 12:29
How StartNew responds to CancellationToken
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class StartNewCancellationTokenUnitTests
{
[TestMethod]
public void CancellationTokenPassedToStartNew_CancelsTaskWithTaskCanceledException()
@StephenCleary
StephenCleary / AutoResetCancellationTokenSource.cs
Created September 14, 2023 12:30
Resettable CTSs using InterlockedState
using System.Threading;
/// <summary>
/// A <see cref="CancellationTokenSource"/> that resets itself to uncanceled after <see cref="Cancel"/> is called.
/// </summary>
public sealed class AutoResetCancellationTokenSource
{
/// <summary>
/// Cancels any tokens previously returned from <see cref="Token"/>, and resets this instance to an uncancelled state.
/// </summary>
@StephenCleary
StephenCleary / CustomBoundedChannel.cs
Created October 23, 2023 20:05
A System.Threading.Channel<T> that allows custom bounds (more complex than counting items)
using System.Threading.Channels;
using Nito.AsyncEx;
// All members must be safe to call while under lock.
public interface ICustomBounds<in T>
{
bool IsEmpty { get; }
bool IsFull { get; }
void Add(T item);
void Subtract(T item);
@StephenCleary
StephenCleary / ProducerConsumerStream.cs
Created August 24, 2022 21:11
Producer/Consumer Stream
public sealed class ProducerConsumerStream
{
public static Stream Create(Func<Stream, Task> producer, PipeOptions? options = null)
{
var pipe = new Pipe(options ?? PipeOptions.Default);
var readStream = pipe.Reader.AsStream();
var writeStream = pipe.Writer.AsStream();
Run();
return readStream;
@StephenCleary
StephenCleary / InterlockedState.cs
Last active October 2, 2023 02:50
Helper methods for working with interlocked state
using System;
using System.Threading;
/// <summary>
/// Interlocked helper methods.
/// </summary>
public static class InterlockedState
{
/// <summary>
/// Executes a state transition from one state to another.
@StephenCleary
StephenCleary / memory_docs_samples.md
Created September 14, 2023 12:41 — forked from GrabYourPitchforks/memory_docs_samples.md
Memory<T> API documentation and samples

Memory<T> API documentation and samples

This document describes the APIs of Memory<T>, IMemoryOwner<T>, and MemoryManager<T> and their relationships to each other.

See also the Memory<T> usage guidelines document for background information.

First, a brief summary of the basic types

  • Memory<T> is the basic type that represents a contiguous buffer. This type is a struct, which means that developers cannot subclass it and override the implementation. The basic implementation of the type is aware of contigious memory buffers backed by T[] and System.String (in the case of ReadOnlyMemory<char>).