AstarMemory.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. namespace Pathfinding.Util {
  3. /// <summary>Various utilities for handling arrays and memory</summary>
  4. public static class Memory {
  5. /// <summary>
  6. /// Sets all values in an array to a specific value faster than a loop.
  7. /// Only faster for large arrays. Slower for small ones.
  8. /// Tests indicate it becomes faster somewhere when the length of the array grows above around 100.
  9. /// For large arrays this can be magnitudes faster. Up to 40 times faster has been measured.
  10. ///
  11. /// Note: Only works on primitive value types such as int, long, float, etc.
  12. ///
  13. /// <code>
  14. /// //Set all values to 8 in the array
  15. /// int[] arr = new int[20000];
  16. /// Pathfinding.Util.Memory.MemSet<int> (arr, 8, sizeof(int));
  17. /// </code>
  18. /// See: System.Buffer.BlockCopy
  19. /// </summary>
  20. /// <param name="array">the array to fill</param>
  21. /// <param name="value">the value to fill the array with</param>
  22. /// <param name="byteSize">size in bytes of every element in the array. e.g 4 bytes for an int, or 8 bytes for a long.
  23. /// It can be efficiently got using the sizeof built-in function.</param>
  24. public static void MemSet<T>(T[] array, T value, int byteSize) where T : struct {
  25. if (array == null) {
  26. throw new ArgumentNullException("array");
  27. }
  28. int block = 32, index = 0;
  29. int length = Math.Min(block, array.Length);
  30. //Fill the initial array
  31. while (index < length) {
  32. array[index] = value;
  33. index++;
  34. }
  35. length = array.Length;
  36. while (index < length) {
  37. Buffer.BlockCopy(array, 0, array, index*byteSize, Math.Min(block, length-index)*byteSize);
  38. index += block;
  39. block *= 2;
  40. }
  41. }
  42. /// <summary>
  43. /// Sets all values in an array to a specific value faster than a loop.
  44. /// Only faster for large arrays. Slower for small ones.
  45. /// Tests indicate it becomes faster somewhere when the length of the array grows above around 100.
  46. /// For large arrays this can be magnitudes faster. Up to 40 times faster has been measured.
  47. ///
  48. /// Note: Only works on primitive value types such as int, long, float, etc.
  49. ///
  50. /// It can be efficiently got using the sizeof built-in function.
  51. ///
  52. /// <code>
  53. /// //Set all values to 8 in the array
  54. /// int[] arr = new int[20000];
  55. /// Pathfinding.Util.Memory.MemSet<int> (arr, 8, sizeof(int));
  56. /// </code>
  57. /// See: System.Buffer.BlockCopy
  58. /// </summary>
  59. /// <param name="array">the array to fill</param>
  60. /// <param name="value">the value to fill the array with</param>
  61. /// <param name="byteSize">size in bytes of every element in the array. e.g 4 bytes for an int, or 8 bytes for a long.</param>
  62. /// <param name="totalSize">all indices in the range [0, totalSize-1] will be set</param>
  63. public static void MemSet<T>(T[] array, T value, int totalSize, int byteSize) where T : struct {
  64. if (array == null) {
  65. throw new ArgumentNullException("array");
  66. }
  67. int block = 32, index = 0;
  68. int length = Math.Min(block, totalSize);
  69. //Fill the initial array
  70. while (index < length) {
  71. array[index] = value;
  72. index++;
  73. }
  74. length = totalSize;
  75. while (index < length) {
  76. Buffer.BlockCopy(array, 0, array, index*byteSize, Math.Min(block, totalSize-index)*byteSize);
  77. index += block;
  78. block *= 2;
  79. }
  80. }
  81. /// <summary>
  82. /// Returns a new array with at most length newLength.
  83. /// The array will contain a copy of all elements of arr up to but excluding the index newLength.
  84. /// </summary>
  85. public static T[] ShrinkArray<T>(T[] arr, int newLength) {
  86. newLength = Math.Min(newLength, arr.Length);
  87. var shrunkArr = new T[newLength];
  88. Array.Copy(arr, shrunkArr, newLength);
  89. return shrunkArr;
  90. }
  91. /// <summary>Swaps the variables a and b</summary>
  92. public static void Swap<T>(ref T a, ref T b) {
  93. T tmp = a;
  94. a = b;
  95. b = tmp;
  96. }
  97. }
  98. }