Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/DllImporter.cs @ 14247

Last change on this file since 14247 was 14247, checked in by abeham, 8 years ago

#2651:

  • partially reverted builder testsettings
  • added example on how to use native callbacks (depth-first-search and bfs)
File size: 31.8 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;
23using System.IO;
24using System.Runtime.InteropServices;
25
26namespace HeuristicLab.IGraph {
27  internal static class DllImporter {
28    private const string X86Dll = "igraph-0.8.0-pre-x86.dll";
29    private const string X64Dll = "igraph-0.8.0-pre-x64.dll";
30    private readonly static bool X86 = false;
31
32    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
33    private static extern bool SetDllDirectory(string lpPathName);
34
35    static DllImporter() {
36      X86 = !Environment.Is64BitProcess;
37      // Possible workaround as the builder doesn't execute the tests within the bin directory
38      if (Directory.Exists(Path.Combine(Environment.CurrentDirectory, "bin")))
39        SetDllDirectory(Path.Combine(Environment.CurrentDirectory, "bin"));
40    }
41
42    #region igraph
43    #region igraph init/finalize
44    internal static void igraph_empty(igraph_t graph, int n, bool directed) {
45      if (X86) igraph_empty_x86(graph, n, directed);
46      else igraph_empty_x64(graph, n, directed);
47    }
48    internal static int igraph_destroy(igraph_t graph) {
49      return X86 ? igraph_destroy_x86(graph) : igraph_destroy_x64(graph);
50    }
51    #endregion
52
53    #region igraph query
54    internal static int igraph_vcount(igraph_t graph) {
55      return X86 ? igraph_vcount_x86(graph) : igraph_vcount_x64(graph);
56    }
57    #endregion
58
59    #region igraph characteristics
60    internal static int igraph_density(igraph_t graph, out double density, bool loops) {
61      density = double.NaN;
62      return X86 ? igraph_density_x86(graph, ref density, loops) : igraph_density_x64(graph, ref density, loops);
63    }
64
65    internal static int igraph_pagerank(igraph_t graph, igraph_pagerank_algo_t algo, igraph_vector_t vector, out double value, igraph_vs_t vids, bool directed, double damping, igraph_vector_t weights) {
66      value = 1;
67      var options = IntPtr.Zero;
68      if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_ARPACK) {
69        var arpackoptions = GetDefaultArpackOptions();
70        options = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(igraph_arpack_options_t)));
71        Marshal.StructureToPtr(arpackoptions, options, false);
72      } else if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_POWER) {
73        var poweroptions = GetDefaultPowerOptions();
74        options = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(igraph_pagerank_power_options_t)));
75        Marshal.StructureToPtr(poweroptions, options, false);
76      }
77      try {
78        return X86 ? igraph_pagerank_x86(graph, algo, vector, ref value, vids, directed, damping, weights, options) : igraph_pagerank_x64(graph, algo, vector, ref value, vids, directed, damping, weights, options);
79      } finally {
80        if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_ARPACK) {
81          Marshal.DestroyStructure(options, typeof(igraph_arpack_options_t));
82          Marshal.FreeHGlobal(options);
83        } else if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_POWER) {
84          Marshal.DestroyStructure(options, typeof(igraph_pagerank_power_options_t));
85          Marshal.FreeHGlobal(options);
86        }
87      }
88    }
89
90    unsafe private static igraph_arpack_options_t GetDefaultArpackOptions() {
91      var arpackoptions = new igraph_arpack_options_t();
92      arpackoptions.bmat[0] = 'I';
93      arpackoptions.which[0] = arpackoptions.which[1] = 'X';
94      arpackoptions.nev = 1;
95      arpackoptions.ishift = 1;
96      arpackoptions.mxiter = 1000;
97      arpackoptions.nb = 1;
98      arpackoptions.mode = 1;
99      arpackoptions.iparam[0] = arpackoptions.ishift;
100      arpackoptions.iparam[1] = arpackoptions.iparam[4] = arpackoptions.iparam[5] = arpackoptions.iparam[7] = arpackoptions.iparam[8] = arpackoptions.iparam[9] = arpackoptions.iparam[10] = 0;
101      arpackoptions.iparam[2] = arpackoptions.mxiter;
102      arpackoptions.iparam[3] = arpackoptions.nb;
103      arpackoptions.iparam[6] = arpackoptions.mode;
104      return arpackoptions;
105    }
106
107    private static igraph_pagerank_power_options_t GetDefaultPowerOptions() {
108      var poweroptions = new igraph_pagerank_power_options_t();
109      poweroptions.niter = 50;
110      poweroptions.eps = 1e-5;
111      return poweroptions;
112    }
113    #endregion
114
115    #region igraph manipulation
116    internal static int igraph_add_edge(igraph_t graph, int from, int to) {
117      return X86 ? igraph_add_edge_x86(graph, from, to) : igraph_add_edge_x64(graph, from, to);
118    }
119    #endregion
120
121    #region igraph visits
122    internal static int igraph_bfs(igraph_t graph, int root, igraph_vector_t roots, igraph_neimode_t mode, bool unreachable, igraph_vector_t restricted, igraph_vector_t order, igraph_vector_t rank, igraph_vector_t father, igraph_vector_t pred, igraph_vector_t succ, igraph_vector_t dist, igraph_bfshandler_t callback, object tag) {
123      return MarshalIfExistsAndCall(tag, ptr => X86 ? igraph_bfs_x86(graph, root, roots, mode, unreachable, restricted, order, rank, father, pred, succ, dist, callback, ptr) : igraph_bfs_x64(graph, root, roots, mode, unreachable, restricted, order, rank, father, pred, succ, dist, callback, ptr));
124    }
125    internal static int igraph_dfs(igraph_t graph, int root, igraph_neimode_t mode, bool unreachable, igraph_vector_t order, igraph_vector_t order_out, igraph_vector_t father, igraph_vector_t dist, igraph_dfshandler_t inWrapper, igraph_dfshandler_t outWrapper, object tag) {
126      return MarshalIfExistsAndCall(tag, ptr => X86 ? igraph_dfs_x86(graph, root, mode, unreachable, order, order_out, father, dist, inWrapper, outWrapper, ptr) : igraph_dfs_x64(graph, root, mode, unreachable, order, order_out, father, dist, inWrapper, outWrapper, ptr));
127    }
128    #endregion
129
130    #region Platform specific DLL imports
131    [DllImport(X86Dll, EntryPoint = "igraph_empty", CallingConvention = CallingConvention.Cdecl)]
132    private static extern void igraph_empty_x86([In, Out]igraph_t graph, int n, bool directed);
133    [DllImport(X64Dll, EntryPoint = "igraph_empty", CallingConvention = CallingConvention.Cdecl)]
134    private static extern void igraph_empty_x64([In, Out]igraph_t graph, int n, bool directed);
135    [DllImport(X86Dll, EntryPoint = "igraph_destroy", CallingConvention = CallingConvention.Cdecl)]
136    private static extern int igraph_destroy_x86([In, Out]igraph_t graph);
137    [DllImport(X64Dll, EntryPoint = "igraph_destroy", CallingConvention = CallingConvention.Cdecl)]
138    private static extern int igraph_destroy_x64([In, Out]igraph_t graph);
139    [DllImport(X86Dll, EntryPoint = "igraph_vcount", CallingConvention = CallingConvention.Cdecl)]
140    private static extern int igraph_vcount_x86(igraph_t graph);
141    [DllImport(X64Dll, EntryPoint = "igraph_vcount", CallingConvention = CallingConvention.Cdecl)]
142    private static extern int igraph_vcount_x64(igraph_t graph);
143    [DllImport(X86Dll, EntryPoint = "igraph_add_edge", CallingConvention = CallingConvention.Cdecl)]
144    private static extern int igraph_add_edge_x86([In, Out]igraph_t graph, int from, int to);
145    [DllImport(X64Dll, EntryPoint = "igraph_add_edge", CallingConvention = CallingConvention.Cdecl)]
146    private static extern int igraph_add_edge_x64([In, Out]igraph_t graph, int from, int to);
147    [DllImport(X86Dll, EntryPoint = "igraph_density", CallingConvention = CallingConvention.Cdecl)]
148    private static extern int igraph_density_x86(igraph_t graph, ref double density, bool loops);
149    [DllImport(X64Dll, EntryPoint = "igraph_density", CallingConvention = CallingConvention.Cdecl)]
150    private static extern int igraph_density_x64(igraph_t graph, ref double density, bool loops);
151    [DllImport(X86Dll, EntryPoint = "igraph_pagerank", CallingConvention = CallingConvention.Cdecl)]
152    private static extern int igraph_pagerank_x86(igraph_t graph, igraph_pagerank_algo_t algo, [In, Out]igraph_vector_t vector, ref double value, igraph_vs_t vids, bool directed, double damping, igraph_vector_t weights, IntPtr options);
153    [DllImport(X64Dll, EntryPoint = "igraph_pagerank", CallingConvention = CallingConvention.Cdecl)]
154    private static extern int igraph_pagerank_x64(igraph_t graph, igraph_pagerank_algo_t algo, [In, Out]igraph_vector_t vector, ref double value, igraph_vs_t vids, bool directed, double damping, igraph_vector_t weights, IntPtr options);
155    [DllImport(X86Dll, EntryPoint = "igraph_bfs", CallingConvention = CallingConvention.Cdecl)]
156    private static extern int igraph_bfs_x86(igraph_t graph, int root, igraph_vector_t roots, igraph_neimode_t mode, bool unreachable, igraph_vector_t restricted, [In, Out]igraph_vector_t order, [In, Out]igraph_vector_t rank, [In, Out]igraph_vector_t father, [In, Out]igraph_vector_t pred, [In, Out]igraph_vector_t succ, [In, Out]igraph_vector_t dist, igraph_bfshandler_t callback, IntPtr extra);
157    [DllImport(X64Dll, EntryPoint = "igraph_bfs", CallingConvention = CallingConvention.Cdecl)]
158    private static extern int igraph_bfs_x64(igraph_t graph, int root, igraph_vector_t roots, igraph_neimode_t mode, bool unreachable, igraph_vector_t restricted, [In, Out]igraph_vector_t order, [In, Out]igraph_vector_t rank, [In, Out]igraph_vector_t father, [In, Out]igraph_vector_t pred, [In, Out]igraph_vector_t succ, [In, Out]igraph_vector_t dist, igraph_bfshandler_t callback, IntPtr extra);
159    [DllImport(X86Dll, EntryPoint = "igraph_dfs", CallingConvention = CallingConvention.Cdecl)]
160    private static extern int igraph_dfs_x86(igraph_t graph, int root, igraph_neimode_t mode, bool unreachable, [In, Out]igraph_vector_t order, [In, Out]igraph_vector_t order_out, [In, Out]igraph_vector_t father, [In, Out]igraph_vector_t dist, igraph_dfshandler_t in_callback, igraph_dfshandler_t out_callback, IntPtr extra);
161    [DllImport(X64Dll, EntryPoint = "igraph_dfs", CallingConvention = CallingConvention.Cdecl)]
162    private static extern int igraph_dfs_x64(igraph_t graph, int root, igraph_neimode_t mode, bool unreachable, [In, Out]igraph_vector_t order, [In, Out]igraph_vector_t order_out, [In, Out]igraph_vector_t father, [In, Out]igraph_vector_t dist, igraph_dfshandler_t in_callback, igraph_dfshandler_t out_callback, IntPtr extra);
163    #endregion
164    #endregion
165
166    #region igraph_rng
167    internal static int igraph_rng_get_integer(int l, int h) {
168      return X86 ? igraph_rng_get_integer_x86(igraph_rng_default_x86(), l, h) : igraph_rng_get_integer_x64(igraph_rng_default_x64(), l, h);
169    }
170    internal static int igraph_rng_seed(uint seed) {
171      return X86 ? igraph_rng_seed_x86(igraph_rng_default_x86(), seed) : igraph_rng_seed_x64(igraph_rng_default_x64(), seed);
172    }
173
174    #region Platform specific DLL imports
175    [DllImport(X86Dll, EntryPoint = "igraph_rng_default", CallingConvention = CallingConvention.Cdecl)]
176    private static extern IntPtr igraph_rng_default_x86();
177    [DllImport(X64Dll, EntryPoint = "igraph_rng_default", CallingConvention = CallingConvention.Cdecl)]
178    private static extern IntPtr igraph_rng_default_x64();
179    [DllImport(X86Dll, EntryPoint = "igraph_rng_get_integer", CallingConvention = CallingConvention.Cdecl)]
180    private static extern int igraph_rng_get_integer_x86(IntPtr rng, int l, int h);
181    [DllImport(X64Dll, EntryPoint = "igraph_rng_get_integer", CallingConvention = CallingConvention.Cdecl)]
182    private static extern int igraph_rng_get_integer_x64(IntPtr rng, int l, int h);
183    [DllImport(X86Dll, EntryPoint = "igraph_rng_seed", CallingConvention = CallingConvention.Cdecl)]
184    private static extern int igraph_rng_seed_x86(IntPtr rng, uint seed);
185    [DllImport(X64Dll, EntryPoint = "igraph_rng_seed", CallingConvention = CallingConvention.Cdecl)]
186    private static extern int igraph_rng_seed_x64(IntPtr rng, uint seed);
187    #endregion
188    #endregion
189
190    #region igraph_vector
191    internal static int igraph_vector_init(igraph_vector_t vector, int length) {
192      return X86 ? igraph_vector_init_x86(vector, length) : igraph_vector_init_x64(vector, length);
193    }
194    internal static int igraph_vector_init_copy(igraph_vector_t vector, double[] vec) {
195      return X86 ? igraph_vector_init_copy_x86(vector, vec, vec.Length) : igraph_vector_init_copy_x64(vector, vec, vec.Length);
196    }
197    internal static void igraph_vector_destroy(igraph_vector_t vector) {
198      if (X86) igraph_vector_destroy_x86(vector);
199      else igraph_vector_destroy_x64(vector);
200    }
201    internal static int igraph_vector_copy(igraph_vector_t to, igraph_vector_t from) {
202      return X86 ? igraph_vector_copy_x86(to, from) : igraph_vector_copy_x64(to, from);
203    }
204    internal static double[] igraph_vector_to_array(igraph_vector_t from) {
205      var len = igraph_vector_size(from);
206      var result = new double[len];
207      if (X86) igraph_vector_copy_to_x86(from, result);
208      else igraph_vector_copy_to_x64(from, result);
209      return result;
210    }
211
212    internal static int igraph_vector_size(igraph_vector_t vector) {
213      return X86 ? igraph_vector_size_x86(vector) : igraph_vector_size_x64(vector);
214    }
215
216    internal static double igraph_vector_e(igraph_vector_t vector, int index) {
217      return X86 ? igraph_vector_e_x86(vector, index) : igraph_vector_e_x64(vector, index);
218    }
219
220    internal static void igraph_vector_set(igraph_vector_t vector, int index, double value) {
221      if (X86) igraph_vector_set_x86(vector, index, value);
222      else igraph_vector_set_x64(vector, index, value);
223    }
224    internal static void igraph_vector_fill(igraph_vector_t vector, double v) {
225      if (X86) igraph_vector_fill_x86(vector, v);
226      else igraph_vector_fill_x64(vector, v);
227    }
228    internal static void igraph_vector_scale(igraph_vector_t vector, double by) {
229      if (X86) igraph_vector_scale_x86(vector, by);
230      else igraph_vector_scale_x64(vector, by);
231    }
232
233    internal static int igraph_vector_reverse(igraph_vector_t vector) {
234      return X86 ? igraph_vector_reverse_x86(vector) : igraph_vector_reverse_x64(vector);
235    }
236    internal static int igraph_vector_shuffle(igraph_vector_t vector) {
237      return X86 ? igraph_vector_shuffle_x86(vector) : igraph_vector_shuffle_x64(vector);
238    }
239
240    #region Platform specific DLL imports
241    [DllImport(X86Dll, EntryPoint = "igraph_vector_init", CallingConvention = CallingConvention.Cdecl)]
242    private static extern int igraph_vector_init_x86([In, Out]igraph_vector_t vector, int length);
243    [DllImport(X64Dll, EntryPoint = "igraph_vector_init", CallingConvention = CallingConvention.Cdecl)]
244    private static extern int igraph_vector_init_x64([In, Out]igraph_vector_t vector, int length);
245    [DllImport(X86Dll, EntryPoint = "igraph_vector_init_copy", CallingConvention = CallingConvention.Cdecl)]
246    private static extern int igraph_vector_init_copy_x86([In, Out]igraph_vector_t vector, double[] vec, int length);
247    [DllImport(X64Dll, EntryPoint = "igraph_vector_init_copy", CallingConvention = CallingConvention.Cdecl)]
248    private static extern int igraph_vector_init_copy_x64([In, Out]igraph_vector_t vector, double[] vec, int length);
249    [DllImport(X86Dll, EntryPoint = "igraph_vector_destroy", CallingConvention = CallingConvention.Cdecl)]
250    private static extern void igraph_vector_destroy_x86([In, Out]igraph_vector_t vector);
251    [DllImport(X64Dll, EntryPoint = "igraph_vector_destroy", CallingConvention = CallingConvention.Cdecl)]
252    private static extern void igraph_vector_destroy_x64([In, Out]igraph_vector_t vector);
253    [DllImport(X86Dll, EntryPoint = "igraph_vector_size", CallingConvention = CallingConvention.Cdecl)]
254    private static extern int igraph_vector_size_x86(igraph_vector_t vector);
255    [DllImport(X64Dll, EntryPoint = "igraph_vector_size", CallingConvention = CallingConvention.Cdecl)]
256    private static extern int igraph_vector_size_x64(igraph_vector_t vector);
257    [DllImport(X86Dll, EntryPoint = "igraph_vector_e", CallingConvention = CallingConvention.Cdecl)]
258    private static extern double igraph_vector_e_x86(igraph_vector_t vector, int index);
259    [DllImport(X64Dll, EntryPoint = "igraph_vector_e", CallingConvention = CallingConvention.Cdecl)]
260    private static extern double igraph_vector_e_x64(igraph_vector_t vector, int index);
261    [DllImport(X86Dll, EntryPoint = "igraph_vector_set", CallingConvention = CallingConvention.Cdecl)]
262    private static extern double igraph_vector_set_x86([In, Out]igraph_vector_t vector, int index, double value);
263    [DllImport(X64Dll, EntryPoint = "igraph_vector_set", CallingConvention = CallingConvention.Cdecl)]
264    private static extern double igraph_vector_set_x64([In, Out]igraph_vector_t vector, int index, double value);
265    [DllImport(X86Dll, EntryPoint = "igraph_vector_copy", CallingConvention = CallingConvention.Cdecl)]
266    private static extern int igraph_vector_copy_x86([In, Out]igraph_vector_t to, igraph_vector_t from);
267    [DllImport(X64Dll, EntryPoint = "igraph_vector_copy", CallingConvention = CallingConvention.Cdecl)]
268    private static extern int igraph_vector_copy_x64([In, Out]igraph_vector_t to, igraph_vector_t from);
269    [DllImport(X86Dll, EntryPoint = "igraph_vector_copy_to", CallingConvention = CallingConvention.Cdecl)]
270    private static extern void igraph_vector_copy_to_x86(igraph_vector_t from, [In, Out]double[] to);
271    [DllImport(X64Dll, EntryPoint = "igraph_vector_copy", CallingConvention = CallingConvention.Cdecl)]
272    private static extern void igraph_vector_copy_to_x64(igraph_vector_t from, [In, Out]double[] to);
273    [DllImport(X86Dll, EntryPoint = "igraph_vector_fill", CallingConvention = CallingConvention.Cdecl)]
274    private static extern int igraph_vector_fill_x86([In, Out]igraph_vector_t vector, double v);
275    [DllImport(X64Dll, EntryPoint = "igraph_vector_fill", CallingConvention = CallingConvention.Cdecl)]
276    private static extern int igraph_vector_fill_x64([In, Out]igraph_vector_t vector, double v);
277    [DllImport(X86Dll, EntryPoint = "igraph_vector_reverse", CallingConvention = CallingConvention.Cdecl)]
278    private static extern int igraph_vector_reverse_x86([In, Out]igraph_vector_t vector);
279    [DllImport(X64Dll, EntryPoint = "igraph_vector_reverse", CallingConvention = CallingConvention.Cdecl)]
280    private static extern int igraph_vector_reverse_x64([In, Out]igraph_vector_t vector);
281    [DllImport(X86Dll, EntryPoint = "igraph_vector_shuffle", CallingConvention = CallingConvention.Cdecl)]
282    private static extern int igraph_vector_shuffle_x86([In, Out]igraph_vector_t vector);
283    [DllImport(X64Dll, EntryPoint = "igraph_vector_shuffle", CallingConvention = CallingConvention.Cdecl)]
284    private static extern int igraph_vector_shuffle_x64([In, Out]igraph_vector_t vector);
285    [DllImport(X86Dll, EntryPoint = "igraph_vector_scale", CallingConvention = CallingConvention.Cdecl)]
286    private static extern void igraph_vector_scale_x86([In, Out]igraph_vector_t vector, double by);
287    [DllImport(X64Dll, EntryPoint = "igraph_vector_scale", CallingConvention = CallingConvention.Cdecl)]
288    private static extern void igraph_vector_scale_x64([In, Out]igraph_vector_t vector, double by);
289    #endregion
290    #endregion
291
292    #region igraph_matrix
293    internal static int igraph_matrix_init(igraph_matrix_t matrix, int nrows, int ncols) {
294      return X86 ? igraph_matrix_init_x86(matrix, nrows, ncols) : igraph_matrix_init_x64(matrix, nrows, ncols);
295    }
296    internal static void igraph_matrix_destroy(igraph_matrix_t matrix) {
297      if (X86) igraph_matrix_destroy_x86(matrix);
298      else igraph_matrix_destroy_x64(matrix);
299    }
300    internal static int igraph_matrix_copy(igraph_matrix_t to, igraph_matrix_t from) {
301      return X86 ? igraph_matrix_copy_x86(to, from) : igraph_matrix_copy_x64(to, from);
302    }
303
304    internal static double igraph_matrix_e(igraph_matrix_t matrix, int row, int col) {
305      return X86 ? igraph_matrix_e_x86(matrix, row, col) : igraph_matrix_e_x64(matrix, row, col);
306    }
307
308    internal static void igraph_matrix_set(igraph_matrix_t matrix, int row, int col, double value) {
309      if (X86) igraph_matrix_set_x86(matrix, row, col, value);
310      else igraph_matrix_set_x64(matrix, row, col, value);
311    }
312
313    internal static void igraph_matrix_fill(igraph_matrix_t matrix, double v) {
314      if (X86) igraph_matrix_fill_x86(matrix, v);
315      else igraph_matrix_fill_x64(matrix, v);
316    }
317
318    internal static int igraph_matrix_transpose(igraph_matrix_t matrix) {
319      return X86 ? igraph_matrix_transpose_x86(matrix) : igraph_matrix_transpose_x64(matrix);
320    }
321
322    internal static void igraph_matrix_scale(igraph_matrix_t matrix, double by) {
323      if (X86) igraph_matrix_scale_x86(matrix, by);
324      else igraph_matrix_scale_x64(matrix, by);
325    }
326
327    #region Platform specific DLL imports
328    [DllImport(X86Dll, EntryPoint = "igraph_matrix_init", CallingConvention = CallingConvention.Cdecl)]
329    private static extern int igraph_matrix_init_x86([In, Out]igraph_matrix_t matrix, int nrow, int ncol);
330    [DllImport(X64Dll, EntryPoint = "igraph_matrix_init", CallingConvention = CallingConvention.Cdecl)]
331    private static extern int igraph_matrix_init_x64([In, Out]igraph_matrix_t matrix, int nrow, int ncol);
332    [DllImport(X86Dll, EntryPoint = "igraph_matrix_destroy", CallingConvention = CallingConvention.Cdecl)]
333    private static extern void igraph_matrix_destroy_x86([In, Out]igraph_matrix_t matrix);
334    [DllImport(X64Dll, EntryPoint = "igraph_matrix_destroy", CallingConvention = CallingConvention.Cdecl)]
335    private static extern void igraph_matrix_destroy_x64([In, Out]igraph_matrix_t matrix);
336    [DllImport(X86Dll, EntryPoint = "igraph_matrix_e", CallingConvention = CallingConvention.Cdecl)]
337    private static extern double igraph_matrix_e_x86(igraph_matrix_t matrix, int row, int col);
338    [DllImport(X64Dll, EntryPoint = "igraph_matrix_e", CallingConvention = CallingConvention.Cdecl)]
339    private static extern double igraph_matrix_e_x64(igraph_matrix_t matrix, int row, int col);
340    [DllImport(X86Dll, EntryPoint = "igraph_matrix_set", CallingConvention = CallingConvention.Cdecl)]
341    private static extern double igraph_matrix_set_x86([In, Out]igraph_matrix_t matrix, int row, int col, double value);
342    [DllImport(X64Dll, EntryPoint = "igraph_matrix_set", CallingConvention = CallingConvention.Cdecl)]
343    private static extern double igraph_matrix_set_x64([In, Out]igraph_matrix_t matrix, int row, int col, double value);
344    [DllImport(X86Dll, EntryPoint = "igraph_matrix_copy", CallingConvention = CallingConvention.Cdecl)]
345    private static extern int igraph_matrix_copy_x86([In, Out]igraph_matrix_t to, igraph_matrix_t from);
346    [DllImport(X64Dll, EntryPoint = "igraph_matrix_copy", CallingConvention = CallingConvention.Cdecl)]
347    private static extern int igraph_matrix_copy_x64([In, Out]igraph_matrix_t to, igraph_matrix_t from);
348    [DllImport(X86Dll, EntryPoint = "igraph_matrix_fill", CallingConvention = CallingConvention.Cdecl)]
349    private static extern void igraph_matrix_fill_x86([In, Out]igraph_matrix_t matrix, double v);
350    [DllImport(X64Dll, EntryPoint = "igraph_matrix_fill", CallingConvention = CallingConvention.Cdecl)]
351    private static extern void igraph_matrix_fill_x64([In, Out]igraph_matrix_t matrix, double v);
352    [DllImport(X86Dll, EntryPoint = "igraph_matrix_transpose", CallingConvention = CallingConvention.Cdecl)]
353    private static extern int igraph_matrix_transpose_x86([In, Out]igraph_matrix_t matrix);
354    [DllImport(X64Dll, EntryPoint = "igraph_matrix_transpose", CallingConvention = CallingConvention.Cdecl)]
355    private static extern int igraph_matrix_transpose_x64([In, Out]igraph_matrix_t matrix);
356    [DllImport(X86Dll, EntryPoint = "igraph_matrix_scale", CallingConvention = CallingConvention.Cdecl)]
357    private static extern int igraph_matrix_scale_x86([In, Out]igraph_matrix_t matrix, double by);
358    [DllImport(X64Dll, EntryPoint = "igraph_matrix_scale", CallingConvention = CallingConvention.Cdecl)]
359    private static extern int igraph_matrix_scale_x64([In, Out]igraph_matrix_t matrix, double by);
360    #endregion
361    #endregion
362
363    #region igraph_layout
364    internal static int igraph_layout_fruchterman_reingold(igraph_t graph, igraph_matrix_t res, bool use_seed, int niter, double start_temp, igraph_layout_grid_t grid, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy) {
365      return X86 ? igraph_layout_fruchterman_reingold_x86(graph, res, use_seed, niter, start_temp, grid, weights, minx, maxx, miny, maxy) : igraph_layout_fruchterman_reingold_x64(graph, res, use_seed, niter, start_temp, grid, weights, minx, maxx, miny, maxy);
366    }
367    internal static int igraph_layout_kamada_kawai(igraph_t graph, igraph_matrix_t res, bool use_seed, int maxiter, double epsilon, double kkconst, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy) {
368      return X86 ? igraph_layout_kamada_kawai_x86(graph, res, use_seed, maxiter, epsilon, kkconst, weights, minx, maxx, miny, maxy) : igraph_layout_kamada_kawai_x64(graph, res, use_seed, maxiter, epsilon, kkconst, weights, minx, maxx, miny, maxy);
369    }
370    internal static int igraph_layout_davidson_harel(igraph_t graph, igraph_matrix_t res, bool use_seed, int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist) {
371      return X86 ? igraph_layout_davidson_harel_x86(graph, res, use_seed, maxiter, fineiter, cool_fact, weight_node_dist, weight_border, weight_edge_lengths, weight_edge_crossings, weight_node_edge_dist) : igraph_layout_davidson_harel_x64(graph, res, use_seed, maxiter, fineiter, cool_fact, weight_node_dist, weight_border, weight_edge_lengths, weight_edge_crossings, weight_node_edge_dist);
372    }
373    internal static int igraph_layout_mds(igraph_t graph, igraph_matrix_t res, igraph_matrix_t dist = null, int dim = 2) {
374      return MarshalIfExistsAndCall(GetDefaultArpackOptions(), ptr => X86 ? igraph_layout_mds_x86(graph, res, dist, dim, ptr) : igraph_layout_mds_x64(graph, res, dist, dim, ptr));
375    }
376
377    #region Platform specific DLL imports
378    [DllImport(X86Dll, EntryPoint = "igraph_layout_fruchterman_reingold", CallingConvention = CallingConvention.Cdecl)]
379    private static extern int igraph_layout_fruchterman_reingold_x86(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int niter, double start_temp, igraph_layout_grid_t grid, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
380    [DllImport(X64Dll, EntryPoint = "igraph_layout_fruchterman_reingold", CallingConvention = CallingConvention.Cdecl)]
381    private static extern int igraph_layout_fruchterman_reingold_x64(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int niter, double start_temp, igraph_layout_grid_t grid, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
382    [DllImport(X86Dll, EntryPoint = "igraph_layout_kamada_kawai", CallingConvention = CallingConvention.Cdecl)]
383    private static extern int igraph_layout_kamada_kawai_x86(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, double epsilon, double kkconst, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
384    [DllImport(X64Dll, EntryPoint = "igraph_layout_kamada_kawai", CallingConvention = CallingConvention.Cdecl)]
385    private static extern int igraph_layout_kamada_kawai_x64(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, double epsilon, double kkconst, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
386    [DllImport(X86Dll, EntryPoint = "igraph_layout_davidson_harel", CallingConvention = CallingConvention.Cdecl)]
387    private static extern int igraph_layout_davidson_harel_x86(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist);
388    [DllImport(X64Dll, EntryPoint = "igraph_layout_davidson_harel", CallingConvention = CallingConvention.Cdecl)]
389    private static extern int igraph_layout_davidson_harel_x64(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist);
390    [DllImport(X86Dll, EntryPoint = "igraph_layout_mds", CallingConvention = CallingConvention.Cdecl)]
391    private static extern int igraph_layout_mds_x86(igraph_t graph, [In, Out]igraph_matrix_t res, igraph_matrix_t dist, int dim, IntPtr options);
392    [DllImport(X64Dll, EntryPoint = "igraph_layout_mds", CallingConvention = CallingConvention.Cdecl)]
393    private static extern int igraph_layout_mds_x64(igraph_t graph, [In, Out]igraph_matrix_t res, igraph_matrix_t dist, int dim, IntPtr options);
394    #endregion
395    #endregion
396
397    #region igraph_iterators
398    internal static int igraph_vs_all(ref igraph_vs_t vs) {
399      return X86 ? igraph_vs_all_x86(ref vs) : igraph_vs_all_x64(ref vs);
400    }
401    internal static void igraph_vs_destroy(ref igraph_vs_t vs) {
402      if (X86) igraph_vs_destroy_x86(ref vs);
403      else igraph_vs_destroy_x64(ref vs);
404    }
405
406    #region Platform specific DLL imports
407    [DllImport(X86Dll, EntryPoint = "igraph_vs_all", CallingConvention = CallingConvention.Cdecl)]
408    private static extern int igraph_vs_all_x86([In, Out]ref igraph_vs_t vs);
409    [DllImport(X64Dll, EntryPoint = "igraph_vs_all", CallingConvention = CallingConvention.Cdecl)]
410    private static extern int igraph_vs_all_x64([In, Out]ref igraph_vs_t vs);
411    [DllImport(X86Dll, EntryPoint = "igraph_vs_destroy", CallingConvention = CallingConvention.Cdecl)]
412    private static extern void igraph_vs_destroy_x86([In, Out]ref igraph_vs_t vs);
413    [DllImport(X64Dll, EntryPoint = "igraph_vs_destroy", CallingConvention = CallingConvention.Cdecl)]
414    private static extern void igraph_vs_destroy_x64([In, Out]ref igraph_vs_t vs);
415    #endregion
416    #endregion
417
418    private static int MarshalIfExistsAndCall(object o, Func<IntPtr, int> call) {
419      var ptr = IntPtr.Zero;
420      if (o != null) {
421        try {
422          ptr = Marshal.AllocHGlobal(Marshal.SizeOf(o));
423        } catch (OutOfMemoryException e) { throw new InvalidOperationException("Not enough memory to perform operation.", e); }
424        Marshal.StructureToPtr(o, ptr, false);
425      }
426      try {
427        return call(ptr);
428      } finally {
429        if (o != null) {
430          Marshal.DestroyStructure(ptr, o.GetType());
431          Marshal.FreeHGlobal(ptr);
432        }
433      }
434    }
435
436    private static void MarshalIfExistsAndCall(object o, Action<IntPtr> call) {
437      var ptr = IntPtr.Zero;
438      if (o != null) {
439        try {
440          ptr = Marshal.AllocHGlobal(Marshal.SizeOf(o));
441        } catch (OutOfMemoryException e) { throw new InvalidOperationException("Not enough memory to perform operation.", e); }
442        Marshal.StructureToPtr(o, ptr, false);
443      }
444      try {
445        call(ptr);
446      } finally {
447        if (o != null) {
448          Marshal.DestroyStructure(ptr, o.GetType());
449          Marshal.FreeHGlobal(ptr);
450        }
451      }
452    }
453  }
454}
Note: See TracBrowser for help on using the repository browser.