-
Notifications
You must be signed in to change notification settings - Fork 67
/
pair_graph.hpp
112 lines (98 loc) · 1.75 KB
/
pair_graph.hpp
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
#pragma once
#include <memory>
#include <vector>
class PairGraph
{
public:
PairGraph(int nrows, int ncols) : nrows(nrows), ncols(ncols)
{
this->rows.resize(nrows);
this->cols.resize(ncols);
}
/**
* Returns the column index of the pair matching this row
*/
inline int colForRow(int row) const
{
return this->rows[row];
}
/**
* Returns the row index of the pair matching this column
*/
inline int rowForCol(int col) const
{
return this->cols[col];
}
/**
* Creates a pair between row and col
*/
inline void set(int row, int col)
{
this->rows[row] = col;
this->cols[col] = row;
}
inline bool isRowSet(int row) const
{
return rows[row] >= 0;
}
inline bool isColSet(int col) const
{
return cols[col] >= 0;
}
inline bool isPair(int row, int col)
{
return rows[row] == col;
}
/**
* Clears pair between row and col
*/
inline void reset(int row, int col)
{
this->rows[row] = -1;
this->cols[col] = -1;
}
/**
* Clears all pairs in graph
*/
void clear()
{
for (int i = 0; i < this->nrows; i++)
{
this->rows[i] = -1;
}
for (int j = 0; j < this->ncols; j++)
{
this->cols[j] = -1;
}
}
int numPairs()
{
int count = 0;
for (int i = 0; i < nrows; i++)
{
if (rows[i] >= 0)
{
count++;
}
}
return count;
}
std::vector<std::pair<int, int>> pairs()
{
std::vector<std::pair<int, int>> p(numPairs());
int count = 0;
for (int i = 0; i < nrows; i++)
{
if (isRowSet(i))
{
p[count++] = {i, colForRow(i)};
}
}
return p;
}
const int nrows;
const int ncols;
private:
std::vector<int> rows;
std::vector<int> cols;
};