GraphDom
Loading...
Searching...
No Matches
full_labeled_set_digraph.h
1/*
2 * Copyright 2026 Michele Comparini
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef GRAPHDOM_FULL_LABELED_SET_DIGRAPH_IMPL_H
8#define GRAPHDOM_FULL_LABELED_SET_DIGRAPH_IMPL_H
9
10#include "../full_labeled_set_digraph.h"
11
12template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
14full_labeled_set_digraph(const Compare& v_comp, const VertexLabellerType& v_lab, const EdgeLabellerType& e_lab) :
15labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(v_lab),
16labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(e_lab),
17vertices( custom_vertices_set_compare(v_comp) ) {}
18
19template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
21full_labeled_set_digraph(const Compare& v_comp, const VertexLabellerType& v_lab, EdgeLabellerType&& e_lab) :
22labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(v_lab),
23labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(std::move(e_lab)),
24vertices( custom_vertices_set_compare(v_comp) ) {}
25
26template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
28full_labeled_set_digraph(const Compare& v_comp, VertexLabellerType&& v_lab, const EdgeLabellerType& e_lab) :
29labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(std::move(v_lab)),
30labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(e_lab),
31vertices( custom_vertices_set_compare(v_comp) ) {}
32
33template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
35full_labeled_set_digraph(const Compare& v_comp, VertexLabellerType&& v_lab, EdgeLabellerType&& e_lab) :
36labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(std::move(v_lab)),
37labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(std::move(e_lab)),
38vertices( custom_vertices_set_compare(v_comp) ) {}
39
40template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
42full_labeled_set_digraph(Compare&& v_comp, const VertexLabellerType& v_lab, const EdgeLabellerType& e_lab) :
43labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(v_lab),
44labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(e_lab),
45vertices( custom_vertices_set_compare(std::move(v_comp)) ) {}
46
47template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
49full_labeled_set_digraph(Compare&& v_comp, const VertexLabellerType& v_lab, EdgeLabellerType&& e_lab) :
50labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(v_lab),
51labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(std::move(e_lab)),
52vertices( custom_vertices_set_compare(std::move(v_comp)) ) {}
53
54template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
56full_labeled_set_digraph(Compare&& v_comp, VertexLabellerType&& v_lab, const EdgeLabellerType& e_lab) :
57labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(std::move(v_lab)),
58labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(e_lab),
59vertices( custom_vertices_set_compare(std::move(v_comp)) ) {}
60
61template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
63full_labeled_set_digraph(Compare&& v_comp, VertexLabellerType&& v_lab, EdgeLabellerType&& e_lab) :
64labeled_vertex_set_graph<VertexType,VertexLabelType,VertexLabellerType>(std::move(v_lab)),
65labeled_edge_non_mixed_graph<VertexType,EdgeLabelType,EdgeLabellerType>(std::move(e_lab)),
66vertices( custom_vertices_set_compare(std::move(v_comp)) ) {}
67
68template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
69graphdom::full_labeled_set_digraph<VertexType,VertexLabelType,EdgeLabelType,Compare,VertexLabellerType,EdgeLabellerType>::~full_labeled_set_digraph() {
70 for (auto digraph_vertices_itr = vertices.begin(); digraph_vertices_itr != vertices.end(); ++digraph_vertices_itr) {
71 auto& dv_itr_vertex_container = *digraph_vertices_itr;
72 auto& dv_itr_vc_adj = dv_itr_vertex_container.adj;
73 for (auto edge_endpoint_to_deallocate = dv_itr_vc_adj.begin(); edge_endpoint_to_deallocate != dv_itr_vc_adj.end(); ++edge_endpoint_to_deallocate) {
74 safe_edge_endpoint_deallocation(*edge_endpoint_to_deallocate); // This is to avoid memory leaks
75 }
76 dv_itr_vc_adj.clear();
77 }
78}
79
80template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
84
85template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
87 const typename graph<VertexType>::vertex_const_handle& vertex) const {
88 if ( graphdom::graph<VertexType>::get_owner_graph(vertex) != this ) {
89 throw std::runtime_error("Error"); //TODO: write a better message
90 }
91 const auto* const vertex_container_ptr = static_cast< const vertex_container* >( graphdom::graph< VertexType >::get_vertex_container( vertex ) );
92 return vertex_container_ptr->vertex_label;
93}
94
95template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
97const typename graph<VertexType>::adj_list_const_iterator& edge ) const {
98 if ( graphdom::graph<VertexType>::get_owner_graph( edge ) != this ) {
99 throw std::runtime_error("Error"); //TODO: write a better message
100 }
101 return (
102 *(
103 static_cast<edge_endpoint*>(
105 )
106 )
107 ).edge_label;
108}
109
110template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
113 if ( graphdom::graph<VertexType>::get_owner_graph(vertex) == this ) {
114 const auto vertex_container_to_erase_ptr = graphdom::graph<VertexType>::get_vertex_container(vertex);
115 if ( vertex_container_to_erase_ptr != nullptr ) {
116 auto vertex_container_to_erase_found_vertices_itr = vertices.end();
117 for (auto digraph_vertices_itr = vertices.begin(); digraph_vertices_itr != vertices.end(); ++digraph_vertices_itr) {
118 auto& digraph_vertices_itr_vertex_container = *digraph_vertices_itr;
119 if ( &digraph_vertices_itr_vertex_container == vertex_container_to_erase_ptr ) {
120 vertex_container_to_erase_found_vertices_itr = digraph_vertices_itr;
121 }
122 auto& digraph_vertices_itr_vertex_container_adj = digraph_vertices_itr_vertex_container.adj;
123 auto itr_vertex_container_adj_found_result_itr = digraph_vertices_itr_vertex_container_adj.find(vertex_container_to_erase_ptr);
124 if (itr_vertex_container_adj_found_result_itr != digraph_vertices_itr_vertex_container_adj.end()) {
125 safe_edge_endpoint_deallocation(*itr_vertex_container_adj_found_result_itr); // This is to avoid memory leaks
126 digraph_vertices_itr_vertex_container_adj.erase(itr_vertex_container_adj_found_result_itr);
127 }
128 }
129 if ( vertex_container_to_erase_found_vertices_itr != vertices.end() ) {
130 auto& vertex_container_to_erase = *vertex_container_to_erase_found_vertices_itr;
131 auto& vertex_container_to_erase_adj = vertex_container_to_erase.adj;
132 for(auto vertex_container_to_erase_adj_itr = vertex_container_to_erase_adj.begin();
133 vertex_container_to_erase_adj_itr != vertex_container_to_erase_adj.end();) {
134 safe_edge_endpoint_deallocation(*vertex_container_to_erase_adj_itr); // This is to avoid memory leaks
135 vertex_container_to_erase_adj_itr = vertex_container_to_erase_adj.erase(vertex_container_to_erase_adj_itr);
136 }
137 vertices.erase(vertex_container_to_erase_found_vertices_itr);
138 }
139 //TODO:: Evaluate a possible exception throw HERE
140 }
141 //TODO:: Evaluate a possible exception throw HERE
142 }
143 //TODO:: Evaluate a possible exception throw HERE
144}
145
146template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
147typename graphdom::graph<VertexType>::adj_list_iterator
149 const typename graph<VertexType>::adj_list_const_iterator& edge_itr) {
150 if ( graphdom::graph<VertexType>::get_owner_graph(edge_itr) != this ) {
151 throw std::runtime_error("Error"); //TODO: write a better message
152 }
153 auto const edge_itr_begin_point = static_cast< const vertex_container* >( graphdom::graph<VertexType>::get_begin_point(edge_itr) );
154 auto edge_itr_inner_iterator = graphdom::set_graph<VertexType>::get_inner_iterator( edge_itr );
155 auto const edge_itr_endpoint = *edge_itr_inner_iterator;
156 safe_edge_endpoint_deallocation(edge_itr_endpoint);
158 this,
159 edge_itr_begin_point,
160 directed,
161 ( edge_itr_begin_point->adj ).erase( edge_itr_inner_iterator )
162 );
163}
164
165template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
167 const typename graph<VertexType>::vertex_const_handle& vertex) {
168 if ( graphdom::graph<VertexType>::get_owner_graph(vertex) != this ) {
169 throw std::runtime_error("Error"); //TODO: write a better message
170 }
171 const auto* const vertex_container_ptr = static_cast< const vertex_container* >( graphdom::graph< VertexType >::get_vertex_container( vertex ) );
172 return const_cast< VertexLabelType& >( (*vertex_container_ptr).vertex_label );
173}
174
175template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
176std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>
178 const VertexType& v_core, const VertexLabelType& vertex_label) {
179 auto inner_insertion_result = vertices.emplace(
180 v_core,
181 vertex_label
182 );
183 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
184 graphdom::graph<VertexType>::vertex_handle_factory(
185 this,
186 *(inner_insertion_result.first),
188 ),
189 inner_insertion_result.second
190 );
191}
192
193template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
194std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>
196const VertexType& v_core, VertexLabelType&& vertex_label) {
197 const auto lower_bound = vertices.lower_bound(v_core);
198 if ( lower_bound == vertices.cend() ) {
199 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
200 graphdom::graph<VertexType>::vertex_handle_factory(
201 this,
202 *( vertices.emplace_hint(lower_bound, v_core, std::move(vertex_label)) ),
204 ),
205 true
206 );
207 }
208 if ( ( vertices.key_comp() )( v_core, *lower_bound ) ) {
209 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
210 graphdom::graph<VertexType>::vertex_handle_factory(
211 this,
212 *( vertices.emplace_hint(lower_bound, v_core, std::move(vertex_label)) ),
214 ),
215 true
216 );
217 }
218 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
219 graphdom::graph<VertexType>::vertex_handle_factory(
220 this,
221 *( lower_bound ),
223 ),
224 false
225 );
226}
227
228template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
229std::pair<typename graphdom::graph<VertexType>::vertex_handle,bool>
231VertexType&& v_core, const VertexLabelType& vertex_label) {
232 const auto lower_bound = vertices.lower_bound(v_core);
233 if ( lower_bound == vertices.cend() ) {
234 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
235 graphdom::graph<VertexType>::vertex_handle_factory(
236 this,
237 *( vertices.emplace_hint(lower_bound, std::move(v_core), vertex_label) ),
239 ),
240 true
241 );
242 }
243 if ( ( vertices.key_comp() )( v_core, *lower_bound ) ) {
244 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
245 graphdom::graph<VertexType>::vertex_handle_factory(
246 this,
247 *( vertices.emplace_hint(lower_bound, std::move(v_core), vertex_label) ),
249 ),
250 true
251 );
252 }
253 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
254 graphdom::graph<VertexType>::vertex_handle_factory(
255 this,
256 *( lower_bound ),
258 ),
259 false
260 );
261}
262
263template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
264std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>
266VertexType&& v_core, VertexLabelType&& vertex_label) {
267 const auto lower_bound = vertices.lower_bound(v_core);
268 if ( lower_bound == vertices.cend() ) {
269 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
270 graphdom::graph<VertexType>::vertex_handle_factory(
271 this,
272 *( vertices.emplace_hint(lower_bound, std::move(v_core), std::move(vertex_label)) ),
274 ),
275 true
276 );
277 }
278 if ( ( vertices.key_comp() )( v_core, *lower_bound ) ) {
279 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
280 graphdom::graph<VertexType>::vertex_handle_factory(
281 this,
282 *( vertices.emplace_hint(lower_bound, std::move(v_core), std::move(vertex_label)) ),
284 ),
285 true
286 );
287 }
288 return std::pair<typename graphdom::graph<VertexType>::vertex_handle, bool>(
289 graphdom::graph<VertexType>::vertex_handle_factory(
290 this,
291 *( lower_bound ),
293 ),
294 false
295 );
296}
297
298template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
300 const typename graph<VertexType>::adj_list_const_iterator& edge ) {
301 if ( graphdom::graph<VertexType>::get_owner_graph( edge ) != this ) {
302 throw std::runtime_error("Error"); //TODO: write a better message
303 }
304 return (
305 *(
306 static_cast<edge_endpoint*>(
308 )
309 )
310 ).edge_label;
311}
312
313template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
315 const typename graph<VertexType>::vertex_const_handle& tail,
316 const typename graph<VertexType>::vertex_const_handle& head,
317 const EdgeLabelType& edge_label) {
318 if (
319 graphdom::graph<VertexType>::get_owner_graph( tail ) != this ||
320 graphdom::graph<VertexType>::get_owner_graph( head ) != this
321 ) {
322 throw std::runtime_error("Error"); //TODO: write a better message
323 }
324 auto const begin_point_vertex_container = static_cast< const vertex_container* >( graphdom::graph<VertexType>::get_vertex_container( tail ) );
325 auto const end_point_vertex_container = graphdom::graph<VertexType>::get_vertex_container( head );
326 if ( begin_point_vertex_container == nullptr || end_point_vertex_container == nullptr ) {
327 throw std::runtime_error("Error"); //TODO: write a better message
328 }
329 std::unique_ptr< edge_endpoint > edge_endpoint_to_insert( new edge_endpoint( end_point_vertex_container , edge_label ) );
330 const auto inner_insertion_result = ( ( begin_point_vertex_container->adj ).insert( edge_endpoint_to_insert.get() ) ).second;
331 if ( inner_insertion_result ) {
332 edge_endpoint_to_insert.release();
333 }
334}
335
336template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
338 const typename graph<VertexType>::vertex_const_handle& tail,
339 const typename graph<VertexType>::vertex_const_handle& head,
340 EdgeLabelType&& edge_label) {
341 if (
342 graphdom::graph<VertexType>::get_owner_graph( tail ) != this ||
343 graphdom::graph<VertexType>::get_owner_graph( head ) != this
344 ) {
345 throw std::runtime_error("Error"); //TODO: write a better message
346 }
347 auto const begin_point_vertex_container = static_cast< const vertex_container* >( graphdom::graph<VertexType>::get_vertex_container( tail ) );
348 auto const end_point_vertex_container = graphdom::graph<VertexType>::get_vertex_container( head );
349 if ( begin_point_vertex_container == nullptr || end_point_vertex_container == nullptr ) {
350 throw std::runtime_error("Error"); //TODO: write a better message
351 }
352 const auto lower_bound = ( begin_point_vertex_container->adj ).lower_bound( end_point_vertex_container );
353 if ( lower_bound == ( begin_point_vertex_container->adj ).cend() ) {
354 ( begin_point_vertex_container->adj ).emplace_hint( lower_bound, new edge_endpoint( end_point_vertex_container , std::move(edge_label) ) );
355 }
356 else {
357 if ( ( ( begin_point_vertex_container->adj ).key_comp() )( end_point_vertex_container, *lower_bound ) ) {
358 ( begin_point_vertex_container->adj ).emplace_hint( lower_bound, new edge_endpoint( end_point_vertex_container , std::move(edge_label) ) );
359 }
360 }
361 /*
362 std::unique_ptr< edge_endpoint > edge_endpoint_to_insert( new edge_endpoint( end_point_vertex_container , std::move( edge_label_to_insert ) ) );
363 const auto inner_insertion_result = ( ( begin_point_vertex_container->adj ).insert( edge_endpoint_to_insert.get() ) ).second;
364 if ( inner_insertion_result ) {
365 edge_endpoint_to_insert.release();
366 }
367 */
368}
369
370template<typename VertexType, typename VertexLabelType, typename EdgeLabelType, typename Compare, typename VertexLabellerType, typename EdgeLabellerType>
373 typename graphdom::graph<VertexType>::template edge_endpoint<VertexContainerPointerType>* ee_ptr) {
374 delete static_cast<edge_endpoint*>(ee_ptr);
375}
376
377#endif //GRAPHDOM_FULL_LABELED_SET_DIGRAPH_IMPL_H
Definition full_labeled_set_digraph.h:37
const VertexLabelType & get_vertex_label(const typename graph< VertexType >::vertex_const_handle &vertex) const override
Definition full_labeled_set_digraph.h:86
void erase_vertex(const typename graphdom::graph< VertexType >::vertex_const_handle &vertex) override
Removes the vertex identified by vertex.
Definition full_labeled_set_digraph.h:111
std::size_t order() const override
Returns the order of *this, i.e. the number of vertices inside the graph.
Definition full_labeled_set_digraph.h:81
void insert_edge(const typename graph< VertexType >::vertex_const_handle &tail, const typename graph< VertexType >::vertex_const_handle &head, const EdgeLabelType &edge_label) override
Definition full_labeled_set_digraph.h:314
graphdom::graph< VertexType >::adj_list_iterator erase_edge(const typename graph< VertexType >::adj_list_const_iterator &) override
Definition full_labeled_set_digraph.h:148
std::pair< typename graph< VertexType >::vertex_handle, bool > insert_vertex(const VertexType &v_core, const VertexLabelType &vertex_label) override
Definition full_labeled_set_digraph.h:177
const EdgeLabelType & get_edge_label(const typename graph< VertexType >::adj_list_const_iterator &) const override
Definition full_labeled_set_digraph.h:96
Every valid instance of this class can be used to identify a specific vertex of a graph and to access...
Definition vertex_const_handle.h:35
Every graph created using this library is an instance of a concrete class publicly derived,...
Definition graph.h:43
Every set graph created using this library is an instance of a concrete class publicly derived,...
Definition set_graph.h:18
@ directed
This enum value means directed edge.
Definition graph.h:23