Mercurial > projects > dynamin
annotate dynamin/core/list.d @ 84:7653269724db
Add comments for the List class and its constructors.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sun, 18 Jul 2010 23:45:00 -0500 |
parents | 3cfc83a99cbc |
children | 7cf356729515 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.core.list; | |
27 | |
28 import dynamin.core.global; | |
29 import dynamin.core.string; | |
30 import dynamin.core.math; | |
31 import tango.io.Stdout; | |
32 | |
33 // TODO: QuickSearch() | |
34 // TODO: BinarySearch() | |
35 // MUST use (high + low) >>> 1 to find the average | |
36 // TODO: Search() | |
37 // TODO: QuickSort() | |
38 // TODO: HeapSort() | |
39 // TODO: Sort() - calls HeapSort() so stable sort is default | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
40 |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
41 /// Represents the ways items can be changed in a list. |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
42 enum ListChangeType { |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
43 /// An item was added to the list. |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
44 Added, |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
45 /// An item was removed from the list. |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
46 Removed, |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
47 /// An item was replaced with another item. |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
48 Replaced |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
49 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
50 |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
51 /** |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
52 * A list of items backed by an array. If changeNotification is true, then a delegate is passed |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
53 * into the constructor as a callback. The delegate will be called when any item is added to or |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
54 * removed from the list. Also, when changeNotfication is true, some methods are not |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
55 * available. These are marked in their documentation. |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
56 */ |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
57 class List(T, bool changeNotification = false) { |
0 | 58 protected: |
59 T[] _data; | |
60 uint _count; | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
61 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
62 void delegate(ListChangeType, T, T, uint) whenChanged; |
80
f1e176a76c00
Rearrange List constructors and add a constant for the default capacity.
Jordan Miner <jminer7@gmail.com>
parents:
56
diff
changeset
|
63 const int DefaultCapacity = 16; |
0 | 64 public: |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
65 /** |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
66 * Creates a list with the specified capacity. |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
67 * |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
68 * Only available if changeNotification is false. |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
69 */ |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
70 this() { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
71 static if(changeNotification) |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
72 throw new Exception("not available"); |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
73 this(DefaultCapacity); |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
74 } |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
75 /// ditto |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
76 this(uint capacity) { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
77 static if(changeNotification) |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
78 throw new Exception("not available"); |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
79 _data = new T[capacity]; |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
80 } |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
81 |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
82 /** |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
83 * Creates a list with the specified capacity and with a delegate that will be called |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
84 * when an item is added to or removed from the list. The type specifies whether an |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
85 * item was added, removed, or replaced. oldItem contains the item that was removed, |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
86 * and newItem contains the item that was added. So if type is ListChangeType.Added, |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
87 * oldItem will be T.init (which is null for reference types). If type is |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
88 * ListChangeType.Removed, newItem will be T.init. The index of the item after being |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
89 * added or before being removed is also passed to the delegate. |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
90 * |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
91 * Only available if changeNotification is true. |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
92 */ |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
93 this(void delegate(ListChangeType type, T oldItem, T newItem, uint index) whenChanged) { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
94 static if(changeNotification) { |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
95 this(DefaultCapacity, whenChanged); |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
96 } else { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
97 throw new Exception("not available"); |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
98 } |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
99 } |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
100 /// ditto |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
101 this(uint capacity, |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
102 void delegate(ListChangeType type, T oldItem, T newItem, uint index) whenChanged) { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
103 static if(changeNotification) { |
80
f1e176a76c00
Rearrange List constructors and add a constant for the default capacity.
Jordan Miner <jminer7@gmail.com>
parents:
56
diff
changeset
|
104 _data = new T[capacity]; |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
105 this.whenChanged = whenChanged; |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
106 } else { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
107 throw new Exception("not available"); |
80
f1e176a76c00
Rearrange List constructors and add a constant for the default capacity.
Jordan Miner <jminer7@gmail.com>
parents:
56
diff
changeset
|
108 } |
0 | 109 } |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
110 |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
111 static List!(T) fromArray(T[] arr...) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
112 auto list = new List!(T)(); |
0 | 113 list._data = arr.dup; |
114 list._count = arr.length; | |
115 return list; | |
116 } | |
117 uint count() { | |
118 return _count; | |
119 } | |
120 uint capacity() { | |
121 return _data.length; | |
122 } | |
123 T[] toArray() { | |
124 return _data[0.._count].dup; | |
125 } | |
126 T[] data() { | |
127 return _data[0.._count]; | |
128 } | |
129 /*string toString() { | |
130 string str = "["; | |
131 if(Count > 0) | |
132 str ~= ToString(this[0]); | |
133 foreach(item; this) { | |
134 str ~= ", "; | |
135 str ~= ToString(item); | |
136 } | |
137 str ~= "]"; | |
138 return str; | |
139 }*/ | |
140 protected void maybeEnlarge(uint neededCap) { | |
141 if(neededCap <= capacity) | |
142 return; | |
143 _data.length = max(neededCap, (capacity+1)*2); | |
144 } | |
145 T opIndex(uint index) { | |
146 return _data[0.._count][index]; | |
147 } | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
148 void opIndexAssign(T item, uint index) { |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
149 T oldItem = _data[0.._count][index]; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
150 _data[0.._count][index] = item; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
151 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
152 whenChanged(ListChangeType.Replaced, oldItem, item, index); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
153 } |
0 | 154 void push(T item) { |
155 add(item); | |
156 } | |
157 T pop() { | |
158 if(_count < 1) | |
56
c2566ab82535
Edit comments and fix a typo.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
159 throw new Exception("List.pop() - List is empty"); |
0 | 160 T item = _data[_count-1]; |
161 // must null out to allow to be collected | |
162 static if(is(T == class) || is(T == interface)) | |
163 _data[_count-1] = cast(T)null; | |
164 --_count; | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
165 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
166 whenChanged(ListChangeType.Removed, item, T.init, _count); |
0 | 167 return item; |
168 } | |
169 void add(T item) { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
170 insert(item, _count); |
0 | 171 } |
172 void remove(T item) { | |
173 uint i = find(item); | |
174 if(i == -1) | |
175 return; | |
81
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
176 removeAt(i); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
177 } |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
178 void removeAt(uint index) { |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
179 T item = _data[index]; |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
180 |
81
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
181 for(uint i = index + 1; i < _count; ++i) |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
182 _data[i-1] = _data[i]; |
0 | 183 // must null out to allow to be collected |
184 static if(is(T == class) || is(T == interface)) | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
185 _data[_count-1] = cast(T)null; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
186 --_count; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
187 |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
188 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
189 whenChanged(ListChangeType.Removed, item, T.init, index); |
0 | 190 } |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
191 void insert(T item, uint index) { |
0 | 192 maybeEnlarge(_count+1); |
193 arrayCopy!(T)(_data, index, _data, index+1, _count - index); | |
194 _data[index] = item; | |
195 ++_count; | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
196 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
197 whenChanged(ListChangeType.Added, T.init, item, index); |
0 | 198 } |
199 void clear() { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
200 for(; _count > 0; --_count) { |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
201 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
202 whenChanged(ListChangeType.Removed, _data[_count-1], T.init, _count-1); |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
203 // must null out to allow to be collected |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
204 static if(is(T == class) || is(T == interface)) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
205 data[_count-1] = cast(T)null; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
206 } |
0 | 207 } |
208 uint find(T item) { | |
209 foreach(i, item2; _data) | |
210 if(item == item2) // if(item2 == item) would crash on a null item | |
211 return i; | |
212 return -1; | |
213 } | |
214 //trimCapacity() | |
215 //opConcat | |
216 //opEquals | |
217 //opSlice | |
218 //opSliceAssign | |
219 int opApply(int delegate(inout T item) dg) { | |
220 for(uint i = 0; i < _count; ++i) { | |
221 if(int result = dg(_data[i])) | |
222 return result; | |
223 } | |
224 return 0; | |
225 } | |
226 //so you can do: | |
227 //foreach(i, item; list) | |
228 int opApply(int delegate(inout uint index, inout T item) dg) { | |
229 for(uint i = 0; i < _count; ++i) { | |
230 if(int result = dg(i, _data[i])) | |
231 return result; | |
232 } | |
233 return 0; | |
234 } | |
235 | |
236 } | |
237 unittest { | |
238 auto list = List!(char).fromArray("Hello, Mat"); | |
239 list.add('t'); | |
240 assert(list.data == "Hello, Matt"); | |
241 assert(list.pop() == 't'); | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
242 assert(list.data == "Hello, Mat"); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
243 list.insert('!', 5); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
244 assert(list.data == "Hello!, Mat"); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
245 list.remove('l'); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
246 assert(list.data == "Helo!, Mat"); |
81
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
247 list.removeAt(0); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
248 assert(list.data == "elo!, Mat"); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
249 list.removeAt(8); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
250 assert(list.data == "elo!, Ma"); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
251 list.removeAt(1); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
252 assert(list.data == "eo!, Ma"); |
0 | 253 list.clear(); |
254 assert(list.data == ""); | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
255 |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
256 int a = 0, r = 0, r2 = 0; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
257 void changed(ListChangeType t, string o, string n, uint) { |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
258 if(t == ListChangeType.Added) { |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
259 a++; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
260 assert(o == "" && n != ""); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
261 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
262 if(t == ListChangeType.Removed) { |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
263 r++; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
264 assert(n == "" && o != ""); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
265 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
266 if(t == ListChangeType.Replaced) { |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
267 r2++; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
268 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
269 }; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
270 auto list2 = new List!(string, true)(&changed); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
271 assert(a == 0 && r == 0 && r2 == 0); |
0 | 272 list2.add("hello"); |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
273 assert(a == 1 && r == 0 && r2 == 0); |
0 | 274 assert(list2.pop() == "hello"); |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
275 assert(a == 1 && r == 1 && r2 == 0); |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
276 |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
277 list2.add("Hi"); |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
278 list2.add("Jacob"); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
279 assert(a == 3 && r == 1 && r2 == 0); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
280 list2[1] = "Matt"; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
281 assert(a == 3 && r == 1 && r2 == 1); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
282 list2.insert("John", 1); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
283 list2.insert("Pete", 3); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
284 assert(list2.data == ["Hi", "John", "Matt", "Pete"]); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
285 assert(a == 5 && r == 1 && r2 == 1); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
286 list2.removeAt(0); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
287 assert(list2.data == ["John", "Matt", "Pete"]); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
288 assert(a == 5 && r == 2 && r2 == 1); |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
289 list2.clear(); |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
290 assert(a == 5 && r == 5 && r2 == 1); |
0 | 291 } |
292 |