HAL
neighborhood.cpp
Go to the documentation of this file.
2 
3 namespace hal
4 {
5  class Gate;
6 
7  namespace graph_algorithm
8  {
9  Result<std::vector<std::vector<u32>>> get_neighborhood(NetlistGraph* graph, const std::vector<Gate*>& start_gates, u32 order, NetlistGraph::Direction direction, u32 min_dist)
10  {
11  if (!graph)
12  {
13  return ERR("graph is a nullptr");
14  }
15 
16  if (start_gates.empty())
17  {
18  return ERR("no start gates provided");
19  }
20 
21  igraph_vector_int_t i_gates;
22  if (auto res = graph->get_vertices_from_gates_igraph(start_gates); res.is_ok())
23  {
24  i_gates = std::move(res.get());
25  }
26  else
27  {
28  return ERR(res.get_error());
29  }
30 
31  auto res = get_neighborhood_igraph(graph, &i_gates, order, direction, min_dist);
32 
33  igraph_vector_int_destroy(&i_gates);
34 
35  if (res.is_error())
36  {
37  return ERR(res.get_error());
38  }
39 
40  return res;
41  }
42 
43  Result<std::vector<std::vector<u32>>> get_neighborhood(NetlistGraph* graph, const std::vector<u32>& start_vertices, u32 order, NetlistGraph::Direction direction, u32 min_dist)
44  {
45  if (!graph)
46  {
47  return ERR("graph is a nullptr");
48  }
49 
50  if (start_vertices.empty())
51  {
52  return ERR("no start vertices provided");
53  }
54 
55  igraph_vector_int_t i_gates;
56  if (auto res = igraph_vector_int_init(&i_gates, start_vertices.size()); res != IGRAPH_SUCCESS)
57  {
58  return ERR(igraph_strerror(res));
59  }
60 
61  for (u32 i = 0; i < start_vertices.size(); i++)
62  {
63  VECTOR(i_gates)[i] = start_vertices.at(i);
64  }
65 
66  auto res = get_neighborhood_igraph(graph, &i_gates, order, direction, min_dist);
67 
68  igraph_vector_int_destroy(&i_gates);
69 
70  if (res.is_error())
71  {
72  return ERR(res.get_error());
73  }
74 
75  return res;
76  }
77 
78  Result<std::vector<std::vector<u32>>> get_neighborhood_igraph(NetlistGraph* graph, const igraph_vector_int_t* start_vertices, u32 order, NetlistGraph::Direction direction, u32 min_dist)
79  {
80  if (!graph)
81  {
82  return ERR("graph is a nullptr");
83  }
84 
85  igraph_vs_t v_sel = igraph_vss_vector(start_vertices);
86  igraph_neimode_t mode;
87  switch (direction)
88  {
90  mode = IGRAPH_IN;
91  break;
93  mode = IGRAPH_OUT;
94  break;
96  mode = IGRAPH_ALL;
97  break;
99  igraph_vs_destroy(&v_sel);
100  return ERR("invalid direction 'NONE'");
101  }
102 
103  igraph_vector_int_list_t neighborhoods_raw;
104  if (auto res = igraph_vector_int_list_init(&neighborhoods_raw, 1); res != IGRAPH_SUCCESS)
105  {
106  igraph_vs_destroy(&v_sel);
107  return ERR(igraph_strerror(res));
108  }
109 
110  if (auto res = igraph_neighborhood(graph->get_graph(), &neighborhoods_raw, v_sel, order, mode, min_dist); res != IGRAPH_SUCCESS)
111  {
112  igraph_vs_destroy(&v_sel);
113  igraph_vector_int_list_destroy(&neighborhoods_raw);
114  return ERR(igraph_strerror(res));
115  }
116 
117  std::vector<std::vector<u32>> neighborhoods;
118  const u32 num_neighborhoods = igraph_vector_int_list_size(&neighborhoods_raw);
119  for (u32 i = 0; i < num_neighborhoods; i++)
120  {
121  auto vec = igraph_vector_int_list_get_ptr(&neighborhoods_raw, i);
122 
123  const u32 vec_size = igraph_vector_int_size(vec);
124  std::vector<u32> tmp(vec_size);
125  for (u32 j = 0; j < vec_size; j++)
126  {
127  tmp[j] = VECTOR(*vec)[j];
128  }
129  neighborhoods.push_back(std::move(tmp));
130  }
131 
132  igraph_vs_destroy(&v_sel);
133  igraph_vector_int_list_destroy(&neighborhoods_raw);
134 
135  return OK(neighborhoods);
136  }
137  } // namespace graph_algorithm
138 } // namespace hal
A directed graph corresponding to a netlist.
Definition: netlist_graph.h:57
Direction
The direction of exploration within the graph.
Definition: netlist_graph.h:64
@ ALL
Explore in both directions, i.e., treat the graph as undirected.
@ NONE
No direction, invalid default setting.
@ IN
Explore through the inputs of the current node, i.e., traverse backwards.
@ OUT
Explore through the outputs of the current node, i.e., traverse forwards.
Result< igraph_vector_int_t > get_vertices_from_gates_igraph(const std::vector< Gate * > &gates) const
Get the vertices corresponding to the specified gates.
igraph_t * get_graph() const
Get the graph object of the netlist graph.
#define ERR(message)
Definition: result.h:53
#define OK(...)
Definition: result.h:49
Result< std::vector< std::vector< u32 > > > get_neighborhood_igraph(NetlistGraph *graph, const igraph_vector_int_t *start_vertices, u32 order, NetlistGraph::Direction direction, u32 min_dist=0)
Compute the neighborhood of the given order for each of the specified vertices within the given netli...
Result< std::vector< std::vector< u32 > > > get_neighborhood(NetlistGraph *graph, const std::vector< Gate * > &start_gates, u32 order, NetlistGraph::Direction direction, u32 min_dist=0)
Compute the neighborhood of the given order for each of the specified gates within the given netlist ...
Definition: neighborhood.cpp:9
This file contains functions related to neighborhoods in graphs.
quint32 u32
PinDirection direction