-
Notifications
You must be signed in to change notification settings - Fork 0
/
poi.h
93 lines (72 loc) · 1.99 KB
/
poi.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
//
// Created by sunnysab on 24-9-17.
//
#pragma once
#include "graph.h"
/// POI 点类型
using PoiType = unsigned int;
/// 用户兴趣值
using Interest = unsigned short;
/// 无效的 POI 类型
inline PoiType INVALID_POI_TYPE = 0;
struct Poi {
/// POI 对应顶点
Vertex v;
/// POI 类型
PoiType type;
/// POI 兴趣度(权重)
Interest interest;
};
/// POI 集合
struct PoiSet {
std::unordered_map<Vertex, Poi> pois_map;
size_t size() const {
return this->pois_map.size();
}
void add(const Poi &poi) {
this->pois_map[poi.v] = poi;
}
void remove(const Poi &poi) {
this->pois_map.erase(poi.v);
}
void clear() {
this->pois_map.clear();
}
const Poi& operator[](const Vertex &v) const {
return this->pois_map.at(v);
}
bool contains(const Vertex &v) const {
return this->pois_map.contains(v);
}
std::unordered_map<Vertex, Interest> get_interests(const std::vector<PoiType> &filter = {}) const {
std::unordered_map<Vertex, Interest> interests;
for (const auto &[v, poi] : this->pois_map) {
if (!filter.empty() && ranges::find(filter, poi.type) == filter.end()) {
continue;
}
interests[v] = poi.interest;
}
return interests;
}
Interest interest_or_zero(const Vertex v) const {
if (this->pois_map.contains(v)) {
return this->pois_map.at(v).interest;
}
return 0;
}
optional<Poi> get(const Vertex v) const {
if (const auto it = this->pois_map.find(v); it != this->pois_map.cend()) {
return it->second;
}
return std::nullopt;
}
std::vector<Vertex> vertex_of_type(const PoiType type) const {
std::vector<Vertex> result;
for (const auto &[v, poi] : this->pois_map) {
if (poi.type == type) {
result.push_back(v);
}
}
return result;
}
};