-
Notifications
You must be signed in to change notification settings - Fork 213
/
demo.js
175 lines (158 loc) · 4.24 KB
/
demo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function Member(name) {
this.name = name;
this.friends = [];
}
var a = new Member('Alice'),
b = new Member('Bob'),
c = new Member('Carol'),
d = new Member('Dieter'),
e = new Member('Eli'),
f = new Member('Fatima');
a.friends.push(b);
b.friends.push(c);
c.friends.push(e);
d.friends.push(b);
e.friends.push(d, f);
// 上面的代码初步建立了一个社交网络
// 查找一个人的社交网络中是否包含另一个人
Member.prototype.inNetWork = function(other) {
// 访问过的人
var visited = {};
// 社交圈
var workset = {};
// 初始化社交圈子
workset[this.name] = this;
// 进行查找
for(var name in workset) {
// 获取圈子中的每一个人
var member = workset[name];
// 删除访问过的人
delete workset[name];
// 检查是否已经访问过了
if(name in visited) {
continue;
}
visited[name] = member;
// 找到相同的人
if(member === other) {
return true;
}
member.friends.forEach(function(friend) {
workset[friend.name] = friend;
})
}
return false;
};
console.log(e.inNetWork(d)); // false 结果与我们期望的相反
// 我们的字典类
function Dict(elements) {
this.elements = elements || [];
this.hasSpecialProto = false;
this.specialProto = undefined;
}
Dict.prototype.has = function(key) {
if('__proto__' === key) {
return this.hasSpecialProto;
}
return Object.hasOwnProperty.call(this.elements, key);
};
Dict.prototype.get = function(key) {
if('__proto__' === key) {
return this.specialProto;
}
return this.elements.has(key) ? this.elements[key] : undefined;
};
Dict.prototype.set = function(key, value) {
if('__proto__' === key) {
this.hasSpecialProto = true;
this.specialProto = value;
}
else {
this.elements[key] = value;
}
};
Dict.prototype.remove = function(key) {
if('__proto__' === key) {
this.hasSpecialProto = false;
this.specialProto = undefined;
}
else {
delete this.elements[key];
}
};
Dict.prototype.pick = function() {
for(var key in this.elements) {
if(this.has(key)) {
return key;
}
}
throw new Error('empty dictionary');
};
// 定义我们自己的WorkSet
function WorkSet() {
this.entries = new Dict();
this.count = 0;
}
WorkSet.prototype.isEmpty = function() {
return 0 === this.count;
};
WorkSet.prototype.add = function(key, val) {
if(this.entries.has(key)) {
return;
}
this.entries.set(key, val);
this.count++;
};
WorkSet.prototype.get = function(key) {
return this.entries.get(key);
};
WorkSet.prototype.remove = function(key) {
if(!this.entries.has(key)) {
return;
}
this.entries.remove(key);
this.count--;
};
// 上面的方法我感觉实在是太拖沓了,还是自己实现一个吧
function Member1(name) {
this.name = name;
this.friends = [];
}
var a1 = new Member('Alice'),
b1 = new Member('Bob'),
c1 = new Member('Carol'),
d1 = new Member('Dieter'),
e1 = new Member('Eli'),
f1 = new Member('Fatima');
a1.friends.push(b1, c1, d1, e1, f1);
b1.friends.push(a1, c1, d1, e1, f1);
c1.friends.push(a1, b1, d1, e1, f1);
d1.friends.push(a1, b1, c1, e1, f1);
e1.friends.push(a1, b1, d1, c1, f1);
f1.friends.push(a1, b1, c1, e1, d1);
Member.prototype.findMember = function(other) {
// 查找所有的社交成员
var allMyMembers = [];
// 查找成员
findMyMembers(this);
console.log(allMyMembers); // 输出所有成员
for(var i = 0; i < allMyMembers.length; i++) {
if(other.name === allMyMembers[i]) {
return true;
}
}
// 一个工具函数,又来查找当前对象的成员
function findMyMembers(my) {
if(-1 === allMyMembers.indexOf(my.name)) {
allMyMembers.push(my.name);
}
for(var i = 0; i < my.friends.length; i++) {
if(-1 == allMyMembers.indexOf(my.friends[i].name)) {
allMyMembers.push(my.friends[i].name);
findMyMembers(my.friends[i]);
}
}
}
return false;
};
console.log(f1.findMember(d1)); // true