Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14247


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
Files:
1 added
9 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}
  • trunk/sources/HeuristicLab.Tests/Builder.testsettings

    r14246 r14247  
    33  <Description>Test settings to run unit tests on the HL build server.</Description>
    44  <Deployment>
    5     <DeploymentItem filename="bin\igraph-0.8.0-pre-x64.dll" />
    65    <DeploymentItem filename="bin\" />
    7     <DeploymentItem filename="bin\igraph-0.8.0-pre-x86.dll" />
    86  </Deployment>
    97  <Execution hostProcessPlatform="MSIL">
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphLayoutTest.cs

    r14245 r14247  
    3232    [TestCategory("igraph")]
    3333    [TestProperty("Time", "short")]
    34     public void FruchtermanReingoldLayoutTest() {
     34    public void IGraphWrappersLayoutFruchtermanReingoldTest() {
    3535      var graph = new Graph(5, new[] {
    3636        Tuple.Create(0, 1),
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersGraphTest.cs

    r14245 r14247  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.IGraph.Wrappers;
     
    2728namespace HeuristicLab.Tests {
    2829  [TestClass]
     30  [DeploymentItem("igraph-0.8.0-pre-x86.dll")]
     31  [DeploymentItem("igraph-0.8.0-pre-x64.dll")]
    2932  public class IGraphWrappersGraphTest {
    3033    [TestMethod]
     
    3235    [TestCategory("igraph")]
    3336    [TestProperty("Time", "short")]
    34     public void IGraphWrappersGraphConstructionAndFinalization() {
     37    public void IGraphWrappersGraphConstructionAndFinalizationTest() {
    3538      var graph = new Graph(5, new[] {
    3639        Tuple.Create(0, 1),
     
    5760    [TestCategory("igraph")]
    5861    [TestProperty("Time", "short")]
    59     public void TestDensity() {
     62    public void IGraphWrappersGraphDensityTest() {
    6063      var graph = new Graph(5, new[] {
    6164        Tuple.Create(0, 1),
     
    9194    [TestCategory("igraph")]
    9295    [TestProperty("Time", "short")]
    93     public void TestPageRank() {
     96    public void IGraphWrappersGraphPageRankTest() {
    9497      var graph = new Graph(4, new[] {
    9598        Tuple.Create(0, 1),
     
    134137      Assert.AreEqual(0.173, ranks[3], 0.01);
    135138    }
     139
     140    [TestMethod]
     141    [TestCategory("ExtLibs")]
     142    [TestCategory("igraph")]
     143    [TestProperty("Time", "short")]
     144    public void IGraphWrappersGraphBreadthFirstWalkTest() {
     145      var graph = new Graph(4, new[] {
     146        Tuple.Create(0, 1),
     147        Tuple.Create(0, 2),
     148        Tuple.Create(1, 2),
     149        Tuple.Create(2, 0),
     150        Tuple.Create(3, 2),
     151      }, directed: true);
     152      var visited = new HashSet<int>();
     153      BreadthFirstHandler handler = (graph1, currentVertexId, previousVertexId, nextVertexId, rank, distance, tag) => {
     154        visited.Add(currentVertexId);
     155        return false;
     156      };
     157      graph.BreadthFirstWalk(handler, 0, DirectedWalkMode.All, true, null);
     158      Assert.AreEqual(4, visited.Count);
     159      Assert.IsTrue(visited.Contains(0));
     160      Assert.IsTrue(visited.Contains(1));
     161      Assert.IsTrue(visited.Contains(2));
     162      Assert.IsTrue(visited.Contains(3));
     163    }
     164
     165    [TestMethod]
     166    [TestCategory("ExtLibs")]
     167    [TestCategory("igraph")]
     168    [TestProperty("Time", "short")]
     169    public void IGraphWrappersGraphDepthFirstWalkTest() {
     170      var graph = new Graph(4, new[] {
     171        Tuple.Create(0, 1),
     172        Tuple.Create(0, 2),
     173        Tuple.Create(1, 2),
     174        Tuple.Create(2, 0),
     175        Tuple.Create(3, 2),
     176      }, directed: true);
     177      var visited = new HashSet<int>();
     178      DepthFirstHandler handler = (graph1, vertexId, distance, tag) => {
     179        visited.Add(vertexId);
     180        return false;
     181      };
     182      graph.DepthFirstWalk(handler, handler, 0, DirectedWalkMode.All, true, null);
     183      Assert.AreEqual(4, visited.Count);
     184      Assert.IsTrue(visited.Contains(0));
     185      Assert.IsTrue(visited.Contains(1));
     186      Assert.IsTrue(visited.Contains(2));
     187      Assert.IsTrue(visited.Contains(3));
     188    }
    136189  }
    137190}
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersMatrixTest.cs

    r14245 r14247  
    3030    [TestCategory("igraph")]
    3131    [TestProperty("Time", "short")]
    32     public void IGraphWrappersMatrixConstructionAndFinalization() {
     32    public void IGraphWrappersMatrixConstructionAndFinalizationTest() {
    3333      var matrix = new Matrix(3, 2);
    3434      Assert.AreEqual(3, matrix.Rows);
     
    8686    [TestCategory("igraph")]
    8787    [TestProperty("Time", "short")]
    88     public void IGraphWrappersMatrixFill() {
     88    public void IGraphWrappersMatrixFillTest() {
    8989      var matrix = new Matrix(3, 2);
    9090      matrix.Fill(2.6);
     
    101101    [TestCategory("igraph")]
    102102    [TestProperty("Time", "short")]
    103     public void IGraphWrappersMatrixTranspose() {
     103    public void IGraphWrappersMatrixTransposeTest() {
    104104      var matrix = new Matrix(3, 2);
    105105      matrix.Transpose();
     
    112112    [TestCategory("igraph")]
    113113    [TestProperty("Time", "short")]
    114     public void IGraphWrappersMatrixScale() {
     114    public void IGraphWrappersMatrixScaleTest() {
    115115      var matrix = new Matrix(3, 2);
    116116      matrix[0, 0] = matrix[0, 1] = 4;
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersVectorTest.cs

    r14245 r14247  
    3131    [TestCategory("igraph")]
    3232    [TestProperty("Time", "short")]
    33     public void IGraphWrappersVectorConstructionAndFinalization() {
     33    public void IGraphWrappersVectorConstructionAndFinalizationTest() {
    3434      var vector = new Vector(7);
    3535      Assert.AreEqual(7, vector.Length);
Note: See TracChangeset for help on using the changeset viewer.