Skip to content

Instantly share code, notes, and snippets.

@jmcd
jmcd / FixedCapacityMovingBuffer.cs
Created August 19, 2011 11:08
A fixed capacity buffer that allows adding of items beyond the capacity by discarding earlier items. Adding is O(1), rather than O(N) because the internal representation is overwritten rather than actually moved.
public class FixedCapacityMovingBuffer<T>
{
private readonly int _capacity;
private readonly T[] _buffer;
private int _addCount;
public FixedCapacityMovingBuffer(int capacity)
{
_capacity = capacity;
@jmcd
jmcd / StreamExtensions.cs
Created October 12, 2011 10:37
An extension method to read to the end of a stream
using System;
using System.IO;
namespace jmcd
{
public static class StreamExtensions
{
public static byte[] ReadToEnd(this Stream stream)
{
var result = new byte[0];
@jmcd
jmcd / MoveOperation.cs
Created October 14, 2011 15:16
General purpose move operation on entities with an integer rank
using System;
using System.Collections.Generic;
using System.Linq;
namespace jmcd
{
public class MoveOperation<T>
{
private readonly Func<T, int> _positionOf;
private readonly Action<T, int> _reposition;
@jmcd
jmcd / WtfApp.cs
Created December 2, 2011 14:24
A console app to print the current memory usage of all worker processes of an IIS app-pool.
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Web.Administration;
namespace WtfApp
{
internal class Program
@jmcd
jmcd / Example AddressFields.cshtml
Created March 20, 2012 16:00
Extension to render partials of nested models with correct element name prefix
@model gts.Web.Models.AddressFields
<ul>
<li>
@Html.HumanLabelFor(x => x.Line1)
@Html.TextBoxFor(x => x.Line1)
@Html.ValidationMessageFor(x => x.Line1)
</li>
<li>
@Html.HumanLabelFor(x => x.Line2)
@jmcd
jmcd / multiSelectToCheckboxes.js
Created April 2, 2012 15:52
jQuery plugin to convert multi-select HTML to checkbox list
(function($) {
var methods = {
init: function() {
var $ul = $("<ul/>").insertAfter(this);
var baseId = "_" + $(this).attr("id");
$(this).children("option").each(function(index) {
var $option = $(this);
var id = baseId + index;
var $li = $("<li/>").appendTo($ul);
@jmcd
jmcd / Foo.cs
Created April 19, 2012 09:42
QueryOver select except
var scheduleDateAlias = default(ScheduleDate);
return session.QueryOver(() => scheduleDateAlias)
.WithSubquery.WhereNotExists(
QueryOver.Of<CommunityGrantApplicationChequeRequest>()
.Where(x => x.ScheduleDate.Id == scheduleDateAlias.Id)
.Select(x => x.ScheduleDate)
)
.OrderBy(scheduleDate => scheduleDate.Rank).Asc
.List<ScheduleDate>();
@jmcd
jmcd / ByteArrayBuilder.cs
Created April 26, 2012 15:41
Byte-array builder
public class ByteArrayBuilder
{
private byte[] _buffer;
public int Length { get; set; }
public ByteArrayBuilder()
{
_buffer = new byte[4096];
Length = 0;
}
@jmcd
jmcd / ComboMaskFactory.cs
Created May 8, 2012 07:49
Creates permutations of an integer for a fixed number of '1' bits
public class ComboMaskFactory
{
private int _current;
private readonly int _lim;
public ComboMaskFactory(int maxNumberOfBitsInMask, int numberOfOneBits)
{
if (maxNumberOfBitsInMask > 32) throw new Exception("ComboMaskFactory can only be used with maxNumberOfBitsInMask up to 32.");
_current = GetLowestValueUsingSuppliedNumberOfOneBits(numberOfOneBits);
_lim = 1 + _current << maxNumberOfBitsInMask - numberOfOneBits;
@jmcd
jmcd / IntExtensions.cs
Created May 8, 2012 07:56
Ordinal suffix of an integer
public static class IntExtensions
{
public static string GetOrdinalSuffix(this int @this)
{
switch (@this % 100)
{
case 11:
case 12:
case 13:
return "th";