4 # include <tpl_dynSetTree.H>
6 # include <tpl_sgraph.H>
10 using namespace Aleph;
12 template <
typename Node_Info = Empty_Class>
15 static const size_t Contract_Factor = 4;
16 static const size_t Default_Cap = 4;
22 contract_threshold = arcs_dim/Contract_Factor;
27 arc_array = (
void**) malloc(arcs_dim*
sizeof(
void*));
28 if (arc_array == NULL)
29 throw std::bad_alloc();
38 size_t contract_threshold;
50 Graph_Anode(
const Node_Info & info) : node_info(info)
62 if (arc_array != NULL)
66 void allocate_more(
size_t new_size)
71 void ** new_array = (
void**) realloc(arc_array, new_size*
sizeof(
void*));
72 if (new_array == NULL)
73 throw std::bad_alloc();
75 arc_array = new_array;
77 contract_threshold = arcs_dim/Contract_Factor;
80 void * insert_arc(
void * arc)
82 if (num_arcs == arcs_dim)
83 allocate_more(arcs_dim << 1);
85 arc_array[num_arcs++] = arc;
90 void remove_arc(
void * arc)
93 for (
size_t i = 0; i < num_arcs; ++i)
94 if (arc_array[i] == arc)
96 arc_array[i] = arc_array[--num_arcs];
102 throw std::domain_error(
"arc for deleting not found");
104 if (num_arcs > contract_threshold)
108 size_t new_sz = arcs_dim >> 1;
109 arc_array = (
void**) realloc(arc_array, new_sz*
sizeof(
void*));
111 contract_threshold = arcs_dim/Contract_Factor;
116 void ** new_array = (
void**) realloc(arc_array, num_arcs*
sizeof(
void*));
117 if (new_array == NULL)
120 arc_array = new_array;
122 contract_threshold = num_arcs/Contract_Factor;
129 template <
typename Arc_Info = Empty_Class>
142 : arc_info(info), src_node(NULL), tgt_node(NULL)
147 Graph_Aarc(
void * src,
void * tgt,
const Arc_Info & data)
148 : arc_info(data), src_node(src), tgt_node(tgt)
153 Graph_Aarc(
void * src,
void * tgt) : src_node(src), tgt_node(tgt)
160 template <
class __Graph_Node = Graph_Anode<
int>,
161 class __Graph_Arc = Graph_Aarc<
int>>
197 return this->get_curr();
228 return this->get_curr();
256 :
Array_Iterator<void*>(src->arc_array, src->num_arcs), src_node(src)
278 return (Node*) a->get_connected_node(src_node);
282 GRAPH_ITERATIVE_METHODS;
284 GRAPH_SEARCH_METHODS;
286 virtual Node * insert_node(Node * p)
290 Node ** ret_val = node_set.append(p);
294 I(num_nodes = node_set.
size());
302 node_set.for_each([] (Node * p) { p->compress(); });
307 Arc * try_insert_arc(Node * src, Node * tgt,
void * a)
309 Arc * aptr = (Arc*) a;
311 aptr->src_node = src;
312 aptr->tgt_node = tgt;
313 src->insert_arc(aptr);
315 if (not digraph and src != tgt)
319 tgt->insert_arc(aptr);
321 catch (std::bad_alloc)
323 src->remove_arc(aptr);
330 arc_set.append(aptr);
332 I(num_arcs == arc_set.
size());
334 catch (std::bad_alloc)
336 src->remove_arc(aptr);
337 if (not digraph and src != tgt)
338 tgt->remove_arc(aptr);
347 Arc * connect_arc(Arc * arc)
349 return try_insert_arc(get_src_node(arc), get_tgt_node(arc), arc);
354 Arc * insert_arc(Node * src, Node * tgt,
void * a)
356 bool compress_done =
false;
361 return try_insert_arc(src, tgt, a);
369 compress_done =
true;
376 Arc * disconnect_arc(Arc * arc)
378 Node * src = (Node*) arc->src_node;
379 Node * tgt = (Node*) arc->tgt_node;
381 src->remove_arc(arc);
382 if (not digraph and src != tgt)
383 tgt->remove_arc(arc);
387 I(num_arcs == arc_set.
size());
392 GRAPH_INSERTION_METHODS;
394 virtual void remove_arc(Arc * a)
396 delete disconnect_arc(a);
399 virtual void remove_node(Node * p)
404 for (
size_t i = 0; i < num_arcs; ++i)
406 Arc * arc = arc_set(i);
408 if (get_src_node(arc) == p or get_tgt_node(arc) == p)
412 for (
size_t i = 0, n = p->num_arcs; i < n; ++i)
414 Arc * arc = (Arc*) p->arc_array[i];
418 for (
size_t i = 0; i < arcs.
size(); ++i)
424 I(num_nodes == node_set.
size());
429 Node * get_first_node()
const
431 return const_cast<DynSetNode&
>(node_set).get_first();
434 Arc * get_first_arc()
const
436 return const_cast<DynSetArc&
>(arc_set).get_first();
439 Arc * get_first_arc(Node * p)
const
441 if (get_num_arcs(p) == 0)
442 throw std::range_error(
"Node has not arcs");
443 return (Arc*) p->arc_array[0];
455 arc_set.for_each( [] (
const Arc * p) {
delete p; } );
456 node_set.for_each( [] (
const Node * p) {
delete p; } );
469 copy_graph(*
this, const_cast<Array_Graph&>(g),
false);
475 node_set.
swap(g.node_set);
476 arc_set.
swap(g.arc_set);
490 copy_graph(*
this, const_cast<Array_Graph&>(g),
false);
508 Cmp_Arc(Cmp && __cmp = Cmp()) : cmp(__cmp) { }
510 Cmp_Arc(Cmp & __cmp) : cmp(__cmp) { }
512 bool operator () (Arc * a1, Arc * a2)
const
520 template <
class Compare>
521 void sort_arcs(Compare &)
523 throw std::domain_error(
"sortarcs is not defined for Array_Graph");
526 template <
class Compare>
527 void sort_arcs(Compare &&)
529 throw std::domain_error(
"sortarcs is not defined for Array_Graph");
552 template <
typename __Graph_Node = Graph_Anode<
int>,
553 typename __Graph_Arc = Graph_Aarc<
int>>
560 typedef __Graph_Node Node;
562 typedef __Graph_Arc Arc;
566 this->digraph =
true;
572 this->digraph =
true;
587 this->digraph =
true;
605 # endif // TPL_AGRAPH_H
Definition: tpl_agraph.H:239
Node * Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_agraph.H:249
Definition: tpl_agraph.H:554
Array_Graph Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_agraph.H:184
Array_Graph Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_agraph.H:216
size_t remove(const Key &key)
Definition: tpl_dynSetTree.H:274
const size_t & size() const
Retorna la cardinalidad del conjunto.
Definition: tpl_dynSetTree.H:408
size_t size() const
Retorna dimensión actual (más lejano índice escrito)
Definition: tpl_dynArray.H:523
Arc * Item_Type
El tipo de dato que retorna get_current().
Definition: tpl_agraph.H:246
Node * get_tgt_node()
Retorna el nodo destino del arco actual (sólo cuenta si es digrafo)
Definition: tpl_agraph.H:235
Definition: array_it.H:11
Definition: tpl_agraph.H:162
void swap(DynSetTree &dset)
Definition: tpl_dynSetTree.H:65
Node_Arc_Iterator()
Instancia un iterador vacío (inválido).
Definition: tpl_agraph.H:252
Node_Arc_Iterator(Node *src)
Instancia un iterador sobre el nodo _src_node.
Definition: tpl_agraph.H:255
Definition: tpl_agraph.H:13
Arc * Item_Type
Tipo de elemento que retorna get_current()
Definition: tpl_agraph.H:213
Definition: tpl_agraph.H:130
Definition: tpl_agraph.H:208
Node * get_tgt_node() const
Retorna el nodo destino del arco actual.
Definition: tpl_agraph.H:275
T & append()
Definition: tpl_dynArray.H:1115
Arc * get_current() const
Definition: tpl_agraph.H:269
void copy_graph(GT >gt, GT &gsrc, const bool cookie_map=false)
Definition: tpl_graph.H:2505
Definition: tpl_dynArray.H:70
Node * get_src_node()
Retorna el nodo origen del arco actual (sólo cuenta si es digrafo)
Definition: tpl_agraph.H:232
Arc * get_current_arc() const
Retorna un puntero al arco actual.
Definition: tpl_agraph.H:226
Node * get_current_node() const
retorna el nodo actual.
Definition: tpl_agraph.H:195
Definition: tpl_agraph.H:176
Arc * get_current_arc() const
Retorna el arco actual.
Definition: tpl_agraph.H:262
Node * Item_Type
Tipo de elemento que retorna get_current()
Definition: tpl_agraph.H:181
Arc * get_curr() const
Definition: tpl_agraph.H:272