< Summary

Information
Class: Ice.UtilInternal.Collections
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/UtilInternal/Collections.cs
Tag: 71_18251537082
Line coverage
84%
Covered lines: 28
Uncovered lines: 5
Coverable lines: 33
Total lines: 83
Line coverage: 84.8%
Branch coverage
83%
Covered branches: 15
Total branches: 18
Branch coverage: 83.3%
Method coverage
100%
Covered methods: 5
Total methods: 5
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Shuffle<T>(...)25%6.74444.44%
Sort<T>(...)100%11100%
Sort1<T>(...)100%22100%
Merge<T>(...)100%1212100%
.cctor()100%11100%

File(s)

/home/runner/work/ice/ice/csharp/src/Ice/UtilInternal/Collections.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Diagnostics;
 4
 5namespace Ice.UtilInternal;
 6
 7public static class Collections
 8{
 9    public static void Shuffle<T>(ref List<T> l)
 10    {
 111        lock (_rand)
 12        {
 113            for (int j = 0; j < l.Count - 1; ++j)
 14            {
 015                int r = _rand.Next(l.Count - j) + j;
 16                Debug.Assert(r >= j && r < l.Count);
 017                if (r != j)
 18                {
 019                    T tmp = l[j];
 020                    l[j] = l[r];
 021                    l[r] = tmp;
 22                }
 23            }
 124        }
 125    }
 26
 27    internal static void Sort<T>(ref List<T> array, IComparer<T> comparator) =>
 28        //
 29        // This Sort method implements the merge sort algorithm
 30        // which is a stable sort (unlike the Sort method of the
 31        // System.Collections.ArrayList which is unstable).
 32        //
 133        Sort1(ref array, 0, array.Count, comparator);
 34
 35    private static void Sort1<T>(ref List<T> array, int begin, int end, IComparer<T> comparator)
 36    {
 37        int mid;
 138        if (end - begin <= 1)
 39        {
 140            return;
 41        }
 42
 143        mid = (begin + end) / 2;
 144        Sort1(ref array, begin, mid, comparator);
 145        Sort1(ref array, mid, end, comparator);
 146        Merge(ref array, begin, mid, end, comparator);
 147    }
 48
 49    private static void Merge<T>(ref List<T> array, int begin, int mid, int end, IComparer<T> comparator)
 50    {
 151        int i = begin;
 152        int j = mid;
 153        int k = 0;
 54
 155        var tmp = new T[end - begin];
 156        while (i < mid && j < end)
 57        {
 158            if (comparator.Compare(array[i], array[j]) <= 0)
 59            {
 160                tmp[k++] = array[i++];
 61            }
 62            else
 63            {
 164                tmp[k++] = array[j++];
 65            }
 66        }
 67
 168        while (i < mid)
 69        {
 170            tmp[k++] = array[i++];
 71        }
 172        while (j < end)
 73        {
 174            tmp[k++] = array[j++];
 75        }
 176        for (i = 0; i < (end - begin); ++i)
 77        {
 178            array[begin + i] = tmp[i];
 79        }
 180    }
 81
 182    private static readonly Random _rand = new(unchecked((int)DateTime.Now.Ticks));
 83}