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 dguihub.core.collection;
10 
11 class Collection(T) {
12    private T[] _t;
13 
14    public final int add(T t) {
15       this._t ~= t;
16       return this._t.length - 1;
17    }
18 
19    public final void clear() {
20       this._t.length = 0;
21    }
22 
23    public final T[] get() {
24       return this._t;
25    }
26 
27    @property public final int length() {
28       return this._t.length;
29    }
30 
31    public final void remove(T t) {
32       this.removeAt(this.find(t));
33    }
34 
35    public final void removeAt(int idx) {
36       int x = 0;
37       T[] newT = new T[this._t.length - 1];
38 
39       foreach (int i, T t; this._t) {
40          if (i != idx) {
41             newT[x] = t;
42             x++;
43          }
44       }
45 
46       this._t = newT;
47    }
48 
49    public final int find(T t) {
50       foreach (int i, T ft; this._t) {
51          if (ft is t) {
52             return i;
53          }
54       }
55 
56       return -1;
57    }
58 
59    public T opIndex(int i) nothrow {
60       if (i >= 0 && i < this._t.length) {
61          return this._t[i];
62       }
63 
64       assert(false, "Index out of range");
65    }
66 
67    public int opApply(int delegate(ref T) dg) {
68       int res = 0;
69 
70       if (this._t.length) {
71          for (int i = 0; i < this._t.length; i++) {
72             res = dg(this._t[i]);
73 
74             if (res) {
75                break;
76             }
77          }
78       }
79 
80       return res;
81    }
82 
83    public int opApply(int delegate(ref int, ref T) dg) {
84       int res = 0;
85 
86       if (this._t.length) {
87          for (int i = 0; i < this._t.length; i++) {
88             res = dg(i, this._t[i]);
89 
90             if (res) {
91                break;
92             }
93          }
94       }
95 
96       return res;
97    }
98 }