-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterrelated.js
72 lines (63 loc) · 1.75 KB
/
interrelated.js
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
export default class interrelated {
constructor() {
this.A的集合 = new Map()
this.B的集合 = new Map()
}
关联数据(A, B) {
let value = this.A的集合.get(A) || new Map()
if (!value.size) this.A的集合.set(A, value)
value.set(B, true)
let valux = this.B的集合.get(B) || new Map()
if (!valux.size) this.B的集合.set(B, valux)
valux.set(A, true)
}
取消关联(A, B) {
let value = this.A的集合.get(A)
if (value && value.size) value.delete(B)
if (value && value.size === 0) this.A的集合.delete(A)
let valux = this.B的集合.get(B)
if (valux && valux.size) valux.delete(A)
if (valux && valux.size === 0) this.B的集合.delete(B)
}
A中取B(A, callback) {
let value = this.A的集合.get(A)
if (value && value.size) {
value.forEach((value, B) => callback(B))
}
}
B中取A(B, callback) {
let valux = this.B的集合.get(B)
if (valux && valux.size) {
valux.forEach((value, A) => callback(A))
}
}
A中除B(B) {
let value = this.B的集合.get(B)
if (value && value.size) value.forEach((value, A) => {
let valux = this.A的集合.get(A)
if (valux && valux.size) valux.delete(B)
})
this.B的集合.delete(B)
}
B中除A(A) {
let value = this.A的集合.get(A)
if (value && value.size) value.forEach((value, B) => {
let valux = this.B的集合.get(B)
if (valux && valux.size) valux.delete(A)
})
this.A的集合.delete(A)
}
// 读取所有 a
// aall(uid, callback) {
// this.channels.forEach((value, key) => {
// callback(key)
// })
// }
//
// 读取所有 b
// ball(fid, callback) {
// this.users.forEach((value, key) => {
// callback(key)
// })
// }
}