1 | using System;
|
---|
2 | using System.Runtime.InteropServices;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.igraph.Wrappers {
|
---|
5 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
---|
6 | public class Matrix : IDisposable {
|
---|
7 | igraph_matrix_t matrix;
|
---|
8 | internal igraph_matrix_t NativeInstance { get { return matrix; } }
|
---|
9 | public int Rows { get { return matrix.nrow; } }
|
---|
10 | public int Columns { get { return matrix.ncol; } }
|
---|
11 |
|
---|
12 | public Matrix(int nrow, int ncol) {
|
---|
13 | matrix = new igraph_matrix_t();
|
---|
14 | DllImporter.igraph_matrix_init(matrix, nrow, ncol);
|
---|
15 | }
|
---|
16 | ~Matrix() {
|
---|
17 | DllImporter.igraph_matrix_destroy(matrix);
|
---|
18 | }
|
---|
19 |
|
---|
20 | public void Dispose() {
|
---|
21 | if (matrix == null) return;
|
---|
22 | DllImporter.igraph_matrix_destroy(matrix);
|
---|
23 | matrix = null;
|
---|
24 | GC.SuppressFinalize(this);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public double this[int row, int col] {
|
---|
28 | get {
|
---|
29 | if (row < 0 || row > Rows || col < 0 || col > Columns) throw new IndexOutOfRangeException("Trying to get cell(" + row + ";" + col + ") of matrix(" + Rows + ";" + Columns + ").");
|
---|
30 | return DllImporter.igraph_matrix_e(matrix, row, col);
|
---|
31 | }
|
---|
32 | set {
|
---|
33 | if (row < 0 || row > Rows || col < 0 || col > Columns) throw new IndexOutOfRangeException("Trying to set cell(" + row + ";" + col + ") of matrix(" + Rows + ";" + Columns + ").");
|
---|
34 | DllImporter.igraph_matrix_set(matrix, row, col, value);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public double[,] ToMatrix() {
|
---|
39 | var result = new double[Rows, Columns];
|
---|
40 | for (var i = 0; i < Rows; i++) {
|
---|
41 | for (var j = 0; j < Columns; j++) {
|
---|
42 | result[i, j] = DllImporter.igraph_matrix_e(matrix, i, j);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | return result;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|