-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFriendships.cpp
More file actions
90 lines (79 loc) · 1.99 KB
/
Copy pathFriendships.cpp
File metadata and controls
90 lines (79 loc) · 1.99 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
#include "Friendships.h"
namespace codesmith
{
bool Friendships::FriendPair::operator==(const FriendPair& other)
{
return ((m_id1 == other.m_id1 && m_id2 == other.m_id2) ||
(m_id1 == other.m_id2 && m_id2 == other.m_id1));
}
bool Friendships::FriendPair::operator!=(const FriendPair& other)
{
return !(*this == other);
}
bool Friendships::areFriends(const Person& p1, const Person& p2) const
{
return areFriends(p1.getId(), p2.getId());
}
bool Friendships::areFriends(int p1_id, int p2_id) const
{
bool res = false;
FriendPair pair(p1_id, p2_id);
for (auto p : m_friendRelations) {
if (p == pair) {
res = true;
break;
}
}
return res;
}
std::vector<int> Friendships::getFriends(int id)
{
std::vector<int> friends;
for (auto r : m_friendRelations) {
if (r.m_id1 == id) {
friends.push_back(r.m_id1);
}
else if (r.m_id2 == id) {
friends.push_back(r.m_id2);
}
}
return friends;
}
std::vector<int> Friendships::getFriends(const Person& person)
{
return getFriends(person.getId());
}
void Friendships::add(const Person& p1, const Person& p2)
{
if (!areFriends(p1, p2)) {
m_friendRelations.push_back(FriendPair(p1.getId(), p2.getId()));
}
}
void Friendships::add(int p1_id, int p2_id)
{
if (!areFriends(p1_id, p2_id)) {
m_friendRelations.push_back(FriendPair(p1_id, p2_id));
}
}
void Friendships::unfriend(int p1_id, int p2_id)
{
std::vector<Friendships::FriendPair>::iterator it = findFriendshipIterByPair(FriendPair(p1_id, p2_id));
if (it != m_friendRelations.end()) {
m_friendRelations.erase(it);
}
}
void Friendships::unfriend(const Person& p1, const Person& p2)
{
unfriend(p1.getId(), p2.getId());
}
std::vector<Friendships::FriendPair>::iterator Friendships::findFriendshipIterByPair(const FriendPair& pair)
{
std::vector<FriendPair>::iterator it = m_friendRelations.begin();
for (; it != m_friendRelations.end(); it++) {
if (areFriends((*it).m_id1, (*it).m_id2)) {
break;
}
}
return it;
}
}