-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMember.cpp
More file actions
83 lines (66 loc) · 1.75 KB
/
Member.cpp
File metadata and controls
83 lines (66 loc) · 1.75 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
#include "Member.h"
#include <iterator>
unsigned int Member::ACTIVE_MEMBERS = 0;
unsigned int Member::COUNTER_ID = 1;
Member::Member(){
this->following = 0;
this->followers = 0;
this->id = COUNTER_ID;
COUNTER_ID++;
ACTIVE_MEMBERS++;
}
void Member::follow(Member& m)
{
//checking if "this" tried to follow himself
if(this->id == m.id)
return;
//checking if "this" already follows him
if(this->following_list.count(m.id) > 0)
return;
this->following_list.insert(pair<unsigned int, Member*>(m.id, &m));
this->following++;
m.followers_list.insert(pair<unsigned int, Member*>(this->id, this));
m.followers++;
}
void Member::unfollow(Member& m)
{
//checking if "this" tried to unfollow himself
if(this->id == m.id)
return;
//checking if "this" does not follow him
if(this->following_list.count(m.id) == 0)
return;
this->following_list.erase(m.id);
this->following--;
m.followers_list.erase(this->id);
m.followers--;
}
unsigned int Member::numFollowing() const
{
return this->following;
}
unsigned int Member::numFollowers() const
{
return this->followers;
}
unsigned int Member::count()
{
return ACTIVE_MEMBERS;
}
Member::~Member()
{
map<unsigned int, Member*>::iterator itr;
for(itr = this->followers_list.begin(); itr != this->followers_list.end(); ++itr )
{
Member* m = itr->second;
m->following_list.erase(this->id);
m->following--;
}
for(itr = this->following_list.begin(); itr != this->following_list.end(); ++itr)
{
Member* m = itr->second;
m->followers_list.erase(this->id);
m->followers--;
}
ACTIVE_MEMBERS--;
}