|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | + |
| 5 | +namespace SA3D.Common.Lookup |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// A dictionary for mapping unique objects to unique offsets. |
| 9 | + /// </summary> |
| 10 | + /// <typeparam name="T">Type of the objects to map</typeparam> |
| 11 | + public class OffsetDictionary<T> where T : notnull |
| 12 | + { |
| 13 | + private protected readonly Dictionary<long, T> _fromOffset; |
| 14 | + private protected readonly Dictionary<T, long> _toOffset; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Number of entries in the dictionary |
| 18 | + /// </summary> |
| 19 | + public int Count => _fromOffset.Count; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Creates a new pointer dictionary |
| 23 | + /// </summary> |
| 24 | + public OffsetDictionary() |
| 25 | + { |
| 26 | + _fromOffset = []; |
| 27 | + _toOffset = []; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets the value mapped to the specified offset. |
| 32 | + /// </summary> |
| 33 | + /// <param name="offset">The offset of the value to get.</param> |
| 34 | + /// <returns>The value if successful; <see langword="default"/> if unsuccessful.</returns> |
| 35 | + /// <exception cref="ArgumentNullException"/> |
| 36 | + public T? GetValue(long offset) |
| 37 | + { |
| 38 | + if(_fromOffset.TryGetValue(offset, out T? value)) |
| 39 | + { |
| 40 | + return value; |
| 41 | + } |
| 42 | + |
| 43 | + return default; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets the offset mapped to the specifed value. |
| 48 | + /// </summary> |
| 49 | + /// <param name="value">The value of the offset to get.</param> |
| 50 | + /// <returns>The offset if successful; <see langword="null"/> if unsuccessful.</returns> |
| 51 | + /// <exception cref="ArgumentNullException"/> |
| 52 | + public long? GetAddress(T value) |
| 53 | + { |
| 54 | + if(_toOffset.TryGetValue(value, out long offset)) |
| 55 | + { |
| 56 | + return offset; |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets the offset value to the specifed offset. |
| 64 | + /// </summary> |
| 65 | + /// <param name="offset">The offset to get the value of.</param> |
| 66 | + /// <param name="result">When this method returns, contains the value associated with the specified offset, if the offset is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param> |
| 67 | + /// <returns><see langword="true"/> if the <see cref="OffsetDictionary{T}"/> contains a value with the specified offset, otherwise <see langword="false"/>.</returns> |
| 68 | + /// <exception cref="ArgumentNullException"/> |
| 69 | + public bool TryGetValue(long offset, [MaybeNullWhen(false)] out T result) |
| 70 | + { |
| 71 | + return _fromOffset.TryGetValue(offset, out result); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Gets the offset to the specifed offset. |
| 76 | + /// </summary> |
| 77 | + /// <param name="value">The value to get the offset of.</param> |
| 78 | + /// <param name="result">When this method returns, contains the offset associated with the specified value, if the value is found; otherwise, <see langword="null"/>. This parameter is passed uninitialized.</param> |
| 79 | + /// <returns><see langword="true"/> if the <see cref="OffsetDictionary{T}"/> contains an offset with the specified value, otherwise <see langword="false"/>.</returns> |
| 80 | + /// <exception cref="ArgumentNullException"/> |
| 81 | + public bool TryGetAddress(T value, [MaybeNullWhen(false)] out long result) |
| 82 | + { |
| 83 | + return _toOffset.TryGetValue(value, out result); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Adds a new offset/value pair to the dictionary. |
| 88 | + /// </summary> |
| 89 | + /// <param name="offset">The offset to add.</param> |
| 90 | + /// <param name="value">The value to add.</param> |
| 91 | + /// <exception cref="ArgumentException"/> |
| 92 | + public void Add(long offset, T value) |
| 93 | + { |
| 94 | + if(!_fromOffset.TryAdd(offset, value)) |
| 95 | + { |
| 96 | + throw new ArgumentException($"An item with the same offset has already been added. Address: {offset:X8}"); |
| 97 | + } |
| 98 | + |
| 99 | + if(!_toOffset.TryAdd(value, offset)) |
| 100 | + { |
| 101 | + _fromOffset.Remove(offset); |
| 102 | + throw new ArgumentException($"An item with the same value has already been added. Value: {value}"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Attempts to add a new offset/value pair to the dictionary. |
| 108 | + /// </summary> |
| 109 | + /// <param name="offset"></param> |
| 110 | + /// <param name="value"></param> |
| 111 | + /// <returns><see langword="true"/> if the offset/value pair was successfully added to the dictionary; otherwise <see langword="false"/>.</returns> |
| 112 | + public bool TryAdd(long offset, T value) |
| 113 | + { |
| 114 | + if(!_fromOffset.TryAdd(offset, value)) |
| 115 | + { |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + if(!_toOffset.TryAdd(value, offset)) |
| 120 | + { |
| 121 | + _fromOffset.Remove(offset); |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Get a copy of the dictionary mapping values to offsets. |
| 130 | + /// </summary> |
| 131 | + /// <returns></returns> |
| 132 | + public Dictionary<long, T> GetDictFrom() |
| 133 | + { |
| 134 | + return new(_fromOffset); |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Get a copy of the dictionary mapping offsets to values. |
| 139 | + /// </summary> |
| 140 | + /// <returns></returns> |
| 141 | + public Dictionary<T, long> GetDictTo() |
| 142 | + { |
| 143 | + return new(_toOffset); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments