-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.h
2641 lines (2526 loc) · 77.3 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
/* Copyright (C) 2015-2016 Felix Salfelder
* Author: Felix Salfelder
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*------------------------------------------------------------------
* graph abstraction with low-level access
*/
#ifndef GALA_GRAPH_H
#define GALA_GRAPH_H
#include "platform.h"
#include "trace.h"
#include <algorithm>
#include <vector>
#include <map>
#include <forward_list>
#ifdef HAVE_STX_BTREE_SET_H
#include <stx/btree_set.h>
#endif
#include <type_traits>
#include <boost/serialization/static_warning.hpp>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include "sfinae.h"
#include <boost/mpl/bool.hpp>
/*--------------------------------------------------------------------------*/
#ifndef GALA_DEFAULT_OVERRIDE
# include <set>
# include <vector>
# define GALA_DEFAULT_SET std::set
# define GALA_DEFAULT_VECTOR std::vector
#endif
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace gala{
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
struct vertex_ptr_tag {};
/*--------------------------------------------------------------------------*/
#define galaPARMS template<class T, typename... > class ECT, \
template<class T, typename... > class VCT, \
class VDP, \
template<class GG> class CFG
#define VCTtemplate template< galaPARMS >
#define SGARGS ECT, VCT, VDP, CFG
/*--------------------------------------------------------------------------*/
#define VCTtemplateP template< \
template<class T, typename... > class ECT, \
template<class T, typename... > class VCT \
template<class G> class CFG \
>
#define SGARGSP ECT, VCT, CFG
/*--------------------------------------------------------------------------*/
#define STPARMS template<class T, typename... > class ECT, \
template<class T, typename... > class VCT, \
class VDP
#define STtemplate template< STPARMS >
#define STARGS ECT, VCT, VDP
/*--------------------------------------------------------------------------*/
struct directed_tag { };
struct undirected_tag { };
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<typename T>
struct tovoid { typedef void type; };
/*--------------------------------------------------------------------------*/
namespace bits{ //
/*--------------------------------------------------------------------------*/
template<class C, class E>
void my_push_back(C& c, E e)
{ untested();
c.insert(e);
}
template<class E>
void my_push_back(std::vector<unsigned>& c, E e)
{ untested();
// it's better not there yet (up to you).
c.push_back(e);
}
template<class E>
void my_push_back(std::vector<short unsigned>& c, E e)
{ untested();
c.push_back(e);
}
/*--------------------------------------------------------------------------*/
// TODO: move to bits header
template<class S, class X=void>
struct container_helper {
template<class C, class E>
static bool exists(C& c, E e) { itested();
return c.find(e) != c.end();
}
template<class C, class E>
static void add(C& c, E e) {
bool done=c.insert(e).second;
assert(done); (void)done;
}
template<class C, class E>
static bool insert(C& c, E e) {
bool done=c.insert(e).second;
return done;
}
template<class C, class E>
static bool remove(C& c, E e) { untested();
return c.erase(e);
}
};
// TODO:: exploit forced ordering?
template<class S>
struct container_helper<S, typename sfinae::is_vector<S>::type > {
template<class C, class E>
static bool exists(C const& c, E e) {
for(auto i : c){
if(e==i){ itested();
return true;
}else{ itested();
}
}
return false;
}
template<class C, class E>
static void add(C& c, E e){
// not really, just TDLIB
// assert(std::find(c.begin(), c.end(), e)==c.end());
c.push_back(e);
}
template<class C, class E>
static bool insert(C& c, E e){
if(std::find(c.begin(), c.end(), e)==c.end()){
c.push_back(e);
return true;
}else{
return false;
}
}
template<class C, class E>
static bool remove(C& c, E e) {
BOOST_STATIC_WARNING(false); // inefficient.
auto what=std::find(c.begin(), c.end(), e);
if(what==c.end()){
return false;
}else{
c.erase(what);
return true;
}
}
};
template<class S>
struct container_helper<S, typename sfinae::is_hash<S>::type > {
template<class C, class E>
static bool exists(C&, E)
{ incomplete();
return false;
}
};
/*--------------------------------------------------------------------------*/
template<class C, class E>
bool edge_insert(C& c, E e)
{
return container_helper<C>::insert(c, e);
}
#if 0
template<class E, class C>
bool edge_insert(C& c, E e, typename std::enable_if< sfinae::is_vector<C>::value, bool>::type=false)
{ untested();
if(std::find(c.begin(), c.end(), e)==c.end()){ untested();
c.push_back(e);
return true;
}else{ untested();
return false;
}
}
#endif
/*--------------------------------------------------------------------------*/
template<class C, class E>
void edge_add(C& c, E e)
{
container_helper<C>::add(c, e);
}
#if 0
template<class E, class C>
bool edge_add(C& c, E e, typename std::enable_if< sfinae::is_vector<C>::value, bool>::type=false)
{ untested();
c.push_back(e);
return true;
}
#endif
/*--------------------------------------------------------------------------*/
// stub. maybe merge into some storage helper.
template<bool s,
template<class T, typename... > class ECT,
class X=void>
struct order_helper {
template<class V>
static void do_it(V&){
// nop
}
};
template<template<class T, typename... > class ECT>
struct order_helper<true, ECT,
typename tovoid < typename std::enable_if<
sfinae::is_seq_tpl<ECT>::value
>::type >::type > { //
template<class V>
static void do_it(V& v){
for( auto& i : v){
std::sort(i.begin(), i.end());
}
}
};
/*--------------------------------------------------------------------------*/
template<template<class T, typename... > class S, class X=void>
struct outedge_helper {
template<class C, class V>
static void erase(C& s, V w){ untested();
s.erase(w);
}
template<class C, class E>
bool exists(C& c, E e)
{ itested();
return c.find(e) != c.end();
}
};
template<template<class T, typename... > class ECT>
struct outedge_helper<ECT, typename sfinae::is_vec_tpl<ECT>::type > {
//this is inefficient. probably not what you need.
//just for backwards compatibility
template<class C, class V>
static void erase(C& s, V w){
for(auto& i: s){
if(i==w){
i=s.back();
s.pop_back();
}
}
}
template<class C, class E>
bool exists(C&, E)
{ incomplete();
return false;
}
};
/*--------------------------------------------------------------------------*/
template<class VDP>
struct vertex_helper{ //
template<class T, class V, class VC>
static bool contains(T& v, V& w, VC*)
{ itested();
return container_helper<VC>::exists(v, w);
}
template<class T, class V, class VC>
static void add(T& v, V& w, VC*)
{
// BUG? insert vs add.
return container_helper<VC>::add(v, w);
}
template<class T, class V, class VC>
static void insert(T& v, V& w, VC*)
{
edge_insert(v, w);
}
template<class T, class V, class VC>
static bool remove(T& v, V& w, VC*)
{ itested();
return container_helper<VC>::remove(v, w);
}
template<class VL>
static bool is_valid(VDP const& v, VL const& _v)
{
return v<_v.size();
}
template<class E, class S>
static void rebase(E& e, S const& s, intptr_t /*delta*/)
{itested();
e=s;
}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<>
struct vertex_helper<vertex_ptr_tag>{
template<class T, class V, class VC>
static bool contains(T& v, V&, VC* wp)
{ itested();
incomplete();
// return contains(v.n, wp);
return container_helper<VC>::exists(v.n, wp);
}
template<class T, class V, class VC>
static void insert(T& v, V&, VC* wp)
{ untested();
v.n.insert(wp);
}
template<class T, class V, class VC>
static void add(T& v, V&, VC* wp)
{ untested();
container_helper<VC>::add(v.n, wp);
}
template<class T, class V, class VC>
static bool remove(T&, V&, VC*)
{ untested();
incomplete();
return false;
}
template<class VL>
static bool is_valid(typename VL::value_type const* v, VL const& _v)
{ itested();
intptr_t B= intptr_t(v) - intptr_t(&*_v.begin());
if(!std::is_same<std::random_access_iterator_tag,
typename VL::iterator::iterator_category > :: value) { untested();
return true; // don't know...
}else if(B<0){untested();
return false;
}else if(B>=intptr_t(&*_v.end())) {untested();
return false;
}else if(B%sizeof(typename VL::value_type)) { untested();
return false;
}else{ itested();
return true;
}
}
template<class T, class S>
static void rebase(T& tgt, S const& src, intptr_t delta)
{ untested();
tgt.clear();
for(auto i : src) { untested();
#ifdef NOHINT_REBASE
tgt.insert( typename T::value_type((intptr_t(i)+delta)));
#else // faster. check applicability
auto e=tgt.end();
tgt.insert(e, (typename T::value_type)((intptr_t(i)+delta)));
#endif
}
}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class c_iter_tag, class VDP, bool is_directed, bool is_multiedge>
struct iter_helper{ //
template<class iter, class VL>
static size_t fill_pos(iter first, iter last, VL& _v, bool dir=false,
bool dups=true)
{
unsigned all=0;
if(is_multiedge){ untested();
if(is_directed){ untested();
}else{ untested();
if(dups){ untested();
}else{ untested();
}
}
}else{
if(is_directed){ untested();
}else{
}
}
auto nv=_v.size(); (void)nv;
trace1("fill_pos", nv);
assert(!dir); (void) dir;
size_t c=0;
for(;first!=last; ++first){
++all;
unsigned v=(*first).first;
unsigned w=(*first).second;
trace2("fill_pos", v, w);
assert(v<nv);
assert(w<nv);
// FIXME: use add_edge...
bool doit;
if(is_multiedge){ untested();
// use template arg!
doit=true;
}else if(!dups){
doit=true;
}else if(vertex_helper<VDP>::contains(_v[v], w, &_v[w])){
doit=false;
}else{
doit=true;
}
if(doit){
vertex_helper<VDP>::add(_v[v], w, &_v[w]);
vertex_helper<VDP>::add(_v[w], v, &_v[v]);
++c;
}else{
}
}
return c;
}
};
/*--------------------------------------------------------------------------*/
inline size_t source(std::pair<size_t, size_t> const& p){
return p.first;
}
inline size_t target(std::pair<size_t, size_t> const& p){
return p.second;
}
/*--------------------------------------------------------------------------*/
template<class c_iter_tag, class VDP, bool is_multiedge>
struct iter_helper<c_iter_tag, VDP, /*directed*/ true, is_multiedge> {
template<class iter, class VL>
static size_t fill_pos(iter first, iter last, VL& _v, bool dir=false, bool dups=true)
{
if(dups){
}else{
}
assert(dir); (void) dir;
auto nv = _v.size();
size_t c=0;
for(;first!=last; ++first){
unsigned v=source(*first);
unsigned w=target(*first);
assert(v<nv);
assert(w<nv);
// FIXME: use add_edge...
vertex_helper<VDP>::insert(_v[v], w, &_v[w]);
++c;
}
return c;
}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class VDP, bool is_directed, bool is_multiedge>
struct iter_helper<std::bidirectional_iterator_tag, VDP, is_directed, is_multiedge>{ //
template<class iter, class VL>
static size_t fill_pos(iter first, iter last, /* size_t nv, */ VL& _v, bool dir, bool dup=false)
{ untested();
if(dup){incomplete();
}
if(dir){ incomplete();
}
typedef typename VL::value_type v_t;
size_t nv=_v.size();
if(nv){ untested();
}else{ untested();
}
GALA_DEFAULT_VECTOR<v_t*> vmap(_v.size());
size_t index=0;
for(auto& i : _v){ untested();
vmap[index++] = &i;
}
assert(index==_v.size());
size_t c=0;
for(;first!=last; ++first){ untested();
unsigned v=first->first;
unsigned w=first->second;
assert(v<nv);
assert(w<nv);
v_t* _V = vmap[v];
v_t* _W = vmap[w];
v_t& V = *_V;
v_t& W = *_W;
vertex_helper<VDP>::insert(V, W, _W);
if(is_directed){ untested();
// hmm.
}else{ untested();
vertex_helper<VDP>::insert(W, V, _V);
}
++c;
}
return c;
}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// select vertex type depending on 3rd template parameter
template<template<class T, typename... > class ECT, class VDP>
struct vertex_selector{ //
typedef VDP type;
typedef const VDP const_type;
typedef ECT<type> stype;
typedef VDP vertices_size_type;
typedef size_t edges_size_type; // TODO.
typedef unsigned vertex_index_type; // INCOMPLETE
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template< template<class T, typename... > class ECT >
struct vertex_selector<ECT, vertex_ptr_tag>{ //
typedef struct vertex_{ //
ECT<vertex_*> n;
size_t size() const { return n.size(); }
} vertex_;
typedef vertex_* type;
typedef vertex_ const* const_type;
typedef vertex_ stype; // storage type
typedef uintptr_t vertices_size_type;
typedef size_t edges_size_type; // TODO.
typedef unsigned vertex_index_type; // INCOMPLETE
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// part of storage? not sure yet.
STtemplate
struct rewire_helper{
template<class A, class B>
static void rewire_nodes(A& new_vl, B offset)
{
// specialize... (will be expensive)
assert( sfinae::is_vec_tpl< VCT >::value );
trace1("rewire", offset);
for(auto& node : new_vl){
// node.n.insert(NULL); ??
auto& N = node.n;
for(auto& neigh : N) {
*(const_cast<typename A::value_type**>(&neigh)) =
(typename A::value_type*) ( uintptr_t(neigh) + uintptr_t(offset));
}
}
}
};
/*--------------------------------------------------------------------------*/
STtemplate
struct storage_base{ //
typedef vertex_selector<ECT,VDP> vs;
typedef typename vs::type vertex_type;
typedef typename vs::stype vertex_;
typedef VCT<vertex_> container_type;
typedef typename vs::vertex_index_type vertex_index_type;
static vertex_index_type num_edges(vertex_index_type n, container_type const&)
{ untested();
return n;
}
};
/*--------------------------------------------------------------------------*/
template<class vertex_type>
struct edge_type_ : std::pair<vertex_type, vertex_type>{
typedef std::pair<vertex_type, vertex_type> base;
template<class vt, class vt2>
edge_type_(vt a, vt2 b) : base(a,b) {
}
edge_type_() : base() {
}
};
/*--------------------------------------------------------------------------*/
STtemplate
struct storage : storage_base<STARGS>{ //
using typename storage_base<STARGS>::vs;
using typename storage_base<STARGS>::vertex_;
using typename storage_base<STARGS>::vertex_type;
using typename storage_base<STARGS>::container_type;
typedef typename vs::vertex_index_type vertex_index_type;
typedef typename vs::const_type const_vertex_type;
typedef edge_type_<vertex_type> edge_type;
typedef ECT<vertex_type> edge_container_type; // -> storage base?
typedef VCT<vertex_> VL;
static bool need_rewire() { untested();
return false;
}
static vertex_type new_node(container_type& _v) {
vertex_ n;
auto s=_v.size();
_v.push_back(n);
return s;
}
static VDP degree(const VDP v, container_type const& _v) {
assert(v<_v.size());
return _v[v].size();
}
static VDP degree(container_type const& _v) { untested();
vertex_index_type ret=0;
for(auto const& e : _v){ untested();
vertex_index_type d = e.size();
if(d>ret) ret=d;
}
return ret;
}
static void clear_vertex(const VDP v, container_type& _v) {
_v[v].clear();
}
static edge_container_type& out_edges(vertex_type& v, container_type& _v) {
return _v[v];
}
static edge_container_type& out_edges(const_vertex_type& v, container_type& _v) {
return _v[v];
}
static const edge_container_type& out_edges(const_vertex_type& v, const container_type& _v) { itested();
return _v[v];
}
static void remove_edge_single(vertex_index_type v, vertex_index_type w,
container_type& _v)
{
outedge_helper<ECT>::erase(out_edges(v, _v), w);
}
static void add_pos_edge(vertex_index_type v, vertex_index_type w,
container_type& _v)
{ untested();
assert(v<w); // for now
_v[v].insert(w);
_v[w].insert(v);
}
template<class E, class S>
static void rebase(E& e, S const& s, intptr_t /*delta*/)
{ incomplete();
e = s;
}
}; // storage<VDP>
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<template<class T, typename... > class ECT,
template<class T, typename... > class VCT>
struct storage<ECT, VCT, vertex_ptr_tag> : public storage_base<ECT, VCT, vertex_ptr_tag>{ //
typedef vertex_ptr_tag VDP;
typedef typename bits::vertex_selector<ECT,VDP>::stype vertex_;
typedef typename bits::vertex_selector<ECT,VDP>::type vertex_type;
typedef typename bits::vertex_selector<ECT,VDP>::vertex_index_type vertex_index_type;
typedef typename bits::vertex_selector<ECT,VDP>::const_type const_vertex_type;
typedef edge_type_<vertex_type> edge_type;
typedef VCT<vertex_> container_type;
typedef ECT<vertex_type> edge_container_type;
typedef VCT<vertex_> VL;
static bool need_rewire()
{ untested();
return true;
}
static vertex_type new_node(container_type& _v)
{ itested();
unsigned int s = _v.size();
auto old_begin=_v.begin();
_v.resize(s+1);
vertex_type offset = (vertex_type) (uintptr_t(&*_v.begin()) - uintptr_t(&*old_begin));
if(!offset){
}else{
// std::cerr << "add rewire " << _v.size() << "\n";
rewire_helper<STARGS>::rewire_nodes(_v, offset);
}
return &_v.back();
}
static vertex_index_type degree(const vertex_type& v, container_type const&){ // ??
return v->n.size();
}
static vertex_index_type degree(const_vertex_type& v, container_type const&){
return v->n.size();
}
static vertex_index_type degree(container_type const& _v)
{ untested();
vertex_index_type ret=0;
typename container_type::const_iterator v = _v.begin();
typename container_type::const_iterator e = _v.end();
for(; v!=e ; ++v){ untested();
vertex_index_type d = v->n.size();
if(d>ret) ret=d;
}
return ret;
}
static void clear_vertex(const vertex_type v, container_type&)
{ untested();
v->n.clear();
}
static edge_container_type& out_edges(vertex_& v, container_type&)
{
return v.n;
}
static const edge_container_type& out_edges(vertex_& v, const container_type&)
{ untested();
return v.n;
}
static edge_container_type& out_edges(vertex_type& v, container_type&)
{
return v->n;
}
static const edge_container_type& out_edges(const_vertex_type& v, const container_type&)
{ untested();
return v->n;
}
public:
static void remove_edge_single(vertex_type v, vertex_type w,
container_type& _v)
{ untested();
out_edges(v, _v).erase(w);
}
static void add_pos_edge(vertex_index_type v, vertex_index_type w,
container_type& _v)
{ untested();
assert(v<w); // for now
_v[v].n.insert(&_v[w]);
_v[w].n.insert(&_v[v]);
}
// copy over outedges.
template<class T, class S>
static void rebase(T& tgt, S const& src, intptr_t delta)
{ incomplete();
tgt.clear();
auto e=tgt.end();
for(auto i : src) { untested();
tgt.insert( (vertex_*) (intptr_t(i)+delta), e);
}
}
}; //storage<vertex_ptr_tag>
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<STPARMS, bool is_directed, bool is_simple, bool is_symmetric>
struct edge_helper : public storage<STARGS> {
};
/*--------------------------------------------------------------------------*/
template<STPARMS, bool is_symmetric>
struct edge_helper<STARGS, false, false, is_symmetric> : public storage<STARGS>{
// undirected implementation.
using typename storage<STARGS>::edge_type;
using typename storage<STARGS>::vertex_type;
using storage<STARGS>::out_edges;
// to storage base?!
template<class N, class VC>
static std::pair<edge_type, bool> add_edge(vertex_type a, vertex_type b,
N& num_edges, VC& vc)
{ untested();
// trace0("undiredted add_edge");
vertex_type* A=&a;
vertex_type* B=&b;
// don't attempt to avoid dups
edge_add(out_edges(*B, vc), *A);
edge_add(out_edges(*A, vc), *B);
++num_edges;
return std::make_pair(edge_type(*A, *B), true);
}
typedef typename storage<STARGS>::container_type vertex_container_type;
template<class E>
static void add_reverse_edges(vertex_container_type& /*_v*/, E& )
{ unreachable();
}
};
/*--------------------------------------------------------------------------*/
template<STPARMS, bool is_symmetric>
struct edge_helper<STARGS, false, true, is_symmetric> : public storage<STARGS>{
// undirected implementation.
using typename storage<STARGS>::edge_type;
using typename storage<STARGS>::vertex_type;
using storage<STARGS>::out_edges;
// to storage base?!
template<class N, class VC>
static std::pair<edge_type, bool> add_edge(vertex_type a, vertex_type b,
N& num_edges, VC& vc)
{
vertex_type* A=&a;
vertex_type* B=&b;
size_t s = out_edges(*A, vc).size();
edge_insert(out_edges(*A, vc), (*B));
// since the graph is undirected,
// if (a, b) already exists, then (b, a) does too
bool added=false;
if(s == out_edges(*A, vc).size()){
}else{
edge_add(out_edges(*B, vc), *A);
++num_edges;
added = true;
}
return std::make_pair(edge_type(*A, *B), added);
}
typedef typename storage<STARGS>::container_type vertex_container_type;
template<class E>
static void add_reverse_edges(vertex_container_type& /*_v*/, E& )
{ unreachable();
}
};
/*--------------------------------------------------------------------------*/
template<STPARMS, bool is_simple, bool is_symmetric>
struct edge_helper<STARGS, true, is_simple, is_symmetric>
// directed implementation.
: public storage<STARGS>{ //
typedef typename storage<STARGS>::container_type vertex_container_type;
using typename storage<STARGS>::edge_type;
using typename storage<STARGS>::vertex_type;
using storage<STARGS>::out_edges;
template<class N, class VC>
static std::pair<edge_type, bool> add_edge(vertex_type a, vertex_type b,
N& num_edges, VC& vc)
{
vertex_type* A=&a;
vertex_type* B=&b;
size_t s = out_edges(*A, vc).size();
bool added=false;
// TODO: use more overrides
if(is_symmetric){
size_t s = out_edges(*B, vc).size();
if(is_simple){
edge_insert(out_edges(*B, vc), *A);
}else{ untested();
edge_add(out_edges(*B, vc), *A);
}
if(s == out_edges(*B, vc).size()){
}else{
++num_edges; // BUG, multiplier?
added = true;
}
}else{
}
{
if(is_simple){
edge_insert(out_edges(*A, vc), *B);
}else{
edge_add(out_edges(*A, vc), *B);
}
if(s == out_edges(*A, vc).size()){
}else{
++num_edges; // BUG, multiplier?
added = true;
}
}
return std::make_pair(edge_type(*A, *B), added);
}
};
/*--------------------------------------------------------------------------*/
template<STPARMS, class X=void>
struct reverse_helper : public storage<STARGS> { //
// typedef typename storage<STARGS>::container_type vertex_container_type;
// using typename storage<STARGS>::edge_type;
// using typename storage<STARGS>::vertex_type;
//typedef foo incomplete;
// template<class E>
// static void make_symmetric(vertex_container_type& , E& , bool /*oriented*/)
// { untested();
// // base case. hmm better don't use yet.
//
// assert(false);
// incomplete();
// }
};
/*--------------------------------------------------------------------------*/
#if 0 // see below
template< template<class T, typename... > class ECT,
template<class T, typename... > class VCT>
struct reverse_helper<ECT, VCT, vertex_ptr_tag,
typename sfinae::is_set_tpl<ECT>::type >
: public storage<ECT, VCT, vertex_ptr_tag> { //
};
#endif
/// hmm better constexpr flags for
// - directed
// - oriented
// - loopless?
template< template<class T, typename... > class ECT,
template<class T, typename... > class VCT, class VDP>
struct reverse_helper<ECT, VCT, VDP, typename tovoid < typename sfinae::is_set_tpl<ECT>::type > :: type >
: public storage<ECT, VCT, VDP
// typename std::enable_if< std::is_unsigned< VDP >::value, VDP >::type
> { //
typedef typename storage<ECT,VCT,VDP>::container_type vertex_container_type;
using typename storage<ECT,VCT,VDP>::edge_type;
using typename storage<ECT,VCT,VDP>::vertex_type;
// typename sfinae::is_set_tpl<ECT>::type BLA;
typedef typename sfinae::is_set< ECT<sfinae::any> >::type hmm;
template<class E>
static void make_symmetric(vertex_container_type& _v, E& e, bool /*oriented*/)
{ itested();
unsigned ii=0;
for(auto & i : _v){ itested();
for(auto & j : i){ itested();
bool ins=_v[j].insert(ii).second;
// trace2("",e,ins);
e+=ins;
}
++ii;
}
}
};
/*--------------------------------------------------------------------------*/
template<STPARMS>
struct reverse_helper<ECT, VCT, VDP,
// typename std::enable_if< std::is_unsigned< VDP >::value, ECT<VDP> >::type,
typename sfinae::is_seq_tpl< ECT >::type >
: public storage<ECT, VCT, VDP > { //
typedef typename sfinae::is_seq< ECT<sfinae::any> >::type hmm;
typedef typename std::enable_if< std::is_unsigned< VDP >::value, VDP>::type hmm2;
typedef typename storage<ECT,VCT,VDP>::container_type vertex_container_type;
using typename storage<ECT,VCT,VDP>::edge_type;
using typename storage<ECT,VCT,VDP>::vertex_type;
template<class E>
static void make_symmetric(vertex_container_type& _v, E& e, bool oriented)
{
// oriented: "there is only one edge {u,v}"
trace3("make_symmetric", oriented, e, _v.size());
#ifndef NDEBUG
auto ebefore=e;
#endif
std::vector<vertex_type> howmany(_v.size());
vertex_type i=0;
unsigned checksum=0;
for(auto& vertex : _v){
howmany[i++] = vertex.size();
checksum+= vertex.size();
}
trace4("make_symmetric", oriented, e, checksum, _v.size());
assert(checksum==e || !oriented);
// ssg only. for now.
if(oriented){ untested();
vertex_type vpos=0;
for(unsigned j=0; j<_v.size(); ++j){ itested();
auto K = _v[j].begin();
for(unsigned i=0; i<howmany[j]; ++i ){ itested();
my_push_back(_v[*K],(j));
++K;
++e;
}
++vpos;
}
}else{
// BUG: this is inefficient if
vertex_type vpos=0;
for(unsigned j=0; j<_v.size(); ++j){
auto K = _v[j].begin();
for(unsigned i=0; i<howmany[j]; ++i ){
e += edge_insert(_v[*K], j);
++K;
}
++vpos;
}
}
#ifndef NDEBUG
trace3("make_symm", oriented, ebefore, e);
if(oriented){ untested();
assert(ebefore*2==e);
}else{
}
#endif
}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<template<class T, typename... > class ECT,
template<class T, typename... > class VCT >
struct reverse_helper<ECT, VCT, vertex_ptr_tag,
typename sfinae::is_set_tpl< ECT >::type
>
: public storage<ECT, VCT, vertex_ptr_tag > { //
typedef typename sfinae::is_set< ECT<sfinae::any> >::type hmm;
typedef typename storage<ECT, VCT, vertex_ptr_tag>::container_type vertex_container_type;
using typename storage<ECT, VCT, vertex_ptr_tag>::edge_type;
using typename storage<ECT, VCT, vertex_ptr_tag>::vertex_type;
template<class E>
static void make_symmetric(vertex_container_type& _v, E& e, bool /*oriented*/)
{ untested();
// concept check?
auto B=_v[0];
auto I=B.n.begin();
for(auto & i : _v){ untested();
for(auto & j : i.n){ itested();
bool ins=j->n.insert(&i).second;
e+=ins;
}
}
}
};
/*--------------------------------------------------------------------------*/
// vector outedge set ...?
template<template<class T, typename... > class ECT,
template<class T, typename... > class VCT>
struct reverse_helper<ECT, VCT, vertex_ptr_tag,
typename sfinae::is_seq< ECT<sfinae::any> >::type
> : public storage<ECT, VCT, vertex_ptr_tag > { //
typedef vertex_ptr_tag VDP;
typedef typename storage<ECT,VCT,VDP>::container_type vertex_container_type;
using typename storage<ECT,VCT,VDP>::edge_type;
using typename storage<ECT,VCT,VDP>::vertex_type;
template<class E>
static void make_symmetric(vertex_container_type& /*_v*/, E& , bool /*oriented*/)