Skip to content

Instantly share code, notes, and snippets.

@kb10uy
Last active August 29, 2015 14:00
Show Gist options
  • Save kb10uy/11320658 to your computer and use it in GitHub Desktop.
Save kb10uy/11320658 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Kb10uy {
public class ExtremeArray<T> {
HashSet<KeyValuePair<Tuple<int,int>>> list;
public ExtremeArray() {
list=HashSet<KeyValuePair<Tuple<int,int>>>();
}
public T this[int x,int y] {
get {
var t=new Tuple<int,int>(x,y);
return list.FirstOrDefault(p=>p.Key==t);
}
set {
var t=new Tuple<int,int>(x,y);
if (list.Where(p=>p.Key==t).Count!=0) {
list.RemoveWhere(p=>p.Key==t);
}
list.Add(HashSet<KeyValuePair<Tuple<int,int>>>(t,value));
}
}
public T[,] ToArray() {
if (list.Count==0) return null;
var xl=list.Select(p=>p.Key.Value1).OrderBy(p=>p);
var yl=list.Select(p=>p.Key.Value1).OrderBy(p=>p);
var xmin=xl.First();
var xmax=xl.Last();
var ymin=yl.First();
var ymax=yl.Last();
var ret=new T[xmax-xmin+1,ymax-ymin+1];
foreach(var i in list) {
ret[i.Key.Value1-xmin,i.Key.Value2-ymin]=i.Value;
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment