Let's say, I have a vec<int>
containing a list of integers that may be non-continuous (due to element being removed from database).
Example:
$occupiedNumbers = vec[1, 2, 3, 5, 6, 8, 10, 11, 12, 13, 15, 16];
Now what I need is to check if this vec
contains a given number, and if yes, increase it by 1 and repeat checking.
$newItemNumber = count($occupiedNumbers);
while (/* check if $occupiedNumbers contains $newItemNumber */) {
$a++;
}
In vanilla PHP there is in_array()
for this, but would that work for a vec
? Also, there is a builtin HH\Vector::linearSearch()
but it's meant to be used with vec
's predecessor, HH\Vector
.
What is the right solution to check if a vec
contains a given value?
Neither HHVM nor Hack docs say about that use case for a vec
. Also, I'm missing some sort of REPL tool to check it manually off-project without building a whole (possibly faulty) project.
The recommendation from the HHVM/HackLang guys is to use C\contains()
as you can read here but you need to install the HSL package (hhvm/hsl
) using composer
. This library contains loads of functions to deal with the new array-type structures: vec
, dict
, keyset
. Those functions in the HSL Library are prefixed with C
, Vec
, Dict
, Keyset
and you'll find also Math
, Str
, etc., very useful for other needs.
After installing that package it becomes available but typically it is more handy to add use namespace
to avoid the long prefix:
use namespace HH\Lib\C;
This is how you can do it:
$exists = C\contains($occupiedNumbers, $newItemNumber);