30 # include <tpl_dynSetTree.H> 31 # include <array_it.H> 32 # include <tpl_sgraph.H> 36 using namespace Aleph;
42 template <
typename Node_Info = Empty_Class>
50 static const size_t Contract_Factor = 4;
51 static const size_t Default_Cap = 4;
57 contract_threshold = arcs_dim/Contract_Factor;
62 arc_array = (
void**) malloc(arcs_dim*
sizeof(
void*));
63 if (arc_array ==
nullptr)
64 throw std::bad_alloc();
71 size_t contract_threshold;
94 noexcept(std::is_nothrow_copy_assignable<Node_Info>::value)
98 this->node_info = node.node_info;
109 if (arc_array !=
nullptr)
113 void allocate_more(
size_t new_size)
118 void ** new_array = (
void**) realloc(arc_array, new_size*
sizeof(
void*));
119 if (new_array ==
nullptr)
120 throw std::bad_alloc();
122 arc_array = new_array;
124 contract_threshold = arcs_dim/Contract_Factor;
127 void * insert_arc(
void * arc)
130 allocate_more(arcs_dim << 1);
137 void remove_arc_ne(
void * arc) noexcept
139 bool removed =
false;
140 for (
size_t i = 0; i < this->
num_arcs; ++i)
141 if (arc_array[i] == arc)
143 arc_array[i] = arc_array[--(this->
num_arcs)];
148 if (this->num_arcs > contract_threshold)
152 size_t new_sz = arcs_dim >> 1;
153 arc_array = (
void**) realloc(arc_array, new_sz*
sizeof(
void*));
155 contract_threshold = arcs_dim/Contract_Factor;
158 void remove_arc(
void * arc)
160 bool removed =
false;
161 for (
size_t i = 0; i < this->
num_arcs; ++i)
162 if (arc_array[i] == arc)
164 arc_array[i] = arc_array[--(this->
num_arcs)];
170 throw std::domain_error(
"arc for deleting not found");
172 if (this->num_arcs > contract_threshold)
176 size_t new_sz = arcs_dim >> 1;
177 arc_array = (
void**) realloc(arc_array, new_sz*
sizeof(
void*));
179 contract_threshold = arcs_dim/Contract_Factor;
182 bool compress() noexcept
184 void ** new_array = (
void**) realloc(arc_array,
186 if (new_array ==
nullptr)
189 arc_array = new_array;
191 contract_threshold = this->
num_arcs/Contract_Factor;
198 template <
typename Arc_Info = Empty_Class>
208 Graph_Aarc(
const Arc_Info & info)
209 noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value) : Base(info)
214 Graph_Aarc(Arc_Info && info = Arc_Info())
215 noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value)
221 Graph_Aarc(
const Graph_Aarc & arc)
222 noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value)
223 : Graph_Aarc(arc.arc_info) { }
225 Graph_Aarc & operator = (
const Graph_Aarc & arc)
226 noexcept(std::is_nothrow_copy_assignable<Arc_Info>::value)
230 this->arc_info = arc.arc_info;
234 Graph_Aarc(
void * src,
void * tgt,
const Arc_Info & data)
235 noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value)
236 : Base(src, tgt, data)
241 Graph_Aarc(
void * src,
void * tgt, Arc_Info && data = Arc_Info())
242 noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value)
243 : Base(src, tgt, move(data))
250 template <
class __Graph_Node = Graph_Anode<
unsigned long>,
251 class __Graph_Arc = Graph_Aarc<
unsigned long>>
253 :
public GraphCommon<Array_Graph<__Graph_Node, __Graph_Arc>,
254 __Graph_Node, __Graph_Arc>
258 using Node = __Graph_Node;
259 using Arc = __Graph_Arc;
261 using Arc_Type =
typename Arc::Arc_Type;
263 friend class GraphCommon<Array_Graph<__Graph_Node, __Graph_Arc>,
264 __Graph_Node, __Graph_Arc>;
267 __Graph_Node, __Graph_Arc>;
269 using CommonBase::insert_node;
270 using CommonBase::insert_arc;
288 :
Base(const_cast<Dlink&>(g.node_set))
307 :
Base(const_cast<Dlink&>(g.arc_set))
315 Node * src_node =
nullptr;
326 Node_Arc_Iterator() noexcept { }
329 Node_Arc_Iterator(Node * src) noexcept
331 src->arc_array, src->arcs_dim, src->num_arcs),
337 Arc * get_curr()
const 339 return (Arc*)
const_cast<Node_Arc_Iterator*
>(
this)->
343 Arc * get_curr_ne()
const noexcept
345 return (Arc*)
const_cast<Node_Arc_Iterator*
>(
this)->
350 Arc * get_current_arc_ne()
const noexcept {
return get_curr_ne(); }
353 Arc * get_current_arc()
const {
return get_curr(); }
356 Node * get_tgt_node_ne()
const 358 Arc * a = get_curr_ne();
359 return (Node*) a->get_connected_node(src_node);
362 Node * get_tgt_node()
const 364 Arc * a = get_curr();
365 return (Node*) a->get_connected_node(src_node);
369 virtual Node * insert_node(Node * p)
371 assert(p !=
nullptr);
372 assert(p->num_arcs == 0);
382 for (Node_Iterator it(*
this); it.has_curr(); it.next_ne())
383 it.get_curr_ne()->compress();
388 Arc * try_insert_arc(Node * src, Node * tgt,
void * a)
390 Arc * aptr = (Arc*) a;
392 aptr->src_node = src;
393 aptr->tgt_node = tgt;
394 src->insert_arc(aptr);
396 if (not this->digraph and src != tgt)
400 tgt->insert_arc(aptr);
402 catch (std::bad_alloc)
404 src->remove_arc(aptr);
414 catch (std::bad_alloc)
416 src->remove_arc(aptr);
417 if (not this->digraph and src != tgt)
418 tgt->remove_arc(aptr);
427 Arc * connect_arc(Arc * arc)
430 try_insert_arc(this->get_src_node(arc), this->get_tgt_node(arc), arc);
435 Arc * insert_arc(Node * src, Node * tgt,
void * a)
437 bool compress_done =
false;
442 return try_insert_arc(src, tgt, a);
450 compress_done =
true;
457 Arc * disconnect_arc(Arc * arc)
459 Node * src = (Node*) arc->src_node;
460 Node * tgt = (Node*) arc->tgt_node;
462 src->remove_arc_ne(arc);
463 if (not this->digraph and src != tgt)
464 tgt->remove_arc_ne(arc);
472 virtual void remove_arc(Arc * a)
474 delete disconnect_arc(a);
477 virtual void remove_node(Node * p)
482 for (
Arc_Iterator it(*
this); it.has_curr(); it.next_ne())
484 Arc * arc = it.get_curr_ne();
485 if (this->get_src_node(arc) == p or this->get_tgt_node(arc) == p)
490 for (
size_t i = 0, n = p->num_arcs; i < n; ++i)
492 Arc * arc = (Arc*) p->arc_array[i];
496 arcs.
for_each([
this] (
auto arc) { this->remove_arc(arc); });
504 Node * get_first_node()
const 509 Arc * get_first_arc()
const 514 Arc * get_first_arc(Node * p)
const 516 if (get_num_arcs(p) == 0)
517 throw std::range_error(
"Node has not arcs");
518 return (Arc*) p->arc_array[0];
523 assert(this->num_nodes == 0 and this->
num_arcs == 0 and
529 void clear() noexcept
537 this->num_nodes = this->
num_arcs = 0;
542 virtual ~Array_Graph() noexcept
547 Array_Graph(
const Array_Graph & g)
549 assert(this->num_nodes == 0 and this->
num_arcs == 0);
553 void swap(Array_Graph & g) noexcept
555 this->common_swap(g);
556 node_set.
swap(g.node_set);
557 arc_set.
swap(g.arc_set);
560 Array_Graph(Array_Graph && g) noexcept
565 Array_Graph & operator = (
const Array_Graph & g)
575 Array_Graph & operator = (Array_Graph && g) noexcept
588 Cmp_Arc(Cmp && __cmp = Cmp())
589 noexcept(std::is_nothrow_constructible<Cmp>::value and
590 std::is_nothrow_move_assignable<Cmp>::value)
593 Cmp_Arc(Cmp & __cmp) noexcept(std::is_nothrow_copy_assignable<Cmp>::value)
596 bool operator () (Arc * a1, Arc * a2)
const noexcept
625 template <
class Compare>
inline 626 void sort_nodes(Compare & cmp) noexcept
629 mergesort(node_set, c);
633 template <
class Compare>
inline 634 void sort_nodes(Compare && cmp = Compare()) noexcept
660 template <
class Compare>
661 void sort_arcs(Compare & cmp)
664 mergesort(arc_set, c);
668 template <
class Compare>
669 void sort_arcs(Compare && cmp) { sort_arcs(cmp); }
689 template <
typename __Graph_Node = Graph_Anode<
int>,
690 typename __Graph_Arc = Graph_Aarc<
int>>
693 using GT = Array_Graph<__Graph_Node, __Graph_Arc>;
697 using Node = __Graph_Node;
699 using Arc = __Graph_Arc;
703 this->digraph =
true;
709 this->digraph =
true;
724 this->digraph =
true;
739 # endif // TPL_AGRAPH_H NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition: graph-dry.H:242
Definition: tpl_agraph.H:691
Definition: tpl_agraph.H:301
void init() noexcept
Definition: dlink.H:186
Definition: graph-dry.H:170
Array_Graph Set_Type
The type of container on which iterate.
Definition: graph-dry.H:92
void for_each(Operation &operation) noexcept(noexcept(operation))
Definition: htlist.H:589
Definition: array_it.H:45
void copy_graph(GT >gt, const GT &gsrc, const bool cookie_map=false)
Definition: tpl_graph.H:3482
Definition: graph-dry.H:83
Definition: graph-dry.H:272
void swap(Dlink *link) noexcept
Definition: dlink.H:82
Definition: graph-dry.H:329
size_t num_arcs
data associated to the node. Access it with get_info()
Definition: graph-dry.H:227
Definition: tpl_agraph.H:43
Definition: graph-dry.H:42
DynList< typename GT::Arc * > arcs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1893
Definition: graph-dry.H:206
Arc * Item_Type
The type of item that returns the iterator.
Definition: graph-dry.H:89
Node_Info Node_Type
The node.
Definition: graph-dry.H:233
bool is_empty() const noexcept
Return true if this (as header node) is empty.
Definition: dlink.H:192
Dlink *& get_first() const noexcept
If this is a header node, it return the first node of this
Definition: dlink.H:261
Definition: tpl_graph.H:1270
void append(Dlink *node) noexcept
Definition: dlink.H:228
T & append(const T &item)
Definition: htlist.H:1471
Dlink * remove_first() noexcept
Definition: dlink.H:459
Definition: graph-dry.H:147