Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jashkenas/underscore
base: f10180b7f5
...
head fork: jashkenas/underscore
compare: 96b9a8dfd0
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Commits on Apr 19, 2012
@kevinoid kevinoid Tighten specification for _.sortedIndex
sortedIndex should specify that it returns the lowest index at which an
item may be inserted to maintain sorted order, rather than any index
which satisfies this property, in order to provide easier use
maintaining a sorted list of unique elements.

For example, with the previous specification, the following behavior is
possible:

_.sortedIndex([10, 20, 30, 40], 30)
=> 3

To accommodate this behavior users would have to check both array[si]
and array[si-1] (when si > 0) before inserting to ensure uniqueness, or
to examine the underscore.js implementation and rely on the
implementation not changing.

Note:  This specification change does not require any changes to the
code.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
06c773b
@kevinoid kevinoid Add test for tightened sortedIndex specification
Test that the lowest index, rather than any index, at which the element
should be inserted to maintain sorted order is returned.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
1aaeb41
@jashkenas Merge pull request #563 from kevinoid/sorted-index-spec
Tighten specification for _.sortedIndex
96b9a8d
Showing with 7 additions and 4 deletions.
  1. +2 −2 docs/underscore.html
  2. +3 −0  test/collections.js
  3. +2 −2 underscore.js
View
4 docs/underscore.html
@@ -198,8 +198,8 @@
<span class="p">(</span><span class="nx">result</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">||</span> <span class="p">(</span><span class="nx">result</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])).</span><span class="nx">push</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
<span class="p">});</span>
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Use a comparator function to figure out at what index an object should
-be inserted so as to maintain order. Uses binary search.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">sortedIndex</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">)</span> <span class="p">{</span>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Use a comparator function to figure out the smallest index at which
+an object should be inserted so as to maintain order. Uses binary search.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">sortedIndex</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">iterator</span> <span class="o">||</span> <span class="p">(</span><span class="nx">iterator</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">low</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">high</span> <span class="o">=</span> <span class="nx">array</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="nx">low</span> <span class="o">&lt;</span> <span class="nx">high</span><span class="p">)</span> <span class="p">{</span>
View
3  test/collections.js
@@ -260,6 +260,9 @@ $(document).ready(function() {
var numbers = [10, 20, 30, 40, 50], num = 35;
var index = _.sortedIndex(numbers, num);
equal(index, 3, '35 should be inserted at index 3');
+
+ var index2 = _.sortedIndex(numbers, 30);
+ equal(index2, 2, '30 should be inserted at index 2');
});
test('collections: shuffle', function() {
View
4 underscore.js
@@ -285,8 +285,8 @@
return result;
};
- // Use a comparator function to figure out at what index an object should
- // be inserted so as to maintain order. Uses binary search.
+ // Use a comparator function to figure out the smallest index at which
+ // an object should be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iterator) {
iterator || (iterator = _.identity);
var low = 0, high = array.length;

No commit comments for this range

Something went wrong with that request. Please try again.