Guest User

Untitled

a guest
Jul 15th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. Most lightweight .NET collection
  2. public class Stack<T>{
  3. readonly T _head;
  4. readonly Stack<T> _tail;
  5. public Stack(T head,Stack<T> tail){
  6. _head = head;
  7. _tail = tail;
  8. }
  9.  
  10. public Stack<T> Push(T element){
  11. return new Stack<T>(element,this);
  12. }
  13.  
  14. public IEnumerator<T> GetEnumerator(){
  15. yield return _head;
  16. var current = _tail;
  17. while(_tail != null){
  18. yield return current._head;
  19. current = current._tail;
  20. }
  21. }
  22. }
  23.  
  24. public class Stack<T>{
  25. T[] _heads;
  26. T[] _tail;
  27. int _next;
  28. public Stack(T head,T[] tail){
  29. _heads = new T[tail.length];
  30. _next = _heads.Length -2;
  31. _heads[_heads.Length -1] = head;
  32. _tail = tail;
  33. }
  34.  
  35. public void Push(T element){
  36. if(_next < 0){
  37. var length = _heads.Length;
  38. var _new = new T[length * 2];
  39. _heads = new T[length * 2];
  40. _next = length * 2-1;
  41. Array.Copy(_heads,_new,length);
  42. Array.Copy(_tails,0,_new,length,length);
  43. _tails = _new;
  44. } else{
  45. _heads[_next--] = element;
  46. }
  47. }
  48.  
  49. public IEnumerator<T> GetEnumerator(){
  50. yield return _head;
  51. var current = _tail;
  52. while(_tail != null){
  53. yield return current._head;
  54. current = current._tail;
  55. }
  56. }
  57. }
  58.  
  59. var myArray = new int[10];
  60. myArray[0] = 123;
  61. myArray[1] = 234;
Advertisement
Add Comment
Please, Sign In to add comment