DotNet Reference

DotNet 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 using System;
17 using System.Collections.Generic;
18 
19 public static class NestedArrayHelper
20 {
21  public static T[] GetFlatArray<T>(T[][] arr)
22  {
23  int flatLength = 0;
24  for (var i = 0; i < arr.GetLength(0); i++)
25  flatLength += arr[i].GetLength(0);
26 
27  int idx = 0;
28  T[] flat = new T[flatLength];
29 
30  for (int i = 0; i < arr.GetLength(0); i++)
31  {
32  for (int j = 0; j < arr[i].GetLength(0); j++)
33  flat[idx++] = arr[i][j];
34  }
35 
36  return flat;
37  }
38 
39  public static T[] GetFlatArrayFromMatrix<T>(T[,] arr)
40  {
41  int flatLength = arr.GetLength(0) * arr.GetLength(1);
42 
43  int idx = 0;
44  T[] flat = new T[flatLength];
45 
46  for (int i = 0; i < arr.GetLength(0); i++)
47  {
48  for (int j = 0; j < arr.GetLength(1); j++)
49  flat[idx++] = arr[i, j];
50  }
51 
52  return flat;
53  }
54 
55  public static int[] GetArraySecondSize<T>(T[][]arr)
56  {
57  var result = new int[arr.GetLength(0)];
58  for (var i=0; i<arr.GetLength(0); i++)
59  {
60  if (arr[i] != null)
61  result[i] = arr[i].Length;
62  }
63  return result;
64  }
65 }
66 } // namespace Google.OrTools
static T[] GetFlatArray< T >(T[][] arr)
static int[] GetArraySecondSize< T >(T[][]arr)
static T[] GetFlatArrayFromMatrix< T >(T[,] arr)