Exemplo n.º 1
0
 public function testFindBinary()
 {
     $array = CArray::fromElements("oua", "vnf", "fnf", "aod", "tvi", "nbt", "jny", "vor", "rfd", "cvm", "hyh", "kng", "ggo", "uea", "hkb", "qbk", "xla", "uod", "jzi", "chw", "ssy", "olr", "bzl", "oux", "ltk", "bah", "khu", "msr", "pqv", "npb", "mtb", "eku", "vcv", "vbv", "wuo", "lrw", "bkw", "ezz", "jtc", "dwk", "dsq", "kzu", "oey", "vbi", "seh", "klz", "asj", "gzg", "ccs", "qop");
     $arrayOrig = CArray::makeCopy($array);
     $len = CArray::length($arrayOrig);
     // Sort the array first.
     CArray::sort($array, CComparator::ORDER_ASC);
     // Using the default comparators.
     for ($i = 0; $i < $len; $i += 3) {
         $string = $arrayOrig[$i];
         $foundAtPos0;
         CArray::find($array, $string, CComparator::EQUALITY, $foundAtPos0);
         $foundAtPos1;
         $found = CArray::findBinary($array, $string, CComparator::ORDER_ASC, $foundAtPos1);
         $this->assertTrue($found);
         $this->assertTrue($foundAtPos1 == $foundAtPos0);
     }
     // Using custom comparators.
     $comparatorEquality = function ($string0, $string1) {
         return CString::toLowerCase($string0) === CString::toLowerCase($string1);
     };
     $comparatorOrderAsc = function ($string0, $string1) {
         return CString::compare(CString::toLowerCase($string0), CString::toLowerCase($string1));
     };
     for ($i = 0; $i < $len; $i += 5) {
         $string = CString::toUpperCase($arrayOrig[$i]);
         $foundAtPos0;
         CArray::find($array, $string, $comparatorEquality, $foundAtPos0);
         $foundAtPos1;
         $found = CArray::findBinary($array, $string, $comparatorOrderAsc, $foundAtPos1);
         $this->assertTrue($found);
         $this->assertTrue($foundAtPos1 == $foundAtPos0);
     }
     // Special cases.
     $array = CArray::fromElements("a", "b");
     $found = CArray::findBinary($array, "a");
     $this->assertTrue($found);
     $found = CArray::findBinary($array, "b");
     $this->assertTrue($found);
     $found = CArray::findBinary($array, "c");
     $this->assertFalse($found);
     $array = CArray::fromElements("a");
     $found = CArray::findBinary($array, "a");
     $this->assertTrue($found);
     $array = CArray::make();
     $found = CArray::findBinary($array, "a");
     $this->assertFalse($found);
 }
Exemplo n.º 2
0
 /**
  * Determines if an array contains an element with a specified value, using faster binary search.
  *
  * Binary search is much faster than linear search inside an array that was preliminary sorted. So using binary
  * search makes sense when many searches need to be run over the same large array. When looking for elements with
  * this method, you should be using the same comparator that was used for sorting the array (typically
  * `CComparator::ORDER_ASC`).
  *
  * @param  mixed $whatElement The value of the searched element.
  * @param  callable $comparator **OPTIONAL. Default is** `CComparator::ORDER_ASC`. The function or method to be
  * used for the comparison of element values while searching. If this parameter is provided, the comparator should
  * take two parameters, with the first parameter being an element from the array and the second parameter being the
  * searched element, and return `-1` if the value of the first parameter would need to go before the value of the
  * second parameter if the two were being ordered in separate, `1` if the other way around, and `0` if the two
  * elements are equal.
  * @param  reference $foundAtPos **OPTIONAL. OUTPUT.** If an element has been found after the method was called
  * with this parameter provided, the parameter's value, which is of type `int`, indicates the position of the first
  * found element (if the searched element is not unique in the array, the position can differ from that with linear
  * search).
  *
  * @return bool `true` if such element was found in the array, `false` otherwise.
  *
  * @link   CComparator.html CComparator
  */
 public function findBinary($whatElement, $comparator = CComparator::ORDER_ASC, &$foundAtPos = null)
 {
     return CArray::findBinary($this->m_splArray, $whatElement, $comparator, $foundAtPos);
 }