Skip to content

Commit

Permalink
Tighten specification for _.sortedIndex
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kevinoid committed Apr 19, 2012
1 parent 67706cf commit 06c773b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/underscore.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 06c773b

Please sign in to comment.