< Summary

Class:Itinero.Network.Storage.BitCoder
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Storage/BitCoder.cs
Covered lines:296
Uncovered lines:75
Coverable lines:371
Total lines:491
Line coverage:79.7% (296 of 371)
Covered branches:61
Total branches:80
Branch coverage:76.2% (61 of 80)
Tag:251_23667616543

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%10%
GetDynamicInt64(...)100%10%
SetDynamicUInt32Nullable(...)75%4100%
GetDynamicUInt32Nullable(...)100%2100%
SetDynamicUInt64Nullable(...)0%40%
GetDynamicUInt64Nullable(...)0%40%
SetDynamicInt64Nullable(...)50%260%
GetDynamicInt64Nullable(...)50%272.72%
SetFixed(...)100%2100%
GetFixed(...)100%2100%
SetGlobalEdgeId(...)100%10%
GetGlobalEdgeId(...)100%10%
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)
 103785211    {
 103785212        var d0 = (byte)(value & Mask);
 103785213        value >>= 7;
 103785214        if (value == 0)
 62845615        {
 62845616            data[(int)i] = d0;
 62845617            return 1;
 18        }
 19
 40939620        d0 += 128;
 40939621        var d1 = (byte)(value & Mask);
 40939622        value >>= 7;
 40939623        if (value == 0)
 37167424        {
 37167425            data[(int)i] = d0;
 37167426            data[(int)i + 1] = d1;
 37167427            return 2;
 28        }
 29
 3772230        d1 += 128;
 3772231        var d2 = (byte)(value & Mask);
 3772232        value >>= 7;
 3772233        if (value == 0)
 3771434        {
 3771435            data[(int)i] = d0;
 3771436            data[(int)i + 1] = d1;
 3771437            data[(int)i + 2] = d2;
 3771438            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;
 103785261    }
 62
 63    public static byte SetDynamicUInt64(this byte[] data, long i, ulong value)
 5730664    {
 5730665        var d0 = (byte)(value & Mask);
 5730666        value >>= 7;
 5730667        if (value == 0)
 5275068        {
 5275069            data[(int)i] = d0;
 5275070            return 1;
 71        }
 72
 455673        d0 += 128;
 455674        var d1 = (byte)(value & Mask);
 455675        value >>= 7;
 455676        if (value == 0)
 177        {
 178            data[(int)i] = d0;
 179            data[(int)i + 1] = d1;
 180            return 2;
 81        }
 82
 455583        d1 += 128;
 455584        var d2 = (byte)(value & Mask);
 455585        value >>= 7;
 455586        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
 455494        d2 += 128;
 455495        var d3 = (byte)(value & Mask);
 455496        value >>= 7;
 455497        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
 4553106        d3 += 128;
 4553107        var d4 = (byte)(value & Mask);
 4553108        value >>= 7;
 4553109        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
 4552119        d4 += 128;
 4552120        var d5 = (byte)(value & Mask);
 4552121        value >>= 7;
 4552122        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
 4551133        d5 += 128;
 4551134        var d6 = (byte)(value & Mask);
 4551135        value >>= 7;
 4551136        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
 4550148        d6 += 128;
 4550149        var d7 = (byte)(value & Mask);
 4550150        value >>= 7;
 4550151        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
 4550164        d7 += 128;
 4550165        var d8 = (byte)(value & Mask);
 4550166        value >>= 7;
 4550167        if (value == 0)
 4549168        {
 4549169            data[(int)i] = d0;
 4549170            data[(int)i + 1] = d1;
 4549171            data[(int)i + 2] = d2;
 4549172            data[(int)i + 3] = d3;
 4549173            data[(int)i + 4] = d4;
 4549174            data[(int)i + 5] = d5;
 4549175            data[(int)i + 6] = d6;
 4549176            data[(int)i + 7] = d7;
 4549177            data[(int)i + 8] = d8;
 4549178            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;
 57306194    }
 195
 196    public static byte GetDynamicUInt32(this byte[] data, long i, out uint value)
 30361712197    {
 30361712198        var d = data[(int)i];
 30361712199        if (d < 128)
 7518241200        {
 7518241201            value = d;
 7518241202            return 1;
 203        }
 204
 22843471205        value = (uint)d - 128;
 22843471206        d = data[(int)i + 1];
 22843471207        if (d < 128)
 18728794208        {
 18728794209            value += (uint)d << 7;
 18728794210            return 2;
 211        }
 212
 4114677213        d -= 128;
 4114677214        value += (uint)d << 7;
 4114677215        d = data[(int)i + 2];
 4114677216        if (d < 128)
 4114672217        {
 4114672218            value += (uint)d << 14;
 4114672219            return 3;
 220        }
 221
 5222        d -= 128;
 5223        value += (uint)d << 14;
 5224        d = data[(int)i + 3];
 5225        if (d < 128)
 4226        {
 4227            value += (uint)d << 21;
 4228            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;
 30361712236    }
 237
 238    public static byte GetDynamicUInt64(this byte[] data, long i, out ulong value)
 13087987239    {
 13087987240        var d = data[(int)i];
 13087987241        if (d < 128)
 2198930242        {
 2198930243            value = d;
 2198930244            return 1;
 245        }
 246
 10889057247        value = (ulong)d - 128;
 10889057248        d = data[(int)i + 1];
 10889057249        if (d < 128)
 10777093250        {
 10777093251            value += (uint)d << 7;
 10777093252            return 2;
 253        }
 254
 111964255        d -= 128;
 111964256        value += (ulong)d << 7;
 111964257        d = data[(int)i + 2];
 111964258        if (d < 128)
 1259        {
 1260            value += (uint)d << 14;
 1261            return 3;
 262        }
 263
 111963264        d -= 128;
 111963265        value += (ulong)d << 14;
 111963266        d = data[(int)i + 3];
 111963267        if (d < 128)
 1268        {
 1269            value += (ulong)d << 21;
 1270            return 4;
 271        }
 272
 111962273        d -= 128;
 111962274        value += (ulong)d << 21;
 111962275        d = data[(int)i + 4];
 111962276        if (d < 128)
 1277        {
 1278            value += (ulong)d << 28;
 1279            return 5;
 280        }
 281
 111961282        d -= 128;
 111961283        value += (ulong)d << 28;
 111961284        d = data[(int)i + 5];
 111961285        if (d < 128)
 1286        {
 1287            value += (ulong)d << 35;
 1288            return 6;
 289        }
 290
 111960291        d -= 128;
 111960292        value += (ulong)d << 35;
 111960293        d = data[(int)i + 6];
 111960294        if (d < 128)
 1295        {
 1296            value += (ulong)d << 42;
 1297            return 7;
 298        }
 299
 111959300        d -= 128;
 111959301        value += (ulong)d << 42;
 111959302        d = data[(int)i + 7];
 111959303        if (d < 128)
 0304        {
 0305            value += (ulong)d << 49;
 0306            return 8;
 307        }
 308
 111959309        d -= 128;
 111959310        value += (ulong)d << 49;
 111959311        d = data[(int)i + 8];
 111959312        if (d < 128)
 111958313        {
 111958314            value += (ulong)d << 56;
 111958315            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;
 13087987323    }
 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)
 149363349    {
 149363350        return (uint)((value << 1) ^ (value >> 31));
 149363351    }
 352
 353    public static int ZigZagDecode32(uint value)
 2717438354    {
 2717438355        return (int)((value >> 1) ^ (~(value & 1) + 1));
 2717438356    }
 357
 358    public static ulong ZigZagEncode64(long value)
 2359    {
 2360        return (ulong)((value << 1) ^ (value >> 63));
 2361    }
 362
 363    public static long ZigZagDecode64(ulong value)
 2364    {
 2365        return (long)((value >> 1) ^ (~(value & 1) + 1));
 2366    }
 367
 368    public static byte SetDynamicInt32(this byte[] data, long i, int value)
 149363369    {
 149363370        return data.SetDynamicUInt32(i, ZigZagEncode32(value));
 149363371    }
 372
 373    public static byte GetDynamicInt32(this byte[] data, long i, out int value)
 2717438374    {
 2717438375        var c = data.GetDynamicUInt32(i, out var unsigned);
 2717438376        value = ZigZagDecode32(unsigned);
 2717438377        return c;
 2717438378    }
 379
 380    public static byte SetDynamicInt64(this byte[] data, long i, long value)
 0381    {
 0382        return data.SetDynamicUInt64(i, ZigZagEncode64(value));
 0383    }
 384
 385    public static byte GetDynamicInt64(this byte[] data, long i, out long value)
 0386    {
 0387        var c = data.GetDynamicUInt64(i, out var unsigned);
 0388        value = ZigZagDecode64(unsigned);
 0389        return c;
 0390    }
 391
 392    public static byte SetDynamicUInt32Nullable(this byte[] data, long i, uint? value)
 103838393    {
 103838394        value = value == null ? 0 : value + 1;
 103838395        return data.SetDynamicUInt32(i, value.Value);
 103838396    }
 397
 398    public static byte GetDynamicUInt32Nullable(this byte[] data, long i, out uint? value)
 13086088399    {
 13086088400        var c = data.GetDynamicUInt32(i, out var unsigned);
 13086088401        value = unsigned == 0 ? null : (uint?)unsigned - 1;
 13086088402        return c;
 13086088403    }
 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)
 52748419    {
 105496420        if (value == null) return data.SetDynamicUInt64(i, 0);
 421
 0422        var unsigned = ZigZagEncode64(value.Value) + 1;
 0423        return data.SetDynamicUInt64(i, unsigned);
 52748424    }
 425
 426    public static byte GetDynamicInt64Nullable(this byte[] data, long i, out long? value)
 1890427    {
 1890428        var c = data.GetDynamicUInt64(i, out var unsigned);
 1890429        if (unsigned == 0)
 1890430        {
 1890431            value = null;
 1890432        }
 433        else
 0434        {
 0435            value = ZigZagDecode64(unsigned - 1);
 0436        }
 1890437        return c;
 1890438    }
 439
 440    public static void SetFixed(this byte[] data, long i, int bytes, int value)
 73885441    {
 591004442        for (var b = 0; b < bytes; b++)
 221617443        {
 221617444            data[(int)i + b] = (byte)(value & byte.MaxValue);
 221617445            value >>= 8;
 221617446        }
 73885447    }
 448
 449    public static void GetFixed(this byte[] data, long i, int bytes, out int value)
 11638700450    {
 11638700451        value = 0;
 93109422452        for (var b = 0; b < bytes; b++)
 34916011453        {
 34916011454            value += data[(int)i + b] << (b * 8);
 34916011455        }
 11638700456    }
 457
 458    public static byte SetGlobalEdgeId(this byte[] data, long p, GlobalEdgeId globalEdgeId)
 0459    {
 0460        var c = data.SetDynamicInt64(p, globalEdgeId.EdgeId);
 0461        c += data.SetDynamicUInt32(p + c, globalEdgeId.Tail);
 0462        c += data.SetDynamicUInt32(p + c, globalEdgeId.Head);
 0463        return c;
 0464    }
 465
 466    public static byte GetGlobalEdgeId(this byte[] data, long p, out GlobalEdgeId globalEdgeId)
 0467    {
 0468        var c = data.GetDynamicInt64(p, out var edgeId);
 0469        c += data.GetDynamicUInt32(p + c, out var tail);
 0470        c += data.GetDynamicUInt32(p + c, out var head);
 471
 0472        globalEdgeId = GlobalEdgeId.Create(edgeId, tail, head);
 473
 0474        return c;
 0475    }
 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}