Skip to content

Instantly share code, notes, and snippets.

View stanroze's full-sized avatar
😵‍💫

Stan Rozenberg stanroze

😵‍💫
View GitHub Profile
public class Test
{
}
LICENSE_TAXONOMY_TEMPLATE_ID
bool hasLicensingTaxonomy = baseTemplates.Any(o => (o.ID.Equals(LICENSE_TAXONOMY_TEMPLATE_ID)));
@stanroze
stanroze / simpleDbLayer.cs
Created April 7, 2013 18:06
Simple DB layer example, if not possible to use ORM or domain-driven design.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DbLayerSample
{
class Program
{
static void Main(string[] args)
public double Sqrt(double a, int times)
{
if (a < 0)
throw new Exception("Can not sqrt a negative number");
double x = 1;
while (times-- > 0)
x = x / 2 + a / (2 * x);
return x;
}
@stanroze
stanroze / Sqrt.cs
Created June 14, 2013 15:35
Binary Search of Square Root
public static decimal Sqrt(decimal i)
{
decimal lo = 0;
decimal hi = i/2;
while(lo < hi)
{
decimal mid = lo + (hi-lo)/2;
if(mid * mid > i )
hi = mid - 1;
else if(mid * mid < i)
@stanroze
stanroze / SimpleAsyncHost.cs
Created April 17, 2014 20:12
Simple async HttpListener implementation.
public class SimpleAsyncHost
{
private HttpListener _httpListener;
private bool _stop;
public SimpleAsyncHost()
{
_httpListener = new HttpListener();
_httpListener.Prefixes.Add("http://localhost:10090/");
@stanroze
stanroze / TaskTimeoutExtension.cs
Last active August 29, 2015 14:00
Task with TimeOut extension.
public static class TaskExtentions
{
public static async Task<T> WithTimeout<T>(this Task<T> task, int timeout)
{
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
return await task;
return default(T);
}
}
@stanroze
stanroze / Program.cs
Created April 23, 2014 14:14
simple async server and client.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
@stanroze
stanroze / reversestr.cs
Last active August 29, 2015 14:16
Reverse string in place, wouldn't work for UTF-16
public string ReverseWords(string text)
{
var tarr = text.ToCharArray();
//reverse the whole string first
reverse(tarr, 0, tarr.Length-1);
//reverse parts of the string
int start = 0;
for(int i = 0; i<tarr.Length; i++)
{
@stanroze
stanroze / stupiddict.cs
Last active August 29, 2015 14:25
Stupid dictionary implementation. Does not resize, does not implement anything other than Add.
public class StupidDictionary<T>
{
private readonly int _numOfBuckets = 3;
private readonly bool _quiet;
private readonly int _capacity;
private int _totalCollisions;
private int[] _buckets;
private Slot[] _slots;
private int _lastIndex;
private decimal _count;