2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_ARRAY_UTIL_H_
18 #define incl_HPHP_ARRAY_UTIL_H_
20 #include "hphp/runtime/base/type-variant.h"
21 #include "hphp/runtime/base/req-hash-set.h"
22 #include "hphp/util/functional.h" // for pointer_hash
24 #include "hphp/util/hdf.h"
27 ///////////////////////////////////////////////////////////////////////////////
32 * Utility array functions.
34 struct ArrayUtil final
{
35 /////////////////////////////////////////////////////////////////////////////
39 * Removes the elements designated by offset and length and replace them
40 * with supplied array.
42 static Variant
Splice(const Array
& input
, int offset
, int64_t length
= 0,
43 const Variant
& replacement
= uninit_variant
,
44 Array
*removed
= nullptr);
47 * Returns a copy of input array padded with pad_value to size pad_size.
49 static Variant
PadLeft(const Array
& input
, const Variant
& pad_value
,
51 static Variant
PadRight(const Array
& input
, const Variant
& pad_value
,
55 * Create an array containing the range of integers or characters from low
56 * to high (inclusive).
58 static Variant
Range(unsigned char low
, unsigned char high
, int64_t step
= 1);
59 static Variant
Range(double low
, double high
, double step
= 1.0);
60 static Variant
Range(int64_t low
, int64_t high
, int64_t step
= 1);
62 /////////////////////////////////////////////////////////////////////////////
63 // Information and calculations.
66 * Return the value as key and the frequency of that value in input
69 static Variant
CountValues(const Array
& input
);
71 /////////////////////////////////////////////////////////////////////////////
72 // Manipulations. Note, all these functions will create a new array than
73 // modifying input, although names of these functions sound like mutating.
76 * Retuns an array with all string keys lowercased [or uppercased].
78 static Variant
ChangeKeyCase(const Array
& input
, bool lower
);
81 * Return input as a new array with the order of the entries reversed.
83 static Variant
Reverse(const Array
& input
, bool preserve_keys
= false);
86 * Randomly shuffle the contents of an array.
88 static Variant
Shuffle(const Array
& input
);
91 * Return key/keys for random entry/entries in the array.
93 static Variant
RandomKeys(const Array
& input
, int num_req
= 1);
96 * Removes duplicate string values from array.
98 static Variant
StringUnique(const Array
& input
);
101 * Removes values whose numeric conversion is duplicate from array.
103 static Variant
NumericUnique(const Array
& input
);
106 * Removes values that compare as equal and that end up in contiguous
107 * positions if the input array is sorted.
109 static Variant
RegularSortUnique(const Array
& input
);
111 /////////////////////////////////////////////////////////////////////////////
114 using PointerSet
= req::fast_set
<const ArrayData
*,
115 pointer_hash
<const ArrayData
>>;
118 * Apply a user function to every member of an array.
120 using PFUNC_WALK
= void (*)(Variant
& value
, const Variant
& key
,
121 const Variant
& userdata
, const void *data
);
122 static void Walk(Variant
&input
, PFUNC_WALK walk_function
, const void *data
,
123 bool recursive
= false, PointerSet
*seen
= nullptr,
124 const Variant
& userdata
= uninit_variant
);
127 * Iteratively reduce the array to a single value via the callback.
129 using PFUNC_REDUCE
= Variant (*)(const Variant
& result
,
130 const Variant
& operand
,
132 static Variant
Reduce(const Array
& input
, PFUNC_REDUCE reduce_function
,
133 const void *data
, const Variant
& initial
= uninit_variant
);
136 ///////////////////////////////////////////////////////////////////////////////
139 #endif // incl_HPHP_ARRAY_UTIL_H_