| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Itinero.IO; |
| | 7 | |
|
| | 8 | | namespace Itinero.Indexes; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// A default attribute set index using a dictionary internally. |
| | 12 | | /// </summary> |
| | 13 | | public sealed class AttributeSetDictionaryIndex : AttributeSetIndex |
| | 14 | | { |
| | 15 | | private readonly List<IReadOnlyList<(string key, string value)>> _edgeProfiles; |
| | 16 | | private readonly Dictionary<IReadOnlyList<(string key, string value)>, uint> _edgeProfilesIndex; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Creates a new attribute set index. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="edgeProfiles"></param> |
| 316 | 22 | | public AttributeSetDictionaryIndex(List<IReadOnlyList<(string key, string value)>>? edgeProfiles = null) |
| 316 | 23 | | { |
| 632 | 24 | | _edgeProfiles = edgeProfiles ?? new List<IReadOnlyList<(string key, string value)>> { Array.Empty<(string key, s |
| 316 | 25 | | _edgeProfilesIndex = |
| 316 | 26 | | new Dictionary<IReadOnlyList<(string key, string value)>, uint>(AttributeSetEqualityComparer.Default); |
| 316 | 27 | | this.UpdateIndex(); |
| 316 | 28 | | } |
| | 29 | |
|
| | 30 | | internal void UpdateIndex() |
| 321 | 31 | | { |
| 321 | 32 | | _edgeProfilesIndex.Clear(); |
| 1290 | 33 | | for (var p = 0; p < _edgeProfiles.Count; p++) |
| 324 | 34 | | { |
| 324 | 35 | | _edgeProfilesIndex[_edgeProfiles[p]] = (uint)p; |
| 324 | 36 | | } |
| 321 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Gets the number of distinct sets. |
| | 41 | | /// </summary> |
| | 42 | | public override uint Count |
| | 43 | | { |
| | 44 | | get |
| 3 | 45 | | { |
| 3 | 46 | | return (uint)_edgeProfiles.Count; |
| 3 | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Gets the attributes for the given id. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="id">The id.</param> |
| | 54 | | /// <returns>The attributes in the type.</returns> |
| | 55 | | public override IEnumerable<(string key, string value)> GetById(uint id) |
| 8 | 56 | | { |
| 9 | 57 | | if (id > _edgeProfiles.Count) throw new ArgumentOutOfRangeException(nameof(id)); |
| | 58 | |
|
| 7 | 59 | | return _edgeProfiles[(int)id]; |
| 7 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets the type id for the given attributes set. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="attributes">The attributes.</param> |
| | 66 | | /// <returns>The id, if any.</returns> |
| | 67 | | public override uint Get(IEnumerable<(string key, string value)> attributes) |
| 125 | 68 | | { |
| 125 | 69 | | var attributeSet = attributes.ToArray(); |
| | 70 | |
|
| | 71 | | // sort array. |
| 129 | 72 | | Array.Sort(attributeSet, (x, y) => x.CompareTo(y)); |
| | 73 | |
|
| | 74 | | // check if profile already there. |
| 125 | 75 | | if (_edgeProfilesIndex.TryGetValue(attributeSet, out var edgeProfileId)) |
| 108 | 76 | | { |
| 108 | 77 | | return edgeProfileId; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | // add new profile. |
| 17 | 81 | | edgeProfileId = (uint)_edgeProfiles.Count; |
| 17 | 82 | | _edgeProfiles.Add(attributeSet); |
| 17 | 83 | | _edgeProfilesIndex.Add(attributeSet, edgeProfileId); |
| | 84 | |
|
| 17 | 85 | | return edgeProfileId; |
| 125 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <inheritdoc/> |
| | 89 | | public override Task WriteTo(Stream stream) |
| 5 | 90 | | { |
| | 91 | | // write version #. |
| 5 | 92 | | stream.WriteVarInt32(2); |
| | 93 | |
|
| | 94 | | // write type. |
| 5 | 95 | | stream.WriteWithSize("dictionary-index"); |
| | 96 | |
|
| | 97 | | // write pairs. |
| 5 | 98 | | stream.WriteVarInt32(_edgeProfiles.Count); |
| 31 | 99 | | foreach (var attributes in _edgeProfiles) |
| 8 | 100 | | { |
| 8 | 101 | | stream.WriteVarInt32(attributes.Count); |
| 32 | 102 | | foreach (var (key, value) in attributes) |
| 4 | 103 | | { |
| 4 | 104 | | stream.WriteWithSize(key); |
| 4 | 105 | | stream.WriteWithSize(value); |
| 4 | 106 | | } |
| 8 | 107 | | } |
| | 108 | |
|
| 5 | 109 | | return Task.CompletedTask; |
| 5 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <inheritdoc/> |
| | 113 | | public override Task ReadFrom(Stream stream) |
| 5 | 114 | | { |
| | 115 | | // get version #. |
| 5 | 116 | | var version = stream.ReadVarInt32(); |
| 5 | 117 | | if (version != 2) throw new InvalidDataException("Unexpected version #."); |
| | 118 | |
|
| | 119 | | // read type. |
| 5 | 120 | | var type = stream.ReadWithSizeString(); |
| 5 | 121 | | if (type != "dictionary-index") throw new InvalidDataException("Unexpected index type."); |
| | 122 | |
|
| | 123 | | // read pairs. |
| 5 | 124 | | var count = stream.ReadVarInt32(); |
| 5 | 125 | | _edgeProfiles.Clear(); |
| 26 | 126 | | for (var i = 0; i < count; i++) |
| 8 | 127 | | { |
| 8 | 128 | | var c = stream.ReadVarInt32(); |
| 8 | 129 | | var attribute = new (string key, string value)[c]; |
| 24 | 130 | | for (var a = 0; a < c; a++) |
| 4 | 131 | | { |
| 4 | 132 | | var key = stream.ReadWithSizeString(); |
| 4 | 133 | | var value = stream.ReadWithSizeString(); |
| 4 | 134 | | attribute[a] = (key, value); |
| 4 | 135 | | } |
| | 136 | |
|
| 8 | 137 | | _edgeProfiles.Add(attribute); |
| 8 | 138 | | } |
| 5 | 139 | | this.UpdateIndex(); |
| 5 | 140 | | return Task.CompletedTask; |
| 5 | 141 | | } |
| | 142 | | } |