Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Collections
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Collections
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Collections/CollectionBase.cs
r2768 r4068 1 1 2 2 using System; 3 using System.IO;4 using System.Runtime.Serialization;5 using System.Runtime.Serialization.Formatters.Binary;6 3 using System.Collections; 7 4 using System.Collections.Generic; 8 5 using System.Diagnostics; 6 using System.IO; 7 using System.Runtime.Serialization.Formatters.Binary; 9 8 using Netron.NetronLight; 10 namespace Netron.Diagramming.Core 11 { 12 /// <summary> 13 /// CollectionBase is a base class that can be used to more easily implement the 14 /// generic ICollection<T> and non-generic ICollection interfaces. 15 /// </summary> 16 /// <remarks> 17 /// <para>To use CollectionBase as a base class, the derived class must override 18 /// the Count, GetEnumerator, Add, Clear, and Remove methods. </para> 19 /// <para>ICollection<T>.Contains need not be implemented by the 20 /// derived class, but it should be strongly considered, because the CollectionBase implementation 21 /// may not be very efficient.</para> 9 namespace Netron.Diagramming.Core { 10 /// <summary> 11 /// CollectionBase is a base class that can be used to more easily implement the 12 /// generic ICollection<T> and non-generic ICollection interfaces. 13 /// </summary> 14 /// <remarks> 15 /// <para>To use CollectionBase as a base class, the derived class must override 16 /// the Count, GetEnumerator, Add, Clear, and Remove methods. </para> 17 /// <para>ICollection<T>.Contains need not be implemented by the 18 /// derived class, but it should be strongly considered, because the CollectionBase implementation 19 /// may not be very efficient.</para> 20 /// </remarks> 21 /// <typeparam name="T">The item type of the collection.</typeparam> 22 23 [DebuggerDisplay("{DebuggerDisplayString()}")] 24 public partial class CollectionBase<T> : ICollection<T>, ICollection, IList<T>, ICollectionBase<T>, IShowable { 25 #region Events 26 /// <summary> 27 /// Occurs when an item is added to the collection. 28 /// </summary> 29 public event EventHandler<CollectionEventArgs<T>> OnItemAdded; 30 /// <summary> 31 /// Occurs when an item is removed from the collection. 32 /// </summary> 33 public event EventHandler<CollectionEventArgs<T>> OnItemRemoved; 34 /// <summary> 35 /// Occurs when the collection is cleared. 36 /// </summary> 37 public event EventHandler OnClear; 38 #endregion 39 40 #region Fields 41 /// <summary> 42 /// the internal generic list on which this collection is based 43 /// </summary> 44 protected List<T> innerList; 45 /// <summary> 46 /// whether this collection is readonly 47 /// </summary> 48 bool mReadOnly = false; 49 #endregion 50 51 #region Properties 52 /// <summary> 53 /// Indicates whether the collection is read-only. 54 /// </summary> 55 bool ICollection<T>.IsReadOnly { 56 get { 57 return mReadOnly; 58 } 59 } 60 61 /// <summary> 62 /// Gets a value indicating whether this instance is empty. 63 /// </summary> 64 /// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value> 65 public virtual bool IsEmpty { 66 get { return this.innerList.Count == 0; } 67 } 68 #endregion 69 70 71 72 #region Constructor 73 74 /// <summary> 75 /// Creates a new CollectionBase. 76 /// </summary> 77 public CollectionBase() { 78 innerList = new List<T>(); 79 } 80 81 /// <summary> 82 /// Initializes a new instance of the <see cref="T:CollectionBase<T>"/> class. 83 /// </summary> 84 /// <param name="baseCollection">The base collection.</param> 85 public CollectionBase(ICollectionBase<T> baseCollection) 86 : this() { 87 innerList.AddRange(baseCollection); 88 } 89 90 /// <summary> 91 /// Initializes a new instance of the <see cref="T:CollectionBase<T>"/> class. 92 /// </summary> 93 /// <param name="baseCollection">The base collection.</param> 94 /// <param name="readOnly">if set to <c>true</c> [read only].</param> 95 public CollectionBase(ICollectionBase<T> baseCollection, bool readOnly) 96 : this() { 97 mReadOnly = readOnly; 98 //innerList = new System.Collections.ObjectModel.ReadOnlyCollection<T>(baseCollection); 99 innerList.AddRange(baseCollection); 100 } 101 102 103 104 105 #endregion 106 107 /// <summary> 108 /// Shows the string representation of the collection. The string representation contains 109 /// a list of the items in the collection. Contained collections (except string) are expanded 110 /// recursively. 111 /// </summary> 112 /// <returns>The string representation of the collection.</returns> 113 public override string ToString() { 114 return Algorithms.ToString(this); 115 } 116 /// <summary> 117 /// Returns a string representation of this collection. 118 /// </summary> 119 /// <param name="format">The format.</param> 120 /// <param name="formatProvider">The format provider.</param> 121 /// <returns></returns> 122 public virtual string ToString(string format, IFormatProvider formatProvider) { 123 return Showing.ShowString(this, format, formatProvider); 124 } 125 126 #region ICollection<T> Members 127 128 /// <summary> 129 /// Must be overridden to allow adding items to this collection. 130 /// </summary> 131 /// <remarks><p>This method is not abstract, although derived classes should always 132 /// override it. It is not abstract because some derived classes may wish to reimplement 133 /// Add with a different return type (typically bool). In C#, this can be accomplished 134 /// with code like the following:</p> 135 /// <code> 136 /// public class MyCollection<T>: CollectionBase<T>, ICollection<T> 137 /// { 138 /// public new bool Add(T item) { 139 /// /* Add the item */ 140 /// } 141 /// 142 /// void ICollection<T>.Add(T item) { 143 /// Add(item); 144 /// } 145 /// } 146 /// </code> 22 147 /// </remarks> 23 /// <typeparam name="T">The item type of the collection.</typeparam> 24 25 [DebuggerDisplay("{DebuggerDisplayString()}")] 26 public partial class CollectionBase<T> : ICollection<T>, ICollection, IList<T>, ICollectionBase<T>, IShowable 27 { 28 #region Events 29 /// <summary> 30 /// Occurs when an item is added to the collection. 31 /// </summary> 32 public event EventHandler<CollectionEventArgs<T>> OnItemAdded; 33 /// <summary> 34 /// Occurs when an item is removed from the collection. 35 /// </summary> 36 public event EventHandler<CollectionEventArgs<T>> OnItemRemoved; 37 /// <summary> 38 /// Occurs when the collection is cleared. 39 /// </summary> 40 public event EventHandler OnClear; 41 #endregion 42 43 #region Fields 44 /// <summary> 45 /// the internal generic list on which this collection is based 46 /// </summary> 47 protected List<T> innerList; 48 /// <summary> 49 /// whether this collection is readonly 50 /// </summary> 51 bool mReadOnly = false; 52 #endregion 53 54 #region Properties 55 /// <summary> 56 /// Indicates whether the collection is read-only. 57 /// </summary> 58 bool ICollection<T>.IsReadOnly 59 { 60 get 61 { 62 return mReadOnly; 63 } 148 /// <param name="item">Item to be added to the collection.</param> 149 /// <exception cref="NotImplementedException">Always throws this exception to indicated 150 /// that the method must be overridden or re-implemented in the derived class.</exception> 151 public virtual void Add(T item) { 152 if (item == null) throw new InconsistencyException("Adding 'null' to the collection is not allowed."); 153 if (mReadOnly) 154 throw new InconsistencyException("The collection is read only"); 155 this.innerList.Add(item); 156 RaiseOnItemAdded(item); 157 158 } 159 /// <summary> 160 /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"></see>. 161 /// </summary> 162 /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"></see>.</param> 163 /// <returns> 164 /// The index of item if found in the list; otherwise, -1. 165 /// </returns> 166 public int IndexOf(T item) { 167 return this.innerList.IndexOf(item); 168 } 169 170 /// <summary> 171 /// Inserts the specified item at the specified index. 172 /// </summary> 173 /// <param name="index">The index.</param> 174 /// <param name="item">A parameter of the generics Type T</param> 175 public virtual void Insert(int index, T item) { 176 if (mReadOnly) 177 throw new InconsistencyException("The collection is read only"); 178 if (item.Equals(default(T))) 179 return; 180 this.innerList.Insert(index, item); 181 RaiseOnItemAdded(item); 182 } 183 184 /// <summary> 185 /// Adds a collection range to this collection. 186 /// </summary> 187 /// <param name="items">The items.</param> 188 public virtual void AddRange(CollectionBase<T> items) { 189 if (mReadOnly) 190 throw new InconsistencyException("The collection is read only"); 191 this.innerList.AddRange(items); 192 foreach (T item in items) { 193 RaiseOnItemAdded(item); 194 } 195 } 196 197 private void RaiseOnItemAdded(T item) { 198 if (OnItemAdded != null) 199 OnItemAdded(this, new CollectionEventArgs<T>(item)); 200 } 201 202 private void RaiseOnItemRemoved(T item) { 203 if (OnItemRemoved != null) 204 OnItemRemoved(this, new CollectionEventArgs<T>(item)); 205 } 206 private void RaiseOnClear() { 207 if (OnClear != null) 208 OnClear(this, EventArgs.Empty); 209 } 210 211 212 213 /// <summary> 214 /// Must be overridden to allow clearing this collection. 215 /// </summary> 216 public virtual void Clear() { 217 RaiseOnClear(); 218 innerList.Clear(); 219 } 220 221 /// <summary> 222 /// Must be overridden to allow removing items from this collection. 223 /// </summary> 224 /// <returns>True if <paramref name="item"/> existed in the collection and 225 /// was removed. False if <paramref name="item"/> did not exist in the collection.</returns> 226 public virtual bool Remove(T item) { 227 228 bool result = this.innerList.Remove(item); 229 if (result) { 230 RaiseOnItemRemoved(item); 231 } 232 return result; 233 } 234 235 /// <summary> 236 /// Determines if the collection contains a particular item. This default implementation 237 /// iterates all of the items in the collection via GetEnumerator, testing each item 238 /// against <paramref name="item"/> using IComparable<T>.Equals or 239 /// Object.Equals. 240 /// </summary> 241 /// <remarks>You should strongly consider overriding this method to provide 242 /// a more efficient implementation, or if the default equality comparison 243 /// is inappropriate.</remarks> 244 /// <param name="item">The item to check for in the collection.</param> 245 /// <returns>True if the collection contains <paramref name="item"/>, false otherwise.</returns> 246 public virtual bool Contains(T item) { 247 return this.innerList.Contains(item); 248 249 } 250 251 /// <summary> 252 /// Copies all the items in the collection into an array. Implemented by 253 /// using the enumerator returned from GetEnumerator to get all the items 254 /// and copy them to the provided array. 255 /// </summary> 256 /// <param name="array">Array to copy to.</param> 257 /// <param name="arrayIndex">Starting index in <paramref name="array"/> to copy to.</param> 258 public virtual void CopyTo(T[] array, int arrayIndex) { 259 int count = this.Count; 260 261 if (count == 0) 262 return; 263 264 if (array == null) 265 throw new ArgumentNullException("array"); 266 if (count < 0) 267 throw new ArgumentOutOfRangeException("count", count, Resource1.ArgumentOutOfRange); 268 if (arrayIndex < 0) 269 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex, Resource1.ArgumentOutOfRange); 270 if (arrayIndex >= array.Length || count > array.Length - arrayIndex) 271 throw new ArgumentException("arrayIndex", Resource1.ArrayTooSmall); 272 273 int index = arrayIndex, i = 0; 274 foreach (T item in (ICollection<T>)this) { 275 if (i >= count) 276 break; 277 278 array[index] = item; 279 ++index; 280 ++i; 281 } 282 } 283 284 /// <summary> 285 /// Creates an array of the correct size, and copies all the items in the 286 /// collection into the array, by calling CopyTo. 287 /// </summary> 288 /// <returns>An array containing all the elements in the collection, in order.</returns> 289 public virtual T[] ToArray() { 290 int count = this.Count; 291 292 T[] array = new T[count]; 293 CopyTo(array, 0); 294 return array; 295 } 296 297 /// <summary> 298 /// Must be overridden to provide the number of items in the collection. 299 /// </summary> 300 /// <value>The number of items in the collection.</value> 301 public virtual int Count { get { return innerList.Count; } } 302 303 304 305 #endregion 306 307 #region Delegate operations 308 309 /// <summary> 310 /// Determines if the collection contains any item that satisfies the condition 311 /// defined by <paramref name="predicate"/>. 312 /// </summary> 313 /// <param name="predicate">A delegate that defines the condition to check for.</param> 314 /// <returns>True if the collection contains one or more items that satisfy the condition 315 /// defined by <paramref name="predicate"/>. False if the collection does not contain 316 /// an item that satisfies <paramref name="predicate"/>.</returns> 317 public virtual bool Exists(Predicate<T> predicate) { 318 if (predicate == null) 319 throw new ArgumentNullException("predicate"); 320 321 return Algorithms.Exists(this, predicate); 322 } 323 324 /// <summary> 325 /// Determines if all of the items in the collection satisfy the condition 326 /// defined by <paramref name="predicate"/>. 327 /// </summary> 328 /// <param name="predicate">A delegate that defines the condition to check for.</param> 329 /// <returns>True if all of the items in the collection satisfy the condition 330 /// defined by <paramref name="predicate"/>, or if the collection is empty. False if one or more items 331 /// in the collection do not satisfy <paramref name="predicate"/>.</returns> 332 public virtual bool TrueForAll(Predicate<T> predicate) { 333 if (predicate == null) 334 throw new ArgumentNullException("predicate"); 335 336 return Algorithms.TrueForAll(this, predicate); 337 } 338 /* 339 /// <summary> 340 /// Counts the number of items in the collection that satisfy the condition 341 /// defined by <paramref name="predicate"/>. 342 /// </summary> 343 /// <param name="predicate">A delegate that defines the condition to check for.</param> 344 /// <returns>The number of items in the collection that satisfy <paramref name="predicate"/>.</returns> 345 public virtual int CountWhere(Predicate<T> predicate) 346 { 347 if (predicate == null) 348 throw new ArgumentNullException("predicate"); 349 350 return Algorithms.CountWhere(this, predicate); 351 } 352 */ 353 /// <summary> 354 /// Finds the first item in the collection satisfying the given predicate 355 /// </summary> 356 /// <param name="predicate">a searching predictae</param> 357 /// <returns>an item satisfying the criteria (if any)</returns> 358 public virtual T Find(Predicate<T> predicate) { 359 foreach (T item in this.innerList) { 360 if (predicate(item)) 361 return item; 362 } 363 return default(T); 364 } 365 366 367 /// <summary> 368 /// Removes all the items in the collection that satisfy the condition 369 /// defined by <paramref name="predicate"/>. 370 /// </summary> 371 /// <param name="predicate">A delegate that defines the condition to check for.</param> 372 /// <returns>Returns a collection of the items that were removed, in sorted order.</returns> 373 public virtual ICollection<T> RemoveAll(Predicate<T> predicate) { 374 if (predicate == null) 375 throw new ArgumentNullException("predicate"); 376 377 return Algorithms.RemoveWhere(this, predicate); 378 } 379 380 /// <summary> 381 /// Performs the specified action on each item in this collection. 382 /// </summary> 383 /// <param name="action">An Action delegate which is invoked for each item in this collection.</param> 384 public virtual void ForEach(Action<T> action) { 385 if (action == null) 386 throw new ArgumentNullException("action"); 387 388 Algorithms.ForEach(this, action); 389 } 390 391 392 393 #endregion 394 395 #region IEnumerable<T> Members 396 397 /// <summary> 398 /// Must be overridden to enumerate all the members of the collection. 399 /// </summary> 400 /// <returns>A generic IEnumerator<T> that can be used 401 /// to enumerate all the items in the collection.</returns> 402 public virtual IEnumerator<T> GetEnumerator() { 403 return this.innerList.GetEnumerator(); 404 } 405 406 407 #endregion 408 409 #region ICollection Members 410 411 /// <summary> 412 /// Copies all the items in the collection into an array. Implemented by 413 /// using the enumerator returned from GetEnumerator to get all the items 414 /// and copy them to the provided array. 415 /// </summary> 416 /// <param name="array">Array to copy to.</param> 417 /// <param name="index">Starting index in <paramref name="array"/> to copy to.</param> 418 void ICollection.CopyTo(Array array, int index) { 419 int count = this.Count; 420 421 if (count == 0) 422 return; 423 424 if (array == null) 425 throw new ArgumentNullException("array"); 426 if (index < 0) 427 throw new ArgumentOutOfRangeException("index", index, Resource1.ArgumentOutOfRange); 428 if (index >= array.Length || count > array.Length - index) 429 throw new ArgumentException("index", Resource1.ArrayTooSmall); 430 431 int i = 0; 432 foreach (object o in (ICollection)this) { 433 if (i >= count) 434 break; 435 436 array.SetValue(o, index); 437 ++index; 438 ++i; 439 } 440 } 441 442 /// <summary> 443 /// Indicates whether the collection is synchronized. 444 /// </summary> 445 /// <value>Always returns false, indicating that the collection is not synchronized.</value> 446 bool ICollection.IsSynchronized { 447 get { return IsSynchronized; } 448 } 449 450 /// <summary> 451 /// See code analysis CA1033 452 /// </summary> 453 protected bool IsSynchronized { 454 get { return false; } 455 } 456 /// <summary> 457 /// Indicates the synchronization object for this collection. 458 /// </summary> 459 /// <value>Always returns this.</value> 460 object ICollection.SyncRoot { 461 get { return SyncRoot; } 462 } 463 464 /// <summary> 465 /// See code analysis CA1033 466 /// </summary> 467 protected object SyncRoot { 468 get { return this; } 469 } 470 471 #endregion 472 473 #region IEnumerable Members 474 475 /// <summary> 476 /// Provides an IEnumerator that can be used to iterate all the members of the 477 /// collection. This implementation uses the IEnumerator<T> that was overridden 478 /// by the derived classes to enumerate the members of the collection. 479 /// </summary> 480 /// <returns>An IEnumerator that can be used to iterate the collection.</returns> 481 IEnumerator IEnumerable.GetEnumerator() { 482 foreach (T item in this) { 483 yield return item; 484 } 485 } 486 487 #endregion 488 489 /// <summary> 490 /// Display the contents of the collection in the debugger. This is intentionally private, it is called 491 /// only from the debugger due to the presence of the DebuggerDisplay attribute. It is similar 492 /// format to ToString(), but is limited to 250-300 characters or so, so as not to overload the debugger. 493 /// </summary> 494 /// <returns>The string representation of the items in the collection, similar in format to ToString().</returns> 495 internal string DebuggerDisplayString() { 496 const int MAXLENGTH = 250; 497 498 System.Text.StringBuilder builder = new System.Text.StringBuilder(); 499 500 builder.Append('{'); 501 502 // Call ToString on each item and put it in. 503 bool firstItem = true; 504 foreach (T item in this) { 505 if (builder.Length >= MAXLENGTH) { 506 builder.Append(",..."); 507 break; 64 508 } 65 509 66 /// <summary> 67 /// Gets a value indicating whether this instance is empty. 68 /// </summary> 69 /// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value> 70 public virtual bool IsEmpty 71 { 72 get { return this.innerList.Count == 0; } 73 } 74 #endregion 75 76 77 78 #region Constructor 79 80 /// <summary> 81 /// Creates a new CollectionBase. 82 /// </summary> 83 public CollectionBase() 84 { 85 innerList = new List<T>(); 86 } 87 88 /// <summary> 89 /// Initializes a new instance of the <see cref="T:CollectionBase<T>"/> class. 90 /// </summary> 91 /// <param name="baseCollection">The base collection.</param> 92 public CollectionBase(ICollectionBase<T> baseCollection) : this() 93 { 94 innerList.AddRange(baseCollection); 95 } 96 97 /// <summary> 98 /// Initializes a new instance of the <see cref="T:CollectionBase<T>"/> class. 99 /// </summary> 100 /// <param name="baseCollection">The base collection.</param> 101 /// <param name="readOnly">if set to <c>true</c> [read only].</param> 102 public CollectionBase(ICollectionBase<T> baseCollection, bool readOnly) 103 : this() 104 { 105 mReadOnly = readOnly; 106 //innerList = new System.Collections.ObjectModel.ReadOnlyCollection<T>(baseCollection); 107 innerList.AddRange(baseCollection); 108 } 109 110 111 112 113 #endregion 114 115 /// <summary> 116 /// Shows the string representation of the collection. The string representation contains 117 /// a list of the items in the collection. Contained collections (except string) are expanded 118 /// recursively. 119 /// </summary> 120 /// <returns>The string representation of the collection.</returns> 121 public override string ToString() 122 { 123 return Algorithms.ToString(this); 124 } 125 /// <summary> 126 /// Returns a string representation of this collection. 127 /// </summary> 128 /// <param name="format">The format.</param> 129 /// <param name="formatProvider">The format provider.</param> 130 /// <returns></returns> 131 public virtual string ToString(string format, IFormatProvider formatProvider) 132 { 133 return Showing.ShowString(this, format, formatProvider); 134 } 135 136 #region ICollection<T> Members 137 138 /// <summary> 139 /// Must be overridden to allow adding items to this collection. 140 /// </summary> 141 /// <remarks><p>This method is not abstract, although derived classes should always 142 /// override it. It is not abstract because some derived classes may wish to reimplement 143 /// Add with a different return type (typically bool). In C#, this can be accomplished 144 /// with code like the following:</p> 145 /// <code> 146 /// public class MyCollection<T>: CollectionBase<T>, ICollection<T> 147 /// { 148 /// public new bool Add(T item) { 149 /// /* Add the item */ 150 /// } 151 /// 152 /// void ICollection<T>.Add(T item) { 153 /// Add(item); 154 /// } 155 /// } 156 /// </code> 157 /// </remarks> 158 /// <param name="item">Item to be added to the collection.</param> 159 /// <exception cref="NotImplementedException">Always throws this exception to indicated 160 /// that the method must be overridden or re-implemented in the derived class.</exception> 161 public virtual void Add(T item) 162 { 163 if(item==null) throw new InconsistencyException("Adding 'null' to the collection is not allowed."); 164 if (mReadOnly) 165 throw new InconsistencyException("The collection is read only"); 166 this.innerList.Add(item); 167 RaiseOnItemAdded(item); 168 169 } 170 /// <summary> 171 /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"></see>. 172 /// </summary> 173 /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"></see>.</param> 174 /// <returns> 175 /// The index of item if found in the list; otherwise, -1. 176 /// </returns> 177 public int IndexOf(T item) 178 { 179 return this.innerList.IndexOf(item); 180 } 181 182 /// <summary> 183 /// Inserts the specified item at the specified index. 184 /// </summary> 185 /// <param name="index">The index.</param> 186 /// <param name="item">A parameter of the generics Type T</param> 187 public virtual void Insert(int index, T item) 188 { 189 if (mReadOnly) 190 throw new InconsistencyException("The collection is read only"); 191 if(item.Equals(default(T))) 192 return; 193 this.innerList.Insert(index, item); 194 RaiseOnItemAdded(item); 195 } 196 197 /// <summary> 198 /// Adds a collection range to this collection. 199 /// </summary> 200 /// <param name="items">The items.</param> 201 public virtual void AddRange(CollectionBase<T> items) 202 { 203 if (mReadOnly) 204 throw new InconsistencyException("The collection is read only"); 205 this.innerList.AddRange(items); 206 foreach (T item in items) 207 { 208 RaiseOnItemAdded(item); 209 } 210 } 211 212 private void RaiseOnItemAdded(T item) 213 { 214 if (OnItemAdded != null) 215 OnItemAdded(this, new CollectionEventArgs<T>(item)); 216 } 217 218 private void RaiseOnItemRemoved(T item) 219 { 220 if (OnItemRemoved != null) 221 OnItemRemoved(this, new CollectionEventArgs<T>(item)); 222 } 223 private void RaiseOnClear() 224 { 225 if (OnClear != null) 226 OnClear(this, EventArgs.Empty); 227 } 228 229 230 231 /// <summary> 232 /// Must be overridden to allow clearing this collection. 233 /// </summary> 234 public virtual void Clear() 235 { 236 RaiseOnClear(); 237 innerList.Clear(); 238 } 239 240 /// <summary> 241 /// Must be overridden to allow removing items from this collection. 242 /// </summary> 243 /// <returns>True if <paramref name="item"/> existed in the collection and 244 /// was removed. False if <paramref name="item"/> did not exist in the collection.</returns> 245 public virtual bool Remove(T item) 246 { 247 248 bool result = this.innerList.Remove(item); 249 if (result) 250 { 251 RaiseOnItemRemoved(item); 252 } 253 return result; 254 } 255 256 /// <summary> 257 /// Determines if the collection contains a particular item. This default implementation 258 /// iterates all of the items in the collection via GetEnumerator, testing each item 259 /// against <paramref name="item"/> using IComparable<T>.Equals or 260 /// Object.Equals. 261 /// </summary> 262 /// <remarks>You should strongly consider overriding this method to provide 263 /// a more efficient implementation, or if the default equality comparison 264 /// is inappropriate.</remarks> 265 /// <param name="item">The item to check for in the collection.</param> 266 /// <returns>True if the collection contains <paramref name="item"/>, false otherwise.</returns> 267 public virtual bool Contains(T item) 268 { 269 return this.innerList.Contains(item); 270 271 } 272 273 /// <summary> 274 /// Copies all the items in the collection into an array. Implemented by 275 /// using the enumerator returned from GetEnumerator to get all the items 276 /// and copy them to the provided array. 277 /// </summary> 278 /// <param name="array">Array to copy to.</param> 279 /// <param name="arrayIndex">Starting index in <paramref name="array"/> to copy to.</param> 280 public virtual void CopyTo(T[] array, int arrayIndex) 281 { 282 int count = this.Count; 283 284 if (count == 0) 285 return; 286 287 if (array == null) 288 throw new ArgumentNullException("array"); 289 if (count < 0) 290 throw new ArgumentOutOfRangeException("count", count, Resource1.ArgumentOutOfRange); 291 if (arrayIndex < 0) 292 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex, Resource1.ArgumentOutOfRange); 293 if (arrayIndex >= array.Length || count > array.Length - arrayIndex) 294 throw new ArgumentException("arrayIndex", Resource1.ArrayTooSmall); 295 296 int index = arrayIndex, i = 0; 297 foreach (T item in (ICollection<T>)this) 298 { 299 if (i >= count) 300 break; 301 302 array[index] = item; 303 ++index; 304 ++i; 305 } 306 } 307 308 /// <summary> 309 /// Creates an array of the correct size, and copies all the items in the 310 /// collection into the array, by calling CopyTo. 311 /// </summary> 312 /// <returns>An array containing all the elements in the collection, in order.</returns> 313 public virtual T[] ToArray() 314 { 315 int count = this.Count; 316 317 T[] array = new T[count]; 318 CopyTo(array, 0); 319 return array; 320 } 321 322 /// <summary> 323 /// Must be overridden to provide the number of items in the collection. 324 /// </summary> 325 /// <value>The number of items in the collection.</value> 326 public virtual int Count { get { return innerList.Count; } } 327 328 329 330 #endregion 331 332 #region Delegate operations 333 334 /// <summary> 335 /// Determines if the collection contains any item that satisfies the condition 336 /// defined by <paramref name="predicate"/>. 337 /// </summary> 338 /// <param name="predicate">A delegate that defines the condition to check for.</param> 339 /// <returns>True if the collection contains one or more items that satisfy the condition 340 /// defined by <paramref name="predicate"/>. False if the collection does not contain 341 /// an item that satisfies <paramref name="predicate"/>.</returns> 342 public virtual bool Exists(Predicate<T> predicate) 343 { 344 if (predicate == null) 345 throw new ArgumentNullException("predicate"); 346 347 return Algorithms.Exists(this, predicate); 348 } 349 350 /// <summary> 351 /// Determines if all of the items in the collection satisfy the condition 352 /// defined by <paramref name="predicate"/>. 353 /// </summary> 354 /// <param name="predicate">A delegate that defines the condition to check for.</param> 355 /// <returns>True if all of the items in the collection satisfy the condition 356 /// defined by <paramref name="predicate"/>, or if the collection is empty. False if one or more items 357 /// in the collection do not satisfy <paramref name="predicate"/>.</returns> 358 public virtual bool TrueForAll(Predicate<T> predicate) 359 { 360 if (predicate == null) 361 throw new ArgumentNullException("predicate"); 362 363 return Algorithms.TrueForAll(this, predicate); 364 } 365 /* 366 /// <summary> 367 /// Counts the number of items in the collection that satisfy the condition 368 /// defined by <paramref name="predicate"/>. 369 /// </summary> 370 /// <param name="predicate">A delegate that defines the condition to check for.</param> 371 /// <returns>The number of items in the collection that satisfy <paramref name="predicate"/>.</returns> 372 public virtual int CountWhere(Predicate<T> predicate) 373 { 374 if (predicate == null) 375 throw new ArgumentNullException("predicate"); 376 377 return Algorithms.CountWhere(this, predicate); 378 } 379 */ 380 /// <summary> 381 /// Finds the first item in the collection satisfying the given predicate 382 /// </summary> 383 /// <param name="predicate">a searching predictae</param> 384 /// <returns>an item satisfying the criteria (if any)</returns> 385 public virtual T Find(Predicate<T> predicate) 386 { 387 foreach (T item in this.innerList) 388 { 389 if (predicate(item)) 390 return item; 391 } 392 return default(T); 393 } 394 395 396 /// <summary> 397 /// Removes all the items in the collection that satisfy the condition 398 /// defined by <paramref name="predicate"/>. 399 /// </summary> 400 /// <param name="predicate">A delegate that defines the condition to check for.</param> 401 /// <returns>Returns a collection of the items that were removed, in sorted order.</returns> 402 public virtual ICollection<T> RemoveAll(Predicate<T> predicate) 403 { 404 if (predicate == null) 405 throw new ArgumentNullException("predicate"); 406 407 return Algorithms.RemoveWhere(this, predicate); 408 } 409 410 /// <summary> 411 /// Performs the specified action on each item in this collection. 412 /// </summary> 413 /// <param name="action">An Action delegate which is invoked for each item in this collection.</param> 414 public virtual void ForEach(Action<T> action) 415 { 416 if (action == null) 417 throw new ArgumentNullException("action"); 418 419 Algorithms.ForEach(this, action); 420 } 421 422 423 424 #endregion 425 426 #region IEnumerable<T> Members 427 428 /// <summary> 429 /// Must be overridden to enumerate all the members of the collection. 430 /// </summary> 431 /// <returns>A generic IEnumerator<T> that can be used 432 /// to enumerate all the items in the collection.</returns> 433 public virtual IEnumerator<T> GetEnumerator() 434 { 435 return this.innerList.GetEnumerator(); 436 } 437 438 439 #endregion 440 441 #region ICollection Members 442 443 /// <summary> 444 /// Copies all the items in the collection into an array. Implemented by 445 /// using the enumerator returned from GetEnumerator to get all the items 446 /// and copy them to the provided array. 447 /// </summary> 448 /// <param name="array">Array to copy to.</param> 449 /// <param name="index">Starting index in <paramref name="array"/> to copy to.</param> 450 void ICollection.CopyTo(Array array, int index) 451 { 452 int count = this.Count; 453 454 if (count == 0) 455 return; 456 457 if (array == null) 458 throw new ArgumentNullException("array"); 459 if (index < 0) 460 throw new ArgumentOutOfRangeException("index", index, Resource1.ArgumentOutOfRange); 461 if (index >= array.Length || count > array.Length - index) 462 throw new ArgumentException("index", Resource1.ArrayTooSmall); 463 464 int i = 0; 465 foreach (object o in (ICollection)this) 466 { 467 if (i >= count) 468 break; 469 470 array.SetValue(o, index); 471 ++index; 472 ++i; 473 } 474 } 475 476 /// <summary> 477 /// Indicates whether the collection is synchronized. 478 /// </summary> 479 /// <value>Always returns false, indicating that the collection is not synchronized.</value> 480 bool ICollection.IsSynchronized 481 { 482 get { return IsSynchronized; } 483 } 484 485 /// <summary> 486 /// See code analysis CA1033 487 /// </summary> 488 protected bool IsSynchronized 489 { 490 get { return false; } 491 } 492 /// <summary> 493 /// Indicates the synchronization object for this collection. 494 /// </summary> 495 /// <value>Always returns this.</value> 496 object ICollection.SyncRoot 497 { 498 get { return SyncRoot; } 499 } 500 501 /// <summary> 502 /// See code analysis CA1033 503 /// </summary> 504 protected object SyncRoot 505 { 506 get { return this; } 507 } 508 509 #endregion 510 511 #region IEnumerable Members 512 513 /// <summary> 514 /// Provides an IEnumerator that can be used to iterate all the members of the 515 /// collection. This implementation uses the IEnumerator<T> that was overridden 516 /// by the derived classes to enumerate the members of the collection. 517 /// </summary> 518 /// <returns>An IEnumerator that can be used to iterate the collection.</returns> 519 IEnumerator IEnumerable.GetEnumerator() 520 { 521 foreach (T item in this) 522 { 523 yield return item; 524 } 525 } 526 527 #endregion 528 529 /// <summary> 530 /// Display the contents of the collection in the debugger. This is intentionally private, it is called 531 /// only from the debugger due to the presence of the DebuggerDisplay attribute. It is similar 532 /// format to ToString(), but is limited to 250-300 characters or so, so as not to overload the debugger. 533 /// </summary> 534 /// <returns>The string representation of the items in the collection, similar in format to ToString().</returns> 535 internal string DebuggerDisplayString() 536 { 537 const int MAXLENGTH = 250; 538 539 System.Text.StringBuilder builder = new System.Text.StringBuilder(); 540 541 builder.Append('{'); 542 543 // Call ToString on each item and put it in. 544 bool firstItem = true; 545 foreach (T item in this) 546 { 547 if (builder.Length >= MAXLENGTH) 548 { 549 builder.Append(",..."); 550 break; 551 } 552 553 if (!firstItem) 554 builder.Append(','); 555 556 if (item == null) 557 builder.Append("null"); 558 else 559 builder.Append(item.ToString()); 560 561 firstItem = false; 562 } 563 564 builder.Append('}'); 565 return builder.ToString(); 566 } 567 /// <summary> 568 /// Removes an item at the given index 569 /// </summary> 570 /// <param name="index"></param> 571 public void RemoveAt(int index) 572 { 573 this.innerList.RemoveAt(index); 574 } 575 /// <summary> 576 /// Integer indexer 577 /// </summary> 578 /// <param name="index"></param> 579 /// <returns></returns> 580 public T this[int index] 581 { 582 get 583 { 584 return this.innerList[index]; 585 } 586 set 587 { 588 this.innerList[index] = value; 589 } 590 } 591 592 /// <summary> 593 /// Returns a deep copy of this collection. 594 /// <remarks>The returned collection is not attached to the <see cref="Model"/> 595 /// and is as such on an in-memory collection of instances. You need to 'unwrap' the collection 596 /// in the model and, to make it visible, deploy it in the paintables collection of the model. 597 /// </remarks> 598 /// </summary> 599 /// <returns></returns> 600 public CollectionBase<T> DeepCopy() 601 { 602 /* This doesn't work seemingly.... 603 if (!typeof(T).IsSerializable) 604 throw new InconsistencyException("The generic type on which the collection is based is not serializable, the collection cannot generate a deep copy."); 605 */ 606 607 try 608 { 609 610 CollectionBase<T> newobj = null; 611 MemoryStream stream = new MemoryStream(); 612 GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>(); 613 f.Serialize(stream, this); 614 stream.Seek(0, SeekOrigin.Begin); 615 newobj = f.Deserialize<CollectionBase<T>>(stream); 616 stream.Close(); 617 return newobj; 618 } 619 catch (Exception exc) 620 { 621 throw new InconsistencyException("The copy operation failed.", exc); 622 } 623 624 } 625 /// <summary> 626 /// Returns a copy of this instance. 627 /// </summary> 628 /// <returns></returns> 629 public CollectionBase<T> Copy() 630 { 631 CollectionBase<T> copy = new CollectionBase<T>(); 632 foreach (T item in this.innerList) 633 { 634 copy.Add(item); 635 } 636 return copy; 637 } 638 /// <summary> 639 /// This method creates first a deep copy of the collection and puts 640 /// the result in a <see cref="MemoryStream"/>. 641 /// See the <see cref="CopyTool"/> for details. 642 /// </summary> 643 /// <returns></returns> 644 public MemoryStream ToStream() 645 { 646 try 647 { 648 649 MemoryStream stream = new MemoryStream(); 650 GenericFormatter<BinaryFormatter> f = 651 new GenericFormatter<BinaryFormatter>(); 652 f.Serialize(stream, this); 653 return stream; 654 } 655 catch (Exception exc) 656 { 657 throw new InconsistencyException( 658 "The ToStream() operation failed.", 659 exc); 660 } 661 662 } 663 664 /// <summary> 665 /// Format <code>this</code> using at most approximately <code>rest</code> chars and 666 /// append the result, possibly truncated, to stringbuilder. 667 /// Subtract the actual number of used chars from <code>rest</code>. 668 /// </summary> 669 /// <param name="stringbuilder"></param> 670 /// <param name="rest"></param> 671 /// <param name="formatProvider"></param> 672 /// <returns> 673 /// True if the appended formatted string was complete (not truncated). 674 /// </returns> 675 public virtual bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) 676 { 677 return true; 678 } 679 680 } 681 682 683 684 685 686 510 if (!firstItem) 511 builder.Append(','); 512 513 if (item == null) 514 builder.Append("null"); 515 else 516 builder.Append(item.ToString()); 517 518 firstItem = false; 519 } 520 521 builder.Append('}'); 522 return builder.ToString(); 523 } 524 /// <summary> 525 /// Removes an item at the given index 526 /// </summary> 527 /// <param name="index"></param> 528 public void RemoveAt(int index) { 529 this.innerList.RemoveAt(index); 530 } 531 /// <summary> 532 /// Integer indexer 533 /// </summary> 534 /// <param name="index"></param> 535 /// <returns></returns> 536 public T this[int index] { 537 get { 538 return this.innerList[index]; 539 } 540 set { 541 this.innerList[index] = value; 542 } 543 } 544 545 /// <summary> 546 /// Returns a deep copy of this collection. 547 /// <remarks>The returned collection is not attached to the <see cref="Model"/> 548 /// and is as such on an in-memory collection of instances. You need to 'unwrap' the collection 549 /// in the model and, to make it visible, deploy it in the paintables collection of the model. 550 /// </remarks> 551 /// </summary> 552 /// <returns></returns> 553 public CollectionBase<T> DeepCopy() { 554 /* This doesn't work seemingly.... 555 if (!typeof(T).IsSerializable) 556 throw new InconsistencyException("The generic type on which the collection is based is not serializable, the collection cannot generate a deep copy."); 557 */ 558 559 try { 560 561 CollectionBase<T> newobj = null; 562 MemoryStream stream = new MemoryStream(); 563 GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>(); 564 f.Serialize(stream, this); 565 stream.Seek(0, SeekOrigin.Begin); 566 newobj = f.Deserialize<CollectionBase<T>>(stream); 567 stream.Close(); 568 return newobj; 569 } 570 catch (Exception exc) { 571 throw new InconsistencyException("The copy operation failed.", exc); 572 } 573 574 } 575 /// <summary> 576 /// Returns a copy of this instance. 577 /// </summary> 578 /// <returns></returns> 579 public CollectionBase<T> Copy() { 580 CollectionBase<T> copy = new CollectionBase<T>(); 581 foreach (T item in this.innerList) { 582 copy.Add(item); 583 } 584 return copy; 585 } 586 /// <summary> 587 /// This method creates first a deep copy of the collection and puts 588 /// the result in a <see cref="MemoryStream"/>. 589 /// See the <see cref="CopyTool"/> for details. 590 /// </summary> 591 /// <returns></returns> 592 public MemoryStream ToStream() { 593 try { 594 595 MemoryStream stream = new MemoryStream(); 596 GenericFormatter<BinaryFormatter> f = 597 new GenericFormatter<BinaryFormatter>(); 598 f.Serialize(stream, this); 599 return stream; 600 } 601 catch (Exception exc) { 602 throw new InconsistencyException( 603 "The ToStream() operation failed.", 604 exc); 605 } 606 607 } 608 609 /// <summary> 610 /// Format <code>this</code> using at most approximately <code>rest</code> chars and 611 /// append the result, possibly truncated, to stringbuilder. 612 /// Subtract the actual number of used chars from <code>rest</code>. 613 /// </summary> 614 /// <param name="stringbuilder"></param> 615 /// <param name="rest"></param> 616 /// <param name="formatProvider"></param> 617 /// <returns> 618 /// True if the appended formatted string was complete (not truncated). 619 /// </returns> 620 public virtual bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) { 621 return true; 622 } 623 624 } 625 626 627 628 629 630 687 631 } -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Collections/ICollectionBase.cs
r2768 r4068 1 1 using System; 2 using System.Collections;3 2 using System.Collections.Generic; 4 using System.IO; 5 namespace Netron.Diagramming.Core 6 { 3 namespace Netron.Diagramming.Core { 4 /// <summary> 5 /// Describes the CollectionBase collection. 6 /// </summary> 7 /// <typeparam name="T"></typeparam> 8 public interface ICollectionBase<T> : ICollection<T>, IList<T> { 9 #region Events 7 10 /// <summary> 8 /// Describes the CollectionBase collection.11 /// Occurs when the collection is cleared. 9 12 /// </summary> 10 /// <typeparam name="T"></typeparam> 11 public interface ICollectionBase<T> : ICollection<T>, IList<T> 12 { 13 #region Events 14 /// <summary> 15 /// Occurs when the collection is cleared. 16 /// </summary> 17 event EventHandler OnClear; 18 /// <summary> 19 /// Occurs when an item is added to the collection. 20 /// </summary> 21 event EventHandler<CollectionEventArgs<T>> OnItemAdded; 22 /// <summary> 23 /// Occurs when en item is removed from the collection. 24 /// </summary> 25 event EventHandler<CollectionEventArgs<T>> OnItemRemoved; 26 #endregion 13 event EventHandler OnClear; 14 /// <summary> 15 /// Occurs when an item is added to the collection. 16 /// </summary> 17 event EventHandler<CollectionEventArgs<T>> OnItemAdded; 18 /// <summary> 19 /// Occurs when en item is removed from the collection. 20 /// </summary> 21 event EventHandler<CollectionEventArgs<T>> OnItemRemoved; 22 #endregion 27 23 28 #region Properties 29 30 #endregion 24 #region Properties 31 25 32 #region Methods 33 /// <summary> 34 /// Adds the range of items to the collection. 35 /// </summary> 36 /// <param name="items">The items.</param> 37 void AddRange(CollectionBase<T> items); 38 /// <summary> 39 /// Copies this instance. 40 /// </summary> 41 /// <returns></returns> 42 CollectionBase<T> Copy(); 43 /// <summary> 44 /// Creates a deep copy of this instance. 45 /// </summary> 46 /// <returns></returns> 47 CollectionBase<T> DeepCopy(); 48 /// <summary> 49 /// Uses the given predicate to test the existence of a certain item in the collection. 50 /// </summary> 51 /// <param name="predicate">The predicate.</param> 52 /// <returns></returns> 53 bool Exists(Predicate<T> predicate); 54 /// <summary> 55 /// Finds the specified predicate. 56 /// </summary> 57 /// <param name="predicate">The predicate.</param> 58 /// <returns></returns> 59 T Find(Predicate<T> predicate); 60 /// <summary> 61 /// Uses the given Action to act on the collection items 62 /// </summary> 63 /// <param name="action">The action.</param> 64 void ForEach(Action<T> action); 65 66 67 ICollection<T> RemoveAll(Predicate<T> predicate); 26 #endregion 68 27 69 /// <summary> 70 /// Converts the collection to an array. 71 /// </summary> 72 /// <returns></returns> 73 T[] ToArray(); 74 /// <summary> 75 /// Specific copy/paste utility function. 76 /// </summary> 77 /// <returns></returns> 78 System.IO.MemoryStream ToStream(); 79 /// <summary> 80 /// Returns a string representation of the collection. 81 /// </summary> 82 /// <param name="format">The format.</param> 83 /// <param name="formatProvider">The format provider.</param> 84 /// <returns></returns> 85 string ToString(string format, IFormatProvider formatProvider); 86 /// <summary> 87 /// Returns a string representation of the collection. 88 /// </summary> 89 /// <returns></returns> 90 string ToString(); 91 /// <summary> 92 /// Checks, using the given predicate, whether a certain property is true for all items in the collection. 93 /// </summary> 94 /// <param name="predicate">The predicate.</param> 95 /// <returns></returns> 96 bool TrueForAll(Predicate<T> predicate); 97 #endregion 28 #region Methods 29 /// <summary> 30 /// Adds the range of items to the collection. 31 /// </summary> 32 /// <param name="items">The items.</param> 33 void AddRange(CollectionBase<T> items); 34 /// <summary> 35 /// Copies this instance. 36 /// </summary> 37 /// <returns></returns> 38 CollectionBase<T> Copy(); 39 /// <summary> 40 /// Creates a deep copy of this instance. 41 /// </summary> 42 /// <returns></returns> 43 CollectionBase<T> DeepCopy(); 44 /// <summary> 45 /// Uses the given predicate to test the existence of a certain item in the collection. 46 /// </summary> 47 /// <param name="predicate">The predicate.</param> 48 /// <returns></returns> 49 bool Exists(Predicate<T> predicate); 50 /// <summary> 51 /// Finds the specified predicate. 52 /// </summary> 53 /// <param name="predicate">The predicate.</param> 54 /// <returns></returns> 55 T Find(Predicate<T> predicate); 56 /// <summary> 57 /// Uses the given Action to act on the collection items 58 /// </summary> 59 /// <param name="action">The action.</param> 60 void ForEach(Action<T> action); 98 61 99 100 101 } 62 63 ICollection<T> RemoveAll(Predicate<T> predicate); 64 65 /// <summary> 66 /// Converts the collection to an array. 67 /// </summary> 68 /// <returns></returns> 69 T[] ToArray(); 70 /// <summary> 71 /// Specific copy/paste utility function. 72 /// </summary> 73 /// <returns></returns> 74 System.IO.MemoryStream ToStream(); 75 /// <summary> 76 /// Returns a string representation of the collection. 77 /// </summary> 78 /// <param name="format">The format.</param> 79 /// <param name="formatProvider">The format provider.</param> 80 /// <returns></returns> 81 string ToString(string format, IFormatProvider formatProvider); 82 /// <summary> 83 /// Returns a string representation of the collection. 84 /// </summary> 85 /// <returns></returns> 86 string ToString(); 87 /// <summary> 88 /// Checks, using the given predicate, whether a certain property is true for all items in the collection. 89 /// </summary> 90 /// <param name="predicate">The predicate.</param> 91 /// <returns></returns> 92 bool TrueForAll(Predicate<T> predicate); 93 #endregion 94 95 96 97 } 102 98 } -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Collections/StackBase.cs
r2768 r4068 2 2 using System.Collections; 3 3 using System.Collections.Generic; 4 using System.Diagnostics;5 4 6 5 7 namespace Netron.Diagramming.Core 8 { 6 namespace Netron.Diagramming.Core { 7 /// <summary> 8 /// This is a wrapper around the <see cref="Stack"/> which communicates Push and Pop events 9 /// </summary> 10 /// <typeparam name="T"></typeparam> 11 public class StackBase<T> { 12 #region Fields 9 13 /// <summary> 10 /// This is a wrapper around the <see cref="Stack"/> which communicates Push and Pop events14 /// the internal stack based on the .Net <see cref="Stack"/> implementation 11 15 /// </summary> 12 /// <typeparam name="T"></typeparam> 13 public class StackBase<T> 14 { 15 #region Fields 16 /// <summary> 17 /// the internal stack based on the .Net <see cref="Stack"/> implementation 18 /// </summary> 19 private Stack<T> InnerStack; 20 #endregion 16 private Stack<T> InnerStack; 17 #endregion 21 18 22 23 24 25 26 27 28 29 30 31 19 #region Events 20 /// <summary> 21 /// Occurs when an item is pushed onto the stack 22 /// </summary> 23 public event EventHandler<CollectionEventArgs<T>> OnItemPushed; 24 /// <summary> 25 /// Occurs when an item is popped from the stack 26 /// </summary> 27 public event EventHandler<CollectionEventArgs<T>> OnItemPopped; 28 #endregion; 32 29 33 #region Constructor 34 /// <summary> 35 /// Initializes a new instance of the <see cref="T:StackBase<T>"/> class. 36 /// </summary> 37 public StackBase() 38 { 39 InnerStack = new Stack<T>(); 40 } 41 #endregion 30 #region Constructor 31 /// <summary> 32 /// Initializes a new instance of the <see cref="T:StackBase<T>"/> class. 33 /// </summary> 34 public StackBase() { 35 InnerStack = new Stack<T>(); 36 } 37 #endregion 42 38 43 39 #region Methods 44 40 45 /// <summary> 46 /// Gets the number of elements in the stack 47 /// </summary> 48 /// <value>The count.</value> 49 public int Count 50 { 51 get { return InnerStack.Count; } 52 } 41 /// <summary> 42 /// Gets the number of elements in the stack 43 /// </summary> 44 /// <value>The count.</value> 45 public int Count { 46 get { return InnerStack.Count; } 47 } 53 48 54 49 55 /// <summary> 56 /// Pushes the specified item. 57 /// </summary> 58 /// <param name="item">A parameter of the generics Type T</param> 59 public void Push(T item) 60 { 50 /// <summary> 51 /// Pushes the specified item. 52 /// </summary> 53 /// <param name="item">A parameter of the generics Type T</param> 54 public void Push(T item) { 61 55 62 63 56 this.InnerStack.Push(item); 57 RaiseOnItemPushed(item); 64 58 65 59 } 66 60 67 /// <summary> 68 /// Pops the next item from the stack 69 /// </summary> 70 /// <returns></returns> 71 public T Pop() 72 { 73 T item = 74 InnerStack.Pop(); 75 RaiseOnItemPopped(item); 76 return item; 77 } 61 /// <summary> 62 /// Pops the next item from the stack 63 /// </summary> 64 /// <returns></returns> 65 public T Pop() { 66 T item = 67 InnerStack.Pop(); 68 RaiseOnItemPopped(item); 69 return item; 70 } 78 71 79 /// <summary> 80 /// Peeks the next item in the stack 81 /// </summary> 82 /// <returns></returns> 83 public T Peek() 84 { 85 return InnerStack.Peek(); 86 } 72 /// <summary> 73 /// Peeks the next item in the stack 74 /// </summary> 75 /// <returns></returns> 76 public T Peek() { 77 return InnerStack.Peek(); 78 } 87 79 88 80 89 /// <summary> 90 /// Converts the stack to an array. 91 /// </summary> 92 /// <returns></returns> 93 public T[] ToArray() 94 { 95 return InnerStack.ToArray(); 96 } 81 /// <summary> 82 /// Converts the stack to an array. 83 /// </summary> 84 /// <returns></returns> 85 public T[] ToArray() { 86 return InnerStack.ToArray(); 87 } 97 88 98 89 99 /// <summary> 100 /// Raises the <see cref="OnItemPushed"/>. 101 /// </summary> 102 /// <param name="item">A parameter of the generics Type T</param> 103 private void RaiseOnItemPushed(T item) 104 { 105 EventHandler<CollectionEventArgs<T>> handler = OnItemPushed; 106 handler(this, new CollectionEventArgs<T>(item)); 107 } 90 /// <summary> 91 /// Raises the <see cref="OnItemPushed"/>. 92 /// </summary> 93 /// <param name="item">A parameter of the generics Type T</param> 94 private void RaiseOnItemPushed(T item) { 95 EventHandler<CollectionEventArgs<T>> handler = OnItemPushed; 96 handler(this, new CollectionEventArgs<T>(item)); 97 } 108 98 109 /// <summary> 110 /// Raises the on OnItemPopped event. 111 /// </summary> 112 /// <param name="item">A parameter of the generics Type T</param> 113 private void RaiseOnItemPopped(T item) 114 { 115 EventHandler<CollectionEventArgs<T>> handler = OnItemPopped; 116 handler(this, new CollectionEventArgs<T>(item)); 117 } 99 /// <summary> 100 /// Raises the on OnItemPopped event. 101 /// </summary> 102 /// <param name="item">A parameter of the generics Type T</param> 103 private void RaiseOnItemPopped(T item) { 104 EventHandler<CollectionEventArgs<T>> handler = OnItemPopped; 105 handler(this, new CollectionEventArgs<T>(item)); 106 } 118 107 119 108 120 109 #endregion 121 110 122 111 } 123 112 }
Note: See TracChangeset
for help on using the changeset viewer.