This repository was archived by the owner on Jul 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsorted-array.js
112 lines (100 loc) · 3.81 KB
/
sorted-array.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
var SortedArray = (function () {
var SortedArray = defclass({
constructor: function (array, compare) {
this.array = [];
this.compare = compare || compareDefault;
var length = array.length,
index = 0;
while (index < length) this.insert(array[index++]);
},
insert: function (element) {
var array = this.array,
compare = this.compare,
high = array.length-1,
low = 0,
pos = -1,
index,
ordering;
// The array is sorted. You must find the position of new element in O(log(n)), not O(n).
while (high >= low) {
index = (high + low) / 2 >>> 0;
ordering = compare(array[index], element);
if (ordering < 0) low = index + 1;
else if (ordering > 0) high = index - 1;
else {
pos = index;
break;
};
}
if (pos === -1) {
// if element was not found, high < low.
pos = high;
}
// This assures that equal elements inserted after will be in a higher position in array.
// They can be equal for comparison purposes, but different objects with different data.
// Respecting the chronological order can be important for many applications.
pos++;
high = array.length-1;
while ((pos < high) && (compare(element, array[pos]) === 0)){
pos++;
}
index = array.length;
// Just to increase array size.
array.push(element);
// Much faster. No need to elements swap.
while (index > pos) {
array[index] = array[--index];
}
// Set the new element on its correct position.
array[pos] = element;
return this;
},
search: function (element) {
var array = this.array,
compare = this.compare,
high = array.length-1,
low = 0,
// In most languages, inner variable declaration makes the code slower.
index,
ordering;
while (high >= low) {
index = (high + low) / 2 >>> 0;
ordering = compare(array[index], element);
if (ordering < 0) low = index + 1;
else if (ordering > 0) high = index - 1;
else return index;
}
return -1;
},
remove: function (element) {
var index = this.search(element);
if (index >= 0) this.array.splice(index, 1);
return this;
}
});
SortedArray.comparing = function (property, array) {
return new SortedArray(array, function (a, b) {
// This should be faster than calling functions.
// Besides, this way it is not needed to create useless function to return property value.
return compareDefault(a[property], b[property]);
});
};
return SortedArray;
function defclass(prototype) {
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
function compareDefault(a, b) {
// Equality has a very low chance to happen. It should be the last option.
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
}());
if (typeof module === "object") module.exports = SortedArray;
if (typeof define === "function" && define.amd)
define(function () { return SortedArray; });