-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetList.js
98 lines (96 loc) · 1.93 KB
/
SetList.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
const {listen} = require("express/lib/application");
class SetList extends Set{
constructor(lst=null) {
super()
if(lst!=null)
lst.forEach(x=>this.add(x));
}
remove(x){
this.delete(x)
}
addAll(l){
l.forEach(x=>this.add(x));
}
removeAll(l){
l.forEach(x=>this.remove(x))
}
sort(f){
return Array.from([...this]).sort(f);
}
filter(f){
return new SetList([...this].filter(f));
}
forEach(f){
return [...this].forEach(f);
}
length(){
return this.size;
}
toList(){
return Array.from([...this]);
}
toSet(){
return new Set([...this]);
}
clear(){
[...this].clear();
}
values(){
return [...this].values();
}
slice(a,b,f=null){
var l=Array.from([...this]);
if(f!=null)
l=l.sort(f);
return l.slice(a,b);
}
replace(e1,e2){
this.remove(e1);
this.add(e2);
}
map(f){
return Array.from([...this]).map(f);
}
find(f){
var l=this.filter(f);
if(l.length==0)
return null;
return l[0];
}
findIndex(x){
var i=0;
while(i<this.length()){
if(this.get(i)==x)
return(i)
i++;
}
return -1;
}
get(i,f=null){
var lst=null;
if(f!=null)
lst=this.sort(f);
else
lst=this.toList();
return lst[i];
}
pickRandom(remove=false){
var v=Math.floor(Math.random() * this.length());
v=this.get(v);
if(remove)
this.delete(v);
return v;
}
deleteWhere(f){
this.filter(f).forEach(x=>this.delete(x));
return this;
}
}
class Person{
constructor(name,age,gender) {
this.name=name;
this.age=age;
this.gender=gender;
}
}
module.exports=SetList;