@@ -25,6 +25,47 @@ inline K csrDegree(const vector<O>& offsets, K u) {
25
25
26
26
27
27
28
+ #pragma region HAS EDGE
29
+ /* *
30
+ * Check if an edge exists in the graph.
31
+ * @param offsets offsets of the outgoing edges of vertices
32
+ * @param edgeKeys vertex ids of the outgoing edges of each vertex
33
+ * @param u source vertex id
34
+ * @param v target vertex id
35
+ * @returns true if edge exists, false otherwise
36
+ */
37
+ template <class O , class K >
38
+ inline bool csrHasEdge (const vector<O>& offsets, const vector<K>& edgeKeys, K u, K v) {
39
+ O i = offsets[u];
40
+ O I = offsets[u+1 ];
41
+ for (; i<I; ++i)
42
+ if (edgeKeys[i]==v) return true ;
43
+ return false ;
44
+ }
45
+
46
+
47
+ /* *
48
+ * Check if an edge exists in the graph.
49
+ * @param offsets offsets of the outgoing edges of vertices
50
+ * @param degrees degree of each vertex
51
+ * @param edgeKeys vertex ids of the outgoing edges of each vertex
52
+ * @param u source vertex id
53
+ * @param v target vertex id
54
+ * @returns true if edge exists, false otherwise
55
+ */
56
+ template <class O , class K >
57
+ inline bool csrHasEdge (const vector<O>& offsets, const vector<K>& degrees, const vector<K>& edgeKeys, K u, K v) {
58
+ O i = offsets[u];
59
+ O I = offsets[u] + degrees[u];
60
+ for (; i<I; ++i)
61
+ if (edgeKeys[i]==v) return true ;
62
+ return false ;
63
+ }
64
+ #pragma endregion
65
+
66
+
67
+
68
+
28
69
#pragma region FOREACH
29
70
/* *
30
71
* Iterate over the target vertex ids of a source vertex in the graph.
@@ -340,15 +381,17 @@ inline void csrClearOmpW(vector<O>& offsets, vector<K>& degrees) {
340
381
341
382
/* *
342
383
* Add an edge to the graph.
384
+ * @tparam CHECK check if edge already exists?
343
385
* @param degrees degree of each vertex
344
386
* @param edgeKeys vertex ids of the outgoing edges of each vertex
345
387
* @param offsets offsets of the outgoing edges of vertices
346
388
* @param u source vertex id
347
389
* @param v target vertex id
348
390
* @note Does not check if the edge already exists, or is there is available space.
349
391
*/
350
- template <class O , class K >
392
+ template <bool CHECK= false , class O , class K >
351
393
inline void csrAddEdgeU (vector<K>& degrees, vector<K>& edgeKeys, const vector<O>& offsets, K u, K v) {
394
+ if (CHECK && csrHasEdge (offsets, degrees, edgeKeys, u, v)) return ;
352
395
O n = degrees[u]++;
353
396
O i = offsets[u] + n;
354
397
edgeKeys[i] = v;
@@ -357,26 +400,30 @@ inline void csrAddEdgeU(vector<K>& degrees, vector<K>& edgeKeys, const vector<O>
357
400
#ifdef OPENMP
358
401
/* *
359
402
* Add an edge to the graph.
403
+ * @tparam CHECK check if edge already exists?
360
404
* @param degrees degree of each vertex
361
405
* @param edgeKeys vertex ids of the outgoing edges of each vertex
362
406
* @param offsets offsets of the outgoing edges of vertices
363
407
* @param u source vertex id
364
408
* @param v target vertex id
365
409
* @note Does not check if the edge already exists, or is there is available space.
366
410
*/
367
- template <class O , class K >
411
+ template <bool CHECK= false , class O , class K >
368
412
inline void csrAddEdgeOmpU (vector<K>& degrees, vector<K>& edgeKeys, const vector<O>& offsets, K u, K v) {
413
+ if (CHECK && csrHasEdge (offsets, degrees, edgeKeys, u, v)) return ;
369
414
O n = 0 ;
370
415
#pragma omp atomic capture
371
416
{ n = degrees[u]; ++degrees[u]; }
372
417
O i = offsets[u] + n;
418
+ if (CHECK && offsets[u+1 ]<=i) return ; // Check if there is available space.
373
419
edgeKeys[i] = v;
374
420
}
375
421
#endif
376
422
377
423
378
424
/* *
379
425
* Add a weighted edge to the graph.
426
+ * @tparam CHECK check if edge already exists?
380
427
* @param degrees degree of each vertex
381
428
* @param edgeKeys vertex ids of the outgoing edges of each vertex
382
429
* @param edgeValues edge values of the outgoing edges of each vertex
@@ -386,8 +433,9 @@ inline void csrAddEdgeOmpU(vector<K>& degrees, vector<K>& edgeKeys, const vector
386
433
* @param w associated weight of the edge
387
434
* @note Does not check if the edge already exists, or is there is available space.
388
435
*/
389
- template <class O , class K , class E >
436
+ template <bool CHECK= false , class O , class K , class E >
390
437
inline void csrAddEdgeU (vector<K>& degrees, vector<K>& edgeKeys, vector<E>& edgeValues, const vector<O>& offsets, K u, K v, E w) {
438
+ if (CHECK && csrHasEdge (offsets, degrees, edgeKeys, u, v)) return ;
391
439
O n = degrees[u]++;
392
440
O i = offsets[u] + n;
393
441
edgeKeys[i] = v;
@@ -397,6 +445,7 @@ inline void csrAddEdgeU(vector<K>& degrees, vector<K>& edgeKeys, vector<E>& edge
397
445
#ifdef OPENMP
398
446
/* *
399
447
* Add a weighted edge to the graph.
448
+ * @tparam CHECK check if edge already exists?
400
449
* @param degrees degree of each vertex
401
450
* @param edgeKeys vertex ids of the outgoing edges of each vertex
402
451
* @param edgeValues edge values of the outgoing edges of each vertex
@@ -406,12 +455,14 @@ inline void csrAddEdgeU(vector<K>& degrees, vector<K>& edgeKeys, vector<E>& edge
406
455
* @param w associated weight of the edge
407
456
* @note Does not check if the edge already exists, or is there is available space.
408
457
*/
409
- template <class O , class K , class E >
458
+ template <bool CHECK= false , class O , class K , class E >
410
459
inline void csrAddEdgeOmpU (vector<K>& degrees, vector<K>& edgeKeys, vector<E>& edgeValues, const vector<O>& offsets, K u, K v, E w) {
460
+ if (CHECK && csrHasEdge (offsets, degrees, edgeKeys, u, v)) return ;
411
461
O n = 0 ;
412
462
#pragma omp atomic capture
413
463
{ n = degrees[u]; ++degrees[u]; }
414
464
O i = offsets[u] + n;
465
+ if (CHECK && offsets[u+1 ]<=i) return ; // Check if there is available space.
415
466
edgeKeys[i] = v;
416
467
edgeValues[i] = w;
417
468
}
0 commit comments