-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgl.hxx
executable file
·1692 lines (1455 loc) · 66.8 KB
/
sgl.hxx
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 SGL_HH
#define SGL_HH
#include <functional>
#include <iostream>
#include <memory>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <random>
#include <sstream>
namespace sgl
{
//////////////////////////////////////////////////////////////////////////////
//
// FORWARD DECLARATIONS
//
class uuid;
template <typename DATA_TYPE>
class Vertex;
class VertexPrinter;
template <typename DATA_TYPE>
class DataStructureBase;
template <typename DATA_TYPE>
class AdjacencyList;
template <typename DATA_TYPE>
class AdjacencyMatrix;
template <typename DATA_TYPE, template <typename> typename DATA_STRUCTURE>
class Graph;
template <typename DATA_STRUCTURE>
class BFS;
template <typename DATA_STRUCTURE>
class DFS;
//
// END OF FORWARD DECLARATIONS
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// TYPE DEFINITIONS
//
void version()
{
std::cout << R"(
│ ╔═╗╔═╗╦ Simple Graph Library │
│ ╚═╗║ ╦║ Apache License 2.0 │
│ ╚═╝╚═╝╩═╝ version 0.3.1 │
)" << std::endl;
}
enum VertexFormat
{
SHORTEST,
SHORT,
LONG
};
enum VisitPolicy
{
RELATED,
ALL
};
class uuid
{
public:
uuid()
{
static std::random_device rd;
static std::mt19937_64 gen(1);
static std::uniform_int_distribution<> dis(0, 15);
std::stringstream ss;
for (int i = 0; i < 10; ++i)
{
int rand_val = dis(gen);
char hex_digit = rand_val < 10 ? '0' + rand_val : 'a' + (rand_val - 10);
ss << hex_digit;
}
m_uuid = ss.str();
}
operator std::string() const { return m_uuid; }
auto operator<=>(const uuid &other) const
{
return m_uuid <=> other.m_uuid;
}
auto operator==(const uuid &other) const
{
return m_uuid == other.m_uuid;
}
friend std::ostream &operator<<(std::ostream &os, const uuid &uuid)
{
return os << uuid.m_uuid;
}
private:
std::string m_uuid;
};
template <typename DATA_TYPE>
class Vertex
{
using VERTEX_TYPE = Vertex<DATA_TYPE>;
friend class VertexPrinter;
friend class DataStructureBase<DATA_TYPE>;
friend class AdjacencyList<DATA_TYPE>;
friend class AdjacencyMatrix<DATA_TYPE>;
public:
Vertex(DATA_TYPE &&data) : m_uuid{}, m_data{std::move(data)}, m_data_structure{} {}
Vertex(DATA_TYPE &&data, std::shared_ptr<DataStructureBase<DATA_TYPE>> m_data_structure)
: m_uuid{}, m_data{std::move(data)}, m_data_structure{m_data_structure} {}
Vertex(const Vertex &other) = delete;
Vertex(Vertex &&other) : m_uuid{std::move(other.m_uuid)}, m_data{std::move(other.m_data)}, m_data_structure{std::move(other.m_data_structure)} {}
~Vertex() = default;
const uuid &get_id() const { return m_uuid; }
const DATA_TYPE &data() const { return m_data; }
DATA_TYPE &data() { return m_data; }
typename DataStructureBase<DATA_TYPE>::const_iterator begin() const { return m_data_structure->cbegin(m_uuid); }
typename DataStructureBase<DATA_TYPE>::const_iterator end() const { return m_data_structure->cend(m_uuid); }
typename DataStructureBase<DATA_TYPE>::iterator begin() { return m_data_structure->begin(m_uuid); }
typename DataStructureBase<DATA_TYPE>::iterator end() { return m_data_structure->end(m_uuid); }
size_t size() const { return m_data_structure->size(m_uuid); }
void remove()
{
if (m_data_structure == nullptr)
{
throw std::runtime_error("[void sgl::Vertex::remove()] Vertex is not part of a graph");
}
m_data_structure->remove_vertex(m_uuid);
m_data_structure = nullptr;
}
void remove_edge(const uuid &id)
{
if (m_data_structure == nullptr)
throw std::runtime_error("[void sgl::Vertex::remove_edge(const uuid &id)] Vertex is not part of a graph");
m_data_structure->remove_edge(m_uuid, id);
}
friend std::ostream &operator<<(std::ostream &os, const VERTEX_TYPE &vertex)
{
std::string id = static_cast<std::string>(vertex.m_uuid);
return os << "[ " << vertex.m_data << " ]";
}
private:
void add_data_structure(const std::shared_ptr<DataStructureBase<DATA_TYPE>> &data_structure)
{
m_data_structure = data_structure;
}
const uuid m_uuid;
DATA_TYPE m_data;
std::shared_ptr<DataStructureBase<DATA_TYPE>> m_data_structure;
};
class VertexPrinter
{
public:
VertexPrinter(std::ostream &os, VertexFormat format)
: m_os{os}, m_format{format} {}
template <typename DATA_TYPE>
std::ostream &operator<<(const Vertex<DATA_TYPE> &vertex) const
{
if (m_format == VertexFormat::SHORTEST)
{
m_os << "[ " << vertex.m_data << " ]";
}
else if (m_format == VertexFormat::SHORT)
{
std::string id = static_cast<std::string>(vertex.m_uuid);
m_os << "[ id: {" << id.substr(0, 2) << "..."
<< id.substr(id.size() - 2, 2) << "}, ";
m_os << "data: {" << vertex.m_data;
m_os << "}, num adj: {" << vertex.size() << "} ]";
}
else if (m_format == VertexFormat::LONG)
{
m_os << "[ id: {" << vertex.m_uuid << "}, ";
m_os << "data: {" << vertex.m_data;
m_os << "}, neighbors: {";
for (auto it = vertex.begin(); it != vertex.end(); ++it)
{
if (it == --vertex.end())
{
m_os << *it;
}
else
{
m_os << *it << ", ";
}
}
m_os << "} ]";
}
return m_os;
}
template <typename DATA_TYPE, template <typename> typename DATA_STRUCTURE>
std::ostream &operator<<(const Graph<DATA_TYPE, DATA_STRUCTURE> &graph) const
{
if (m_format == VertexFormat::SHORTEST)
{
VertexPrinter{m_os, VertexFormat::SHORTEST} << *graph.m_data_structure;
}
else if (m_format == VertexFormat::SHORT)
{
VertexPrinter{m_os, VertexFormat::SHORT} << *graph.m_data_structure;
}
else if (m_format == VertexFormat::LONG)
{
VertexPrinter{m_os, VertexFormat::LONG} << *graph.m_data_structure;
}
return m_os;
}
template <typename DATA_TYPE>
std::ostream &operator<<(const AdjacencyList<DATA_TYPE> &adjacency_list) const
{
for (auto it = adjacency_list.cbegin(); it != adjacency_list.cend(); ++it)
{
if (m_format == VertexFormat::SHORTEST)
{
VertexPrinter{m_os, VertexFormat::SHORTEST} << *it;
}
else if (m_format == VertexFormat::SHORT)
{
VertexPrinter{m_os, VertexFormat::SHORT} << *it;
}
else if (m_format == VertexFormat::LONG)
{
VertexPrinter{m_os, VertexFormat::LONG} << *it;
}
if (it != --adjacency_list.cend())
{
m_os << std::endl;
}
}
return m_os;
}
template <typename DATA_TYPE>
std::ostream &operator<<(const AdjacencyMatrix<DATA_TYPE> &adjacency_matrix) const
{
for (auto it = adjacency_matrix.cbegin(); it != adjacency_matrix.cend(); ++it)
{
if (m_format == VertexFormat::SHORTEST)
{
VertexPrinter{m_os, VertexFormat::SHORTEST} << *it;
}
else if (m_format == VertexFormat::SHORT)
{
VertexPrinter{m_os, VertexFormat::SHORT} << *it;
}
else if (m_format == VertexFormat::LONG)
{
VertexPrinter{m_os, VertexFormat::LONG} << *it;
}
if (it != --adjacency_matrix.cend())
{
m_os << std::endl;
}
}
return m_os;
}
private:
std::ostream &m_os;
const VertexFormat m_format;
};
VertexPrinter operator<<(std::ostream &os, VertexFormat format)
{
return VertexPrinter{os, format};
}
template <typename DATA_TYPE>
class DataStructureBase
{
using VERTEX_TYPE = Vertex<DATA_TYPE>;
friend class Vertex<DATA_TYPE>;
public:
DataStructureBase() = default;
virtual ~DataStructureBase() = default;
virtual const uuid &add_vertex(VERTEX_TYPE &&vertex) = 0;
virtual const uuid &add_vertex(DATA_TYPE &&data) = 0;
virtual void add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0) = 0;
virtual void remove_vertex(const uuid &vertex) = 0;
virtual void remove_edge(const uuid &vertex1, const uuid &vertex2) = 0;
virtual const VERTEX_TYPE &vertex(const uuid &id) const = 0;
virtual VERTEX_TYPE &vertex(const uuid &id) = 0;
virtual const float &weight(const uuid &vertex1, const uuid &vertex2) const = 0;
virtual float &weight(const uuid &vertex1, const uuid &vertex2) = 0;
virtual size_t size() const = 0;
virtual size_t size(const uuid &id) const = 0;
virtual void clear() = 0;
virtual std::ostream &print(std::ostream &os = std::cout) const = 0;
protected:
class const_iterator_impl
{
public:
const_iterator_impl() = default;
virtual ~const_iterator_impl() = default;
virtual bool operator==(const const_iterator_impl *other) const = 0;
virtual bool operator!=(const const_iterator_impl *other) const = 0;
virtual const_iterator_impl *operator++() = 0;
virtual const_iterator_impl *operator--() = 0;
virtual const VERTEX_TYPE &operator*() const = 0;
virtual const VERTEX_TYPE *operator->() const = 0;
};
class iterator_impl
{
public:
iterator_impl() = default;
virtual ~iterator_impl() = default;
virtual bool operator==(const iterator_impl *other) const = 0;
virtual bool operator!=(const iterator_impl *other) const = 0;
virtual iterator_impl *operator++() = 0;
virtual iterator_impl *operator--() = 0;
virtual VERTEX_TYPE &operator*() const = 0;
virtual VERTEX_TYPE *operator->() const = 0;
};
public:
class const_iterator
{
public:
const_iterator(const std::shared_ptr<const_iterator_impl> &ptr) : m_ptr{ptr} {}
~const_iterator() = default;
bool operator==(const const_iterator &other) const { return *m_ptr == other.m_ptr.get(); }
bool operator!=(const const_iterator &other) const { return *m_ptr != other.m_ptr.get(); }
const_iterator &operator++()
{
++(*m_ptr);
return *this;
}
const_iterator &operator--()
{
--(*m_ptr);
return *this;
}
const VERTEX_TYPE &operator*() const { return **m_ptr; }
const VERTEX_TYPE *operator->() const { return (*m_ptr).operator->(); }
private:
std::shared_ptr<const_iterator_impl> m_ptr;
};
class iterator
{
public:
iterator(const std::shared_ptr<iterator_impl> &ptr) : m_ptr{ptr} {}
~iterator() = default;
bool operator==(const iterator &other) const { return *m_ptr == other.m_ptr.get(); }
bool operator!=(const iterator &other) const { return *m_ptr != other.m_ptr.get(); }
iterator &operator++()
{
++(*m_ptr);
return *this;
}
iterator &operator--()
{
--(*m_ptr);
return *this;
}
VERTEX_TYPE &operator*() const { return **m_ptr; }
VERTEX_TYPE *operator->() const { return (*m_ptr).operator->(); }
private:
std::shared_ptr<iterator_impl> m_ptr;
};
virtual const_iterator cbegin() const = 0;
virtual const_iterator cend() const = 0;
virtual iterator begin() = 0;
virtual iterator end() = 0;
virtual const_iterator cbegin(const uuid &id) const = 0;
virtual const_iterator cend(const uuid &id) const = 0;
virtual iterator begin(const uuid &id) = 0;
virtual iterator end(const uuid &id) = 0;
};
template <typename DATA_TYPE>
class AdjacencyList : public DataStructureBase<DATA_TYPE>, public std::enable_shared_from_this<AdjacencyList<DATA_TYPE>>
{
public:
~AdjacencyList() = default;
private:
using VERTEX_TYPE = Vertex<DATA_TYPE>;
using base_iterator_impl =
typename DataStructureBase<DATA_TYPE>::iterator_impl;
using base_iterator = typename DataStructureBase<DATA_TYPE>::iterator;
using base_const_iterator_impl =
typename DataStructureBase<DATA_TYPE>::const_iterator_impl;
using base_const_iterator =
typename DataStructureBase<DATA_TYPE>::const_iterator;
using const_neighbor_iterator =
typename std::vector<std::pair<uuid, float>>::const_iterator;
using neighbor_iterator =
typename std::vector<std::pair<uuid, float>>::iterator;
using const_vertex_iterator =
typename std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::vector<std::pair<uuid, float>>>>::const_iterator;
using vertex_iterator =
typename std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::vector<std::pair<uuid, float>>>>::iterator;
friend class BFS<AdjacencyList<DATA_TYPE>>;
friend class DFS<AdjacencyList<DATA_TYPE>>;
friend class Graph<DATA_TYPE, AdjacencyList>;
friend class VertexPrinter;
std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::vector<std::pair<uuid, float>>>> m_vertices;
AdjacencyList() = default;
const uuid &add_vertex(VERTEX_TYPE &&vertex) override
{
auto new_vertex = std::make_shared<VERTEX_TYPE>(std::forward<VERTEX_TYPE>(vertex));
new_vertex->add_data_structure(this->shared_from_this());
const uuid &id = new_vertex->get_id();
m_vertices.insert(std::make_pair(
id,
std::make_pair(std::move(new_vertex),
std::vector<std::pair<uuid, float>>())));
return id;
}
const uuid &add_vertex(DATA_TYPE &&data) override
{
VERTEX_TYPE vertex{std::forward<DATA_TYPE>(data)};
return add_vertex(std::move(vertex));
}
void add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0) override
{
if (vertex1 == vertex2)
{
throw std::invalid_argument("[void sgl::AdjacencyList::add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0)] vertex1 and vertex2 must be different");
}
// check if edge already exists
for (auto &neighbor : m_vertices.at(vertex1).second)
{
if (neighbor.first == vertex2)
{
throw std::invalid_argument("[void sgl::AdjacencyList::add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0)] Edge between vertex with id " + static_cast<std::string>(vertex1) + " and vertex with id " + static_cast<std::string>(vertex2) + " already exists");
}
}
try
{
m_vertices.at(vertex1).second.push_back(std::pair{vertex2, weight});
m_vertices.at(vertex2).second.push_back(std::pair{vertex1, weight});
}
catch (const std::out_of_range &e)
{
throw std::out_of_range{"[void sgl::AdjacencyList::add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0)] Vertex with id " + static_cast<std::string>(vertex1) + " or " + static_cast<std::string>(vertex2) + " not found"};
}
}
void remove_vertex(const uuid &vertex) override
{
if (m_vertices.find(vertex) == m_vertices.end())
{
throw std::out_of_range{"[void sgl::AdjacencyList::remove_vertex(const uuid &vertex) override] Vertex with id " + static_cast<std::string>(vertex) + " not found"};
}
auto &neighbors = m_vertices[vertex].second;
for (auto &neighbor : neighbors)
{
auto &neighbor_neighbors = m_vertices[neighbor.first].second;
neighbor_neighbors.erase(
std::remove_if(
neighbor_neighbors.begin(), neighbor_neighbors.end(),
[vertex](const std::pair<uuid, float> &neighbor_neighbor)
{
return neighbor_neighbor.first == vertex;
}),
neighbor_neighbors.end());
}
m_vertices.erase(vertex);
}
void remove_edge(const uuid &vertex1, const uuid &vertex2) override
{
if (vertex1 == vertex2)
{
throw std::invalid_argument("[void sgl::AdjacencyList::remove_edge(const uuid &vertex1, const uuid &vertex2)] vertex1 and vertex2 must be different");
}
if (m_vertices.find(vertex1) == m_vertices.end())
{
throw std::out_of_range{"[void sgl::AdjacencyList::remove_edge(const uuid &vertex1, const uuid &vertex2)] Vertex with id " + static_cast<std::string>(vertex1) + " not found"};
}
if (m_vertices.find(vertex2) == m_vertices.end())
{
throw std::out_of_range{"[void sgl::AdjacencyList::remove_edge(const uuid &vertex1, const uuid &vertex2)] Vertex with id " + static_cast<std::string>(vertex2) + " not found"};
}
auto &neighbors = m_vertices[vertex1].second;
neighbors.erase(
std::remove_if(neighbors.begin(), neighbors.end(),
[vertex2](const std::pair<uuid, float> &neighbor)
{
return neighbor.first == vertex2;
}),
neighbors.end());
auto &neighbors2 = m_vertices[vertex2].second;
neighbors2.erase(
std::remove_if(neighbors2.begin(), neighbors2.end(),
[vertex1](const std::pair<uuid, float> &neighbor)
{
return neighbor.first == vertex1;
}),
neighbors2.end());
}
template <typename FUNCTION, typename... ARGS>
void remove_if(FUNCTION &&function, ARGS &&...args)
{
if constexpr (!std::is_same_v<
bool, std::invoke_result_t<FUNCTION, const VERTEX_TYPE &,
ARGS...>>)
{
throw std::invalid_argument("[void sgl::AdjacencyList::remove_if(FUNCTION &&function, ARGS &&...args)] return type of function must be bool");
}
else
{
std::vector<uuid> to_remove;
for (auto it = cbegin(); it != cend(); ++it)
{
if (function(*it, std::forward<ARGS>(args)...))
{
to_remove.push_back(it->get_id());
}
}
for (auto &id : to_remove)
{
remove_vertex(id);
}
}
}
const VERTEX_TYPE &vertex(const uuid &id) const override
{
return *m_vertices.at(id).first;
}
VERTEX_TYPE &vertex(const uuid &id) override
{
return *m_vertices.at(id).first;
}
const float &weight(const uuid &vertex1, const uuid &vertex2) const override
{
if (vertex1 == vertex2)
{
throw std::invalid_argument("[const float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2) const] vertex1 and vertex2 must be different");
}
if (m_vertices.find(vertex1) == m_vertices.end())
{
throw std::out_of_range{"[const float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2) const] Vertex with id " + static_cast<std::string>(vertex1) + " not found"};
}
if (m_vertices.find(vertex2) == m_vertices.end())
{
throw std::out_of_range{"[const float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2) const] Vertex with id " + static_cast<std::string>(vertex2) + " not found"};
}
for (auto &neighbor : m_vertices.at(vertex1).second)
{
if (neighbor.first == vertex2)
{
return neighbor.second;
}
}
throw std::out_of_range{"[const float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2) const] Edge between vertex with id " + static_cast<std::string>(vertex1) + " and vertex with id " + static_cast<std::string>(vertex2) + " not found"};
}
float &weight(const uuid &vertex1, const uuid &vertex2) override
{
if (vertex1 == vertex2)
{
throw std::invalid_argument("[float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2)] vertex1 and vertex2 must be different");
}
if (m_vertices.find(vertex1) == m_vertices.end())
{
throw std::out_of_range{"[float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2)] Vertex with id " + static_cast<std::string>(vertex1) + " not found"};
}
if (m_vertices.find(vertex2) == m_vertices.end())
{
throw std::out_of_range{"[float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2)] Vertex with id " + static_cast<std::string>(vertex2) + " not found"};
}
for (auto &neighbor : m_vertices.at(vertex1).second)
{
if (neighbor.first == vertex2)
{
return neighbor.second;
}
}
throw std::out_of_range{"[float& sgl::AdjacencyList::weight(const uuid &vertex1, const uuid &vertex2)] Edge between vertex with id " + static_cast<std::string>(vertex1) + " and vertex with id " + static_cast<std::string>(vertex2) + " not found"};
}
size_t size() const override { return m_vertices.size(); }
size_t size(const uuid &id) const override
{
return m_vertices.at(id).second.size();
}
void clear() override { m_vertices.clear(); }
std::ostream &print(std::ostream &os = std::cout) const override
{
for (auto it = cbegin(); it != cend(); ++it)
{
os << *it << std::endl;
}
return os;
}
template <template <typename> typename ALGORITHM = BFS,
VisitPolicy policy = VisitPolicy::RELATED, typename FUNCTION,
typename... ARGS>
void traverse(const uuid &id, FUNCTION function, ARGS &&...args)
{
ALGORITHM<AdjacencyList<DATA_TYPE>> algorithm;
algorithm.template traverse<policy>(*this, id, function,
std::forward<ARGS>(args)...);
}
template <template <typename> typename ALGORITHM = BFS,
VisitPolicy policy = VisitPolicy::RELATED, typename FUNCTION,
typename... ARGS>
void traverse(FUNCTION function, ARGS &&...args)
{
ALGORITHM<AdjacencyList<DATA_TYPE>> algorithm;
const uuid &id = m_vertices.begin()->first;
algorithm.template traverse<policy>(*this, id, function,
std::forward<ARGS>(args)...);
}
template <template <typename> typename ALGORITHM = BFS,
VisitPolicy policy = VisitPolicy::RELATED>
void traverse(const uuid &id)
{
ALGORITHM<AdjacencyList<DATA_TYPE>> algorithm;
algorithm.template traverse<policy>(*this, id,
[](const Vertex<DATA_TYPE> &vertex)
{
std::cout << vertex << std::endl;
});
}
template <template <typename> typename ALGORITHM = BFS,
VisitPolicy policy = VisitPolicy::RELATED>
void traverse()
{
ALGORITHM<AdjacencyList<DATA_TYPE>> algorithm;
const uuid &id = m_vertices.begin()->first;
algorithm.template traverse<policy>(*this, id,
[](const Vertex<DATA_TYPE> &vertex)
{
std::cout << vertex << std::endl;
});
}
friend std::ostream &operator<<(std::ostream &os, const AdjacencyList &list)
{
return list.print(os);
}
base_const_iterator cbegin() const override { return base_const_iterator{std::make_shared<const_iterator<const_vertex_iterator>>(m_vertices.cbegin(), m_vertices)}; }
base_const_iterator cend() const override { return base_const_iterator{std::make_shared<const_iterator<const_vertex_iterator>>(m_vertices.cend(), m_vertices)}; }
base_iterator begin() override { return base_iterator{std::make_shared<iterator<vertex_iterator>>(m_vertices.begin(), m_vertices)}; }
base_iterator end() override { return base_iterator{std::make_shared<iterator<vertex_iterator>>(m_vertices.end(), m_vertices)}; }
base_const_iterator cbegin(const uuid &id) const override { return base_const_iterator{std::make_shared<const_iterator<const_neighbor_iterator>>(m_vertices.at(id).second.cbegin(), m_vertices)}; }
base_const_iterator cend(const uuid &id) const override { return base_const_iterator{std::make_shared<const_iterator<const_neighbor_iterator>>(m_vertices.at(id).second.cend(), m_vertices)}; }
base_iterator begin(const uuid &id) override { return base_iterator{std::make_shared<iterator<neighbor_iterator>>(m_vertices.at(id).second.begin(), m_vertices)}; }
base_iterator end(const uuid &id) override { return base_iterator{std::make_shared<iterator<neighbor_iterator>>(m_vertices.at(id).second.end(), m_vertices)}; }
template <typename ITERATOR>
class iterator : public base_iterator_impl
{
public:
iterator(const ITERATOR &it, std::map<uuid, std::pair<
std::shared_ptr<VERTEX_TYPE>, std::vector<std::pair<uuid, float>>>>
vertices) : m_it{it}, m_vertices{vertices} {}
bool operator==(const base_iterator_impl *other) const override { return m_it == static_cast<const iterator *>(other)->m_it; }
bool operator!=(const base_iterator_impl *other) const override { return m_it != static_cast<const iterator *>(other)->m_it; }
base_iterator_impl *operator++() override
{
++m_it;
return this;
}
base_iterator_impl *operator--() override
{
--m_it;
return this;
}
VERTEX_TYPE &operator*() const override
{
if constexpr (std::is_same_v<ITERATOR, vertex_iterator>)
{
return *m_it->second.first;
}
else if constexpr (std::is_same_v<ITERATOR, neighbor_iterator>)
{
return *m_vertices.at(m_it->first).first;
}
}
VERTEX_TYPE *operator->() const override
{
if constexpr (std::is_same_v<ITERATOR, vertex_iterator>)
{
return m_it->second.first.get();
}
else if (std::is_same_v<ITERATOR, neighbor_iterator>)
{
return m_vertices.at(m_it->first).first.get();
}
}
private:
ITERATOR m_it;
std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::vector<std::pair<uuid, float>>>> m_vertices;
};
template <typename ITERATOR>
class const_iterator : public base_const_iterator_impl
{
public:
const_iterator(const ITERATOR &it, std::map<uuid, std::pair<
std::shared_ptr<VERTEX_TYPE>, std::vector<std::pair<uuid, float>>>>
vertices) : m_it{it}, m_vertices{vertices} {}
bool operator==(const base_const_iterator_impl *other) const override { return m_it == static_cast<const const_iterator *>(other)->m_it; }
bool operator!=(const base_const_iterator_impl *other) const override { return m_it != static_cast<const const_iterator *>(other)->m_it; }
base_const_iterator_impl *operator++() override
{
++m_it;
return this;
}
base_const_iterator_impl *operator--() override
{
--m_it;
return this;
}
const VERTEX_TYPE &operator*() const override
{
if constexpr (std::is_same_v<ITERATOR, const_vertex_iterator>)
{
return *m_it->second.first;
}
else if constexpr (std::is_same_v<ITERATOR, const_neighbor_iterator>)
{
return *m_vertices.at(m_it->first).first;
}
}
const VERTEX_TYPE *operator->() const override
{
if constexpr (std::is_same_v<ITERATOR, const_vertex_iterator>)
{
return m_it->second.first.get();
}
else if constexpr (std::is_same_v<ITERATOR, const_neighbor_iterator>)
{
return m_vertices.at(m_it->first).first.get();
}
}
private:
ITERATOR m_it;
std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::vector<std::pair<uuid, float>>>> m_vertices;
};
};
template <typename DATA_TYPE>
class AdjacencyMatrix : public DataStructureBase<DATA_TYPE>, public std::enable_shared_from_this<AdjacencyMatrix<DATA_TYPE>>
{
public:
~AdjacencyMatrix() = default;
private:
using VERTEX_TYPE = Vertex<DATA_TYPE>;
using base_iterator_impl =
typename DataStructureBase<DATA_TYPE>::iterator_impl;
using base_iterator = typename DataStructureBase<DATA_TYPE>::iterator;
using base_const_iterator_impl =
typename DataStructureBase<DATA_TYPE>::const_iterator_impl;
using base_const_iterator =
typename DataStructureBase<DATA_TYPE>::const_iterator;
using const_neighbor_iterator =
typename std::map<uuid, float>::const_iterator;
using neighbor_iterator =
typename std::map<uuid, float>::iterator;
using const_vertex_iterator =
typename std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::map<uuid, float>>>::const_iterator;
using vertex_iterator =
typename std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::map<uuid, float>>>::iterator;
friend class BFS<AdjacencyMatrix<DATA_TYPE>>;
friend class DFS<AdjacencyMatrix<DATA_TYPE>>;
friend class Graph<DATA_TYPE, AdjacencyMatrix>;
friend class VertexPrinter;
std::map<uuid, std::pair<std::shared_ptr<VERTEX_TYPE>, std::map<uuid, float>>> m_vertices;
AdjacencyMatrix() = default;
const uuid &add_vertex(VERTEX_TYPE &&vertex) override
{
auto new_vertex = std::make_shared<VERTEX_TYPE>(std::forward<VERTEX_TYPE>(vertex));
new_vertex->add_data_structure(this->shared_from_this());
const uuid &id = new_vertex->get_id();
m_vertices.insert(std::make_pair(id, std::make_pair(std::move(new_vertex), std::map<uuid, float>{})));
for (auto &v : m_vertices)
{
v.second.second.insert(std::make_pair(id, std::nanf("Not adjacent")));
m_vertices.at(id).second.insert(std::make_pair(v.first, std::nanf("Not adjacent")));
}
return id;
}
const uuid &add_vertex(DATA_TYPE &&data) override
{
VERTEX_TYPE vertex{std::forward<DATA_TYPE>(data)};
return add_vertex(std::move(vertex));
}
void add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0) override
{
if (vertex1 == vertex2)
{
throw std::invalid_argument{"[void sgl::AdjacencyMatrix::add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0)] vertex1 and vertex2 must be different"};
}
// check if edge already exists
if (!std::isnan(m_vertices.at(vertex1).second.at(vertex2)))
{
throw std::invalid_argument{"[void sgl::AdjacencyMatrix::add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0)] Edge between vertex with id " + static_cast<std::string>(vertex1) + " and vertex with id " + static_cast<std::string>(vertex2) + " already exists"};
}
try
{
m_vertices.at(vertex1).second.at(vertex2) = weight;
m_vertices.at(vertex2).second.at(vertex1) = weight;
}
catch (const std::out_of_range &e)
{
throw std::out_of_range{"[void sgl::AdjacencyMatrix::add_edge(const uuid &vertex1, const uuid &vertex2, const float weight = 0)] Vertex with id " + static_cast<std::string>(vertex1) + " or " + static_cast<std::string>(vertex2) + " not found"};
}
}
void remove_vertex(const uuid &vertex) override
{
if (m_vertices.find(vertex) == m_vertices.end())
{
throw std::out_of_range{"[void sgl::AdjacencyMatrix::remove_vertex(const uuid &vertex) override] Vertex with id " + static_cast<std::string>(vertex) + " not found"};
}
m_vertices.erase(vertex);
for (auto &v : m_vertices)
{
v.second.second.erase(vertex);
}
}
void remove_edge(const uuid &vertex1, const uuid &vertex2) override
{
if (vertex1 == vertex2)
{
throw std::invalid_argument("[void sgl::AdjacencyMatrix::remove_edge(const uuid &vertex1, const uuid &vertex2)] vertex1 and vertex2 must be different");
}
try
{
m_vertices.at(vertex1).second.at(vertex2) = std::nanf("Not adjacent");
m_vertices.at(vertex2).second.at(vertex1) = std::nanf("Not adjacent");
}
catch (const std::out_of_range &e)
{
throw std::out_of_range{"[void sgl::AdjacencyMatrix::remove_edge(const uuid &vertex1, const uuid &vertex2)] Vertex with id " + static_cast<std::string>(vertex1) + " or " + static_cast<std::string>(vertex2) + " not found"};
}
}
template <typename FUNCTION, typename... ARGS>
void remove_if(FUNCTION &&function, ARGS &&...args)
{
if constexpr (!std::is_same_v<
bool, std::invoke_result_t<FUNCTION, const VERTEX_TYPE &,
ARGS...>>)
{
throw std::invalid_argument("[void sgl::AdjacencyMatrix::remove_if(FUNCTION &&function, ARGS &&...args)] return type of function must be bool");
}
else
{
std::vector<uuid> to_remove;
for (auto it = cbegin(); it != cend(); ++it)
{
if (function(*it, std::forward<ARGS>(args)...))
{
to_remove.push_back(it->get_id());
}
}
for (auto &id : to_remove)
{
remove_vertex(id);
}
}
}
const VERTEX_TYPE &vertex(const uuid &id) const override { return *m_vertices.at(id).first; }
VERTEX_TYPE &vertex(const uuid &id) override { return *m_vertices.at(id).first; }
const float &weight(const uuid &vertex1, const uuid &vertex2) const override
{
if (vertex1 == vertex2)
{
throw std::invalid_argument("[const float &sgl::AdjacencyMatrix::weight(const uuid &vertex1, const uuid &vertex2) const] vertex1 and vertex2 must be different");
}
if (m_vertices.find(vertex1) == m_vertices.end())
{
throw std::out_of_range{"[const float &sgl::AdjacencyMatrix::weight(const uuid &vertex1, const uuid &vertex2) const] Vertex with id " + static_cast<std::string>(vertex1) + " not found"};
}
if (m_vertices.find(vertex2) == m_vertices.end())
{
throw std::out_of_range{"[const float &sgl::AdjacencyMatrix::weight(const uuid &vertex1, const uuid &vertex2) const] Vertex with id " + static_cast<std::string>(vertex2) + " not found"};
}