class Judy implements ArrayAccess, Countable, Iterator, JsonSerializable (View source)

Judy arrays are fast, memory-efficient, ordered sparse dynamic arrays.

A Judy object can be accessed like a PHP array and iterated with foreach.

Constants

BITSET

Judy array as a bitset, with integer keys and boolean values.

INT_TO_INT

Judy array with integer keys and integer values.

INT_TO_MIXED

Judy array with integer keys and values of any type.

STRING_TO_INT

Judy array with string keys and integer values (sorted, trie-based).

STRING_TO_MIXED

Judy array with string keys and values of any type (sorted, trie-based).

INT_TO_PACKED

Judy array with integer keys and packed integer values.

STRING_TO_MIXED_HASH

Judy array with string keys and values of any type (unsorted, hash-based).

STRING_TO_INT_HASH

Judy array with string keys and integer values (unsorted, hash-based).

STRING_TO_MIXED_ADAPTIVE

Judy array with string keys and values of any type (adaptive storage).

STRING_TO_INT_ADAPTIVE

Judy array with string keys and integer values (adaptive storage).

Methods

__construct(int $type, bool $optimizeIteration = false)

Create a new Judy array of the specified type.

__destruct()

Free the Judy array and release all associated resources.

int
getType()

Return the type constant of this Judy array.

bool
isIterationOptimized()

Whether the optimizeIteration trade is actually in effect here.

int
free()

Free the entire Judy array.

int|null
memoryUsage()

Return the memory used by the internal Judy structure.

int
size(mixed $start = null, mixed $end = null)

Return the number of elements, optionally within an inclusive key range.

int
count()

Return the number of elements. Implements Countable.

mixed
byCount(mixed $nth_index)

Locate the Nth index present in the array (1-based).

mixed
first(mixed $index = null)

Search (inclusive) for the first index present that is equal to or greater than the given index.

mixed
searchNext(mixed $index)

Search (exclusive) for the next index present that is greater than the given index.

mixed
last(mixed $index = null)

Search (inclusive) for the last index present that is equal to or less than the given index.

mixed
prev(mixed $index)

Search (exclusive) for the previous index present that is less than the given index.

mixed
firstEmpty(mixed $index = null)

Search (inclusive) for the first absent index that is equal to or greater than the given index. Integer-keyed types only.

mixed
nextEmpty(mixed $index)

Search (exclusive) for the next absent index greater than the given index. Integer-keyed types only.

mixed
lastEmpty(mixed $index = null)

Search (inclusive) for the last absent index that is equal to or less than the given index. Integer-keyed types only.

mixed
prevEmpty(mixed $index)

Search (exclusive) for the previous absent index less than the given index. Integer-keyed types only.

union(Judy $other)

Return a new Judy array containing all indices present in either array.

intersect(Judy $other)

Return a new Judy array containing only indices present in both arrays.

diff(Judy $other)

Return a new Judy array containing indices present in this array but not in the other.

xor(Judy $other)

Return a new Judy array containing indices present in exactly one of the arrays (symmetric difference).

void
mergeWith(Judy $other)

Merge another Judy array into this one in-place. Both arrays must use the same key category (both integer-keyed or both string-keyed).

slice(mixed $start, mixed $end)

Return a new Judy array containing entries in the [$start, $end] range (inclusive). For string-keyed types, comparison is lexicographic.

bool
offsetExists(TKey $offset)

Check whether the given offset exists in the array.

mixed
offsetGet(TKey $offset)

Return the value at the given offset.

void
offsetSet(TKey $offset, TValue $value)

Set the value at the given offset.

void
offsetUnset(TKey $offset)

Remove the element at the given offset.

mixed
jsonSerialize()

Return data suitable for json_encode(). Implements JsonSerializable.

array
__serialize()

Return serialization data as ['type' => int, 'data' => array].

void
__unserialize(array $data)

Restore a Judy array from serialized data.

array
toArray(mixed $start = null, mixed $end = null)

Convert the Judy array to a native PHP array, optionally limited to an inclusive key range. Uses native C iteration internally, faster than a manual foreach. A bounded read is one traversal writing straight into the returned array, so prefer it to slice($start, $end)->toArray().

static Judy
fromArray(int $type, array $data, bool $optimizeIteration = false)

Create a new Judy array from a PHP array.

void
putAll(array $data)

Bulk-insert entries from a PHP array into this Judy array.

array
getAll(array $keys)

Retrieve multiple values at once.

int
increment(mixed $key, int $amount = 1)

Atomically increment the value at the given key. If the key does not exist, it is created with the given amount. The amount may be negative.

void
rewind()

Rewind the iterator to the first element.

bool
valid()

Check whether the current iterator position is valid.

mixed
current()

Return the value at the current iterator position.

mixed
key()

Return the key at the current iterator position.

void
next()

Advance the iterator to the next element.

array
keys(mixed $start = null, mixed $end = null)

Return all keys as a PHP array, optionally limited to an inclusive key range. All key types are supported; string-keyed types require string bounds and compare them lexicographically. This is the primitive to reach for on a bounded read: one traversal writing straight into the returned array, so prefer it to slice($start, $end)->keys().

array
values(mixed $start = null, mixed $end = null)

Return all values as a PHP array, optionally limited to an inclusive key range. The bounds are keys, not values.

void
forEach(callable $callback)

Call a callback for each element, iterating in C. The callback receives ($key, $value) for each element.

filter(callable $predicate)

Return a new Judy array containing only elements matching the predicate.

map(callable $transform)

Return a new Judy array with values transformed by the callback.

int|float
sumValues()

Return the sum of all values in the array. For BITSET, returns the population count.

float|null
averageValues()

Return the average of all values, or null if the array is empty.

int
populationCount(mixed $start = 0, mixed $end = -1)

Return the number of keys in the [$start, $end] range (inclusive).

int
deleteRange(mixed $start, mixed $end)

Delete all keys in the [$start, $end] range (inclusive).

bool
equals(Judy $other)

Check if two Judy arrays have identical type, size, and key-value pairs.

Details

__construct(int $type, bool $optimizeIteration = false)

Create a new Judy array of the specified type.

Parameters

int $type

One of the Judy type constants (e.g. Judy::INT_TO_INT).

bool $optimizeIteration

[optional] Mirror payloads into the key index for faster ordered reads, at a write-path and memory cost. Only Judy::STRING_TO_INT_HASH and Judy::STRING_TO_INT_ADAPTIVE honour it; every other type accepts the argument and ignores it. Cannot be changed after construction. Since 2.5.0.

__destruct()

Free the Judy array and release all associated resources.

int getType()

Return the type constant of this Judy array.

Return Value

int

One of the Judy type constants.

bool isIterationOptimized()

Since: 2.5.0

Whether the optimizeIteration trade is actually in effect here.

Returns what was honoured, not what was asked for: a type that cannot mirror its payload accepts the constructor argument and returns false.

Return Value

bool

True if payloads are mirrored into the key index.

int free()

Free the entire Judy array.

Return Value

int

The number of bytes freed.

int|null memoryUsage()

Return the memory used by the internal Judy structure.

Return Value

int|null

Memory used in bytes, or null for string-keyed types (JudySL/JudyHS do not provide memory accounting).

int size(mixed $start = null, mixed $end = null)

Return the number of elements, optionally within an inclusive key range.

All key types are supported; string-keyed types require string bounds and compare them lexicographically. Counts without materialising the range, so prefer it to count($judy->keys($start, $end)).

The bounds are keys, not offsets. Prior to 2.5.0 the parameters were named $index_start/$index_end, and string bounds were accepted but ignored on string-keyed types, returning the whole-array count.

Parameters

mixed $start

[optional] Inclusive lower bound; null for unbounded.

mixed $end

[optional] Inclusive upper bound; null for unbounded.

Return Value

int

The number of elements in the range.

int count()

Return the number of elements. Implements Countable.

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

mixed byCount(mixed $nth_index)

Locate the Nth index present in the array (1-based).

Parameters

mixed $nth_index

The ordinal position to look up.

Return Value

mixed

The index at the given position. Only supported for integer-keyed types; returns null for string-keyed types.

mixed first(mixed $index = null)

Search (inclusive) for the first index present that is equal to or greater than the given index.

Parameters

mixed $index

[optional] Integer or string index to start from.

Return Value

mixed

The corresponding index in the array.

mixed searchNext(mixed $index)

Search (exclusive) for the next index present that is greater than the given index.

Parameters

mixed $index

Integer or string index to start from.

Return Value

mixed

The corresponding index in the array.

mixed last(mixed $index = null)

Search (inclusive) for the last index present that is equal to or less than the given index.

Parameters

mixed $index

[optional] Integer or string index to start from.

Return Value

mixed

The corresponding index in the array.

mixed prev(mixed $index)

Search (exclusive) for the previous index present that is less than the given index.

Parameters

mixed $index

Integer or string index to start from.

Return Value

mixed

The corresponding index in the array.

mixed firstEmpty(mixed $index = null)

Search (inclusive) for the first absent index that is equal to or greater than the given index. Integer-keyed types only.

Parameters

mixed $index

[optional] Integer index to start from.

Return Value

mixed

The corresponding absent index, or null for string-keyed types.

mixed nextEmpty(mixed $index)

Search (exclusive) for the next absent index greater than the given index. Integer-keyed types only.

Parameters

mixed $index

Integer index to start from.

Return Value

mixed

The corresponding absent index, or null for string-keyed types.

mixed lastEmpty(mixed $index = null)

Search (inclusive) for the last absent index that is equal to or less than the given index. Integer-keyed types only.

Parameters

mixed $index

[optional] Integer index to start from.

Return Value

mixed

The corresponding absent index, or null for string-keyed types.

mixed prevEmpty(mixed $index)

Search (exclusive) for the previous absent index less than the given index. Integer-keyed types only.

Parameters

mixed $index

Integer index to start from.

Return Value

mixed

The corresponding absent index, or null for string-keyed types.

Judy union(Judy $other)

Return a new Judy array containing all indices present in either array.

For integer-valued types, values from the other array overwrite on duplicate keys.

Parameters

Judy $other

Return Value

Judy

Judy intersect(Judy $other)

Return a new Judy array containing only indices present in both arrays.

For integer-valued types, values from this array are used.

Parameters

Judy $other

Return Value

Judy

Judy diff(Judy $other)

Return a new Judy array containing indices present in this array but not in the other.

Parameters

Judy $other

Return Value

Judy

Judy xor(Judy $other)

Return a new Judy array containing indices present in exactly one of the arrays (symmetric difference).

Parameters

Judy $other

Return Value

Judy

void mergeWith(Judy $other)

Merge another Judy array into this one in-place. Both arrays must use the same key category (both integer-keyed or both string-keyed).

Existing keys are overwritten.

Parameters

Judy $other

Return Value

void

Judy slice(mixed $start, mixed $end)

Return a new Judy array containing entries in the [$start, $end] range (inclusive). For string-keyed types, comparison is lexicographic.

Parameters

mixed $start
mixed $end

Return Value

Judy

bool offsetExists(TKey $offset)

Check whether the given offset exists in the array.

Parameters

TKey $offset

An offset to check for.

Return Value

bool

true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

mixed offsetGet(TKey $offset)

Return the value at the given offset.

Parameters

TKey $offset

The offset to retrieve.

Return Value

mixed

Can return all value types.

void offsetSet(TKey $offset, TValue $value)

Set the value at the given offset.

Parameters

TKey $offset

The offset to assign the value to.

TValue $value

The value to set.

Return Value

void

No value is returned.

void offsetUnset(TKey $offset)

Remove the element at the given offset.

Parameters

TKey $offset

The offset to unset.

Return Value

void

No value is returned.

mixed jsonSerialize()

Return data suitable for json_encode(). Implements JsonSerializable.

Return Value

mixed

data which can be serialized by json_encode, which is a value of any type other than a resource.

array __serialize()

Return serialization data as ['type' => int, 'data' => array].

Return Value

array

void __unserialize(array $data)

Restore a Judy array from serialized data.

Parameters

array $data

Return Value

void

array toArray(mixed $start = null, mixed $end = null)

Convert the Judy array to a native PHP array, optionally limited to an inclusive key range. Uses native C iteration internally, faster than a manual foreach. A bounded read is one traversal writing straight into the returned array, so prefer it to slice($start, $end)->toArray().

Parameters

mixed $start

[optional] Inclusive lower bound; null for unbounded. Since 2.5.0.

mixed $end

[optional] Inclusive upper bound; null for unbounded. Since 2.5.0.

Return Value

array

static Judy fromArray(int $type, array $data, bool $optimizeIteration = false)

Create a new Judy array from a PHP array.

Parameters

int $type

One of the Judy type constants.

array $data

Key-value pairs to populate the array with.

bool $optimizeIteration

[optional] See Judy::__construct(). Since 2.5.0.

Return Value

Judy

void putAll(array $data)

Bulk-insert entries from a PHP array into this Judy array.

Parameters

array $data

Return Value

void

array getAll(array $keys)

Retrieve multiple values at once.

Parameters

array $keys

Keys to look up.

Return Value

array

Associative array mapping each requested key to its value (or null if absent).

int increment(mixed $key, int $amount = 1)

Atomically increment the value at the given key. If the key does not exist, it is created with the given amount. The amount may be negative.

Parameters

mixed $key
int $amount

Return Value

int

The new value.

void rewind()

Rewind the iterator to the first element.

Return Value

void

Any returned value is ignored.

bool valid()

Check whether the current iterator position is valid.

Return Value

bool

The return value will be casted to boolean and then evaluated. Returns true on success or false on failure.

mixed current()

Return the value at the current iterator position.

Return Value

mixed

Can return any type.

mixed key()

Return the key at the current iterator position.

Return Value

mixed

TKey on success, or null on failure.

void next()

Advance the iterator to the next element.

Return Value

void

Any returned value is ignored.

array keys(mixed $start = null, mixed $end = null)

Return all keys as a PHP array, optionally limited to an inclusive key range. All key types are supported; string-keyed types require string bounds and compare them lexicographically. This is the primitive to reach for on a bounded read: one traversal writing straight into the returned array, so prefer it to slice($start, $end)->keys().

Note that a string upper bound is a bound, not a prefix match — for a prefix sweep, bound with the prefix and its successor.

Parameters

mixed $start

[optional] Inclusive lower bound; null for unbounded. Since 2.5.0.

mixed $end

[optional] Inclusive upper bound; null for unbounded. Since 2.5.0.

Return Value

array

array values(mixed $start = null, mixed $end = null)

Return all values as a PHP array, optionally limited to an inclusive key range. The bounds are keys, not values.

Parameters

mixed $start

[optional] Inclusive lower bound; null for unbounded. Since 2.5.0.

mixed $end

[optional] Inclusive upper bound; null for unbounded. Since 2.5.0.

Return Value

array

void forEach(callable $callback)

Call a callback for each element, iterating in C. The callback receives ($key, $value) for each element.

Parameters

callable $callback

Return Value

void

Judy filter(callable $predicate)

Return a new Judy array containing only elements matching the predicate.

Parameters

callable $predicate

Return Value

Judy

Judy map(callable $transform)

Return a new Judy array with values transformed by the callback.

Parameters

callable $transform

Return Value

Judy

int|float sumValues()

Return the sum of all values in the array. For BITSET, returns the population count.

Return Value

int|float

float|null averageValues()

Return the average of all values, or null if the array is empty.

For BITSET, always returns 1.0.

Return Value

float|null

int populationCount(mixed $start = 0, mixed $end = -1)

Return the number of keys in the [$start, $end] range (inclusive).

Integer-keyed types only — it answers from libJudy's O(1) population cache, which the JudySL/JudyHS string stores do not have, and throws on a string-keyed array. To count a range on string keys, use Judy::size($start, $end).

Parameters

mixed $start
mixed $end

Return Value

int

int deleteRange(mixed $start, mixed $end)

Delete all keys in the [$start, $end] range (inclusive).

Parameters

mixed $start
mixed $end

Return Value

int

The number of elements deleted.

bool equals(Judy $other)

Check if two Judy arrays have identical type, size, and key-value pairs.

Parameters

Judy $other

Return Value

bool