1 /** DGui project file. 2 3 Copyright: Trogu Antonio Davide 2011-2013 4 5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). 6 7 Authors: Trogu Antonio Davide 8 */ 9 module dgui.core.collection; 10 11 class Collection(T) 12 { 13 private T[] _t; 14 15 public final int add(T t) 16 { 17 this._t ~= t; 18 return this._t.length - 1; 19 } 20 21 public final void clear() 22 { 23 this._t.length = 0; 24 } 25 26 public final T[] get() 27 { 28 return this._t; 29 } 30 31 @property public final int length() 32 { 33 return this._t.length; 34 } 35 36 public final void remove(T t) 37 { 38 this.removeAt(this.find(t)); 39 } 40 41 public final void removeAt(int idx) 42 { 43 int x = 0; 44 T[] newT = new T[this._t.length - 1]; 45 46 foreach(int i, T t; this._t) 47 { 48 if(i != idx) 49 { 50 newT[x] = t; 51 x++; 52 } 53 } 54 55 this._t = newT; 56 } 57 58 public final int find(T t) 59 { 60 foreach(int i, T ft; this._t) 61 { 62 if(ft is t) 63 { 64 return i; 65 } 66 } 67 68 return -1; 69 } 70 71 public T opIndex(int i) nothrow 72 { 73 if(i >= 0 && i < this._t.length) 74 { 75 return this._t[i]; 76 } 77 78 assert(false, "Index out of range"); 79 } 80 81 public int opApply(int delegate(ref T) dg) 82 { 83 int res = 0; 84 85 if(this._t.length) 86 { 87 for(int i = 0; i < this._t.length; i++) 88 { 89 res = dg(this._t[i]); 90 91 if(res) 92 { 93 break; 94 } 95 } 96 } 97 98 return res; 99 } 100 101 public int opApply(int delegate(ref int, ref T) dg) 102 { 103 int res = 0; 104 105 if(this._t.length) 106 { 107 for(int i = 0; i < this._t.length; i++) 108 { 109 res = dg(i, this._t[i]); 110 111 if(res) 112 { 113 break; 114 } 115 } 116 } 117 118 return res; 119 } 120 }