lilypond-0.1.15
[lilypond.git] / flower / directed-graph.cc
blobb13203e4fa19ca307eb39f7e9c2afd6bb9c3f592
1 /*
2 edge_out.cc -- implement Directed_graph_node
4 source file FlowerLib
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include "directed-graph.hh"
11 #ifdef PARANOID // these checks eat huge amounts of time.
12 #define PARANOID_OK() OK()
13 #else
14 #define PARANOID_OK()
15 #endif
18 Link_array<Directed_graph_node> const &
19 Directed_graph_node::get_in_edge_arr() const
21 return edge_in_l_arr_;
24 Link_array<Directed_graph_node> const &
25 Directed_graph_node::get_out_edge_arr() const
27 return edge_out_l_arr_;
30 /**
31 Should not copy deps automatically
33 Directed_graph_node::Directed_graph_node (Directed_graph_node const&)
37 void
38 Directed_graph_node::copy_edges_out (Directed_graph_node const &s)
40 for (int i=0; i < s.edge_out_l_arr_.size(); i++)
41 add (s.edge_out_l_arr_[i]);
44 void
45 Directed_graph_node::OK() const
47 #ifndef NDEBUG
48 for (int i=0; i < edge_out_l_arr_.size(); i++)
50 assert (edge_out_l_arr_[i]->
51 edge_in_l_arr_.find_l (this));
53 for (int i=0; i < edge_in_l_arr_.size(); i++)
54 assert (edge_in_l_arr_[i]->contains_b (this));
55 #endif
58 bool
59 Directed_graph_node::contains_b (const Directed_graph_node *d) const
61 return edge_out_l_arr_.find_l ((Directed_graph_node*)d);
64 void
65 Directed_graph_node::remove_edge_out_idx (int i)
67 PARANOID_OK();
68 Directed_graph_node * d_l = edge_out_l_arr_.get (i);
70 int j = d_l->edge_in_l_arr_.find_i (this);
71 assert (j>=0);
72 d_l->edge_in_l_arr_.unordered_del (j);
73 PARANOID_OK();
76 void
77 Directed_graph_node::remove_edge_in (Directed_graph_node *d_l)
79 PARANOID_OK();
80 d_l->remove_edge_out (this);
81 PARANOID_OK();
84 void
85 Directed_graph_node::remove_edge_out (Directed_graph_node *d_l)
87 PARANOID_OK();
88 for (int i=0; i < edge_out_l_arr_.size();)
90 if (edge_out_l_arr_[i]== d_l)
91 remove_edge_out_idx (i);
92 else
93 i++;
95 PARANOID_OK();
97 bool
98 Directed_graph_node::linked_b() const
100 return edge_out_l_arr_.size() || edge_in_l_arr_.size ();
103 void
104 Directed_graph_node::junk_links()
106 edge_in_l_arr_.set_size (0);
107 edge_out_l_arr_.set_size (0);
111 void
112 Directed_graph_node::unlink()
114 #ifdef PARANOID
115 PARANOID_OK();
117 Link_array<Directed_graph_node> t = edge_out_l_arr_;
118 t.concat (edge_in_l_arr_);
119 #endif
121 while (edge_out_l_arr_.size())
122 remove_edge_out_idx (0);
124 while (edge_in_l_arr_.size())
125 remove_edge_in (edge_in_l_arr_[0]);
127 #ifdef PARANOID
128 for (int i =0; i < t.size(); i++)
129 t[i]->OK();
130 #endif
133 Directed_graph_node::~Directed_graph_node()
135 assert (!linked_b());
139 void
140 Directed_graph_node::add (Directed_graph_node* dep_l)
142 PARANOID_OK();
143 if (!dep_l)
144 return ;
145 dep_l->edge_in_l_arr_.push (this);
146 edge_out_l_arr_.push (dep_l);
147 PARANOID_OK();
151 Directed_graph_node::Directed_graph_node()