-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_base.h
141 lines (96 loc) · 3.03 KB
/
matrix_base.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
//! \file matrix_base.h - class for matrix operation
#ifndef _MATRIX_BASE_H_
#define _MATRIX_BASE_H_
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cassert>
#include <boost/dynamic_bitset.hpp>
using namespace std;
//! A Matrix Class
class Matrix
{
friend Matrix Transpose(const Matrix& );
friend Matrix operator*(const Matrix&, const Matrix&); //matrix mul
friend ostream& operator<< (ostream& osr, const Matrix&);
public:
typedef boost::dynamic_bitset<> BITVECTOR;
//! Default constructor
Matrix();
//! constructor that allocates memory, and initialize all cell of the matrix as 0
Matrix(size_t r, size_t c);
//! Destructor
~Matrix();
const boost::dynamic_bitset<>& operator[](size_t i) const;
//! allocate memory for a matrix of size r X c initialize all entry with 0
void allocate(size_t r, size_t c);
//! Return an element of the matrix [r][c]
bool at(const unsigned int& r, const unsigned int& c) const;
void reset(const unsigned int& r);
//! set the value of the element [r][c]
void set(const unsigned int& r, const unsigned int& c, bool val);
inline size_t row() const {return _row;} //! Getter for _row
inline size_t col() const {return _col;} //! Getter for _col
size_t rowset_cnt(unsigned int i);
bool rowset_empty(unsigned int i) const;
void neighbors(const unsigned int& i, vector<unsigned int>& ret_val) const;
protected:
vector<BITVECTOR> _data;
private:
size_t _row, _col;
};
//! A SqrSymMatrix Class
class SqrSymMatrix : public Matrix {
public:
//! Default Constructor
SqrSymMatrix();
//! Constructor
SqrSymMatrix(size_t n);
int add_vertex();
void change_adj_matrix(int size, const vector<pair<int, int> >& adj_list);
void remove_vertex(size_t i);
bool at(const unsigned int& r, const unsigned int& c) const;
void neighbors(const unsigned int& i, vector<unsigned int>& ret_val) const;
bool essential_edge(int src, int dst) const;
inline size_t size() const {return _size;};
friend class AdjIterator;
friend class NodeAdjIterator;
protected:
size_t _size;
private:
bool dfs_visit(int src, BITVECTOR& visited, int dst) const;
};
//! A AdjIterator Class for Returning each edge only once
class AdjIterator {
public:
//! Constructor
AdjIterator(const SqrSymMatrix* m);
void first();
void next();
pair<size_t, size_t> current() const;
bool is_done() const;
private:
const SqrSymMatrix* _m;
int _i; //!< row-pos
int _j; //!< col-pos
bool _is_done;
size_t _max_i;
};
//! NodeAdjIterator Class to iterate over all the edge adjacent to a given node
class NodeAdjIterator {
public:
//! Constructor
NodeAdjIterator(const SqrSymMatrix* m, size_t i);
void first();
void next();
size_t current() const;
bool is_done() const;
private:
const SqrSymMatrix* _m;
int _i; //!< row-pos
int _j; //!< col-pos
bool _is_done;
size_t _max_i;
};
#endif