-
Notifications
You must be signed in to change notification settings - Fork 0
/
exarray.js
140 lines (139 loc) · 3.06 KB
/
exarray.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
Array.prototype.min = function(){
var ret=this[0];
for(var i=1;i<this.length;i++){
ret = ret<this[i]?ret:this[i];
}
return ret;
}
Array.prototype.max = function(){
var ret=this[0];
for(var i=1;i<this.length;i++){
ret = ret>this[i]?ret:this[i];
}
return ret;
}
Array.prototype.argmax = function(){
var ret=0;
for(var i=1;i<this.length;i++){
ret = this[ret]>this[i]?ret:i;
}
return ret;
}
Array.prototype.argmin = function(){
var ret=0;
for(var i=1;i<this.length;i++){
ret = this[ret]<this[i]?ret:i;
}
return ret;
}
Array.prototype.sum = function(){
var ret = this[0];
for(var i=1;i<this.length;i++){
ret += this[i]
}
return ret;
}
Array.prototype.prod = function(){
var ret = this[0];
for(var i=1;i<this.length;i++){
ret *= this[i]
}
return ret;
}
Array.prototype.mean = function(){
return this.sum()/this.length;
}
Array.prototype.clone = function(){
if ( this[0].constructor == Array ) {
var ar, n;
ar = new Array( this.length );
for ( n = 0; n < ar.length; n++ ) {
ar[n] = this[n].clone();
}
return ar;
}
return [].concat(this);
}
/* Value of multidimensional array
with index array i[d]
this function returns a[i[0]][i[1]][i[2]].
ex.
[[1,2,3],[4,5,6],[7,8,9]].at([2,1])=8;
[[1,2,3],[4,5,6],[7,8,9]].at([2])=[7,8,9];
*/
Array.prototype.at = function(i){
var b = this;
for(var d=0;d<i.length;d++) b = b[i[d]];
return b;
}
/*
*/
Array.zeros = function(s){
var f=function(s, d){
var a = new Array(s[d]);
if(d==s.length-1){
for(var i=0;i<s[d];i++) a[i] = 0;
}else{
for(var i=0;i<s[d];i++) a[i] = f(s,d+1);
}
return a;
};
return f(s,0);
}
Array.prototype.toString = function(){
var s="[";
var i=0;
for(i=0;i<this.length-1;i++){
s+=this[i].toString()+", ";
}
if(this.length==0){
s+="]";
}else{
s+=this[i].toString()+"]";
}
return s;
}
Array.prototype.isEqual = function(a){
if(this.length!=a.length) return false;
for(var i=0;i<this.length;i++){
if(this[i] instanceof Array){
if(!(a[i] instanceof Array)) return false;
if(!this[i].isEqual(a[i])) return false;
}else{
if(this[i]!=a[i]) return false;
}
}
return true;
}
/* -----------------------------------------
push uniquely
i = a.pushUniquely(e);
if e is already member of a:
push e into a if e is not member of a,
and return the index of e in new a.
else
and return the index of e in a.
usage:
x=[2,3,4];
i=x.pushUniquely(10);
// x = [2,3,4,10];
// i = 3;
x=[2,3,4];
i=x.pushUniquely(3);
// x = [2,3,4];
// i = 1;
-------------------------------------------- */
Array.prototype.pushUniquely = function(e){
for(var i=0;i<this.length;i++){
var ae = this[i];
if(ae instanceof Array && e instanceof Array){
if(ae.isEqual(e)) return i;
}else{
if(ae==e) return i;
}
}
return this.push(e)-1;
}
Array.prototype.randomPop = function(){
return this.splice(Math.floor(Math.random()*this.length),1)[0];
}