CurveKeyCollection.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // MIT License - Copyright (C) The Mono.Xna Team
  2. // This file is subject to the terms and conditions defined in
  3. // file 'LICENSE.txt', which is part of this source code package.
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Runtime.Serialization;
  9. namespace CommonLang.Geometry
  10. {
  11. /// <summary>
  12. /// The collection of the <see cref="CurveKey"/> elements and a part of the <see cref="Curve"/> class.
  13. /// </summary>
  14. // TODO : [TypeConverter(typeof(ExpandableObjectConverter))]
  15. public class CurveKeyCollection : ICollection<CurveKey>
  16. {
  17. #region Private Fields
  18. private readonly List<CurveKey> _keys;
  19. #endregion
  20. #region Properties
  21. /// <summary>
  22. /// Indexer.
  23. /// </summary>
  24. /// <param name="index">The index of key in this collection.</param>
  25. /// <returns><see cref="CurveKey"/> at <paramref name="index"/> position.</returns>
  26. public CurveKey this[int index]
  27. {
  28. get { return _keys[index]; }
  29. set
  30. {
  31. if (value == null)
  32. throw new ArgumentNullException();
  33. if (index >= _keys.Count)
  34. throw new IndexOutOfRangeException();
  35. if (_keys[index].Position == value.Position)
  36. _keys[index] = value;
  37. else
  38. {
  39. _keys.RemoveAt(index);
  40. _keys.Add(value);
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// Returns the count of keys in this collection.
  46. /// </summary>
  47. public int Count
  48. {
  49. get { return _keys.Count; }
  50. }
  51. /// <summary>
  52. /// Returns false because it is not a read-only collection.
  53. /// </summary>
  54. public bool IsReadOnly
  55. {
  56. get { return false; }
  57. }
  58. #endregion
  59. #region Constructors
  60. /// <summary>
  61. /// Creates a new instance of <see cref="CurveKeyCollection"/> class.
  62. /// </summary>
  63. public CurveKeyCollection()
  64. {
  65. _keys = new List<CurveKey>();
  66. }
  67. #endregion
  68. IEnumerator IEnumerable.GetEnumerator()
  69. {
  70. return _keys.GetEnumerator();
  71. }
  72. /// <summary>
  73. /// Adds a key to this collection.
  74. /// </summary>
  75. /// <param name="item">New key for the collection.</param>
  76. /// <exception cref="ArgumentNullException">Throws if <paramref name="item"/> is null.</exception>
  77. /// <remarks>The new key would be added respectively to a position of that key and the position of other keys.</remarks>
  78. public void Add(CurveKey item)
  79. {
  80. if (item == null)
  81. throw new ArgumentNullException("item");
  82. if (_keys.Count == 0)
  83. {
  84. this._keys.Add(item);
  85. return;
  86. }
  87. for (int i = 0; i < this._keys.Count; i++)
  88. {
  89. if (item.Position < this._keys[i].Position)
  90. {
  91. this._keys.Insert(i, item);
  92. return;
  93. }
  94. }
  95. this._keys.Add(item);
  96. }
  97. /// <summary>
  98. /// Removes all keys from this collection.
  99. /// </summary>
  100. public void Clear()
  101. {
  102. _keys.Clear();
  103. }
  104. /// <summary>
  105. /// Creates a copy of this collection.
  106. /// </summary>
  107. /// <returns>A copy of this collection.</returns>
  108. public CurveKeyCollection Clone()
  109. {
  110. CurveKeyCollection ckc = new CurveKeyCollection();
  111. foreach (CurveKey key in this._keys)
  112. ckc.Add(key);
  113. return ckc;
  114. }
  115. /// <summary>
  116. /// Determines whether this collection contains a specific key.
  117. /// </summary>
  118. /// <param name="item">The key to locate in this collection.</param>
  119. /// <returns><c>true</c> if the key is found; <c>false</c> otherwise.</returns>
  120. public bool Contains(CurveKey item)
  121. {
  122. return _keys.Contains(item);
  123. }
  124. /// <summary>
  125. /// Copies the keys of this collection to an array, starting at the array index provided.
  126. /// </summary>
  127. /// <param name="array">Destination array where elements will be copied.</param>
  128. /// <param name="arrayIndex">The zero-based index in the array to start copying from.</param>
  129. public void CopyTo(CurveKey[] array, int arrayIndex)
  130. {
  131. _keys.CopyTo(array, arrayIndex);
  132. }
  133. /// <summary>
  134. /// Returns an enumerator that iterates through the collection.
  135. /// </summary>
  136. /// <returns>An enumerator for the <see cref="CurveKeyCollection"/>.</returns>
  137. public IEnumerator<CurveKey> GetEnumerator()
  138. {
  139. return _keys.GetEnumerator();
  140. }
  141. /// <summary>
  142. /// Finds element in the collection and returns its index.
  143. /// </summary>
  144. /// <param name="item">Element for the search.</param>
  145. /// <returns>Index of the element; or -1 if item is not found.</returns>
  146. public int IndexOf(CurveKey item)
  147. {
  148. return _keys.IndexOf(item);
  149. }
  150. /// <summary>
  151. /// Removes element at the specified index.
  152. /// </summary>
  153. /// <param name="index">The index which element will be removed.</param>
  154. public void RemoveAt(int index)
  155. {
  156. _keys.RemoveAt(index);
  157. }
  158. /// <summary>
  159. /// Removes specific element.
  160. /// </summary>
  161. /// <param name="item">The element</param>
  162. /// <returns><c>true</c> if item is successfully removed; <c>false</c> otherwise. This method also returns <c>false</c> if item was not found.</returns>
  163. public bool Remove(CurveKey item)
  164. {
  165. return _keys.Remove(item);
  166. }
  167. }
  168. }