comparison 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
comparison
equal deleted inserted replaced
83:3cfc83a99cbc 84:7653269724db
46 Removed, 46 Removed,
47 /// An item was replaced with another item. 47 /// An item was replaced with another item.
48 Replaced 48 Replaced
49 } 49 }
50 50
51 /**
52 * A list of items backed by an array. If changeNotification is true, then a delegate is passed
53 * into the constructor as a callback. The delegate will be called when any item is added to or
54 * removed from the list. Also, when changeNotfication is true, some methods are not
55 * available. These are marked in their documentation.
56 */
51 class List(T, bool changeNotification = false) { 57 class List(T, bool changeNotification = false) {
52 protected: 58 protected:
53 T[] _data; 59 T[] _data;
54 uint _count; 60 uint _count;
55 static if(changeNotification) 61 static if(changeNotification)
56 void delegate(ListChangeType, T, T, uint) whenChanged; 62 void delegate(ListChangeType, T, T, uint) whenChanged;
57 const int DefaultCapacity = 16; 63 const int DefaultCapacity = 16;
58 public: 64 public:
59 static if(changeNotification) { 65 /**
60 /// whenChanged is called right after an item is added, removed, or replaced 66 * Creates a list with the specified capacity.
61 this(void delegate(ListChangeType, T, T, uint) whenChanged) { 67 *
68 * Only available if changeNotification is false.
69 */
70 this() {
71 static if(changeNotification)
72 throw new Exception("not available");
73 this(DefaultCapacity);
74 }
75 /// ditto
76 this(uint capacity) {
77 static if(changeNotification)
78 throw new Exception("not available");
79 _data = new T[capacity];
80 }
81
82 /**
83 * Creates a list with the specified capacity and with a delegate that will be called
84 * when an item is added to or removed from the list. The type specifies whether an
85 * item was added, removed, or replaced. oldItem contains the item that was removed,
86 * and newItem contains the item that was added. So if type is ListChangeType.Added,
87 * oldItem will be T.init (which is null for reference types). If type is
88 * ListChangeType.Removed, newItem will be T.init. The index of the item after being
89 * added or before being removed is also passed to the delegate.
90 *
91 * Only available if changeNotification is true.
92 */
93 this(void delegate(ListChangeType type, T oldItem, T newItem, uint index) whenChanged) {
94 static if(changeNotification) {
62 this(DefaultCapacity, whenChanged); 95 this(DefaultCapacity, whenChanged);
63 } 96 } else {
64 this(uint capacity, 97 throw new Exception("not available");
65 void delegate(ListChangeType, T, T, uint) whenChanged) { 98 }
99 }
100 /// ditto
101 this(uint capacity,
102 void delegate(ListChangeType type, T oldItem, T newItem, uint index) whenChanged) {
103 static if(changeNotification) {
66 _data = new T[capacity]; 104 _data = new T[capacity];
67 this.whenChanged = whenChanged; 105 this.whenChanged = whenChanged;
68 } 106 } else {
69 } else { 107 throw new Exception("not available");
70 this() { 108 }
71 this(DefaultCapacity); 109 }
72 } 110
73 this(uint capacity) {
74 _data = new T[capacity];
75 }
76 }
77 static List!(T) fromArray(T[] arr...) { 111 static List!(T) fromArray(T[] arr...) {
78 auto list = new List!(T)(); 112 auto list = new List!(T)();
79 list._data = arr.dup; 113 list._data = arr.dup;
80 list._count = arr.length; 114 list._count = arr.length;
81 return list; 115 return list;
176 if(item == item2) // if(item2 == item) would crash on a null item 210 if(item == item2) // if(item2 == item) would crash on a null item
177 return i; 211 return i;
178 return -1; 212 return -1;
179 } 213 }
180 //trimCapacity() 214 //trimCapacity()
181 //opIndex
182 //opIndexAssign
183 //opConcat 215 //opConcat
184 //opEquals 216 //opEquals
185 //opSlice 217 //opSlice
186 //opSliceAssign 218 //opSliceAssign
187 int opApply(int delegate(inout T item) dg) { 219 int opApply(int delegate(inout T item) dg) {