Skip to content

Commit

Permalink
move array *_key and *_value functions into runtime-common (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolyakov authored Mar 1, 2025
1 parent 9542138 commit bd837af
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 58 deletions.
18 changes: 12 additions & 6 deletions builtin-functions/kphp-light/array.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ function array_pop (&$a ::: array) ::: ^1[*];

function array_slice ($a ::: array, $offset ::: int, $length = null, $preserve_keys ::: bool = false) ::: ^1;

function array_first_key ($a ::: array): mixed;

function array_key_first($a ::: array): mixed;

function array_first_value ($a ::: array) ::: ^1[*];

function array_last_key ($a ::: array): mixed;

function array_key_last($a ::: array): mixed;

function array_last_value ($a ::: array) ::: ^1[*];

/** @kphp-extern-func-info cpp_template_call */
function array_replace ($base_array ::: array,
$replacements_1 ::: array = array(), $replacements_2 ::: array = array(), $replacements_3 ::: array = array(),
Expand All @@ -61,14 +73,8 @@ function array_replace ($base_array ::: array,
$replacements_10 ::: array = array(), $replacements_11 ::: array = array())
::: ^1 | ^2 | ^3 | ^4 | ^5 | ^6 | ^7 | ^8 | ^9 | ^10 | ^11 | ^12;

function array_last_value ($a ::: array) ::: ^1[*];

function array_last_key ($a ::: array) ::: mixed;

function array_flip ($a ::: array) ::: mixed[];

function array_first_value ($a ::: array) ::: ^1[*];

/** @kphp-extern-func-info interruptible */
function array_filter ($a ::: array, callable(^1[*] $x):bool $callback = TODO) ::: ^1;

Expand Down
3 changes: 0 additions & 3 deletions builtin-functions/kphp-light/unsupported/arrays.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ function create_vector ($n ::: int, $x ::: any) ::: ^2[];
function getKeyByPos ($a ::: array, $n ::: int) ::: mixed;
function getValueByPos ($a ::: array, $n ::: int) ::: ^1[*];

function array_first_key ($a ::: array) ::: mixed;
function array_key_first($a ::: array): mixed;
function array_key_last($a ::: array): mixed;
function array_swap_int_keys (&$a ::: array, $idx1 ::: int, $idx2 ::: int) ::: void;

function array_splice (&$a ::: array, $offset ::: int, $length ::: int, $replacement ::: array = array()) ::: ^1;
Expand Down
45 changes: 30 additions & 15 deletions runtime-common/stdlib/array/array-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ struct rsort_compare_string {

} // namespace array_functions_impl_

template<class T>
mixed f$array_first_key(const array<T> &a) noexcept {
return a.empty() ? mixed{} : a.begin().get_key();
}

template<class T>
mixed f$array_key_first(const array<T> &a) noexcept {
return f$array_first_key(a);
}

template<class T>
T f$array_first_value(const array<T> &a) noexcept {
return a.empty() ? T{} : a.begin().get_value(); // in PHP 'false' on empty, here T()
}

template<class T>
mixed f$array_last_key(const array<T> &a) noexcept {
return a.empty() ? mixed{} : (--a.end()).get_key();
}

template<class T>
mixed f$array_key_last(const array<T> &a) noexcept {
return f$array_last_key(a);
}

template<class T>
T f$array_last_value(const array<T> &a) noexcept {
return a.empty() ? T{} : (--a.end()).get_value(); // in PHP 'false' on empty, here T()
}

template<class T>
string f$implode(const string &s, const array<T> &a) noexcept {
int64_t count = a.count();
Expand Down Expand Up @@ -412,11 +442,6 @@ array<T> f$array_diff(const array<T> &a1, const array<T1> &a2, const array<T2> &
return f$array_diff(f$array_diff(a1, a2), a3);
}

template<class T>
T f$array_first_value(const array<T> &a) noexcept {
return a.empty() ? T() : a.begin().get_value(); // in PHP 'false' on empty, here T()
}

template<class T>
array<typename array<T>::key_type> f$array_flip(const array<T> &a) noexcept {
static_assert(!std::is_same<T, int>{}, "int is forbidden");
Expand All @@ -434,16 +459,6 @@ array<typename array<T>::key_type> f$array_flip(const array<T> &a) noexcept {
return result;
}

template<class T>
mixed f$array_last_key(const array<T> &a) noexcept {
return a.empty() ? mixed() : (--a.end()).get_key();
}

template<class T>
T f$array_last_value(const array<T> &a) noexcept {
return a.empty() ? T() : (--a.end()).get_value(); // in PHP 'false' on empty, here T()
}

template<class T>
T f$array_replace(const T &base_array, const T &replacements = T()) noexcept {
auto result = T::convert_from(base_array);
Expand Down
15 changes: 0 additions & 15 deletions runtime-light/stdlib/array/array-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,6 @@ array<T> f$create_vector(int64_t /*unused*/, const T & /*unused*/) {
php_critical_error("call to unsupported function");
}

template<class T>
mixed f$array_first_key(const array<T> & /*unused*/) {
php_critical_error("call to unsupported function");
}

template<class T>
void f$array_swap_int_keys(array<T> & /*unused*/, int64_t /*unused*/, int64_t /*unused*/) noexcept {
php_critical_error("call to unsupported function");
Expand Down Expand Up @@ -493,16 +488,6 @@ auto f$array_column(const Optional<T> & /*unused*/, const mixed &column_key,
php_critical_error("call to unsupported function");
}

template<class T>
mixed f$array_key_first(const array<T> & /*unused*/) {
php_critical_error("call to unsupported function");
}

template<class T>
mixed f$array_key_last(const array<T> & /*unused*/) {
php_critical_error("call to unsupported function");
}

template<class T, class T1>
std::tuple<typename array<T>::key_type, T> f$array_find(const array<T> & /*unused*/, const T1 & /*unused*/) {
php_critical_error("call to unsupported function");
Expand Down
18 changes: 0 additions & 18 deletions runtime/array_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ T f$getValueByPos(const array<T> &a, int64_t pos);
template<class T>
inline array<T> f$create_vector(int64_t n, const T &default_value);

template<class T>
mixed f$array_first_key(const array<T> &a);

template<class T>
inline void f$array_swap_int_keys(array<T> &a, int64_t idx1, int64_t idx2) noexcept;

Expand Down Expand Up @@ -627,21 +624,6 @@ array<T> f$create_vector(int64_t n, const T &default_value) {
return res;
}

template<class T>
mixed f$array_first_key(const array<T> &a) {
return a.empty() ? mixed() : a.begin().get_key();
}

template<class T>
mixed f$array_key_first(const array<T> &a) {
return f$array_first_key(a);
}

template<class T>
mixed f$array_key_last(const array<T> &a) {
return f$array_last_key(a);
}

template<class T>
void f$array_swap_int_keys(array<T> &a, int64_t idx1, int64_t idx2) noexcept {
a.swap_int_keys(idx1, idx2);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/1016_array_first_key.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php
require_once 'kphp_tester_include.php';

Expand Down

0 comments on commit bd837af

Please sign in to comment.