| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationAreaPolicy.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Stores one immutable direct-indexed navigation-area policy snapshot. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class NavigationAreaPolicy |
| | | 17 | | { |
| | | 18 | | private const int MaximumRuleCount = ushort.MaxValue + 1; |
| | | 19 | | private readonly NavigationAreaRule[] _rules; |
| | | 20 | | private readonly long _retainedBytes; |
| | | 21 | | |
| | | 22 | | /// <summary>Creates a policy by copying rules whose indices are stable area IDs.</summary> |
| | 196 | 23 | | public NavigationAreaPolicy( |
| | 196 | 24 | | NavigationAreaPolicyKey key, |
| | 196 | 25 | | IReadOnlyList<NavigationAreaRule> rules) |
| | | 26 | | { |
| | 196 | 27 | | key.Validate(nameof(key)); |
| | 196 | 28 | | SwiftThrowHelper.ThrowIfNull(rules, nameof(rules)); |
| | 196 | 29 | | SwiftThrowHelper.ThrowIfArgument( |
| | 196 | 30 | | rules.Count == 0 || rules.Count > MaximumRuleCount, |
| | 196 | 31 | | nameof(rules), |
| | 196 | 32 | | "A policy requires between one and 65,536 direct-indexed rules."); |
| | | 33 | | |
| | 195 | 34 | | Key = key; |
| | 195 | 35 | | _rules = new NavigationAreaRule[rules.Count]; |
| | 1872 | 36 | | for (int i = 0; i < rules.Count; i++) |
| | 741 | 37 | | _rules[i] = rules[i]; |
| | 195 | 38 | | _retainedBytes = checked( |
| | 195 | 39 | | 48L |
| | 195 | 40 | | + ((long)key.PolicyId.Length * sizeof(char)) |
| | 195 | 41 | | + ((long)_rules.Length * 24L)); |
| | 195 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Gets the exact immutable policy identity.</summary> |
| | | 45 | | public NavigationAreaPolicyKey Key { get; } |
| | | 46 | | |
| | | 47 | | /// <summary>Gets the number of configured direct-indexed area rules.</summary> |
| | 4026 | 48 | | public int RuleCount => _rules.Length; |
| | | 49 | | |
| | | 50 | | /// <summary>Attempts to resolve an area rule without allocation or lookup hashing.</summary> |
| | | 51 | | public bool TryGetRule(NavigationAreaId area, out NavigationAreaRule rule) |
| | | 52 | | { |
| | 231568 | 53 | | int index = area.Value; |
| | 231568 | 54 | | if (index < _rules.Length) |
| | | 55 | | { |
| | 231566 | 56 | | rule = _rules[index]; |
| | 231566 | 57 | | return true; |
| | | 58 | | } |
| | | 59 | | |
| | 2 | 60 | | rule = default; |
| | 2 | 61 | | return false; |
| | | 62 | | } |
| | | 63 | | |
| | 1290 | 64 | | internal long RetainedBytes => _retainedBytes; |
| | | 65 | | |
| | | 66 | | internal bool ContentEquals(NavigationAreaPolicy other) |
| | | 67 | | { |
| | 3101 | 68 | | if (ReferenceEquals(this, other)) |
| | 3090 | 69 | | return true; |
| | 11 | 70 | | if (!Key.Equals(other.Key) || _rules.Length != other._rules.Length) |
| | 2 | 71 | | return false; |
| | 34 | 72 | | for (int i = 0; i < _rules.Length; i++) |
| | | 73 | | { |
| | 13 | 74 | | if (!_rules[i].Equals(other._rules[i])) |
| | 5 | 75 | | return false; |
| | | 76 | | } |
| | 4 | 77 | | return true; |
| | | 78 | | } |
| | | 79 | | } |