Skip to content

Instantly share code, notes, and snippets.

@kpol
kpol / Merge.cs
Created March 5, 2018 22:24
Merger two sorted arrays.
/// <summary>
/// Merge two sorted arrays.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static IEnumerable<int> Merge(int[] a, int[] b)
{
int i = 0;
int j = 0;
@kpol
kpol / GetAllPermutations.cs
Last active February 13, 2018 09:14
Gets all permutations. Heap's algorithm.
/// <summary>
/// Heap's algorithm.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static IEnumerable<T[]> GetAllPermutations<T>(IEnumerable<T> source)
{
var result = source.ToArray();
@kpol
kpol / BinarySearchTree.cs
Last active February 11, 2018 22:49
BinarySearchTree C# implementation
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication10
{
public class BinarySearchTreeSet<T> : ICollection<T>
{
private readonly IComparer<T> _comparer;
/// <summary>
/// Gets value from DataReader. Handles <see cref="DBNull"/>.
/// </summary>
/// <typeparam name="T">Any type including <see cref="Nullable{T}"/> values.</typeparam>
/// <param name="reader"></param>
/// <param name="columnName"></param>
/// <returns></returns>
public static T GetValue<T>(this IDataReader reader, string columnName)
{
var value = reader[columnName];
@kpol
kpol / EnumerableExtensions.cs
Last active February 6, 2018 22:14
Enumerable Extensions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace LinqExtensions
{
public static class EnumerableExtensions
{
/// <summary>
@kpol
kpol / TagGeneratorPlugin.cs
Created October 2, 2012 23:51
SpecFlow custom generator plugin
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using BoDi;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Generator.Configuration;
using TechTalk.SpecFlow.Generator.Plugins;
using TechTalk.SpecFlow.Generator.UnitTestConverter;
using TechTalk.SpecFlow.Generator.UnitTestProvider;
@kpol
kpol / gist:3752921
Created September 19, 2012 23:06
CombineHashCodes
public static int CombineHashCodes(params int[] hashCodes)
{
if (hashCodes == null)
{
throw new ArgumentNullException("hashCodes");
}
if (hashCodes.Length == 0)
{
throw new IndexOutOfRangeException();
@kpol
kpol / ValidateJsonAntiForgeryTokenAttribute.cs
Created August 2, 2012 00:39
ASP.NET MVC 2 -- ValidateAntiForgeryTokenAttribute for JSON requests
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ValidateJsonAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}