Skip to content

Commit 0d7a690

Browse files
committed
Implement topology related APIs
1 parent 95fd1ce commit 0d7a690

File tree

3 files changed

+612
-0
lines changed

3 files changed

+612
-0
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/** Copyright 2020 Alibaba Group Holding Limited.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
#include "src/predefine.h"
17+
// GRIN headers
18+
#include "topology/adjacentlist.h"
19+
20+
#if defined(GRIN_ENABLE_ADJACENT_LIST) && !defined(GRIN_WITH_EDGE_PROPERTY)
21+
GRIN_ADJACENT_LIST grin_get_adjacent_list(GRIN_GRAPH, GRIN_DIRECTION,
22+
GRIN_VERTEX);
23+
#endif
24+
25+
#ifdef GRIN_ENABLE_ADJACENT_LIST
26+
void grin_destroy_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al) {
27+
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
28+
delete _al;
29+
}
30+
#endif
31+
32+
#ifdef GRIN_ENABLE_ADJACENT_LIST_ARRAY
33+
size_t grin_get_adjacent_list_size(GRIN_GRAPH g, GRIN_ADJACENT_LIST al) {
34+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
35+
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
36+
auto vtype = _al->vtype_id;
37+
auto vid = _al->vid;
38+
auto etype = _al->etype_id;
39+
auto dir = _al->dir;
40+
auto partition_num = _g->GetPartitionNum();
41+
42+
if (_al->partition_type == ALL_PARTITION) {
43+
auto num = 0;
44+
for (auto i = 0; i < partition_num; i++) {
45+
num += _g->GetAdjacentListSize(vtype, vid, etype, i, dir);
46+
}
47+
return num;
48+
}
49+
50+
auto partition_id = _al->partition_id;
51+
if (_al->partition_type == ONE_PARTITION) {
52+
return _g->GetAdjacentListSize(vtype, vid, etype, partition_id, dir);
53+
}
54+
55+
if (_al->partition_type == ALL_BUT_ONE_PARTITION) {
56+
auto num = 0;
57+
for (auto i = 0; i < partition_num; i++) {
58+
if (i != partition_id) {
59+
num += _g->GetAdjacentListSize(vtype, vid, etype, i, dir);
60+
}
61+
}
62+
return num;
63+
}
64+
65+
return GRIN_NULL_SIZE;
66+
}
67+
68+
GRIN_VERTEX grin_get_neighbor_from_adjacent_list(GRIN_GRAPH g,
69+
GRIN_ADJACENT_LIST al,
70+
size_t idx) {
71+
auto e = grin_get_edge_from_adjacent_list(g, al, idx);
72+
if (e == GRIN_NULL_EDGE) {
73+
return GRIN_NULL_VERTEX;
74+
}
75+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
76+
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
77+
auto& edge = _g->GetEdge(e);
78+
auto v = DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(
79+
_al->vtype_id, _al->vid);
80+
if (edge.GetSource() == v) {
81+
return edge.GetDest();
82+
} else if (edge.GetDest() == v) {
83+
return edge.GetSource();
84+
} else {
85+
return GRIN_NULL_VERTEX;
86+
}
87+
}
88+
89+
GRIN_EDGE grin_get_edge_from_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al,
90+
size_t idx) {
91+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
92+
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
93+
auto vtype = _al->vtype_id;
94+
auto vid = _al->vid;
95+
auto etype = _al->etype_id;
96+
auto dir = _al->dir;
97+
auto partition_num = _g->GetPartitionNum();
98+
size_t prefix = 0;
99+
for (auto i = 0; i < partition_num; i++) {
100+
auto size = _g->GetAdjacentListSize(vtype, vid, etype, i, dir);
101+
if (_al->partition_type == ONE_PARTITION && i != _al->partition_id) {
102+
size = 0;
103+
}
104+
if (_al->partition_type == ALL_BUT_ONE_PARTITION &&
105+
i == _al->partition_id) {
106+
size = 0;
107+
}
108+
if (idx >= prefix && idx < prefix + size) {
109+
auto& edges = _g->GetAdjacentList(vtype, vid, etype, i, dir);
110+
return edges[idx - prefix];
111+
}
112+
prefix += size;
113+
}
114+
return GRIN_NULL_EDGE;
115+
}
116+
#endif
117+
118+
#ifdef GRIN_ENABLE_ADJACENT_LIST_ITERATOR
119+
GRIN_ADJACENT_LIST_ITERATOR grin_get_adjacent_list_begin(
120+
GRIN_GRAPH g, GRIN_ADJACENT_LIST al) {
121+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
122+
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
123+
auto vtype = _al->vtype_id;
124+
auto vid = _al->vid;
125+
auto etype = _al->etype_id;
126+
auto dir = _al->dir;
127+
auto partition_id = _al->partition_id;
128+
auto partition_num = _g->GetPartitionNum();
129+
130+
auto current_partition = 0;
131+
while (current_partition < partition_num) {
132+
auto size =
133+
_g->GetAdjacentListSize(vtype, vid, etype, current_partition, dir);
134+
if (_al->partition_type == ONE_PARTITION &&
135+
current_partition != partition_id) {
136+
size = 0;
137+
}
138+
if (_al->partition_type == ALL_BUT_ONE_PARTITION &&
139+
current_partition == partition_id) {
140+
size = 0;
141+
}
142+
if (size > 0) {
143+
return new GRIN_ADJACENT_LIST_ITERATOR_T(
144+
vtype, vid, dir, etype, _al->partition_type, partition_id,
145+
current_partition, 0);
146+
}
147+
current_partition++;
148+
}
149+
150+
return new GRIN_ADJACENT_LIST_ITERATOR_T(vtype, vid, dir, etype,
151+
_al->partition_type, partition_id,
152+
current_partition, 0);
153+
}
154+
155+
void grin_destroy_adjacent_list_iter(GRIN_GRAPH g,
156+
GRIN_ADJACENT_LIST_ITERATOR ali) {
157+
auto _ali = static_cast<GRIN_ADJACENT_LIST_ITERATOR_T*>(ali);
158+
delete _ali;
159+
}
160+
161+
void grin_get_next_adjacent_list_iter(GRIN_GRAPH g,
162+
GRIN_ADJACENT_LIST_ITERATOR ali) {
163+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
164+
auto _ali = static_cast<GRIN_ADJACENT_LIST_ITERATOR_T*>(ali);
165+
auto vtype = _ali->vtype_id;
166+
auto vid = _ali->vid;
167+
auto etype = _ali->etype_id;
168+
auto dir = _ali->dir;
169+
auto partition_num = _g->GetPartitionNum();
170+
auto partition_id = _ali->partition_id;
171+
172+
_ali->current_offset++;
173+
if (_ali->current_offset >= _g->GetAdjacentListSize(vtype, vid, etype,
174+
_ali->current_partition,
175+
dir)) {
176+
_ali->current_offset = 0;
177+
_ali->current_partition++;
178+
while (_ali->current_partition < partition_num) {
179+
auto size = _g->GetAdjacentListSize(vtype, vid, etype,
180+
_ali->current_partition, dir);
181+
if (_ali->partition_type == ONE_PARTITION &&
182+
_ali->current_partition != partition_id) {
183+
size = 0;
184+
}
185+
if (_ali->partition_type == ALL_BUT_ONE_PARTITION &&
186+
_ali->current_partition == partition_id) {
187+
size = 0;
188+
}
189+
if (size > 0) {
190+
return;
191+
}
192+
_ali->current_partition++;
193+
}
194+
}
195+
}
196+
197+
bool grin_is_adjacent_list_end(GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR ali) {
198+
if (ali == GRIN_NULL_ADJACENT_LIST_ITERATOR) {
199+
return true;
200+
}
201+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
202+
auto _ali = static_cast<GRIN_ADJACENT_LIST_ITERATOR_T*>(ali);
203+
return _ali->current_partition >= _g->GetPartitionNum();
204+
}
205+
206+
GRIN_VERTEX grin_get_neighbor_from_adjacent_list_iter(
207+
GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR ali) {
208+
auto e = grin_get_edge_from_adjacent_list_iter(g, ali);
209+
if (e == GRIN_NULL_EDGE) {
210+
return GRIN_NULL_VERTEX;
211+
}
212+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
213+
auto _ali = static_cast<GRIN_ADJACENT_LIST_ITERATOR_T*>(ali);
214+
auto& edge = _g->GetEdge(e);
215+
auto v = DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(
216+
_ali->vtype_id, _ali->vid);
217+
if (edge.GetSource() == v) {
218+
return edge.GetDest();
219+
} else if (edge.GetDest() == v) {
220+
return edge.GetSource();
221+
} else {
222+
return GRIN_NULL_VERTEX;
223+
}
224+
}
225+
226+
GRIN_EDGE grin_get_edge_from_adjacent_list_iter(
227+
GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR ali) {
228+
auto _g = static_cast<GRIN_GRAPH_T*>(g);
229+
auto _ali = static_cast<GRIN_ADJACENT_LIST_ITERATOR_T*>(ali);
230+
auto& edges = _g->GetAdjacentList(_ali->vtype_id, _ali->vid, _ali->etype_id,
231+
_ali->current_partition, _ali->dir);
232+
if (_ali->current_offset >= edges.size()) {
233+
return GRIN_NULL_EDGE;
234+
}
235+
return edges[_ali->current_offset];
236+
}
237+
#endif

0 commit comments

Comments
 (0)