< Summary

Class:Itinero.Indexes.AttributeSetDictionaryIndex
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Indexes/AttributeSetDictionaryIndex.cs
Covered lines:69
Uncovered lines:0
Coverable lines:69
Total lines:142
Line coverage:100% (69 of 69)
Covered branches:18
Total branches:20
Branch coverage:90% (18 of 20)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%2100%
UpdateIndex()100%2100%
get_Count()100%1100%
GetById(...)100%2100%
Get(...)100%2100%
WriteTo(...)100%4100%
ReadFrom(...)75%8100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Indexes/AttributeSetDictionaryIndex.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Threading.Tasks;
 6using Itinero.IO;
 7
 8namespace Itinero.Indexes;
 9
 10/// <summary>
 11/// A default attribute set index using a dictionary internally.
 12/// </summary>
 13public 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>
 31622    public AttributeSetDictionaryIndex(List<IReadOnlyList<(string key, string value)>>? edgeProfiles = null)
 31623    {
 63224        _edgeProfiles = edgeProfiles ?? new List<IReadOnlyList<(string key, string value)>> { Array.Empty<(string key, s
 31625        _edgeProfilesIndex =
 31626            new Dictionary<IReadOnlyList<(string key, string value)>, uint>(AttributeSetEqualityComparer.Default);
 31627        this.UpdateIndex();
 31628    }
 29
 30    internal void UpdateIndex()
 32131    {
 32132        _edgeProfilesIndex.Clear();
 129033        for (var p = 0; p < _edgeProfiles.Count; p++)
 32434        {
 32435            _edgeProfilesIndex[_edgeProfiles[p]] = (uint)p;
 32436        }
 32137    }
 38
 39    /// <summary>
 40    /// Gets the number of distinct sets.
 41    /// </summary>
 42    public override uint Count
 43    {
 44        get
 345        {
 346            return (uint)_edgeProfiles.Count;
 347        }
 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)
 856    {
 957        if (id > _edgeProfiles.Count) throw new ArgumentOutOfRangeException(nameof(id));
 58
 759        return _edgeProfiles[(int)id];
 760    }
 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)
 12568    {
 12569        var attributeSet = attributes.ToArray();
 70
 71        // sort array.
 12972        Array.Sort(attributeSet, (x, y) => x.CompareTo(y));
 73
 74        // check if profile already there.
 12575        if (_edgeProfilesIndex.TryGetValue(attributeSet, out var edgeProfileId))
 10876        {
 10877            return edgeProfileId;
 78        }
 79
 80        // add new profile.
 1781        edgeProfileId = (uint)_edgeProfiles.Count;
 1782        _edgeProfiles.Add(attributeSet);
 1783        _edgeProfilesIndex.Add(attributeSet, edgeProfileId);
 84
 1785        return edgeProfileId;
 12586    }
 87
 88    /// <inheritdoc/>
 89    public override Task WriteTo(Stream stream)
 590    {
 91        // write version #.
 592        stream.WriteVarInt32(2);
 93
 94        // write type.
 595        stream.WriteWithSize("dictionary-index");
 96
 97        // write pairs.
 598        stream.WriteVarInt32(_edgeProfiles.Count);
 3199        foreach (var attributes in _edgeProfiles)
 8100        {
 8101            stream.WriteVarInt32(attributes.Count);
 32102            foreach (var (key, value) in attributes)
 4103            {
 4104                stream.WriteWithSize(key);
 4105                stream.WriteWithSize(value);
 4106            }
 8107        }
 108
 5109        return Task.CompletedTask;
 5110    }
 111
 112    /// <inheritdoc/>
 113    public override Task ReadFrom(Stream stream)
 5114    {
 115        // get version #.
 5116        var version = stream.ReadVarInt32();
 5117        if (version != 2) throw new InvalidDataException("Unexpected version #.");
 118
 119        // read type.
 5120        var type = stream.ReadWithSizeString();
 5121        if (type != "dictionary-index") throw new InvalidDataException("Unexpected index type.");
 122
 123        // read pairs.
 5124        var count = stream.ReadVarInt32();
 5125        _edgeProfiles.Clear();
 26126        for (var i = 0; i < count; i++)
 8127        {
 8128            var c = stream.ReadVarInt32();
 8129            var attribute = new (string key, string value)[c];
 24130            for (var a = 0; a < c; a++)
 4131            {
 4132                var key = stream.ReadWithSizeString();
 4133                var value = stream.ReadWithSizeString();
 4134                attribute[a] = (key, value);
 4135            }
 136
 8137            _edgeProfiles.Add(attribute);
 8138        }
 5139        this.UpdateIndex();
 5140        return Task.CompletedTask;
 5141    }
 142}