/
types.h
143 lines (118 loc) 路 4.61 KB
/
types.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
142
143
/** Copyright 2020 Alibaba Group Holding Limited.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef GRAPE_TYPES_H_
#define GRAPE_TYPES_H_
#include <istream>
#include <ostream>
#include <type_traits>
// Use the same setting with apache-arrow to avoid possible conflicts
#define nssv_CONFIG_SELECT_STRING_VIEW nssv_STRING_VIEW_NONSTD
#include "string_view/string_view.hpp"
namespace grape {
/**
* @brief EmptyType is the placeholder of VDATA_T and EDATA_T for graphs without
* data on vertices and edges.
*
*/
struct EmptyType {};
inline std::ostream& operator<<(std::ostream& out, const EmptyType) {
return out;
}
inline std::istream& operator>>(std::istream& in, EmptyType) { return in; }
template <typename E>
using enable_enum_t =
typename std::enable_if<std::is_enum<E>::value,
typename std::underlying_type<E>::type>::type;
template <typename E>
constexpr inline enable_enum_t<E> underlying_value(E e) noexcept {
return static_cast<typename std::underlying_type<E>::type>(e);
}
template <typename E, typename T>
constexpr inline typename std::enable_if<
std::is_enum<E>::value && std::is_integral<T>::value, E>::type
to_enum(T value) noexcept {
return static_cast<E>(value);
}
/**
* @brief LoadStrategy specifies the which edges should be loadded when building
* the graph from a location.
*
*/
enum class LoadStrategy {
kOnlyOut = 0,
kOnlyIn = 1,
kBothOutIn = 2,
kNullLoadStrategy = 0xf0,
};
/**
* @brief MessageStrategy specifies the method of message passing between
* fragments.
*
* Assume in an edgecut distributed graph, we have an edge a->b, with vertex a
* on fragment_1 and b on fragment_2, and an edge a<-c with c on f_2.
*
* for fragment_1, a is an inner_vertex and b', c' is outer_vertexs.
*
*/
enum class MessageStrategy {
kAlongOutgoingEdgeToOuterVertex = 0, /// from a to b;
kAlongIncomingEdgeToOuterVertex = 1, /// from c to a;
kAlongEdgeToOuterVertex = 2, /// from a to b, a to c;
kSyncOnOuterVertex = 3, /// from b' to b and c' to c;
};
template <typename APP_T, typename GRAPH_T>
constexpr inline bool check_load_strategy_compatible() {
return ((APP_T::load_strategy == LoadStrategy::kBothOutIn) &&
(GRAPH_T::load_strategy == LoadStrategy::kBothOutIn)) ||
((APP_T::load_strategy == LoadStrategy::kOnlyIn) &&
((GRAPH_T::load_strategy == LoadStrategy::kBothOutIn) ||
(GRAPH_T::load_strategy == LoadStrategy::kOnlyIn))) ||
((APP_T::load_strategy == LoadStrategy::kOnlyOut) &&
((GRAPH_T::load_strategy == LoadStrategy::kBothOutIn) ||
(GRAPH_T::load_strategy == LoadStrategy::kOnlyOut)));
}
template <typename APP_T, typename GRAPH_T>
constexpr inline bool check_message_strategy_valid() {
return ((APP_T::message_strategy ==
MessageStrategy::kAlongEdgeToOuterVertex) &&
(GRAPH_T::load_strategy == LoadStrategy::kBothOutIn)) ||
((APP_T::message_strategy ==
MessageStrategy::kAlongIncomingEdgeToOuterVertex) &&
((GRAPH_T::load_strategy == LoadStrategy::kOnlyIn) ||
(GRAPH_T::load_strategy == LoadStrategy::kBothOutIn))) ||
((APP_T::message_strategy ==
MessageStrategy::kAlongOutgoingEdgeToOuterVertex) &&
((GRAPH_T::load_strategy == LoadStrategy::kOnlyOut) ||
(GRAPH_T::load_strategy == LoadStrategy::kBothOutIn))) ||
(APP_T::message_strategy == MessageStrategy::kSyncOnOuterVertex);
}
template <typename APP_T, typename GRAPH_T>
constexpr inline bool check_app_fragment_consistency() {
return check_load_strategy_compatible<APP_T, GRAPH_T>() &&
check_message_strategy_valid<APP_T, GRAPH_T>();
}
template <typename T>
struct InternalOID {
using type = T;
static type ToInternal(const T& val) { return val; }
static T FromInternal(const type& val) { return val; }
};
template <>
struct InternalOID<std::string> {
using type = nonstd::string_view;
static type ToInternal(const std::string& val) {
return nonstd::string_view(val.data(), val.size());
}
static std::string FromInternal(const type& val) { return std::string(val); }
};
} // namespace grape
#endif // GRAPE_TYPES_H_