-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashGraph.h
More file actions
185 lines (154 loc) · 4.22 KB
/
HashGraph.h
File metadata and controls
185 lines (154 loc) · 4.22 KB
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
/**
*
* Most optimal graph implementation
*
* O(1) Edge Lookup
* O(1) Insert Edge
* O(1) Insert Vertex
* O(|in|) Get Input Vertices (Not yet completed, but starting code is there)
* O(|out|) Get Output Vertices (Not yet completed, but starting code is there)
* O(|in| + |out|) Get Adjacent Vertices (Not yet completed)
*
* Edge<Dim> holds a source and destination vertex
* Point<Dim> is a vertex
*/
#ifndef HASH_GRAPH__H
#define HASH_GRAPH__H
#include "point.h"
#include "edge.h"
#include <vector>
#include <unordered_map>
template <int Dim>
class HashList
{
private:
typedef unsigned long long Code_;
typedef std::pair<Edge<Dim> *, bool> EdgeInfo_;
typedef std::unordered_map<Code_, EdgeInfo_ > EdgeData_;
typedef std::unordered_map<Code_, Code_> Next_;
typedef std::unordered_map<Point<Dim> *, Code_> Heads_;
Code_ ptrToCode_(Point<Dim> *x, Point<Dim> *y) const;
Point<Dim> *codeToDst_(Code_ code) const;
Point<Dim> *codeToSrc_(Code_ code) const;
EdgeData_ edgeData_;
Next_ nextIn_;
Next_ nextOut_;
Heads_ headsIn_;
Heads_ headsOut_;
public:
HashList(){};
~HashList(){};
Edge<Dim> * insertEdge(Point<Dim> * x, Point<Dim> * y);
void insertVertex(Point<Dim> * v);
std::vector<Point<Dim> *> getAdjacent(Point<Dim> *v) const;
std::vector<Edge<Dim> *> getIn(Point<Dim> *v) const;
std::vector<Edge<Dim> *> getOut(Point<Dim> *v) const;
};
template <int Dim>
Point<Dim> *HashList<Dim>::codeToDst_(Code_ code) const
{
return (Point<Dim> *) code;
}
template <int Dim>
Point<Dim> *HashList<Dim>::codeToSrc_(Code_ code) const
{
return (Point<Dim> *) (code >> (sizeof(long) * 8));
}
template <int Dim>
typename HashList<Dim>::Code_ HashList<Dim>::ptrToCode_(Point<Dim> *src, Point<Dim> *dst) const
{
return (Code_(src) << (sizeof(long) * 8)) | Code_(dst);
}
template <int Dim>
void HashList<Dim>::insertVertex(Point<Dim> *v)
{
headsIn_[v] = 0;
headsOut_[v] = 0;
}
template <int Dim>
Edge<Dim> * HashList<Dim>::insertEdge(Point<Dim> *src, Point<Dim> *dst)
{
Edge<Dim> * ret;
Code_ src_code = ptrToCode_(src, dst);
Code_ dst_code = ptrToCode_(dst, src);
EdgeData_::iterator edge_it;
if ((edge_it = edgeData_.find(src_code)) == edgeData_.end())
{
ret = new Edge<Dim>(src, dst);
edgeData_[src_code] = EdgeInfo_(ret, true);
}
else
{
ret = (edge_it->second).first;
if ((edge_it->second).second == false)
(edge_it->second).second = true;
else
{
ret->weight ++;
return ret;
}
}
if (edgeData_.find(dst_code) == edgeData_.end())
edgeData_[dst_code] = EdgeInfo_(new Edge<Dim>(dst, src), false);
nextOut_[src_code] = headsOut_[src];
headsOut_[src] = src_code;
nextIn_[src_code] = headsIn_[dst];
headsIn_[dst] = src_code;
return ret;
}
template <int Dim>
std::vector<Point<Dim>* > HashList<Dim>::getAdjacent(Point<Dim> *v) const
{
std::vector<Point<Dim> *> ret;
std::unordered_map<Point<Dim> *, Code_>::iterator it = heads_.find(v);
if (it == heads_.end())
return ret;
Code_ head = it->second;
while(head != 0)
{
ret.push_back(codeToDst_(head));
head = nextIn_[head];
}
head = it->second;
while(head != 0)
{
ret.push_back(codeToSrc_(head));
head = nextOut_[head];
}
return ret;
}
template <int Dim>
std::vector<Edge<Dim>* > HashList<Dim>::getIn(Point<Dim> *v) const
{
std::vector<Edge<Dim> *> ret;
std::unordered_map<Point<Dim> *, Code_>::const_iterator it = headsIn_.find(v);
if (it == headsIn_.end())
return ret;
Code_ head = it->second;
std::unordered_map<Code_, Code_>::const_iterator head_it = nextIn_.find(head);
while( (head_it = nextIn_.find(head)) != nextIn_.end() )
{
EdgeData_::const_iterator edge_it = edgeData_.find(head);
ret.push_back((edge_it->second).first);
head = head_it->second;
}
return ret;
}
template <int Dim>
std::vector<Edge<Dim>* > HashList<Dim>::getOut(Point<Dim> *v) const
{
std::vector<Edge<Dim> *> ret;
std::unordered_map<Point<Dim> *, Code_>::const_iterator it = headsOut_.find(v);
if (it == headsOut_.end())
return ret;
Code_ head = it->second;
std::unordered_map<Code_, Code_>::const_iterator head_it = nextIn_.find(head);
while( (head_it = nextOut_.find(head)) != nextOut_.end() )
{
EdgeData_::const_iterator edge_it = edgeData_.find(head);
ret.push_back((edge_it->second).first);
head = head_it->second;
}
return ret;
}
#endif /* HASH_GRAPH__H */