-
Notifications
You must be signed in to change notification settings - Fork 0
/
design_twitter.rs
75 lines (66 loc) · 2.04 KB
/
design_twitter.rs
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
#![allow(dead_code)]
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::VecDeque;
#[derive(Default)]
struct Twitter {
users: BTreeMap<i32, BTreeSet<i32>>,
tweets: VecDeque<(i32, i32)>,
}
impl Twitter {
fn new() -> Self {
Self::default()
}
fn post_tweet(&mut self, user_id: i32, tweet_id: i32) {
self.check_and_create_user_if_not_exist(user_id);
self.tweets.push_front((user_id, tweet_id));
}
fn get_news_feed(&mut self, user_id: i32) -> Vec<i32> {
self.check_and_create_user_if_not_exist(user_id);
let mut res = Vec::new();
let following = self.users.get(&user_id).unwrap();
for (usr_id, tweet_id) in &self.tweets {
if *usr_id == user_id || following.contains(&usr_id) {
res.push(*tweet_id);
if res.len() == 10 {
break;
}
}
}
res
}
fn follow(&mut self, follower_id: i32, followee_id: i32) {
self.check_and_create_user_if_not_exist(follower_id);
self.users
.get_mut(&follower_id)
.unwrap()
.insert(followee_id);
}
fn unfollow(&mut self, follower_id: i32, followee_id: i32) {
self.check_and_create_user_if_not_exist(follower_id);
self.users
.get_mut(&follower_id)
.unwrap()
.remove(&followee_id);
}
fn check_and_create_user_if_not_exist(&mut self, follower_id: i32) {
if let None = self.users.get(&follower_id) {
self.users.insert(follower_id, BTreeSet::new());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_twitter() {
let mut twitter = Twitter::new();
twitter.post_tweet(1, 5);
assert_eq!(twitter.get_news_feed(1), vec![5]);
twitter.follow(1, 2);
twitter.post_tweet(2, 6);
assert_eq!(twitter.get_news_feed(1), vec![6, 5]);
twitter.unfollow(1, 2);
assert_eq!(twitter.get_news_feed(1), vec![5]);
}
}