| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Itinero.Network.TurnCosts; |
| | | 4 | | |
| | | 5 | | internal static class OrderCoder |
| | | 6 | | { |
| | | 7 | | internal const int MaxOrderHeadTail = 14; |
| | | 8 | | |
| | | 9 | | public static void SetTailHeadOrder(this byte[] data, long i, byte? tail, byte? head) |
| | 52951 | 10 | | { |
| | 52951 | 11 | | if (tail.HasValue && tail.Value > MaxOrderHeadTail) |
| | 0 | 12 | | { |
| | 0 | 13 | | throw new ArgumentOutOfRangeException(nameof(tail), |
| | 0 | 14 | | $"Maximum order exceeded."); |
| | | 15 | | } |
| | | 16 | | |
| | 52951 | 17 | | if (head.HasValue && head.Value > MaxOrderHeadTail) |
| | 0 | 18 | | { |
| | 0 | 19 | | throw new ArgumentOutOfRangeException(nameof(head), |
| | 0 | 20 | | $"Maximum order exceeded."); |
| | | 21 | | } |
| | | 22 | | |
| | 52951 | 23 | | var d = 0; |
| | 52951 | 24 | | if (tail.HasValue) |
| | 535 | 25 | | { |
| | 535 | 26 | | d = tail.Value + 1; |
| | 535 | 27 | | } |
| | | 28 | | |
| | 52951 | 29 | | if (head.HasValue) |
| | 520 | 30 | | { |
| | 520 | 31 | | d += (head.Value + 1) * 16; |
| | 520 | 32 | | } |
| | | 33 | | |
| | 52951 | 34 | | data[(int)i] = (byte)d; |
| | 52951 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static void GetTailHeadOrder(this byte[] data, long i, ref byte? tail, ref byte? head) |
| | 6543024 | 38 | | { |
| | 6543024 | 39 | | tail = null; |
| | 6543024 | 40 | | head = null; |
| | | 41 | | |
| | 6543024 | 42 | | var d = data[(int)i]; |
| | 6543024 | 43 | | if (d == 0) |
| | 6451820 | 44 | | { |
| | 6451820 | 45 | | return; |
| | | 46 | | } |
| | | 47 | | |
| | 91204 | 48 | | tail = null; |
| | 91204 | 49 | | var t = d % 16; |
| | 91204 | 50 | | if (t > 0) |
| | 46049 | 51 | | { |
| | 46049 | 52 | | tail = (byte)(t - 1); |
| | 46049 | 53 | | } |
| | | 54 | | |
| | 91204 | 55 | | var h = d / 16; |
| | 91204 | 56 | | if (h > 0) |
| | 45456 | 57 | | { |
| | 45456 | 58 | | head = (byte)(h - 1); |
| | 45456 | 59 | | } |
| | 6543024 | 60 | | } |
| | | 61 | | } |