Mercurial > projects > dynamin
annotate dynamin/core/list.d @ 113:4636a64afabc default tip
Add reverse() function.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 19 Jan 2013 21:08:52 -0600 |
parents | acdbb30fee7e |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
87
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
87
diff
changeset
|
5 * Distributed under the Boost Software License, Version 1.0. |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
87
diff
changeset
|
6 * (See accompanying file BOOST_LICENSE.txt or copy at |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
87
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.list; | |
12 | |
13 import dynamin.core.global; | |
14 import dynamin.core.string; | |
15 import dynamin.core.math; | |
16 import tango.io.Stdout; | |
17 | |
18 // TODO: QuickSearch() | |
19 // TODO: BinarySearch() | |
20 // MUST use (high + low) >>> 1 to find the average | |
21 // TODO: Search() | |
22 // TODO: QuickSort() | |
23 // TODO: HeapSort() | |
24 // 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
|
25 |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
26 /// 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
|
27 enum ListChangeType { |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
28 /// 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
|
29 Added, |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
30 /// 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
|
31 Removed, |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
32 /// 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
|
33 Replaced |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
34 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
35 |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
36 /** |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
37 * 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
|
38 * 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
|
39 * 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
|
40 * 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
|
41 */ |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
42 class List(T, bool changeNotification = false) { |
0 | 43 protected: |
44 T[] _data; | |
45 uint _count; | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
46 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
47 void delegate(ListChangeType, T, T, uint) whenChanged; |
106 | 48 enum int DefaultCapacity = 16; |
0 | 49 public: |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
50 /** |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
51 * 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
|
52 * |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
53 * 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
|
54 */ |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
55 this() { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
56 static if(changeNotification) |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
57 throw new Exception("not available"); |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
58 this(DefaultCapacity); |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
59 } |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
60 /// ditto |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
61 this(uint capacity) { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
62 static if(changeNotification) |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
63 throw new Exception("not available"); |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
64 _data = new T[capacity]; |
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 |
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 * 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
|
69 * 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
|
70 * 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
|
71 * 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
|
72 * 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
|
73 * 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
|
74 * 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
|
75 * |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
76 * 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
|
77 */ |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
78 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
|
79 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
|
80 this(DefaultCapacity, whenChanged); |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
81 } else { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
82 throw new Exception("not available"); |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
83 } |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
84 } |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
85 /// ditto |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
86 this(uint capacity, |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
87 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
|
88 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
|
89 _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
|
90 this.whenChanged = whenChanged; |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
91 } else { |
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
92 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
|
93 } |
0 | 94 } |
84
7653269724db
Add comments for the List class and its constructors.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
95 |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
96 static List!(T) fromArray(T[] arr...) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
97 auto list = new List!(T)(); |
0 | 98 list._data = arr.dup; |
99 list._count = arr.length; | |
100 return list; | |
101 } | |
102 uint count() { | |
103 return _count; | |
104 } | |
105 uint capacity() { | |
106 return _data.length; | |
107 } | |
108 T[] toArray() { | |
109 return _data[0.._count].dup; | |
110 } | |
111 T[] data() { | |
112 return _data[0.._count]; | |
113 } | |
114 /*string toString() { | |
106 | 115 mstring str = "["; |
0 | 116 if(Count > 0) |
106 | 117 str ~= toString(this[0]); |
0 | 118 foreach(item; this) { |
119 str ~= ", "; | |
106 | 120 str ~= toString(item); |
0 | 121 } |
122 str ~= "]"; | |
123 return str; | |
124 }*/ | |
125 protected void maybeEnlarge(uint neededCap) { | |
126 if(neededCap <= capacity) | |
127 return; | |
128 _data.length = max(neededCap, (capacity+1)*2); | |
129 } | |
106 | 130 ref T opIndex(uint index) { |
0 | 131 return _data[0.._count][index]; |
132 } | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
133 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
|
134 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
|
135 _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
|
136 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
137 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
|
138 } |
0 | 139 void push(T item) { |
140 add(item); | |
141 } | |
142 T pop() { | |
143 if(_count < 1) | |
56
c2566ab82535
Edit comments and fix a typo.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
144 throw new Exception("List.pop() - List is empty"); |
0 | 145 T item = _data[_count-1]; |
146 // must null out to allow to be collected | |
87
7cf356729515
Set removed items in a List to T.init. T could be a struct containing a pointer
Jordan Miner <jminer7@gmail.com>
parents:
84
diff
changeset
|
147 _data[_count-1] = T.init; |
0 | 148 --_count; |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
149 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
150 whenChanged(ListChangeType.Removed, item, T.init, _count); |
0 | 151 return item; |
152 } | |
153 void add(T item) { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
154 insert(item, _count); |
0 | 155 } |
156 void remove(T item) { | |
157 uint i = find(item); | |
158 if(i == -1) | |
159 return; | |
81
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
160 removeAt(i); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
161 } |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
162 void removeAt(uint index) { |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
163 T item = _data[index]; |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
164 |
81
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
165 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
|
166 _data[i-1] = _data[i]; |
0 | 167 // must null out to allow to be collected |
87
7cf356729515
Set removed items in a List to T.init. T could be a struct containing a pointer
Jordan Miner <jminer7@gmail.com>
parents:
84
diff
changeset
|
168 _data[_count-1] = T.init; |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
169 --_count; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
170 |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
171 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
172 whenChanged(ListChangeType.Removed, item, T.init, index); |
0 | 173 } |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
174 void insert(T item, uint index) { |
0 | 175 maybeEnlarge(_count+1); |
176 arrayCopy!(T)(_data, index, _data, index+1, _count - index); | |
177 _data[index] = item; | |
178 ++_count; | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
179 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
180 whenChanged(ListChangeType.Added, T.init, item, index); |
0 | 181 } |
182 void clear() { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
183 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
|
184 static if(changeNotification) |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
185 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
|
186 // must null out to allow to be collected |
87
7cf356729515
Set removed items in a List to T.init. T could be a struct containing a pointer
Jordan Miner <jminer7@gmail.com>
parents:
84
diff
changeset
|
187 data[_count-1] = T.init; |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
188 } |
0 | 189 } |
190 uint find(T item) { | |
191 foreach(i, item2; _data) | |
192 if(item == item2) // if(item2 == item) would crash on a null item | |
193 return i; | |
194 return -1; | |
195 } | |
196 //trimCapacity() | |
197 //opConcat | |
198 //opEquals | |
199 //opSlice | |
200 //opSliceAssign | |
106 | 201 int opApply(int delegate(ref T item) dg) { |
0 | 202 for(uint i = 0; i < _count; ++i) { |
203 if(int result = dg(_data[i])) | |
204 return result; | |
205 } | |
206 return 0; | |
207 } | |
208 //so you can do: | |
209 //foreach(i, item; list) | |
106 | 210 int opApply(int delegate(ref uint index, ref T item) dg) { |
0 | 211 for(uint i = 0; i < _count; ++i) { |
212 if(int result = dg(i, _data[i])) | |
213 return result; | |
214 } | |
215 return 0; | |
216 } | |
217 | |
218 } | |
219 unittest { | |
106 | 220 auto list = List!(char).fromArray("Hello, Mat".dup); |
0 | 221 list.add('t'); |
222 assert(list.data == "Hello, Matt"); | |
223 assert(list.pop() == 't'); | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
224 assert(list.data == "Hello, Mat"); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
225 list.insert('!', 5); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
226 assert(list.data == "Hello!, Mat"); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
227 list.remove('l'); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
228 assert(list.data == "Helo!, Mat"); |
81
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
229 list.removeAt(0); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
230 assert(list.data == "elo!, Mat"); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
231 list.removeAt(8); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
232 assert(list.data == "elo!, Ma"); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
233 list.removeAt(1); |
de94552446ab
Add List.removeAt() and tests for it.
Jordan Miner <jminer7@gmail.com>
parents:
80
diff
changeset
|
234 assert(list.data == "eo!, Ma"); |
0 | 235 list.clear(); |
236 assert(list.data == ""); | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
237 |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 a++; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
242 assert(o == "" && n != ""); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
243 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
244 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
|
245 r++; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
246 assert(n == "" && o != ""); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
247 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
248 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
|
249 r2++; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
250 } |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
251 }; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
252 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
|
253 assert(a == 0 && r == 0 && r2 == 0); |
0 | 254 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
|
255 assert(a == 1 && r == 0 && r2 == 0); |
0 | 256 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
|
257 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
|
258 |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
259 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
|
260 list2.add("Jacob"); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
261 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
|
262 list2[1] = "Matt"; |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
263 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
|
264 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
|
265 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
|
266 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
|
267 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
|
268 list2.removeAt(0); |
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
269 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
|
270 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
|
271 list2.clear(); |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
81
diff
changeset
|
272 assert(a == 5 && r == 5 && r2 == 1); |
0 | 273 } |
274 |