< 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:263_26948838820

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)
 110250311    {
 110250312        var d0 = (byte)(value & Mask);
 110250313        value >>= 7;
 110250314        if (value == 0)
 66820115        {
 66820116            data[(int)i] = d0;
 66820117            return 1;
 18        }
 19
 43430220        d0 += 128;
 43430221        var d1 = (byte)(value & Mask);
 43430222        value >>= 7;
 43430223        if (value == 0)
 39350624        {
 39350625            data[(int)i] = d0;
 39350626            data[(int)i + 1] = d1;
 39350627            return 2;
 28        }
 29
 4079630        d1 += 128;
 4079631        var d2 = (byte)(value & Mask);
 4079632        value >>= 7;
 4079633        if (value == 0)
 4078834        {
 4078835            data[(int)i] = d0;
 4078836            data[(int)i + 1] = d1;
 4078837            data[(int)i + 2] = d2;
 4078838            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;
 110250361    }
 62
 63    public static byte SetDynamicUInt64(this byte[] data, long i, ulong value)
 6224264    {
 6224265        var d0 = (byte)(value & Mask);
 6224266        value >>= 7;
 6224267        if (value == 0)
 5743968        {
 5743969            data[(int)i] = d0;
 5743970            return 1;
 71        }
 72
 480373        d0 += 128;
 480374        var d1 = (byte)(value & Mask);
 480375        value >>= 7;
 480376        if (value == 0)
 11277        {
 11278            data[(int)i] = d0;
 11279            data[(int)i + 1] = d1;
 11280            return 2;
 81        }
 82
 469183        d1 += 128;
 469184        var d2 = (byte)(value & Mask);
 469185        value >>= 7;
 469186        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
 469094        d2 += 128;
 469095        var d3 = (byte)(value & Mask);
 469096        value >>= 7;
 469097        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
 4689106        d3 += 128;
 4689107        var d4 = (byte)(value & Mask);
 4689108        value >>= 7;
 4689109        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
 4688119        d4 += 128;
 4688120        var d5 = (byte)(value & Mask);
 4688121        value >>= 7;
 4688122        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
 4687133        d5 += 128;
 4687134        var d6 = (byte)(value & Mask);
 4687135        value >>= 7;
 4687136        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
 4686148        d6 += 128;
 4686149        var d7 = (byte)(value & Mask);
 4686150        value >>= 7;
 4686151        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
 4686164        d7 += 128;
 4686165        var d8 = (byte)(value & Mask);
 4686166        value >>= 7;
 4686167        if (value == 0)
 4685168        {
 4685169            data[(int)i] = d0;
 4685170            data[(int)i + 1] = d1;
 4685171            data[(int)i + 2] = d2;
 4685172            data[(int)i + 3] = d3;
 4685173            data[(int)i + 4] = d4;
 4685174            data[(int)i + 5] = d5;
 4685175            data[(int)i + 6] = d6;
 4685176            data[(int)i + 7] = d7;
 4685177            data[(int)i + 8] = d8;
 4685178            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;
 62242194    }
 195
 196    public static byte GetDynamicUInt32(this byte[] data, long i, out uint value)
 30573586197    {
 30573586198        var d = data[(int)i];
 30573586199        if (d < 128)
 7877806200        {
 7877806201            value = d;
 7877806202            return 1;
 203        }
 204
 22695780205        value = (uint)d - 128;
 22695780206        d = data[(int)i + 1];
 22695780207        if (d < 128)
 18604956208        {
 18604956209            value += (uint)d << 7;
 18604956210            return 2;
 211        }
 212
 4090824213        d -= 128;
 4090824214        value += (uint)d << 7;
 4090824215        d = data[(int)i + 2];
 4090824216        if (d < 128)
 4090778217        {
 4090778218            value += (uint)d << 14;
 4090778219            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;
 30573586236    }
 237
 238    public static byte GetDynamicUInt64(this byte[] data, long i, out ulong value)
 12944770239    {
 12944770240        var d = data[(int)i];
 12944770241        if (d < 128)
 2243776242        {
 2243776243            value = d;
 2243776244            return 1;
 245        }
 246
 10700994247        value = (ulong)d - 128;
 10700994248        d = data[(int)i + 1];
 10700994249        if (d < 128)
 10587877250        {
 10587877251            value += (uint)d << 7;
 10587877252            return 2;
 253        }
 254
 113117255        d -= 128;
 113117256        value += (ulong)d << 7;
 113117257        d = data[(int)i + 2];
 113117258        if (d < 128)
 1259        {
 1260            value += (uint)d << 14;
 1261            return 3;
 262        }
 263
 113116264        d -= 128;
 113116265        value += (ulong)d << 14;
 113116266        d = data[(int)i + 3];
 113116267        if (d < 128)
 1268        {
 1269            value += (ulong)d << 21;
 1270            return 4;
 271        }
 272
 113115273        d -= 128;
 113115274        value += (ulong)d << 21;
 113115275        d = data[(int)i + 4];
 113115276        if (d < 128)
 1277        {
 1278            value += (ulong)d << 28;
 1279            return 5;
 280        }
 281
 113114282        d -= 128;
 113114283        value += (ulong)d << 28;
 113114284        d = data[(int)i + 5];
 113114285        if (d < 128)
 1286        {
 1287            value += (ulong)d << 35;
 1288            return 6;
 289        }
 290
 113113291        d -= 128;
 113113292        value += (ulong)d << 35;
 113113293        d = data[(int)i + 6];
 113113294        if (d < 128)
 1295        {
 1296            value += (ulong)d << 42;
 1297            return 7;
 298        }
 299
 113112300        d -= 128;
 113112301        value += (ulong)d << 42;
 113112302        d = data[(int)i + 7];
 113112303        if (d < 128)
 0304        {
 0305            value += (ulong)d << 49;
 0306            return 8;
 307        }
 308
 113112309        d -= 128;
 113112310        value += (ulong)d << 49;
 113112311        d = data[(int)i + 8];
 113112312        if (d < 128)
 113111313        {
 113111314            value += (ulong)d << 56;
 113111315            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;
 12944770323    }
 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)
 149425349    {
 149425350        return (uint)((value << 1) ^ (value >> 31));
 149425351    }
 352
 353    public static int ZigZagDecode32(uint value)
 2919022354    {
 2919022355        return (int)((value >> 1) ^ (~(value & 1) + 1));
 2919022356    }
 357
 358    public static ulong ZigZagEncode64(long value)
 144359    {
 144360        return (ulong)((value << 1) ^ (value >> 63));
 144361    }
 362
 363    public static long ZigZagDecode64(ulong value)
 117364    {
 117365        return (long)((value >> 1) ^ (~(value & 1) + 1));
 117366    }
 367
 368    public static byte SetDynamicInt32(this byte[] data, long i, int value)
 149425369    {
 149425370        return data.SetDynamicUInt32(i, ZigZagEncode32(value));
 149425371    }
 372
 373    public static byte GetDynamicInt32(this byte[] data, long i, out int value)
 2919022374    {
 2919022375        var c = data.GetDynamicUInt32(i, out var unsigned);
 2919022376        value = ZigZagDecode32(unsigned);
 2919022377        return c;
 2919022378    }
 379
 380    public static byte SetDynamicInt64(this byte[] data, long i, long value)
 76381    {
 76382        return data.SetDynamicUInt64(i, ZigZagEncode64(value));
 76383    }
 384
 385    public static byte GetDynamicInt64(this byte[] data, long i, out long value)
 74386    {
 74387        var c = data.GetDynamicUInt64(i, out var unsigned);
 74388        value = ZigZagDecode64(unsigned);
 74389        return c;
 74390    }
 391
 392    public static byte SetDynamicUInt32Nullable(this byte[] data, long i, uint? value)
 106130393    {
 106130394        value = value == null ? 0 : value + 1;
 106130395        return data.SetDynamicUInt32(i, value.Value);
 106130396    }
 397
 398    public static byte GetDynamicUInt32Nullable(this byte[] data, long i, out uint? value)
 12942502399    {
 12942502400        var c = data.GetDynamicUInt32(i, out var unsigned);
 12942502401        value = unsigned == 0 ? null : (uint?)unsigned - 1;
 12942502402        return c;
 12942502403    }
 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)
 57472419    {
 114878420        if (value == null) return data.SetDynamicUInt64(i, 0);
 421
 66422        var unsigned = ZigZagEncode64(value.Value) + 1;
 66423        return data.SetDynamicUInt64(i, unsigned);
 57472424    }
 425
 426    public static byte GetDynamicInt64Nullable(this byte[] data, long i, out long? value)
 2213427    {
 2213428        var c = data.GetDynamicUInt64(i, out var unsigned);
 2213429        if (unsigned == 0)
 2172430        {
 2172431            value = null;
 2172432        }
 433        else
 41434        {
 41435            value = ZigZagDecode64(unsigned - 1);
 41436        }
 2213437        return c;
 2213438    }
 439
 440    public static void SetFixed(this byte[] data, long i, int bytes, int value)
 75477441    {
 603740442        for (var b = 0; b < bytes; b++)
 226393443        {
 226393444            data[(int)i + b] = (byte)(value & byte.MaxValue);
 226393445            value >>= 8;
 226393446        }
 75477447    }
 448
 449    public static void GetFixed(this byte[] data, long i, int bytes, out int value)
 11224752450    {
 11224752451        value = 0;
 89797838452        for (var b = 0; b < bytes; b++)
 33674167453        {
 33674167454            value += data[(int)i + b] << (b * 8);
 33674167455        }
 11224752456    }
 457
 458    public static byte SetGlobalEdgeId(this byte[] data, long p, GlobalEdgeId globalEdgeId)
 45459    {
 45460        var c = data.SetDynamicInt64(p, globalEdgeId.EdgeId);
 45461        c += data.SetDynamicUInt32(p + c, globalEdgeId.Tail);
 45462        c += data.SetDynamicUInt32(p + c, globalEdgeId.Head);
 45463        return c;
 45464    }
 465
 466    public static byte GetGlobalEdgeId(this byte[] data, long p, out GlobalEdgeId globalEdgeId)
 44467    {
 44468        var c = data.GetDynamicInt64(p, out var edgeId);
 44469        c += data.GetDynamicUInt32(p + c, out var tail);
 44470        c += data.GetDynamicUInt32(p + c, out var head);
 471
 44472        globalEdgeId = GlobalEdgeId.Create(edgeId, tail, head);
 473
 44474        return c;
 44475    }
 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}