Kwker

PHP API reference

Every public function of the kwker PHP extension and its Composer package, which have the same functions, with its parameters and results (Kwker 0.1.0).

Constants

KWKER_DESC Page

PHPLoad the kwker extension (or the Composer package).
const KWKER_DESC = 1;

Flag: largest first (descending order). NaNs stay last unless KWKER_NANS_FIRST is also given.

KWKER_NANS_FIRST Page

PHPLoad the kwker extension (or the Composer package).
const KWKER_NANS_FIRST = 2;

Flag: NaN values first instead of last (float arrays). Combine flags with |.

Functions

kwker_sort Page

PHPLoad the kwker extension (or the Composer package).
function kwker_sort(array &$a, int $flags = 0): bool;

Sorts an array of ints or floats in place, smallest first. The array becomes a list (keys 0..n-1).

Arguments

Returns

True

Example

PHPLoad the kwker extension (or the Composer package).
$a = [3, 1, 2]; kwker_sort($a); // [1, 2, 3]

Remarks: Not stable (rule 5): keys that are equal but can be told apart (NaNs with different bits) may change places. Keys follow the key order: -0.0 before +0.0, every NaN in one block, last unless NaNs-first is asked for.

kwker_select Page

PHPLoad the kwker extension (or the Composer package).
function kwker_select(array &$a, int $k, int $flags = 0): bool;

Puts the value a full sort would put at index k into $a[k], smaller or equal values before it and larger or equal values after it. With k = intdiv(count($a), 2) that is the median.

Arguments

Returns

True

Example

PHPLoad the kwker extension (or the Composer package).
$a = [50, 10, 40, 20, 30]; kwker_select($a, 2); // $a[2] is 30

Notes

Remarks: Position k holds the key a full sort puts there; the keys before it are ordered before or equal to it, the keys after it after or equal (rule 7). Keys follow the key order: -0.0 before +0.0, every NaN in one block, last unless NaNs-first is asked for.

kwker_partial_sort Page

PHPLoad the kwker extension (or the Composer package).
function kwker_partial_sort(array &$a, int $k, int $flags = 0): bool;

Puts the k smallest values, sorted, at the front of the array.

Arguments

Returns

True

Notes

Remarks: The first k positions hold exactly what a full sort puts there; the rest hold the other keys in any order (rule 8). Keys follow the key order: -0.0 before +0.0, every NaN in one block, last unless NaNs-first is asked for.

kwker_argsort Page

PHPLoad the kwker extension (or the Composer package).
function kwker_argsort(array $a, int $flags = 0): array;

Returns the positions (0-based) that sort the array. Equal values keep their input order; the array is not changed.

Arguments

Returns

A list of ints, one position per value

Example

PHPLoad the kwker extension (or the Composer package).
kwker_argsort([30, 10, 20]); // [1, 2, 0]

Remarks: Stable (rule 6): equal keys keep their input order, so the same input always gives the same positions. Keys follow the key order: -0.0 before +0.0, every NaN in one block, last unless NaNs-first is asked for.

kwker_top_k Page

PHPLoad the kwker extension (or the Composer package).
function kwker_top_k(array $a, int $k, int $flags = 0): array;

Returns the k smallest values in order with their positions (KWKER_DESC: the k largest).

Arguments

Returns

['values' => list, 'indices' => list of ints]; equal values go to the earlier position first

Example

PHPLoad the kwker extension (or the Composer package).
kwker_top_k([12, 7, 30, 18], 2, KWKER_DESC); // values [30, 18], indices [2, 3]

Remarks: The first k keys of the stable order and their positions; equal keys keep their input order (rule 9). Keys follow the key order: -0.0 before +0.0, every NaN in one block, last unless NaNs-first is asked for.

kwker_isa Page

PHPLoad the kwker extension (or the Composer package).
function kwker_isa(): string;

The engine in use: "avx512", "avx2", "sse42", "neon", "sve" or "portable".

Remarks: The engine changes only the speed, never a result (rule 11).

kwker_version Page

PHPLoad the kwker extension (or the Composer package).
function kwker_version(): string;

The Kwker C library's version, for example "0.1.0".

Applies to Kwker 0.1 · PHP
Last updated
Was this page helpful?
Kwker 0.1.x: the engines each platform chooses from at run time (details)
PlatformEngines
Linux x86-64AVX-512, AVX2, SSE4.2, portable
Linux ARM64SVE / SVE2 (64-bit keys), NEON, portable
Windows x64AVX-512, AVX2, SSE4.2, portable
Windows ARM64NEON, portable
macOS ARM64NEON, portable
macOS x86-64AVX2, SSE4.2, portable
Other CPUs (RISC-V, POWER, x86 without SSE4.2, ...)portable