| | | 1 | | namespace Itinero.Network.Tiles; |
| | | 2 | | |
| | | 3 | | internal static class ArrayBaseExtensions |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Increase the native byte array size with a fixed step (but never more) to ensure the given position fits. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="array">The array.</param> |
| | | 9 | | /// <param name="position">The position.</param> |
| | | 10 | | /// <param name="step">The steps to add.</param> |
| | | 11 | | public static void EnsureMinimumSize(ref byte[] array, long position, long step = 16) |
| | 192047 | 12 | | { |
| | 366013 | 13 | | if (array.Length > position) return; |
| | | 14 | | |
| | | 15 | | // Geometric, floored at step. The fixed-step version here resized once per `step` bytes, |
| | | 16 | | // which is a full Array.Resize each time — quadratic when appending to a tile loaded from |
| | | 17 | | // the cache, because such a tile is sized to exactly its content and has no headroom. |
| | 18081 | 18 | | var newSize = System.Math.Max(position + 1, array.Length + System.Math.Max(step, array.Length >> 3)); |
| | 18081 | 19 | | System.Array.Resize(ref array, (int)newSize); |
| | 192047 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Increase the native int array size with a fixed step (but never more) to ensure the given position fits. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="array">The array.</param> |
| | | 26 | | /// <param name="position">The position.</param> |
| | | 27 | | /// <param name="step">The steps to add.</param> |
| | | 28 | | public static void EnsureMinimumSize(ref int[] array, long position, long step = 16) |
| | 100005 | 29 | | { |
| | 199941 | 30 | | if (array.Length > position) return; |
| | | 31 | | |
| | | 32 | | // Geometric, floored at step. The fixed-step version here resized once per `step` bytes, |
| | | 33 | | // which is a full Array.Resize each time — quadratic when appending to a tile loaded from |
| | | 34 | | // the cache, because such a tile is sized to exactly its content and has no headroom. |
| | 69 | 35 | | var newSize = System.Math.Max(position + 1, array.Length + System.Math.Max(step, array.Length >> 3)); |
| | 69 | 36 | | System.Array.Resize(ref array, (int)newSize); |
| | 100005 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Increase the native uint array size with a fixed step (but never more) to ensure the given position fits. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="array">The array.</param> |
| | | 43 | | /// <param name="position">The position.</param> |
| | | 44 | | /// <param name="step">The steps to add.</param> |
| | | 45 | | public static void EnsureMinimumSize(ref uint[] array, long position, long step = 16) |
| | 51082 | 46 | | { |
| | 95795 | 47 | | if (array.Length > position) return; |
| | | 48 | | |
| | | 49 | | // Geometric, floored at step. The fixed-step version here resized once per `step` bytes, |
| | | 50 | | // which is a full Array.Resize each time — quadratic when appending to a tile loaded from |
| | | 51 | | // the cache, because such a tile is sized to exactly its content and has no headroom. |
| | 6369 | 52 | | var newSize = System.Math.Max(position + 1, array.Length + System.Math.Max(step, array.Length >> 3)); |
| | 6369 | 53 | | System.Array.Resize(ref array, (int)newSize); |
| | 51082 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Increase the native string array size with a fixed step (but never more) to ensure the given position fits. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="array">The array.</param> |
| | | 60 | | /// <param name="position">The position.</param> |
| | | 61 | | /// <param name="step">The steps to add.</param> |
| | | 62 | | public static void EnsureMinimumSize(ref string[] array, long position, long step = 16) |
| | 0 | 63 | | { |
| | 0 | 64 | | if (array.Length > position) return; |
| | | 65 | | |
| | | 66 | | // Geometric, floored at step. The fixed-step version here resized once per `step` bytes, |
| | | 67 | | // which is a full Array.Resize each time — quadratic when appending to a tile loaded from |
| | | 68 | | // the cache, because such a tile is sized to exactly its content and has no headroom. |
| | 0 | 69 | | var newSize = System.Math.Max(position + 1, array.Length + System.Math.Max(step, array.Length >> 3)); |
| | 0 | 70 | | System.Array.Resize(ref array, (int)newSize); |
| | 0 | 71 | | } |
| | | 72 | | } |