< Summary

Class:Itinero.IO.Osm.Collections.UniqueIdMap`1
Assembly:Itinero.IO.Osm
File(s):/home/runner/work/routing2/routing2/src/Itinero.IO.Osm/Collections/UniqueIdMap.cs
Covered lines:0
Uncovered lines:76
Coverable lines:76
Total lines:154
Line coverage:0% (0 of 76)
Covered branches:0
Total branches:16
Branch coverage:0% (0 of 16)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%10%
Set(...)0%20%
get_Item(...)100%10%
set_Item(...)100%10%
Get(...)0%20%
get_NonDefaultIndices()100%10%
get_Start()100%10%
get_End()100%10%
get_Data()100%10%
Get(...)0%40%
Set(...)0%80%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.IO.Osm/Collections/UniqueIdMap.cs

#LineLine coverage
 1/*
 2 *  Licensed to SharpSoftware under one or more contributor
 3 *  license agreements. See the NOTICE file distributed with this work for
 4 *  additional information regarding copyright ownership.
 5 *
 6 *  SharpSoftware licenses this file to you under the Apache License,
 7 *  Version 2.0 (the "License"); you may not use this file except in
 8 *  compliance with the License. You may obtain a copy of the License at
 9 *
 10 *       http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 *  Unless required by applicable law or agreed to in writing, software
 13 *  distributed under the License is distributed on an "AS IS" BASIS,
 14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 *  See the License for the specific language governing permissions and
 16 *  limitations under the License.
 17 */
 18
 19using System.Collections.Generic;
 20
 21namespace Itinero.IO.Osm.Collections;
 22
 23/// <summary>
 24/// A unique id map, only vertex per id.
 25/// </summary>
 26public class UniqueIdMap<T>
 27    where T : struct
 28{
 29    private readonly Dictionary<long, Block> _blocks;
 30    private readonly int _blockSize;
 31    private static T _defaultValue;
 32
 33    /// <summary>
 34    /// Creates a new id map.
 35    /// </summary>
 036    public UniqueIdMap(T defaultValue, int blockSize = 32)
 037    {
 038        _blocks = new Dictionary<long, Block>();
 039        _blockSize = blockSize;
 040        _defaultValue = defaultValue;
 041    }
 42
 43    /// <summary>
 44    /// Sets a tile id.
 45    /// </summary>
 46    public void Set(long id, T vertex)
 047    {
 048        var blockIdx = id / _blockSize;
 049        var offset = id - blockIdx * _blockSize;
 50
 051        if (!_blocks.TryGetValue(blockIdx, out var block))
 052        {
 053            block = new Block
 054            {
 055                Start = (uint)offset,
 056                End = (uint)offset,
 057                Data = new T[] { vertex }
 058            };
 059            _blocks[blockIdx] = block;
 060        }
 61        else
 062        {
 063            block.Set(offset, vertex);
 064            _blocks[blockIdx] = block;
 065        }
 066    }
 67
 68    /// <summary>
 69    /// Gets or sets the tile id for the given id.
 70    /// </summary>
 71    public T this[long id]
 72    {
 073        get => this.Get(id);
 074        set => this.Set(id, value);
 75    }
 76
 77    /// <summary>
 78    /// Gets a tile id.
 79    /// </summary>
 80    public T Get(long id)
 081    {
 082        var blockIdx = id / _blockSize;
 083        var offset = id - blockIdx * _blockSize;
 84
 085        if (!_blocks.TryGetValue(blockIdx, out var block))
 086        {
 087            return _defaultValue;
 88        }
 89
 090        return block.Get(offset);
 091    }
 92
 93    /// <summary>
 94    /// An enumerable with the non-default indices in this map.
 95    /// </summary>
 096    public IEnumerable<long> NonDefaultIndices => throw new System.NotImplementedException();
 97
 98    private struct Block
 99    {
 0100        public uint Start { get; set; }
 101
 0102        public uint End { get; set; }
 103
 0104        public T[] Data { get; set; }
 105
 106        public T Get(long offset)
 0107        {
 0108            if (this.Start > offset)
 0109            {
 0110                return _defaultValue;
 111            }
 0112            else if (offset > this.End)
 0113            {
 0114                return _defaultValue;
 115            }
 116
 0117            return this.Data[offset - this.Start];
 0118        }
 119
 120        public void Set(long offset, T value)
 0121        {
 0122            if (this.Start > offset)
 0123            { // expand at the beginning.
 0124                var newData = new T[this.End - offset + 1];
 0125                this.Data.CopyTo(newData, (int)(this.Start - offset));
 0126                for (var i = 1; i < this.Start - offset; i++)
 0127                {
 0128                    newData[i] = _defaultValue;
 0129                }
 130
 0131                this.Data = newData;
 0132                this.Start = (uint)offset;
 0133                this.Data[0] = value;
 0134            }
 0135            else if (this.End < offset)
 0136            { // expand at the end.
 0137                var newData = new T[offset - this.Start + 1];
 0138                this.Data.CopyTo(newData, 0);
 0139                for (var i = this.End + 1 - this.Start; i < newData.Length - 1; i++)
 0140                {
 0141                    newData[i] = _defaultValue;
 0142                }
 143
 0144                this.Data = newData;
 0145                this.End = (uint)offset;
 0146                this.Data[offset - this.Start] = value;
 0147            }
 148            else
 0149            {
 0150                this.Data[offset - this.Start] = value;
 0151            }
 0152        }
 153    }
 154}