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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
1 // Written in the D programming language
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
2 // www.digitalmars.com/d/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
3
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
4 /*
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
5 * The contents of this file are subject to the Mozilla Public License Version
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
6 * 1.1 (the "License"); you may not use this file except in compliance with
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
7 * the License. You may obtain a copy of the License at
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
8 * http://www.mozilla.org/MPL/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
9 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
10 * Software distributed under the License is distributed on an "AS IS" basis,
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
12 * for the specific language governing rights and limitations under the
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
13 * License.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
14 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
15 * The Original Code is the Dynamin library.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
16 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
17 * The Initial Developer of the Original Code is Jordan Miner.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
19 * the Initial Developer. All Rights Reserved.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
20 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
21 * Contributor(s):
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
22 * Jordan Miner <jminer7@gmail.com>
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
23 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
24 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
25
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
26 module dynamin.core.list;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
27
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
28 import dynamin.core.global;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
29 import dynamin.core.string;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
30 import dynamin.core.math;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
31 import tango.io.Stdout;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
32
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
33 // TODO: QuickSearch()
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
34 // TODO: BinarySearch()
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
35 // MUST use (high + low) >>> 1 to find the average
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
36 // TODO: Search()
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
37 // TODO: QuickSort()
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
38 // TODO: HeapSort()
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
58 protected:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
59 T[] _data;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
113 list._data = arr.dup;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
114 list._count = arr.length;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
115 return list;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
116 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
117 uint count() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
118 return _count;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
119 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
120 uint capacity() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
121 return _data.length;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
122 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
123 T[] toArray() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
124 return _data[0.._count].dup;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
125 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
126 T[] data() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
127 return _data[0.._count];
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
128 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
129 /*string toString() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
130 string str = "[";
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
131 if(Count > 0)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
132 str ~= ToString(this[0]);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
133 foreach(item; this) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
134 str ~= ", ";
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
135 str ~= ToString(item);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
136 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
137 str ~= "]";
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
138 return str;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
139 }*/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
140 protected void maybeEnlarge(uint neededCap) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
141 if(neededCap <= capacity)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
142 return;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
143 _data.length = max(neededCap, (capacity+1)*2);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
144 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
145 T opIndex(uint index) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
146 return _data[0.._count][index];
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
154 void push(T item) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
155 add(item);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
156 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
157 T pop() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
160 T item = _data[_count-1];
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
161 // must null out to allow to be collected
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
162 static if(is(T == class) || is(T == interface))
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
163 _data[_count-1] = cast(T)null;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
167 return item;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
168 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
171 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
172 void remove(T item) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
173 uint i = find(item);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
174 if(i == -1)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
183 // must null out to allow to be collected
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
192 maybeEnlarge(_count+1);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
193 arrayCopy!(T)(_data, index, _data, index+1, _count - index);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
194 _data[index] = item;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
198 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
207 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
208 uint find(T item) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
209 foreach(i, item2; _data)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
210 if(item == item2) // if(item2 == item) would crash on a null item
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
211 return i;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
212 return -1;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
213 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
214 //trimCapacity()
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
215 //opConcat
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
216 //opEquals
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
217 //opSlice
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
218 //opSliceAssign
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
219 int opApply(int delegate(inout T item) dg) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
220 for(uint i = 0; i < _count; ++i) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
221 if(int result = dg(_data[i]))
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
222 return result;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
223 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
224 return 0;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
225 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
226 //so you can do:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
227 //foreach(i, item; list)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
228 int opApply(int delegate(inout uint index, inout T item) dg) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
229 for(uint i = 0; i < _count; ++i) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
230 if(int result = dg(i, _data[i]))
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
231 return result;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
232 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
233 return 0;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
234 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
235
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
236 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
237 unittest {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
238 auto list = List!(char).fromArray("Hello, Mat");
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
239 list.add('t');
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
240 assert(list.data == "Hello, Matt");
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
253 list.clear();
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
291 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
292