Judy
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
Create a new Judy array of the specified type.
Free the Judy array and release all associated resources.
Return the type constant of this Judy array.
Whether the optimizeIteration trade is actually in effect here.
Free the entire Judy array.
Return the memory used by the internal Judy structure.
Return the number of elements, optionally within an inclusive key range.
Return the number of elements. Implements Countable.
Locate the Nth index present in the array (1-based).
Search (inclusive) for the first index present that is equal to or greater than the given index.
Search (exclusive) for the next index present that is greater than the given index.
Search (inclusive) for the last index present that is equal to or less than the given index.
Search (exclusive) for the previous index present that is less than the given index.
Search (inclusive) for the first absent index that is equal to or greater than the given index. Integer-keyed types only.
Search (exclusive) for the next absent index greater than the given index. Integer-keyed types only.
Search (inclusive) for the last absent index that is equal to or less than the given index. Integer-keyed types only.
Search (exclusive) for the previous absent index less than the given index. Integer-keyed types only.
Return a new Judy array containing entries in the [$start, $end] range (inclusive). For string-keyed types, comparison is lexicographic.
Check whether the given offset exists in the array.
Return the value at the given offset.
Set the value at the given offset.
Remove the element at the given offset.
Return data suitable for json_encode(). Implements JsonSerializable.
Return serialization data as ['type' => int, 'data' => array].
Restore a Judy array from serialized data.
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().
Create a new Judy array from a PHP array.
Bulk-insert entries from a PHP array into this Judy array.
Retrieve multiple values at once.
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.
Rewind the iterator to the first element.
Check whether the current iterator position is valid.
Return the value at the current iterator position.
Return the key at the current iterator position.
Advance the iterator to the next element.
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().
Return all values as a PHP array, optionally limited to an inclusive key range. The bounds are keys, not values.
Call a callback for each element, iterating in C. The callback receives ($key, $value) for each element.
Return a new Judy array containing only elements matching the predicate.
Return the sum of all values in the array. For BITSET, returns the population count.
Return the average of all values, or null if the array is empty.
Return the number of keys in the [$start, $end] range (inclusive).
Delete all keys in the [$start, $end] range (inclusive).
Details
__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.
Returns what was honoured, not what was asked for: a type that cannot mirror its payload accepts the constructor argument and returns false.
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.
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.
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.
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.
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.
Judy
diff(Judy $other)
Return a new Judy array containing indices present in this array but not in the other.
Judy
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).
Existing keys are overwritten.
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.
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().
Note that a string upper bound is a bound, not a prefix match — for a prefix sweep, bound with the prefix and its successor.
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.
Judy
filter(callable $predicate)
Return a new Judy array containing only elements matching the predicate.
Judy
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.
For BITSET, always returns 1.0.
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).
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.