4 * Copyright (c) 2004-present, Facebook, Inc.
7 * This source code is licensed under the MIT license found in the
8 * LICENSE file in the hphp/hsl/ subdirectory of this source tree.
14 use namespace HH\Lib\Vec
;
17 * Returns true if the given Traversable<Tv> is sorted in ascending order.
18 * If two neighbouring elements compare equal, this will be considered sorted.
20 * If no $comparator is provided, the `<=>` operator will be used.
21 * This will sort numbers by value, strings by alphabetical order
22 * or by the numeric value, if the strings are well-formed numbers,
23 * and DateTime/DateTimeImmutable by their unixtime. If the comparison
24 * operator `<=>` is not useful on Tv and no $comparator is provided,
25 * the result of is_sorted will not be useful.
27 * When specified, a comparator should exhibit the same behavior as the `<=>`
28 * operator: a negative value when the left operand compares less than the
29 * right operand, 0 when the operands compare equal, and a positive value when
30 * the left operand compares greater than the right operand.
32 * To check the order of other types or mixtures of the
33 * aforementioned types, see C\is_sorted_by.
35 * Time complexity: O((n * c), where c is the complexity of the
36 * comparator function (which is O(1) if not provided explicitly)
37 * Space complexity: O(n)
39 function is_sorted
<Tv
>(
40 Traversable
<Tv
> $traversable,
41 ?
(function(Tv
, Tv
)[_
]: num
) $comparator = null,
42 )[ctx
$comparator]: bool {
43 $vec = Vec\
cast_clear_legacy_array_mark($traversable);
48 $comparator ??
= (Tv
$a, Tv
$b) ==>
49 /*HH_FIXME[4240] Comparison may not be useful on Tv*/$a <=> $b;
51 $previous = firstx($vec);
52 foreach ($vec as $next) {
53 if ($comparator($next, $previous) < 0) {
63 * Returns true if the given Traversable<Tv> would be sorted in ascending order
64 * after having been `Vec\map`ed with $scalar_func sorted in ascending order.
65 * If two neighbouring elements compare equal, this will be considered sorted.
67 * If no $comparator is provided, the `<=>` operator will be used.
68 * This will sort numbers by value, strings by alphabetical order
69 * or by the numeric value, if the strings are well-formed numbers,
70 * and DateTime/DateTimeImmutable by their unixtime. If the comparison
71 * operator `<=>` is not useful on Ts and no $comparator is provided, the
72 * result of is_sorted_by will not be useful.
74 * When specified, a comparator should exhibit the same behavior as the `<=>`
75 * operator: a negative value when the left operand compares less than the
76 * right operand, 0 when the operands compare equal, and a positive value when
77 * the left operand compares greater than the right operand.
79 * To check the order without a mapping function,
82 * Time complexity: O((n * c), where c is the complexity of the
83 * comparator function (which is O(1) if not provided explicitly)
84 * Space complexity: O(n)
86 function is_sorted_by
<Tv
, Ts
>(
87 Traversable
<Tv
> $traversable,
88 (function(Tv
)[_
]: Ts
) $scalar_func,
89 ?
(function(Ts
, Ts
)[_
]: num
) $comparator = null,
90 )[ctx
$scalar_func, ctx
$comparator]: bool {
91 $vec = Vec\
cast_clear_legacy_array_mark($traversable);
96 $comparator ??
= (Ts
$a, Ts
$b) ==>
97 /*HH_FIXME[4240] Comparison may not be useful on Ts*/$a <=> $b;
99 $previous = $scalar_func(firstx($vec));
100 foreach ($vec as $next) {
101 $next = $scalar_func($next);
102 if ($comparator($next, $previous) < 0) {