DotNet Reference

.Net Reference

NestedArrayHelper.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 namespace Google.OrTools
15 {
16 
17  using System;
18  using System.Collections.Generic;
19 
20  public static class NestedArrayHelper
21  {
22  public static T[] GetFlatArray<T>(T[][] arr)
23  {
24  int flatLength = 0;
25  for (var i = 0; i < arr.GetLength(0); i++)
26  flatLength += arr[i].GetLength(0);
27 
28  int idx = 0;
29  T[] flat = new T[flatLength];
30 
31  for (int i = 0; i < arr.GetLength(0); i++)
32  {
33  for (int j = 0; j < arr[i].GetLength(0); j++)
34  flat[idx++] = arr[i][j];
35  }
36 
37  return flat;
38  }
39 
40  public static T[] GetFlatArrayFromMatrix<T>(T[,] arr)
41  {
42  int flatLength = arr.GetLength(0) * arr.GetLength(1);
43 
44  int idx = 0;
45  T[] flat = new T[flatLength];
46 
47  for (int i = 0; i < arr.GetLength(0); i++)
48  {
49  for (int j = 0; j < arr.GetLength(1); j++)
50  flat[idx++] = arr[i, j];
51  }
52 
53  return flat;
54  }
55 
56  public static int[] GetArraySecondSize<T>(T[][] arr)
57  {
58  var result = new int[arr.GetLength(0)];
59  for (var i = 0; i < arr.GetLength(0); i++)
60  {
61  if (arr[i] != null)
62  result[i] = arr[i].Length;
63  }
64  return result;
65  }
66  }
67 } // namespace Google.OrTools
static int[] GetArraySecondSize< T >(T[][] arr)
static T[] GetFlatArray< T >(T[][] arr)
using System
Definition: Program.cs:14
static T[] GetFlatArrayFromMatrix< T >(T[,] arr)