Skip to content

Commit 2e7356c

Browse files
committed
feat(_array): add sortedIndex, docblocks
1 parent f149513 commit 2e7356c

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

src/UnderscoreArray.php

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function first($n = 1)
1818

1919
/**
2020
* Alias of first().
21+
*
22+
* @param int $n
23+
*
24+
* @return array
2125
*/
2226
public function head($n = 1)
2327
{
@@ -26,6 +30,10 @@ public function head($n = 1)
2630

2731
/**
2832
* Alias of first().
33+
*
34+
* @param int $n
35+
*
36+
* @return array
2937
*/
3038
public function take($n = 1)
3139
{
@@ -46,6 +54,10 @@ public function last($n = 1)
4654

4755
/**
4856
* Alias of last().
57+
*
58+
* @param int $n
59+
*
60+
* @return array
4961
*/
5062
public function tail($n = 1)
5163
{
@@ -54,6 +66,10 @@ public function tail($n = 1)
5466

5567
/**
5668
* Alias of last().
69+
*
70+
* @param int $n
71+
*
72+
* @return array
5773
*/
5874
public function drop($n = 1)
5975
{
@@ -126,6 +142,10 @@ public function unique($fn = null)
126142

127143
/**
128144
* Alias of unique().
145+
*
146+
* @param callback|string $fn The callback. String is resolved to value of that index.
147+
*
148+
* @return self
129149
*/
130150
public function uniq($fn = null)
131151
{
@@ -149,7 +169,11 @@ public function difference($data)
149169
}
150170

151171
/**
152-
* Alias of without().
172+
* Alias of difference().
173+
*
174+
* @param array|mixed $data Array or array like or array convertible.
175+
*
176+
* @return self
153177
*/
154178
public function without($data)
155179
{
@@ -203,7 +227,7 @@ public function zip($data)
203227
/**
204228
* Hydrate the items into given class or stdClass.
205229
*
206-
* @param string $className FQCN of the class whose constructor accepts two parameters: value and index.
230+
* @param string|null $className FQCN of the class whose constructor accepts two parameters: value and index.
207231
*
208232
* @return self
209233
*/
@@ -221,19 +245,19 @@ public function object($className = null)
221245
*
222246
* @return mixed|null
223247
*/
224-
public function firstIndex($fn = null)
248+
public function findIndex($fn = null)
225249
{
226250
return $this->find($this->valueFn($fn), false);
227251
}
228252

229253
/**
230-
* Find the larst index that passes given truth test.
254+
* Find the last index that passes given truth test.
231255
*
232256
* @param callable $fn The truth test callback.
233257
*
234258
* @return mixed|null
235259
*/
236-
public function lastIndex($fn = null)
260+
public function findLastIndex($fn = null)
237261
{
238262
return (new static(\array_reverse($this->data, true)))->find($this->valueFn($fn), false);
239263
}
@@ -262,6 +286,37 @@ public function lastIndexOf($value)
262286
return (false === $index = \array_search($value, \array_reverse($this->data, true))) ? null : $index;
263287
}
264288

289+
/**
290+
* Gets the smallest index at which an object should be inserted so as to maintain order.
291+
*
292+
* Note that the initial stack must be sorted already.
293+
*
294+
* @param $object The new object which needs to be adjusted in stack.
295+
* @param callback|string $fn The comparator callback.
296+
*
297+
* @return string|int|null
298+
*/
299+
public function sortedIndex($object, $fn)
300+
{
301+
$low = 0;
302+
$high = $this->count();
303+
$data = $this->values();
304+
$fn = $this->valueFn($fn);
305+
$value = $fn($object);
306+
$keys = $this->keys();
307+
308+
while ($low < $high) {
309+
$mid = \intval(($low + $high) / 2);
310+
if ($fn($data[$mid]) < $value) {
311+
$low = $mid + 1;
312+
} else {
313+
$high = $mid;
314+
}
315+
}
316+
317+
return isset($keys[$low]) ? $keys[$low] : null;
318+
}
319+
265320
/**
266321
* Creates a new range from start to stop with given step.
267322
*

0 commit comments

Comments
 (0)