source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Vector.cs @ 14244

Last change on this file since 14244 was 14244, checked in by abeham, 5 years ago

#2651: worked on igraph integration, additional layout algorithms, density, page rank

  • added unit tests
File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23
24namespace HeuristicLab.IGraph.Wrappers {
25  public sealed class Vector : IDisposable {
26    private igraph_vector_t vector;
27
28    internal igraph_vector_t NativeInstance {
29      get { return vector; }
30    }
31
32    public int Length {
33      get { return DllImporter.igraph_vector_size(vector); }
34    }
35
36    public Vector(int length) {
37      if (length < 0) throw new ArgumentException("Rows and Columns must be >= 0");
38      vector = new igraph_vector_t();
39      DllImporter.igraph_vector_init(vector, length);
40    }
41
42    public Vector(Vector other) {
43      if (other == null) throw new ArgumentNullException("other");
44      vector = new igraph_vector_t();
45      DllImporter.igraph_vector_copy(vector, other.NativeInstance);
46    }
47
48    ~Vector() {
49      DllImporter.igraph_vector_destroy(vector);
50    }
51
52    public void Dispose() {
53      if (vector == null) return;
54      DllImporter.igraph_vector_destroy(vector);
55      vector = null;
56      GC.SuppressFinalize(this);
57    }
58
59    public double this[int index] {
60      get {
61        if (index < 0 || index > Length) throw new IndexOutOfRangeException("Trying to get index(" + index + ") of vector(" + Length + ").");
62        return DllImporter.igraph_vector_e(vector, index);
63      }
64      set {
65        if (index < 0 || index > Length) throw new IndexOutOfRangeException("Trying to set index(" + index + ") of vector(" + Length + ").");
66        DllImporter.igraph_vector_set(vector, index, value);
67      }
68    }
69
70    public double[] ToArray() {
71      var result = new double[Length];
72      for (var i = 0; i < result.Length; i++) {
73        result[i] = DllImporter.igraph_vector_e(vector, i);
74      }
75      return result;
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.