< Summary

Class:Itinero.Network.Storage.BitCoder
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Storage/BitCoder.cs
Covered lines:322
Uncovered lines:49
Coverable lines:371
Total lines:491
Line coverage:86.7% (322 of 371)
Covered branches:63
Total branches:80
Branch coverage:78.7% (63 of 80)
Tag:284_38118444584

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
SetDynamicUInt32(...)100%8100%
SetDynamicUInt64(...)94.44%1891.15%
GetDynamicUInt32(...)100%8100%
GetDynamicUInt64(...)94.44%1895.52%
SetGuid(...)0%20%
GetGuid(...)0%20%
ZigZagEncode32(...)100%1100%
ZigZagDecode32(...)100%1100%
ZigZagEncode64(...)100%1100%
ZigZagDecode64(...)100%1100%
SetDynamicInt32(...)100%1100%
GetDynamicInt32(...)100%1100%
SetDynamicInt64(...)100%1100%
GetDynamicInt64(...)100%1100%
SetDynamicUInt32Nullable(...)75%4100%
GetDynamicUInt32Nullable(...)100%2100%
SetDynamicUInt64Nullable(...)0%40%
GetDynamicUInt64Nullable(...)0%40%
SetDynamicInt64Nullable(...)100%2100%
GetDynamicInt64Nullable(...)100%2100%
SetFixed(...)100%2100%
GetFixed(...)100%2100%
SetGlobalEdgeId(...)100%1100%
GetGlobalEdgeId(...)100%1100%
SetGlobalEdgeIdNullable(...)0%20%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Storage/BitCoder.cs

#LineLine coverage
 1using System;
 2using Itinero.Network.Tiles.Standalone.Global;
 3
 4namespace Itinero.Network.Storage;
 5
 6internal static class BitCoder
 7{
 8    private const byte Mask = 128 - 1;
 9
 10    public static byte SetDynamicUInt32(this byte[] data, long i, uint value)
 132932011    {
 132932012        var d0 = (byte)(value & Mask);
 132932013        value >>= 7;
 132932014        if (value == 0)
 84980015        {
 84980016            data[(int)i] = d0;
 84980017            return 1;
 18        }
 19
 47952020        d0 += 128;
 47952021        var d1 = (byte)(value & Mask);
 47952022        value >>= 7;
 47952023        if (value == 0)
 43144424        {
 43144425            data[(int)i] = d0;
 43144426            data[(int)i + 1] = d1;
 43144427            return 2;
 28        }
 29
 4807630        d1 += 128;
 4807631        var d2 = (byte)(value & Mask);
 4807632        value >>= 7;
 4807633        if (value == 0)
 4806834        {
 4806835            data[(int)i] = d0;
 4806836            data[(int)i + 1] = d1;
 4806837            data[(int)i + 2] = d2;
 4806838            return 3;
 39        }
 40
 841        d2 += 128;
 842        var d3 = (byte)(value & Mask);
 843        value >>= 7;
 844        if (value == 0)
 745        {
 746            data[(int)i] = d0;
 747            data[(int)i + 1] = d1;
 748            data[(int)i + 2] = d2;
 749            data[(int)i + 3] = d3;
 750            return 4;
 51        }
 52
 153        d3 += 128;
 154        var d4 = (byte)(value & Mask);
 155        data[(int)i] = d0;
 156        data[(int)i + 1] = d1;
 157        data[(int)i + 2] = d2;
 158        data[(int)i + 3] = d3;
 159        data[(int)i + 4] = d4;
 160        return 5;
 132932061    }
 62
 63    public static byte SetDynamicUInt64(this byte[] data, long i, ulong value)
 8845064    {
 8845065        var d0 = (byte)(value & Mask);
 8845066        value >>= 7;
 8845067        if (value == 0)
 6655268        {
 6655269            data[(int)i] = d0;
 6655270            return 1;
 71        }
 72
 2189873        d0 += 128;
 2189874        var d1 = (byte)(value & Mask);
 2189875        value >>= 7;
 2189876        if (value == 0)
 1284177        {
 1284178            data[(int)i] = d0;
 1284179            data[(int)i + 1] = d1;
 1284180            return 2;
 81        }
 82
 905783        d1 += 128;
 905784        var d2 = (byte)(value & Mask);
 905785        value >>= 7;
 905786        if (value == 0)
 187        {
 188            data[(int)i] = d0;
 189            data[(int)i + 1] = d1;
 190            data[(int)i + 2] = d2;
 191            return 3;
 92        }
 93
 905694        d2 += 128;
 905695        var d3 = (byte)(value & Mask);
 905696        value >>= 7;
 905697        if (value == 0)
 198        {
 199            data[(int)i] = d0;
 1100            data[(int)i + 1] = d1;
 1101            data[(int)i + 2] = d2;
 1102            data[(int)i + 3] = d3;
 1103            return 4;
 104        }
 105
 9055106        d3 += 128;
 9055107        var d4 = (byte)(value & Mask);
 9055108        value >>= 7;
 9055109        if (value == 0)
 1110        {
 1111            data[(int)i] = d0;
 1112            data[(int)i + 1] = d1;
 1113            data[(int)i + 2] = d2;
 1114            data[(int)i + 3] = d3;
 1115            data[(int)i + 4] = d4;
 1116            return 5;
 117        }
 118
 9054119        d4 += 128;
 9054120        var d5 = (byte)(value & Mask);
 9054121        value >>= 7;
 9054122        if (value == 0)
 1123        {
 1124            data[(int)i] = d0;
 1125            data[(int)i + 1] = d1;
 1126            data[(int)i + 2] = d2;
 1127            data[(int)i + 3] = d3;
 1128            data[(int)i + 4] = d4;
 1129            data[(int)i + 5] = d5;
 1130            return 6;
 131        }
 132
 9053133        d5 += 128;
 9053134        var d6 = (byte)(value & Mask);
 9053135        value >>= 7;
 9053136        if (value == 0)
 1137        {
 1138            data[(int)i] = d0;
 1139            data[(int)i + 1] = d1;
 1140            data[(int)i + 2] = d2;
 1141            data[(int)i + 3] = d3;
 1142            data[(int)i + 4] = d4;
 1143            data[(int)i + 5] = d5;
 1144            data[(int)i + 6] = d6;
 1145            return 7;
 146        }
 147
 9052148        d6 += 128;
 9052149        var d7 = (byte)(value & Mask);
 9052150        value >>= 7;
 9052151        if (value == 0)
 0152        {
 0153            data[(int)i] = d0;
 0154            data[(int)i + 1] = d1;
 0155            data[(int)i + 2] = d2;
 0156            data[(int)i + 3] = d3;
 0157            data[(int)i + 4] = d4;
 0158            data[(int)i + 5] = d5;
 0159            data[(int)i + 6] = d6;
 0160            data[(int)i + 7] = d7;
 0161            return 8;
 162        }
 163
 9052164        d7 += 128;
 9052165        var d8 = (byte)(value & Mask);
 9052166        value >>= 7;
 9052167        if (value == 0)
 9051168        {
 9051169            data[(int)i] = d0;
 9051170            data[(int)i + 1] = d1;
 9051171            data[(int)i + 2] = d2;
 9051172            data[(int)i + 3] = d3;
 9051173            data[(int)i + 4] = d4;
 9051174            data[(int)i + 5] = d5;
 9051175            data[(int)i + 6] = d6;
 9051176            data[(int)i + 7] = d7;
 9051177            data[(int)i + 8] = d8;
 9051178            return 9;
 179        }
 180
 1181        d8 += 128;
 1182        var d9 = (byte)(value & Mask);
 1183        data[(int)i] = d0;
 1184        data[(int)i + 1] = d1;
 1185        data[(int)i + 2] = d2;
 1186        data[(int)i + 3] = d3;
 1187        data[(int)i + 4] = d4;
 1188        data[(int)i + 5] = d5;
 1189        data[(int)i + 6] = d6;
 1190        data[(int)i + 7] = d7;
 1191        data[(int)i + 8] = d8;
 1192        data[(int)i + 9] = d9;
 1193        return 10;
 88450194    }
 195
 196    public static byte GetDynamicUInt32(this byte[] data, long i, out uint value)
 31120392197    {
 31120392198        var d = data[(int)i];
 31120392199        if (d < 128)
 8249360200        {
 8249360201            value = d;
 8249360202            return 1;
 203        }
 204
 22871032205        value = (uint)d - 128;
 22871032206        d = data[(int)i + 1];
 22871032207        if (d < 128)
 18714442208        {
 18714442209            value += (uint)d << 7;
 18714442210            return 2;
 211        }
 212
 4156590213        d -= 128;
 4156590214        value += (uint)d << 7;
 4156590215        d = data[(int)i + 2];
 4156590216        if (d < 128)
 4156544217        {
 4156544218            value += (uint)d << 14;
 4156544219            return 3;
 220        }
 221
 46222        d -= 128;
 46223        value += (uint)d << 14;
 46224        d = data[(int)i + 3];
 46225        if (d < 128)
 45226        {
 45227            value += (uint)d << 21;
 45228            return 4;
 229        }
 230
 1231        d -= 128;
 1232        value += (uint)d << 21;
 1233        d = data[(int)i + 4];
 1234        value += (uint)d << 28;
 1235        return 5;
 31120392236    }
 237
 238    public static byte GetDynamicUInt64(this byte[] data, long i, out ulong value)
 13172629239    {
 13172629240        var d = data[(int)i];
 13172629241        if (d < 128)
 2431520242        {
 2431520243            value = d;
 2431520244            return 1;
 245        }
 246
 10741109247        value = (ulong)d - 128;
 10741109248        d = data[(int)i + 1];
 10741109249        if (d < 128)
 10613818250        {
 10613818251            value += (uint)d << 7;
 10613818252            return 2;
 253        }
 254
 127291255        d -= 128;
 127291256        value += (ulong)d << 7;
 127291257        d = data[(int)i + 2];
 127291258        if (d < 128)
 1259        {
 1260            value += (uint)d << 14;
 1261            return 3;
 262        }
 263
 127290264        d -= 128;
 127290265        value += (ulong)d << 14;
 127290266        d = data[(int)i + 3];
 127290267        if (d < 128)
 1268        {
 1269            value += (ulong)d << 21;
 1270            return 4;
 271        }
 272
 127289273        d -= 128;
 127289274        value += (ulong)d << 21;
 127289275        d = data[(int)i + 4];
 127289276        if (d < 128)
 1277        {
 1278            value += (ulong)d << 28;
 1279            return 5;
 280        }
 281
 127288282        d -= 128;
 127288283        value += (ulong)d << 28;
 127288284        d = data[(int)i + 5];
 127288285        if (d < 128)
 1286        {
 1287            value += (ulong)d << 35;
 1288            return 6;
 289        }
 290
 127287291        d -= 128;
 127287292        value += (ulong)d << 35;
 127287293        d = data[(int)i + 6];
 127287294        if (d < 128)
 1295        {
 1296            value += (ulong)d << 42;
 1297            return 7;
 298        }
 299
 127286300        d -= 128;
 127286301        value += (ulong)d << 42;
 127286302        d = data[(int)i + 7];
 127286303        if (d < 128)
 0304        {
 0305            value += (ulong)d << 49;
 0306            return 8;
 307        }
 308
 127286309        d -= 128;
 127286310        value += (ulong)d << 49;
 127286311        d = data[(int)i + 8];
 127286312        if (d < 128)
 127285313        {
 127285314            value += (ulong)d << 56;
 127285315            return 9;
 316        }
 317
 1318        d -= 128;
 1319        value += (ulong)d << 56;
 1320        d = data[(int)i + 9];
 1321        value += (ulong)d << 63;
 1322        return 10;
 13172629323    }
 324
 325    public static long SetGuid(this byte[] data, long i, Guid value)
 0326    {
 0327        var bytes = value.ToByteArray();
 0328        for (var b = 0; b < 16; b++)
 0329        {
 0330            data[(int)i + b] = bytes[b];
 0331        }
 332
 0333        return 16;
 0334    }
 335
 336    public static byte GetGuid(this byte[] data, long i, out Guid value)
 0337    {
 0338        var bytes = new byte[16];
 0339        for (var b = 0; b < 16; b++)
 0340        {
 0341            bytes[b] = data[(int)i + b];
 0342        }
 343
 0344        value = new Guid(bytes);
 0345        return 16;
 0346    }
 347
 348    public static uint ZigZagEncode32(int value)
 171139349    {
 171139350        return (uint)((value << 1) ^ (value >> 31));
 171139351    }
 352
 353    public static int ZigZagDecode32(uint value)
 2919766354    {
 2919766355        return (int)((value >> 1) ^ (~(value & 1) + 1));
 2919766356    }
 357
 358    public static ulong ZigZagEncode64(long value)
 17439359    {
 17439360        return (ulong)((value << 1) ^ (value >> 63));
 17439361    }
 362
 363    public static long ZigZagDecode64(ulong value)
 13824364    {
 13824365        return (long)((value >> 1) ^ (~(value & 1) + 1));
 13824366    }
 367
 368    public static byte SetDynamicInt32(this byte[] data, long i, int value)
 171139369    {
 171139370        return data.SetDynamicUInt32(i, ZigZagEncode32(value));
 171139371    }
 372
 373    public static byte GetDynamicInt32(this byte[] data, long i, out int value)
 2919766374    {
 2919766375        var c = data.GetDynamicUInt32(i, out var unsigned);
 2919766376        value = ZigZagDecode32(unsigned);
 2919766377        return c;
 2919766378    }
 379
 380    public static byte SetDynamicInt64(this byte[] data, long i, long value)
 10126381    {
 10126382        return data.SetDynamicUInt64(i, ZigZagEncode64(value));
 10126383    }
 384
 385    public static byte GetDynamicInt64(this byte[] data, long i, out long value)
 10124386    {
 10124387        var c = data.GetDynamicUInt64(i, out var unsigned);
 10124388        value = ZigZagDecode64(unsigned);
 10124389        return c;
 10124390    }
 391
 392    public static byte SetDynamicUInt32Nullable(this byte[] data, long i, uint? value)
 132592393    {
 132592394        value = value == null ? 0 : value + 1;
 132592395        return data.SetDynamicUInt32(i, value.Value);
 132592396    }
 397
 398    public static byte GetDynamicUInt32Nullable(this byte[] data, long i, out uint? value)
 13158448399    {
 13158448400        var c = data.GetDynamicUInt32(i, out var unsigned);
 13158448401        value = unsigned == 0 ? null : (uint?)unsigned - 1;
 13158448402        return c;
 13158448403    }
 404
 405    public static byte SetDynamicUInt64Nullable(this byte[] data, long i, ulong? value)
 0406    {
 0407        value = value == null ? 0 : value + 1;
 0408        return data.SetDynamicUInt64(i, value.Value);
 0409    }
 410
 411    public static byte GetDynamicUInt64Nullable(this byte[] data, long i, out ulong? value)
 0412    {
 0413        var c = data.GetDynamicUInt64(i, out var unsigned);
 0414        value = unsigned == 0 ? null : (uint?)unsigned - 1;
 0415        return c;
 0416    }
 417
 418    public static byte SetDynamicInt64Nullable(this byte[] data, long i, long? value)
 69264419    {
 131217420        if (value == null) return data.SetDynamicUInt64(i, 0);
 421
 7311422        var unsigned = ZigZagEncode64(value.Value) + 1;
 7311423        return data.SetDynamicUInt64(i, unsigned);
 69264424    }
 425
 426    public static byte GetDynamicInt64Nullable(this byte[] data, long i, out long? value)
 5912427    {
 5912428        var c = data.GetDynamicUInt64(i, out var unsigned);
 5912429        if (unsigned == 0)
 2214430        {
 2214431            value = null;
 2214432        }
 433        else
 3698434        {
 3698435            value = ZigZagDecode64(unsigned - 1);
 3698436        }
 5912437        return c;
 5912438    }
 439
 440    public static void SetFixed(this byte[] data, long i, int bytes, int value)
 93153441    {
 745148442        for (var b = 0; b < bytes; b++)
 279421443        {
 279421444            data[(int)i + b] = (byte)(value & byte.MaxValue);
 279421445            value >>= 8;
 279421446        }
 93153447    }
 448
 449    public static void GetFixed(this byte[] data, long i, int bytes, out int value)
 11324970450    {
 11324970451        value = 0;
 90599582452        for (var b = 0; b < bytes; b++)
 33974821453        {
 33974821454            value += data[(int)i + b] << (b * 8);
 33974821455        }
 11324970456    }
 457
 458    public static byte SetGlobalEdgeId(this byte[] data, long p, GlobalEdgeId globalEdgeId)
 5529459    {
 5529460        var c = data.SetDynamicInt64(p, globalEdgeId.EdgeId);
 5529461        c += data.SetDynamicUInt32(p + c, globalEdgeId.Tail);
 5529462        c += data.SetDynamicUInt32(p + c, globalEdgeId.Head);
 5529463        return c;
 5529464    }
 465
 466    public static byte GetGlobalEdgeId(this byte[] data, long p, out GlobalEdgeId globalEdgeId)
 5528467    {
 5528468        var c = data.GetDynamicInt64(p, out var edgeId);
 5528469        c += data.GetDynamicUInt32(p + c, out var tail);
 5528470        c += data.GetDynamicUInt32(p + c, out var head);
 471
 5528472        globalEdgeId = GlobalEdgeId.Create(edgeId, tail, head);
 473
 5528474        return c;
 5528475    }
 476
 477    public static byte SetGlobalEdgeIdNullable(this byte[] data, long p, GlobalEdgeId? globalEdgeId)
 0478    {
 0479        if (globalEdgeId == null)
 0480        {
 0481            return data.SetDynamicInt64Nullable(p, null);
 482        }
 483        else
 0484        {
 0485            var c = data.SetDynamicInt64Nullable(p, globalEdgeId.Value.EdgeId);
 0486            c += data.SetDynamicUInt32(p + c, globalEdgeId.Value.Tail);
 0487            c += data.SetDynamicUInt32(p + c, globalEdgeId.Value.Head);
 0488            return c;
 489        }
 0490    }
 491}