
Untitled
By: a guest on
Jul 15th, 2012 | syntax:
None | size: 1.43 KB | hits: 11 | expires: Never
Most lightweight .NET collection
public class Stack<T>{
readonly T _head;
readonly Stack<T> _tail;
public Stack(T head,Stack<T> tail){
_head = head;
_tail = tail;
}
public Stack<T> Push(T element){
return new Stack<T>(element,this);
}
public IEnumerator<T> GetEnumerator(){
yield return _head;
var current = _tail;
while(_tail != null){
yield return current._head;
current = current._tail;
}
}
}
public class Stack<T>{
T[] _heads;
T[] _tail;
int _next;
public Stack(T head,T[] tail){
_heads = new T[tail.length];
_next = _heads.Length -2;
_heads[_heads.Length -1] = head;
_tail = tail;
}
public void Push(T element){
if(_next < 0){
var length = _heads.Length;
var _new = new T[length * 2];
_heads = new T[length * 2];
_next = length * 2-1;
Array.Copy(_heads,_new,length);
Array.Copy(_tails,0,_new,length,length);
_tails = _new;
} else{
_heads[_next--] = element;
}
}
public IEnumerator<T> GetEnumerator(){
yield return _head;
var current = _tail;
while(_tail != null){
yield return current._head;
current = current._tail;
}
}
}
var myArray = new int[10];
myArray[0] = 123;
myArray[1] = 234;