FloatCollection.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. namespace FastReport.Utils
  5. {
  6. /// <summary>
  7. /// Represents a collection of float values.
  8. /// </summary>
  9. [TypeConverter(typeof(FastReport.TypeConverters.FloatCollectionConverter))]
  10. public class FloatCollection : CollectionBase
  11. {
  12. /// <summary>
  13. /// Gets or sets the value at the specified index.
  14. /// </summary>
  15. /// <param name="index">Index of a value.</param>
  16. /// <returns>The value at the specified index.</returns>
  17. public float this[int index]
  18. {
  19. get { return (float)List[index]; }
  20. set { List[index] = value; }
  21. }
  22. /// <summary>
  23. /// Adds the specified values to the end of this collection.
  24. /// </summary>
  25. /// <param name="range"></param>
  26. public void AddRange(float[] range)
  27. {
  28. foreach (float t in range)
  29. {
  30. Add(t);
  31. }
  32. }
  33. /// <summary>
  34. /// Adds a value to the end of this collection.
  35. /// </summary>
  36. /// <param name="value">Value to add.</param>
  37. /// <returns>Index of the added value.</returns>
  38. public int Add(float value)
  39. {
  40. return List.Add(value);
  41. }
  42. /// <summary>
  43. /// Inserts a value into this collection at the specified index.
  44. /// </summary>
  45. /// <param name="index">The zero-based index at which value should be inserted.</param>
  46. /// <param name="value">The value to insert.</param>
  47. public void Insert(int index, float value)
  48. {
  49. List.Insert(index, value);
  50. }
  51. /// <summary>
  52. /// Removes the specified value from the collection.
  53. /// </summary>
  54. /// <param name="value">Value to remove.</param>
  55. public void Remove(float value)
  56. {
  57. int i = IndexOf(value);
  58. if (i != -1)
  59. List.RemoveAt(i);
  60. }
  61. /// <summary>
  62. /// Returns the zero-based index of the first occurrence of a value.
  63. /// </summary>
  64. /// <param name="value">The value to locate in the collection.</param>
  65. /// <returns>The zero-based index of the first occurrence of value within the entire collection, if found;
  66. /// otherwise, -1.</returns>
  67. public int IndexOf(float value)
  68. {
  69. for (int i = 0; i < Count; i++)
  70. {
  71. if (Math.Abs(this[i] - value) < 0.01)
  72. return i;
  73. }
  74. return -1;
  75. }
  76. /// <summary>
  77. /// Determines whether a value is in the collection.
  78. /// </summary>
  79. /// <param name="value">The value to locate in the collection.</param>
  80. /// <returns><b>true</b> if value is found in the collection; otherwise, <b>false</b>.</returns>
  81. public bool Contains(float value)
  82. {
  83. return IndexOf(value) != -1;
  84. }
  85. /// <summary>
  86. /// Copies values from another collection.
  87. /// </summary>
  88. /// <param name="source">Collection to copy from.</param>
  89. public void Assign(FloatCollection source)
  90. {
  91. Clear();
  92. foreach (float f in source)
  93. {
  94. Add(f);
  95. }
  96. }
  97. }
  98. }