Skip to content

Commit 5dc0341

Browse files
Added api annotations for binary search
1 parent b4d6748 commit 5dc0341

File tree

2 files changed

+1728
-1600
lines changed

2 files changed

+1728
-1600
lines changed
Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,75 @@
1-
/*
2-
Function: primitives.common.binarySearch
3-
Search sorted list of elements for nearest item.
4-
5-
Parameters:
6-
items - Array of elements.
7-
funcDistance - Call back function used to get ditance for current item.
1+
/**
2+
* Callback for finding distance for a collection item
3+
*
4+
* @callback funcDistance
5+
* @param {Object} item A collection item
6+
* @param {number} index An index of the collection item
7+
* @returns {number} Returns a distance for the item
8+
*/
89

9-
Returns:
10-
Nearest item.
10+
/**
11+
* @typedef {Object} BinarySearchResult
12+
* @property {number} index The index of the nearest item in the collection
13+
* @property {Object} item The nearest item
1114
*/
12-
primitives.common.binarySearch = function (items, funcDistance, startMinimum, startMaximum) {
13-
var result = null,
14-
distance,
15-
bestDistance,
16-
minimum = startMinimum || 0,
17-
maximum = startMaximum || (items.length - 1),
18-
middle,
19-
item;
2015

21-
if (items.length > 0) {
22-
item = items[minimum];
23-
result = { index: minimum, item: item };
24-
distance = funcDistance(item, minimum);
25-
if (distance > 0) {
26-
bestDistance = Math.abs(distance);
16+
/**
17+
* Search sorted list of elements for the nearest item.
18+
*
19+
* @param {Object[]} items - The collection of elements.
20+
* @param {funcDistance} callback - A callback function to get distance for the collection item.
21+
* @param {number} [startMinimum=undefined] - The minimum index in the array to start search from
22+
* @param {number} [startMaximum=undefined] - The maximum index in the array to start search from
23+
* @returns {BinarySearchResult} Returns an item of the collection, which is nearest to optimal measured by callback function
24+
*/
25+
primitives.common.binarySearch = function (items, callback, startMinimum, startMaximum) {
26+
var result = null,
27+
distance,
28+
bestDistance,
29+
minimum = startMinimum || 0,
30+
maximum = startMaximum || (items.length - 1),
31+
middle,
32+
item;
2733

28-
item = items[maximum];
29-
distance = funcDistance(item, maximum);
30-
if (distance >= 0) {
31-
result = { index: maximum, item: item };
32-
} else {
33-
distance = Math.abs(distance);
34-
if (bestDistance > distance) {
35-
bestDistance = distance;
36-
result = { index: maximum, item: item };
37-
}
38-
while (minimum + 1 < maximum) {
39-
middle = Math.round((minimum + maximum) / 2.0);
40-
item = items[middle];
41-
distance = funcDistance(item, middle);
42-
if (distance === 0) {
43-
result = { index: middle, item: item };
44-
break;
45-
} else {
46-
if (distance > 0) {
47-
minimum = middle;
48-
} else {
49-
maximum = middle;
50-
}
51-
distance = Math.abs(distance);
52-
if (bestDistance > distance) {
53-
bestDistance = distance;
54-
result = { index: middle, item: item };
55-
}
56-
}
57-
}
58-
}
59-
}
60-
}
61-
return result;
62-
};
34+
if (items.length > 0) {
35+
item = items[minimum];
36+
result = { index: minimum, item: item };
37+
distance = callback(item, minimum);
38+
if (distance > 0) {
39+
bestDistance = Math.abs(distance);
6340

41+
item = items[maximum];
42+
distance = callback(item, maximum);
43+
if (distance >= 0) {
44+
result = { index: maximum, item: item };
45+
} else {
46+
distance = Math.abs(distance);
47+
if (bestDistance > distance) {
48+
bestDistance = distance;
49+
result = { index: maximum, item: item };
50+
}
51+
while (minimum + 1 < maximum) {
52+
middle = Math.round((minimum + maximum) / 2.0);
53+
item = items[middle];
54+
distance = callback(item, middle);
55+
if (distance === 0) {
56+
result = { index: middle, item: item };
57+
break;
58+
} else {
59+
if (distance > 0) {
60+
minimum = middle;
61+
} else {
62+
maximum = middle;
63+
}
64+
distance = Math.abs(distance);
65+
if (bestDistance > distance) {
66+
bestDistance = distance;
67+
result = { index: middle, item: item };
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
return result;
75+
};

0 commit comments

Comments
 (0)