Skip to content

Instantly share code, notes, and snippets.

View Larry57's full-sized avatar

L4rry Larry57

View GitHub Profile
@Larry57
Larry57 / DictionaryExtension.cs
Created October 22, 2014 08:27
Process an element in a Dictionary only if it exists
namespace System.Collections.Generic
{
public static class DictionaryExtension
{
public static void ProcessValue<TKey, TValue>(this IDictionary<TKey, TValue> x, TKey key, Action<TValue> action)
{
TValue o;
if (x.TryGetValue(key, out o))
action(o);
}
@Larry57
Larry57 / BinarySearch.cs
Created April 16, 2014 20:57
BinarySearch in a list, using a selector
namespace System.Collections.Generic
{
public static class ListExtensions
{
/// <summary>
/// Do a binary search in a generic list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
/// <param name="tf">list</param>
/// Source: http://stackoverflow.com/questions/268321/bidirectional-1-to-1-dictionary-in-c-sharp
/// <summary>
/// This is a dictionary guaranteed to have only one of each value and key.
/// It may be searched either by TFirst or by TSecond, giving a unique answer because it is 1 to 1.
/// </summary>
/// <typeparam name="TFirst">The type of the "key"</typeparam>
/// <typeparam name="TSecond">The type of the "value"</typeparam>
using System.Collections.Generic;
using System;
@Larry57
Larry57 / TeaFileBinarySearch.cs
Created December 13, 2013 21:15
This extension method performs a BinarySearch in a TeaFile file. http://discretelogics.com/teafiles
public static int BinarySearch<T, U>(this TeaFile<T> tf, U target, Func<T, U> indexer) where T : struct
{
var lo = 0;
var hi = (int)tf.Count - 1;
var comp = Comparer<U>.Default;
while(lo <= hi)
{
var median = lo + (hi - lo >> 1);
var num = comp.Compare(indexer(tf.Items[median]), target);
@Larry57
Larry57 / TreeViewExtensions.cs
Last active December 29, 2015 11:39
Save / Restore the TreeView nodes expansion state before and after a Refresh.
using System.Collections.Generic;
using System.Linq;
namespace System.Windows.Forms
{
internal static class TreeViewExtensions
{
internal static List<string> GetExpansionState(this TreeNodeCollection nodes)
{
return nodes.Descendants()
@Larry57
Larry57 / ZedGraphExtensions.cs
Last active December 28, 2015 15:49
ZedGraph extensions. - Independant Pan and Zoom Y axes - YAxis order change in GraphPane - GraphPane order change in MasterPane
public static class ZedGraphExtensions
{
/// <summary>
/// Change the Y axes order
/// </summary>
/// <param name="list">YAxisList</param>
/// <param name="pane">GraphPane</param>
/// <param name="index">index</param>
/// <param name="relativePos">relative position</param>
/// <returns>new index</returns>
@Larry57
Larry57 / GetUniqueKey.cs
Created October 17, 2013 07:41
Returns a new unique key from an existing IEnumerable<string>. The key is based on a prefix followed by a number. For example, in "Customer_0", "Customer_1", "Customer_4"..., GetUniqueKey will return "Customer_2".
public static class Extensions
{
public static string GetUniqueKey(this IEnumerable<string> keys, string prefix, StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase)
{
int count = 0;
while (keys.FirstOrDefault(k => k.StartsWith(prefix + count, comparisonType)) != null)
count++;
return prefix + count;
}
@Larry57
Larry57 / MultiSelectTreeView.cs
Created October 15, 2013 11:47
Multi select TreeView found on Code Project - http://www.codeproject.com/Articles/20581/Multiselect-Treeview-Implementation. This nice little piece of work requires some attention.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Graph.UI.Tools
{
public class MultiSelectTreeview : TreeView
{
#region Selected Node(s) Properties
@Larry57
Larry57 / ini.cs
Last active February 21, 2024 05:05
A single class to read and write INI files.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
public class Ini
{
Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>(StringComparer.InvariantCultureIgnoreCase);
string file;
@Larry57
Larry57 / arrangeItems.cs
Last active December 17, 2015 15:19
Arrange items in a panel like a thumbnails manager
public static void Arrange(Control container, int horizontalGap, int verticalGap, float aspectRatio)
{
var H = container.Height;
var W = container.Width;
var N = container.Controls.OfType<Control>().Count(c => c.Visible);
if (N == 0)
return;
var bestSizedItem = (