-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
1176 lines (929 loc) · 35.4 KB
/
graph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _PTL_ALGORITHM_GRAPH_H
#define _PTL_ALGORITHM_GRAPH_H
#include <list>
#include <vector>
#include <string>
#include "mac.h"
#include "io.h"
#define BEGIN_SPECIALIZE_WEIGHT_NODE(TYPE, BASE_NODE_TYPE, BASE_EDGE_TYPE, CONTAINER_TYPE, ADJACENT_TYPE) \
template <class T> struct TYPE;\
template <class T> struct TYPE \
_EXTENDS BASE_NODE_TYPE<CONTAINER_TYPE<BASE_EDGE_TYPE<TYPE<T>,T> >, ADJACENT_TYPE> \
{ typedef BASE_NODE_TYPE<CONTAINER_TYPE<BASE_EDGE_TYPE<TYPE<T>,T> >, ADJACENT_TYPE> parents_type;
#define BEGIN_WEIGHT_NODE(TYPE, CONTAINTER_TYPE, ADJACENT_TYPE) \
BEGIN_SPECIALIZE_WEIGHT_NODE(TYPE,ptl::node_base,ptl::edge_base,CONTAINTER_TYPE, ADJACENT_TYPE)
#define BEGIN_VECTOR_WEIGHT_NODE(TYPE, ADJACENT_TYPE) \
BEGIN_WEIGHT_NODE(TYPE, std::vector, ADJACENT_TYPE)
#define BEGIN_SPECIALIZE_NODE(TYPE, BASE_TYPE, CONTAINER_TYPE, ADJACENT_TYPE) \
struct TYPE;\
struct TYPE _EXTENDS BASE_TYPE<CONTAINER_TYPE<TYPE*>, ADJACENT_TYPE> \
{ typedef BASE_TYPE<CONTAINER_TYPE<TYPE*>, ADJACENT_TYPE> parents_type;
#define BEGIN_NODE(TYPE, CONTAINER_TYPE, ADJACENT_TYPE) \
BEGIN_SPECIALIZE_NODE(TYPE, ptl::node_base, CONTAINER_TYPE, ADJACENT_TYPE)
#define BEGIN_VECTOR_NODE(TYPE, ADJACENT_TYPE) \
BEGIN_NODE(TYPE, std::vector, ADJACENT_TYPE)
//
#define BEGIN_SPECIALIZE_WEIGHT_NODE(TYPE, BASE_NODE_TYPE, BASE_EDGE_TYPE, CONTAINER_TYPE, ADJACENT_TYPE) \
template <class T> struct TYPE;\
template <class T> struct TYPE \
_EXTENDS BASE_NODE_TYPE<CONTAINER_TYPE<BASE_EDGE_TYPE<TYPE<T>,T> >, ADJACENT_TYPE> \
{ typedef BASE_NODE_TYPE<CONTAINER_TYPE<BASE_EDGE_TYPE<TYPE<T>,T> >, ADJACENT_TYPE> parents_type;
#define BEGIN_WEIGHT_NODE(TYPE, CONTAINTER_TYPE, ADJACENT_TYPE) \
BEGIN_SPECIALIZE_WEIGHT_NODE(TYPE,ptl::node_base,ptl::edge_base,CONTAINTER_TYPE, ADJACENT_TYPE)
#define BEGIN_VECTOR_WEIGHT_NODE(TYPE, ADJACENT_TYPE) \
BEGIN_WEIGHT_NODE(TYPE, std::vector, ADJACENT_TYPE)
//
#define BEGIN_SPECIALIZE_NODE2(TYPE, BASE_TYPE, CONTAINER_TYPE, ADJACENT_TYPE) \
struct TYPE _EXTENDS BASE_TYPE<CONTAINER_TYPE<TYPE*>, ADJACENT_TYPE> \
{ typedef BASE_TYPE<CONTAINER_TYPE<TYPE*>, ADJACENT_TYPE> parents_type;
#define BEGIN_NODE2(TYPE, CONTAINER_TYPE, ADJACENT_TYPE) \
BEGIN_SPECIALIZE_NODE2(TYPE, ptl::node_base, CONTAINER_TYPE, ADJACENT_TYPE)
#define BEGIN_VECTOR_NODE2(TYPE, ADJACENT_TYPE) \
BEGIN_NODE2(TYPE, std::vector, ADJACENT_TYPE)
//
#define BEGIN_SPECIALIZE_WEIGHT_NODE2(TYPE, BASE_NODE_TYPE, BASE_EDGE_TYPE, CONTAINER_TYPE, ADJACENT_TYPE) \
template <class T> struct TYPE;\
template <class T> struct TYPE \
_EXTENDS BASE_NODE_TYPE<CONTAINER_TYPE<BASE_EDGE_TYPE<TYPE<T>,T> >, ADJACENT_TYPE> \
{ typedef BASE_NODE_TYPE<CONTAINER_TYPE<BASE_EDGE_TYPE<TYPE<T>,T> >, ADJACENT_TYPE> parents_type;
#define BEGIN_WEIGHT_NODE2(TYPE, CONTAINTER_TYPE, ADJACENT_TYPE) \
BEGIN_SPECIALIZE_WEIGHT_NODE2(TYPE,ptl::node_base,ptl::edge_base,CONTAINTER_TYPE, ADJACENT_TYPE)
#define BEGIN_VECTOR_WEIGHT_NODE2(TYPE, ADJACENT_TYPE) \
BEGIN_WEIGHT_NODE2(TYPE, std::vector, ADJACENT_TYPE)
//
#define END_SPECLAILIZE_NODE };
#define END_NODE };
namespace ptl {
typedef int adjacent_type;
struct adjacent
{
static const adjacent_type nondirectional = 0x00;
static const adjacent_type inwards = 0x01;
static const adjacent_type outwards = 0x02;
static const adjacent_type bidirectional = inwards|outwards;
};
template <class T, class weight_interface>
class edge_base _EXTENDS weight_interface
{
public:
typedef T node_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef weight_interface weight_type;
inline edge_base() : weight_type() {}
inline edge_base(T* x) : weight_type() { __pt = x; }
template <class T2>
inline edge_base(T* x, const T2& w) : weight_type(w) { __pt = x; }
inline pointer operator-> () { return __pt; }
inline const_pointer operator->() const { return __pt; }
inline reference operator*() { return *__pt; }
inline const_reference operator*() const { return *__pt; }
inline bool operator == (const void* p) const
{
return __pt == p;
}
inline bool operator < (const void* p) const
{
return __pt < p;
}
inline bool operator > (const void* p) const
{
return __pt > p;
}
inline bool operator <= (const void* p) const
{
return __pt <= p;
}
inline bool operator >= (const void* p) const
{
return __pt >= p;
}
private:
T* __pt;
};
template <_INTERFACE container_interface, adjacent_type type = adjacent::outwards>
class node_base
{
inline node_base() { }
};
template <_INTERFACE container_interface>
class node_base<container_interface, adjacent::nondirectional> _EXTENDS container_interface
{
public:
typedef container_interface parents_type;
typedef parents_type inwards;
typedef parents_type outwards;
typedef _TYPENAME parents_type::value_type edge_type;
typedef _TYPENAME outwards::value_type outwards_edge_type;
typedef _TYPENAME inwards::value_type inwards_edge_type;
typedef _TYPENAME parents_type::size_type size_type;
typedef _TYPENAME parents_type::iterator iterator;
//
// Degree
//
inline const size_type degree() const { return self.size(); }
inline const size_type outwards_degree() const { return self.size(); }
inline const size_type inwards_degree() const { return self.size(); }
//
// Add
//
template <class T>
inline void add(T* n)
{
this->push_back(n);
n->push_back(static_cast<T*>(this));
}
template <class T>
inline void add_outwards(T* n) { add(n); }
template <class T>
inline void add_inwards(T* n) { add(n); }
template <class T1, class T2>
inline void add(T1* n, const T2& edge_property)
{
this->push_back(edge_type(n,edge_property));
n->push_back(edge_type(static_cast<T1*>(this), edge_property));
}
template <class T1, class T2>
inline void add_outwards(T1* n, const T2& edge_property) { add(n, edge_property); }
template <class T1, class T2>
inline void add_inwards(T1* n, const T2& edge_property) { add(n, edge_property); }
//
// Remove
//
inline void remove(iterator position)
{
*position = self.back();
self.pop_back();
}
inline void remove_inwards(_TYPENAME inwards::iterator position) { remove(position); }
inline void remove_outwards(_TYPENAME outwards::iterator position) { remove(position); }
//
// Type
//
static inline const adjacent_type type() { return adjacent::nondirectional; }
};
template <_INTERFACE container_interface>
class node_base<container_interface, adjacent::inwards> _EXTENDS container_interface
{
public:
typedef container_interface parents_type;
typedef container_interface inwards;
typedef _TYPENAME parents_type::value_type edge_type;
typedef _TYPENAME inwards::value_type inwards_edge_type;
typedef _TYPENAME parents_type::size_type size_type;
inline const size_type inwards_degree() const { return inwards::size();}
template <class T>
inline void add(T* n)
{
add_inwards(n);
}
template <class T>
inline void add_outwards(T* n)
{
n->inwards::push_back(static_cast<T*>(this));
}
template <class T>
inline void add_inwards(T* n)
{
this->inwards::push_back(n);
}
template <class T1, class T2>
inline void add(T1* n, const T2& edge_property)
{
add_inwards(n, edge_property);
}
template <class T1, class T2>
inline void add_inwards(T1* n, const T2& edge_property)
{
this->inwards::push_back(inwards_edge_type(n,edge_property));
}
template <class T1, class T2>
inline void add_outwards(T1* n, const T2& edge_property)
{
n->inwards::push_back(inwards_edge_type(static_cast<T1*>(this),edge_property));
}
inline void remove_inwards(_TYPENAME inwards::iterator position)
{
*position = inwards::back();
inwards::pop_back();
}
static inline const adjacent_type type() { return adjacent::inwards; }
};
template <_INTERFACE container_interface>
class node_base<container_interface, adjacent::outwards> _EXTENDS container_interface
{
public:
typedef container_interface parents_type;
typedef container_interface outwards;
typedef _TYPENAME parents_type::value_type edge_type;
typedef _TYPENAME outwards::value_type outwards_edge_type;
typedef _TYPENAME parents_type::size_type size_type;
inline const size_type outwards_degree() const { return outwards::size();}
template <class T>
inline void add(T* n)
{
add_outwards(n);
}
template <class T>
inline void add_outwards(T* n)
{
this->outwards::push_back(n);
}
template <class T>
inline void add_inwards(T* n)
{
n->outwards::push_back(static_cast<T*>(this));
}
template <class T1, class T2>
inline void add(T1* n, const T2& edge_property)
{
add_outwards(n, edge_property);
}
template <class T1, class T2>
inline void add_outwards(T1* n, const T2& edge_property)
{
this->outwards::push_back(outwards_edge_type(n,edge_property));
}
template <class T1, class T2>
inline void add_inwards(T1* n, const T2& edge_property)
{
n->outwards::push_back(outwards_edge_type(this,edge_property));
}
inline void remove_outwards(_TYPENAME outwards::iterator position)
{
*position = outwards::back();
outwards::pop_back();
}
static inline const adjacent_type type() { return adjacent::outwards; }
};
template <_INTERFACE outwards_interface, _INTERFACE inwards_interface>
class binode_base : public outwards_interface, public inwards_interface
{
public:
typedef outwards_interface outwards;
typedef inwards_interface inwards;
typedef _TYPENAME outwards::value_type outwards_edge_type;
typedef _TYPENAME inwards::value_type inwards_edge_type;
typedef _TYPENAME outwards::size_type size_type;
template <class T>
inline void add_outwards(T* n)
{
this->outwards::push_back(n);
n->inwards::push_back(static_cast<T*>(this));
}
template <class T>
inline void add_inwards(T* n)
{
n->outwards::push_back(static_cast<T*>(this));
this->inwards::push_back(n);
}
template <class T1, class T2>
inline void add_outwards(T1* n, const T2& edge_property)
{
this->outwards::push_back(outwards_edge_type(n,edge_property));
n->inwards::push_back(inwards_edge_type(static_cast<T1*>(this), edge_property));
}
template <class T1, class T2>
inline void add_inwards(T1* n, const T2& edge_property)
{
n->outwards::push_back(outwards_edge_type(static_cast<T1*>(this),edge_property));
this->inwards::push_back(inwards_edge_type(n,edge_property));
}
static inline const adjacent_type type() { return adjacent::bidirectional; }
};
template <_INTERFACE container_interface>
class node_base<container_interface, adjacent::bidirectional>
_EXTENDS binode_base<node_base<container_interface, adjacent::outwards>,
node_base<container_interface, adjacent::inwards> >
{
};
template <class T>
struct search_out_node _EXTENDS T
{
typedef T parents_type;
typedef _TYPENAME parents_type::outwards outwards;
typedef _TYPENAME outwards::iterator iterator;
typedef _TYPENAME outwards::const_iterator const_iterator;
typedef _TYPENAME outwards::reverse_iterator reverse_iterator;
typedef _TYPENAME outwards::const_reverse_iterator const_reverse_iterator;
iterator begin () { return this->outwards::begin(); }
const_iterator begin () const { return this->outwards::begin(); }
iterator end () { return this->outwards::end(); }
const_iterator end () const { return this->outwards::end(); }
reverse_iterator rbegin () { return this->outwards::rbegin(); }
const_reverse_iterator rbegin () const { return this->outwards::rbegin(); }
reverse_iterator rend () { return this->outwards::rend(); }
const_reverse_iterator rend () const { return this->outwards::rend(); }
};
template <class T>
struct search_in_node _EXTENDS T
{
typedef T parents_type;
typedef _TYPENAME parents_type::inwards inwards;
typedef _TYPENAME inwards::iterator iterator;
typedef _TYPENAME inwards::const_iterator const_iterator;
typedef _TYPENAME inwards::reverse_iterator reverse_iterator;
typedef _TYPENAME inwards::const_reverse_iterator const_reverse_iterator;
iterator begin () { return this->inwards::begin(); }
const_iterator begin () const { return this->inwards::begin(); }
iterator end () { return this->inwards::end(); }
const_iterator end () const { return this->inwards::end(); }
reverse_iterator rbegin () { return this->inwards::rbegin(); }
const_reverse_iterator rbegin () const { return this->inwards::rbegin(); }
reverse_iterator rend () { return this->inwards::rend(); }
const_reverse_iterator rend () const { return this->inwards::rend(); }
};
template <class node_type>
inline search_out_node<node_type>* search_out_cast(node_type* p)
{
return _REINTERPRET_CAST(search_out_node<node_type>*, p);
}
template <class node_type>
inline search_in_node<node_type>* search_in_cast(node_type* p)
{
return _REINTERPRET_CAST(search_in_node<node_type>*, p);
}
template <class new_node_type>
inline new_node_type* search_cast(_TYPENAME new_node_type::parents_type* p)
{
return _REINTERPRET_CAST(new_node_type*, p);
}
template <_INTERFACE container_interface,
_INTERFACE node_interface = _TYPENAME container_interface::value_type>
class link_graph_base _EXTENDS container_interface
{
public:
typedef container_interface parents;
typedef node_interface node_type;
typedef _TYPENAME node_type::size_type size_type;
};
template<_INTERFACE task_interface>
class breadth_first_search _EXTENDS task_interface
{
public:
typedef task_interface task_type;
typedef _TYPENAME task_type::container_type container_type;
typedef _TYPENAME task_type::size_type size_type;
template <class graph_type, class node_type>
void search(graph_type& graph, node_type* start);
template <class graph_type, class node_type>
inline void search_out(graph_type& graph, node_type* start)
{
search(graph, search_out_cast(start));
}
template <class graph_type, class node_type>
inline void search_in(graph_type& graph, node_type* start)
{
search(graph, search_in_cast(start));
}
template <class graph_type, class node_type, class task_type2>
void search(graph_type& graph, node_type* start, task_type2 task);
template <class graph_type, class node_type, class task_type2>
inline void search_out(graph_type& graph, node_type* start, task_type2 task)
{
search(graph, search_out_cast(start), task);
}
template <class graph_type, class node_type, class task_type2>
inline void search_in(graph_type& graph, node_type* start, task_type2 task)
{
search(graph, search_in_cast(start), task);
}
};
template <_INTERFACE task_interface>
template <class graph_type, class node_type>
void breadth_first_search<task_interface>::
search(graph_type& graph, node_type* start)
{
register size_type distance = 1;
register node_type *current = start;
register container_type* last_layer;
entry(last_layer, current);
do
{
for (register _TYPENAME container_type::iterator i = last_layer->begin();
i != last_layer->end(); i ++)
{
register node_type* pnode = _REINTERPRET_CAST(node_type*, *i);
for (register _TYPENAME node_type::iterator j = pnode->begin();
j != pnode->end(); j ++)
{
current = _REINTERPRET_CAST(node_type*, &**j);
if (current->distance() >= distance+1)
{
current->distance() = distance;
if (update(current)) return;
}
}
}
} while (next_layer(last_layer, distance));
}
template <_INTERFACE task_interface>
template <class graph_type, class node_type, class task_type2>
void breadth_first_search<task_interface>::
search(graph_type& graph, node_type* start, task_type2 task)
{
register size_type distance = 1;
register node_type *current = start;
register container_type* last_layer;
entry(last_layer, current);
task(current, start);
do
{
for (register _TYPENAME container_type::iterator i = last_layer->begin();
i != last_layer->end(); i ++)
{
register node_type* pnode = _REINTERPRET_CAST(node_type*, *i);
for (register _TYPENAME node_type::iterator j = pnode->begin();
j != pnode->end(); j ++)
{
current = _REINTERPRET_CAST(node_type*, &**j);
if (current->distance() >= distance+1)
{
current->distance() = distance;
task(current, start);
if (update(current)) return;
}
}
}
} while (next_layer(last_layer, distance));
}
template <_INTERFACE task_interface>
class depth_first_search _EXTENDS task_interface
{
public:
typedef task_interface task_type;
template <class graph_type, class node_type>
void search(graph_type& graph, node_type* start);
template <class graph_type, class node_type>
inline void search_out(graph_type& graph, node_type* start)
{
search(graph, search_out_cast(start));
}
template <class graph_type, class node_type>
inline void search_in(graph_type& graph, node_type* start)
{
search(graph, search_in_cast(start));
}
private:
template <class node_type, class iterator_type>
struct search_stack
{
node_type* node;
iterator_type iterator;
};
};
template <_INTERFACE task_interface>
template <class graph_type, class node_type>
void depth_first_search<task_interface>::
search(graph_type& graph, node_type* start)
{
typedef _TYPENAME node_type::size_type size_type;
typedef _TYPENAME node_type::iterator iterator;
typedef search_stack<node_type, iterator> stack_type;
register node_type *current = start;
register iterator current_iterator = current->begin();
register stack_type *stack_top = new stack_type[graph.size()];
register stack_type *stack_pointer = stack_top;
self.entry(current);
while (true)
{
if (current_iterator == current->end()
|| pop_condition(current, current_iterator))
{
if (stack_pointer == stack_top) break;
pop(current, current_iterator);
-- stack_pointer;
current = stack_pointer->node;
current_iterator = stack_pointer->iterator;
}
else
{
register node_type* next = _REINTERPRET_CAST(node_type*, &**current_iterator);
if (push_condition(current, next, current_iterator))
{
push(current, next, current_iterator);
stack_pointer->node = current;
stack_pointer->iterator = current_iterator;
++ stack_pointer->iterator;
++ stack_pointer;
current = next;
current_iterator = current->begin();
}
else
{
++ current_iterator;
}
}
}
self.exit(current);
delete [] stack_top;
}
struct search_task_base
{
template <class graph_type>
static inline void initialize(graph_type& g){ }
};
template <class container_interface>
struct breadth_first_search_task_base _EXTENDS search_task_base
{
typedef container_interface container_type;
typedef search_task_base parents_type;
_TYPE_INHERIT(container_type, size_type)
template <class node_type>
inline void entry(container_type*& last_layer, node_type* current){}
template <class node_type>
inline bool update(node_type* p){ return false; }
inline bool next_layer(container_type*& last_layer, size_type& distance)
{ return false; }
};
template <class container_interface>
class bf_distance_search_task
_EXTENDS breadth_first_search_task_base<container_interface>
{
public:
typedef container_interface container_type;
typedef breadth_first_search_task_base<container_interface> parents_type;
_TYPE_INHERIT(container_type, size_type)
template <class graph_type>
static void initialize(graph_type& graph);
template <class node_type>
inline void entry(container_type*& last_layer, node_type* current)
{
current_layer = &t1, last_layer = &t2;
last_layer->clear(), current_layer->clear();
last_layer->push_back(current);
current->distance() = 0;
}
template <class node_type>
inline bool update(node_type* p)
{
current_layer->push_back(p);
return false;
}
inline bool next_layer(container_type*& last_layer, size_type& distance)
{
register container_type* temp = last_layer;
last_layer = current_layer, current_layer = temp;
current_layer->clear();
distance ++;
return !last_layer->empty();
}
protected:
container_type t1, t2, *current_layer;
};
template <class container_interface>
template <class graph_type>
void bf_distance_search_task<container_interface>::
initialize(graph_type& graph)
{
for (typename graph_type::iterator p = graph.begin(); p != graph.end(); p ++)
{
//p->second.distance() = graph.size();
p->distance() = graph.size();
}
}
template <class container_interface>
class bf_cut_distance_search_task
_EXTENDS bf_distance_search_task<container_interface>
{
public:
typedef container_interface container_type;
typedef bf_distance_search_task<container_type> parents_type;
_TYPE_INHERIT(container_type, size_type)
template <class graph_type>
void initialize(graph_type& graph) const;
inline bool next_layer(container_type*& last_layer, size_type& distance)
{
return parents_type::next_layer(last_layer, distance) && distance < self.cut();
}
_DEFAULT_IMPLEMENT(size_type, cut)
};
template <class container_interface>
template <class graph_type>
void bf_cut_distance_search_task<container_interface>::
initialize(graph_type& graph) const
{
for (typename graph_type::iterator p = graph.begin(); p != graph.end(); p ++)
{
p->distance() = self.cut();
}
}
class depth_first_search_task_base _EXTENDS search_task_base
{
public:
template <class graph_type>
void initialize(graph_type& graph) const;
protected:
template <class node_type>
static inline void entry(node_type* current)
{
current->used() = true;
}
template <class node_type>
static inline void exit(node_type* current)
{
current->used() = false;
}
template <class node_type, class iterator_type>
static inline const bool
push_condition(node_type* current, node_type* next, iterator_type)
{
return !next->used();
}
template <class node_type, class iterator_type>
static inline void push(node_type* current, node_type* next, iterator_type)
{
next->used() = true;
}
template <class node_type, class iterator_type>
static inline const bool pop_condition(node_type* current, iterator_type)
{
return false;
}
template <class node_type, class iterator_type>
static inline void pop(node_type* current, iterator_type)
{
current->used() = false;
}
};
template <class graph_type>
void depth_first_search_task_base::initialize(graph_type& graph) const
{
for (typename graph_type::iterator p = graph.begin(); p != graph.end(); p ++)
{
p->used() = false;
}
}
class df_connectivity_search_task _EXTENDS depth_first_search_task_base
{
public:
typedef depth_first_search_task_base parents_type;
template <class graph_type>
void initialize(graph_type& graph);
protected:
template <class node_type>
inline void entry(node_type* current) const
{
current->type() = self.type_num();
}
template <class node_type>
inline void exit(node_type* current)
{
self.type_num() ++;
}
template <class node_type, class iterator_type>
inline const bool push_condition(node_type* current,node_type* next, iterator_type) const
{
return next->type() != self.type_num();
}
template <class node_type, class iterator_type>
inline void push(node_type* current, node_type* next, iterator_type) const
{
next->type() = self.type_num();
}
template <class node_type, class iterator_type>
static inline void pop(node_type* current, iterator_type)
{
}
_DEFAULT_IMPLEMENT(size_t, type_num)
};
template <class graph_type>
void df_connectivity_search_task::initialize(graph_type& graph)
{
self.type_num() = 0;
unsigned i = 0;
for (i = 0; i != graph.size(); i ++)
{
graph[i].type() = graph.size();
}
/*
return;
_TYPENAME graph_type::iterator p = graph.begin(), p1 = graph.end();
cout << "hehe" << graph.size() << endl;
for (; p != graph.end(); ++ p)
{
cout << i ++ << endl;
p->type() = graph.size();
}
*/
}
template <_INTERFACE node_interface>
class common_link_graph _EXTENDS link_graph_base<std::vector<node_interface> >
{
public:
typedef node_interface node_type;
typedef _TYPENAME node_type::size_type size_type;
typedef link_graph_base<std::vector<node_type> > parents_type;
typedef node_type* iterator;
typedef const node_type* const_iterator;
inline const_iterator begin() const { return &*parents_type::begin(); }
inline iterator begin() { return &*parents_type::begin(); }
inline const_iterator end() const { return &*parents_type::end(); }
inline iterator end() { return &*parents_type::end(); }
inline size_type index_of(const node_type* node) const
{
return size_type(node-begin());
}
};
template <_INTERFACE node_interface>
class common_link_graph2 _EXTENDS link_graph_base<std::vector<node_interface*>, node_interface>
{
public:
typedef node_interface node_type;
typedef _TYPENAME node_type::size_type size_type;
typedef link_graph_base<std::vector<node_type*>, node_interface> parents_type;
typedef _TYPENAME parents_type::iterator iterator;
inline size_type index_of(const node_type* node) const
{
return node->index();
}
};
template <_INTERFACE node_interface>
class common_list_graph _EXTENDS link_graph_base<std::list<node_interface> >
{
public:
typedef node_interface node_type;
typedef _TYPENAME node_type::size_type size_type;
template <class T>
inline size_type index_of(T node) const { return node->index(); }
};
template <class graph_type>
void load_graph(std::istream& is, graph_type& g)
{
typedef _TYPENAME graph_type::node_type::size_type size_type;
size_type i;
std::vector<unsigned> data;
sscan(is, data);
g.clear();
if (data.empty()) return;
size_type size = *std::max_element(data.begin(), data.end())+1;
g.reserve(size);
g.resize(size);
for (i = 0; i < data.size()/2; i ++)
{
g[data[2*i]].add_outwards(&g[data[2*i+1]]);
}
}
template <class graph_type>
void load_weight_graph(std::istream& is, graph_type& g)
{
typedef _TYPENAME graph_type::node_type::size_type size_type;
size_type n, i;
std::vector<unsigned> data;
sscan(is, data);
g.clear();
if (data.empty()) return;
size_type size = *std::max_element(data.begin(), data.end())+1;
g.reserve(size);
g.resize(size);
for (i = 0; i < data.size()/3; i ++)
{
g[data[3*i]].add_outwards(&g[data[3*i+1]], data[3*i+2]);
}