Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Diagnostics;
using System;
 
namespace Microsoft.ML.OnnxRuntime.Tensors
{
    /// <summary>
    /// This class contains utilities for useful calculations with shape.
    /// </summary>
    public static class ShapeUtils
    {
        /// <summary>
        /// Returns a number of elements in the tensor from the given shape
        /// </summary>
        /// <param name="shape"></param>
        /// <returns>size</returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static long GetSizeForShape(ReadOnlySpan<long> shape)
        {
            long product = 1;
            foreach (var dim in shape)
            {
                if (dim < 0)
                {
                    throw new ArgumentOutOfRangeException($"Shape must not have negative elements: {dim}");
                }
                checked
                {
                    product *= dim;
                }
            }
            return product;
        }
 
        /// <summary>
        /// Gets the set of strides that can be used to calculate the offset of n-dimensions in a 1-dimensional layout
        /// </summary>
        /// <param name="dimensions"></param>
        /// <returns>an array of strides</returns>
        public static long[] GetStrides(ReadOnlySpan<long> dimensions)
        {
            long[] strides = new long[dimensions.Length];
 
            if (dimensions.Length == 0)
            {
                return strides;
            }
 
            long stride = 1;
            for (int i = strides.Length - 1; i >= 0; i--)
            {
                strides[i] = stride;
                if (dimensions[i] < 0)
                {
                    throw new ArgumentException($"Dimension {i} is negative");
                }
                stride *= dimensions[i];
            }
 
            return strides;
        }
 
        /// <summary>
        /// Calculates the 1-d index for n-d indices in layout specified by strides.
        /// </summary>
        /// <param name="strides">pre-calculated strides</param>
        /// <param name="indices">Indices. Must have the same length as strides</param>
        /// <param name="startFromDimension"></param>
        /// <returns>A 1-d index into the tensor buffer</returns>
        public static long GetIndex(ReadOnlySpan<long> strides, ReadOnlySpan<long> indices, int startFromDimension = 0)
        {
            Debug.Assert(strides.Length == indices.Length);
 
            long index = 0;
            for (int i = startFromDimension; i < indices.Length; i++)
            {
                index += strides[i] * indices[i];
            }
 
            return index;
        }
    }
}