Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/09/16 13:49:01 (8 years ago)
Author:
abeham
Message:

#2651:

  • partially reverted builder testsettings
  • added example on how to use native callbacks (depth-first-search and bfs)
Location:
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Datatypes.cs

    r14245 r14247  
    128128  #endregion
    129129
     130  #region Delegates
     131  [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
     132  internal delegate bool igraph_bfshandler_t(igraph_t graph, int vid, int pred, int succ, int rank, int dist, IntPtr extra);
     133
     134  [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
     135  internal delegate bool igraph_dfshandler_t(igraph_t graph, int vid, int dist, IntPtr extra);
     136  #endregion
     137
    130138  #region Enums
    131139  internal enum igraph_layout_grid_t {
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/DllImporter.cs

    r14245 r14247  
    2121
    2222using System;
     23using System.IO;
    2324using System.Runtime.InteropServices;
    2425
     
    2930    private readonly static bool X86 = false;
    3031
     32    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
     33    private static extern bool SetDllDirectory(string lpPathName);
     34
    3135    static DllImporter() {
    3236      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"));
    3340    }
    3441
     
    109116    internal static int igraph_add_edge(igraph_t graph, int from, int to) {
    110117      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));
    111127    }
    112128    #endregion
     
    137153    [DllImport(X64Dll, EntryPoint = "igraph_pagerank", CallingConvention = CallingConvention.Cdecl)]
    138154    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);
    139163    #endregion
    140164    #endregion
     
    348372    }
    349373    internal static int igraph_layout_mds(igraph_t graph, igraph_matrix_t res, igraph_matrix_t dist = null, int dim = 2) {
    350       var arpackoptions = GetDefaultArpackOptions();
    351       var options = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(igraph_arpack_options_t)));
    352       Marshal.StructureToPtr(arpackoptions, options, false);
    353       try {
    354         return X86 ? igraph_layout_mds_x86(graph, res, dist, dim, options) : igraph_layout_mds_x64(graph, res, dist, dim, options);
    355       } finally {
    356         Marshal.DestroyStructure(options, typeof(igraph_arpack_options_t));
    357         Marshal.FreeHGlobal(options);
    358       }
     374      return MarshalIfExistsAndCall(GetDefaultArpackOptions(), ptr => X86 ? igraph_layout_mds_x86(graph, res, dist, dim, ptr) : igraph_layout_mds_x64(graph, res, dist, dim, ptr));
    359375    }
    360376
     
    399415    #endregion
    400416    #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    }
    401453  }
    402454}
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/HeuristicLab.igraph-0.8.0-pre.csproj

    r14244 r14247  
    4949  <ItemGroup>
    5050    <Compile Include="DllImporter.cs" />
     51    <Compile Include="Wrappers\Datatypes.cs" />
    5152    <Compile Include="Wrappers\Graph.cs" />
    5253    <Compile Include="Wrappers\Matrix.cs" />
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Graph.cs

    r14245 r14247  
    132132      return coords;
    133133    }
     134
     135    public void BreadthFirstWalk(BreadthFirstHandler handler, int root, DirectedWalkMode mode, bool includeUnreachableFromRoot = false, object tag = null) {
     136      igraph_bfshandler_t wrapper = (t, vid, pred, succ, rank, dist, extra) => handler != null && handler(this, vid, pred, succ, rank, dist, tag);
     137      DllImporter.igraph_bfs(graph, root, null, (igraph_neimode_t)mode, includeUnreachableFromRoot, null, null, null, null, null, null, null, wrapper, tag);
     138    }
     139
     140    public void DepthFirstWalk(DepthFirstHandler inHandler, DepthFirstHandler outHandler, int root, DirectedWalkMode mode, bool includeUnreachableFromRoot = false, object tag = null) {
     141      igraph_dfshandler_t inWrapper = (t, vid, dist, extra) => inHandler != null && inHandler(this, vid, dist, tag);
     142      igraph_dfshandler_t outWrapper = (t, vid, dist, extra) => outHandler != null && outHandler(this, vid, dist, tag);
     143      DllImporter.igraph_dfs(graph, root, (igraph_neimode_t)mode, includeUnreachableFromRoot, null, null, null, null, inWrapper, outWrapper, tag);
     144    }
    134145  }
    135146}
Note: See TracChangeset for help on using the changeset viewer.