comparison dynamin/core/list.d @ 106:acdbb30fee7e

Port to D2. Most of the effort was dealing with immutable and const.
author Jordan Miner <jminer7@gmail.com>
date Mon, 17 Dec 2012 23:41:50 -0600
parents 73060bc3f004
children
comparison
equal deleted inserted replaced
105:97997a544ac0 106:acdbb30fee7e
43 protected: 43 protected:
44 T[] _data; 44 T[] _data;
45 uint _count; 45 uint _count;
46 static if(changeNotification) 46 static if(changeNotification)
47 void delegate(ListChangeType, T, T, uint) whenChanged; 47 void delegate(ListChangeType, T, T, uint) whenChanged;
48 const int DefaultCapacity = 16; 48 enum int DefaultCapacity = 16;
49 public: 49 public:
50 /** 50 /**
51 * Creates a list with the specified capacity. 51 * Creates a list with the specified capacity.
52 * 52 *
53 * Only available if changeNotification is false. 53 * Only available if changeNotification is false.
110 } 110 }
111 T[] data() { 111 T[] data() {
112 return _data[0.._count]; 112 return _data[0.._count];
113 } 113 }
114 /*string toString() { 114 /*string toString() {
115 string str = "["; 115 mstring str = "[";
116 if(Count > 0) 116 if(Count > 0)
117 str ~= ToString(this[0]); 117 str ~= toString(this[0]);
118 foreach(item; this) { 118 foreach(item; this) {
119 str ~= ", "; 119 str ~= ", ";
120 str ~= ToString(item); 120 str ~= toString(item);
121 } 121 }
122 str ~= "]"; 122 str ~= "]";
123 return str; 123 return str;
124 }*/ 124 }*/
125 protected void maybeEnlarge(uint neededCap) { 125 protected void maybeEnlarge(uint neededCap) {
126 if(neededCap <= capacity) 126 if(neededCap <= capacity)
127 return; 127 return;
128 _data.length = max(neededCap, (capacity+1)*2); 128 _data.length = max(neededCap, (capacity+1)*2);
129 } 129 }
130 T opIndex(uint index) { 130 ref T opIndex(uint index) {
131 return _data[0.._count][index]; 131 return _data[0.._count][index];
132 } 132 }
133 void opIndexAssign(T item, uint index) { 133 void opIndexAssign(T item, uint index) {
134 T oldItem = _data[0.._count][index]; 134 T oldItem = _data[0.._count][index];
135 _data[0.._count][index] = item; 135 _data[0.._count][index] = item;
196 //trimCapacity() 196 //trimCapacity()
197 //opConcat 197 //opConcat
198 //opEquals 198 //opEquals
199 //opSlice 199 //opSlice
200 //opSliceAssign 200 //opSliceAssign
201 int opApply(int delegate(inout T item) dg) { 201 int opApply(int delegate(ref T item) dg) {
202 for(uint i = 0; i < _count; ++i) { 202 for(uint i = 0; i < _count; ++i) {
203 if(int result = dg(_data[i])) 203 if(int result = dg(_data[i]))
204 return result; 204 return result;
205 } 205 }
206 return 0; 206 return 0;
207 } 207 }
208 //so you can do: 208 //so you can do:
209 //foreach(i, item; list) 209 //foreach(i, item; list)
210 int opApply(int delegate(inout uint index, inout T item) dg) { 210 int opApply(int delegate(ref uint index, ref T item) dg) {
211 for(uint i = 0; i < _count; ++i) { 211 for(uint i = 0; i < _count; ++i) {
212 if(int result = dg(i, _data[i])) 212 if(int result = dg(i, _data[i]))
213 return result; 213 return result;
214 } 214 }
215 return 0; 215 return 0;
216 } 216 }
217 217
218 } 218 }
219 unittest { 219 unittest {
220 auto list = List!(char).fromArray("Hello, Mat"); 220 auto list = List!(char).fromArray("Hello, Mat".dup);
221 list.add('t'); 221 list.add('t');
222 assert(list.data == "Hello, Matt"); 222 assert(list.data == "Hello, Matt");
223 assert(list.pop() == 't'); 223 assert(list.pop() == 't');
224 assert(list.data == "Hello, Mat"); 224 assert(list.data == "Hello, Mat");
225 list.insert('!', 5); 225 list.insert('!', 5);