-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy patharrays.js
172 lines (161 loc) · 5.65 KB
/
arrays.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
/**
* @namespace Contains various functions for manipulating arrays.
*/
buckets.arrays = {};
/**
* Returns the index of the first occurrence of the specified item
* within the specified array.
* @param {*} array The array.
* @param {*} item The element to search for.
* @param {function(Object,Object):boolean=} equalsFunction Optional function to
* check equality between two elements. Receives two arguments and returns true if they are equal.
* @return {number} The index of the first occurrence of the specified element
* or -1 if not found.
*/
buckets.arrays.indexOf = function (array, item, equalsFunction) {
var equals = equalsFunction || buckets.defaultEquals,
length = array.length,
i;
for (i = 0; i < length; i += 1) {
if (equals(array[i], item)) {
return i;
}
}
return -1;
};
/**
* Returns the index of the last occurrence of the specified element
* within the specified array.
* @param {*} array The array.
* @param {Object} item The element to search for.
* @param {function(Object,Object):boolean=} equalsFunction Optional function to
* check equality between two elements. Receives two arguments and returns true if they are equal.
* @return {number} The index of the last occurrence of the specified element
* within the specified array or -1 if not found.
*/
buckets.arrays.lastIndexOf = function (array, item, equalsFunction) {
var equals = equalsFunction || buckets.defaultEquals,
length = array.length,
i;
for (i = length - 1; i >= 0; i -= 1) {
if (equals(array[i], item)) {
return i;
}
}
return -1;
};
/**
* Returns true if the array contains the specified element.
* @param {*} array The array.
* @param {Object} item The element to search for.
* @param {function(Object,Object):boolean=} equalsFunction Optional function to
* check equality between two elements. Receives two arguments and returns true if they are equal.
* @return {boolean} True if the specified array contains the specified element.
*/
buckets.arrays.contains = function (array, item, equalsFunction) {
return buckets.arrays.indexOf(array, item, equalsFunction) >= 0;
};
/**
* Removes the first ocurrence of the specified element from the specified array.
* @param {*} array The array.
* @param {*} item The element to remove.
* @param {function(Object,Object):boolean=} equalsFunction Optional function to
* check equality between two elements. Receives two arguments and returns true if they are equal.
* @return {boolean} True If the array changed after this call.
*/
buckets.arrays.remove = function (array, item, equalsFunction) {
var index = buckets.arrays.indexOf(array, item, equalsFunction);
if (index < 0) {
return false;
}
array.splice(index, 1);
return true;
};
/**
* Returns the number of elements in the array equal
* to the specified element.
* @param {Array} array The array.
* @param {Object} item The element.
* @param {function(Object,Object):boolean=} equalsFunction Optional function to
* check equality between two elements. Receives two arguments and returns true if they are equal.
* @return {number} The number of elements in the specified array.
* equal to the specified item.
*/
buckets.arrays.frequency = function (array, item, equalsFunction) {
var equals = equalsFunction || buckets.defaultEquals,
length = array.length,
freq = 0,
i;
for (i = 0; i < length; i += 1) {
if (equals(array[i], item)) {
freq += 1;
}
}
return freq;
};
/**
* Returns true if the provided arrays are equal.
* Two arrays are considered equal if both contain the same number
* of elements and all corresponding pairs of elements
* are equal and are in the same order.
* @param {Array} array1
* @param {Array} array2
* @param {function(Object,Object):boolean=} equalsFunction Optional function to
* check equality between two elements. Receives two arguments and returns true if they are equal.
* @return {boolean} True if the two arrays are equal.
*/
buckets.arrays.equals = function (array1, array2, equalsFunction) {
var equals = equalsFunction || buckets.defaultEquals,
length = array1.length,
i;
if (array1.length !== array2.length) {
return false;
}
for (i = 0; i < length; i += 1) {
if (!equals(array1[i], array2[i])) {
return false;
}
}
return true;
};
/**
* Returns a shallow copy of the specified array.
* @param {*} array The array to copy.
* @return {Array} A copy of the specified array.
*/
buckets.arrays.copy = function (array) {
return array.concat();
};
/**
* Swaps the elements at the specified positions in the specified array.
* @param {Array} array The array.
* @param {number} i The index of the first element.
* @param {number} j The index of second element.
* @return {boolean} True if the array is defined and the indexes are valid.
*/
buckets.arrays.swap = function (array, i, j) {
var temp;
if (i < 0 || i >= array.length || j < 0 || j >= array.length) {
return false;
}
temp = array[i];
array[i] = array[j];
array[j] = temp;
return true;
};
/**
* Executes the provided function once per element present in the array.
* @param {Array} array The array.
* @param {function(Object):*} callback Function to execute,
* invoked with an element as argument. To break the iteration you can
* optionally return false in the callback.
*/
buckets.arrays.forEach = function (array, callback) {
var lenght = array.length,
i;
for (i = 0; i < lenght; i += 1) {
if (callback(array[i]) === false) {
return;
}
}
};