Refactor code. Add signature for `binarySearchWith()`.

This commit is contained in:
CismonX 2018-09-12 13:50:26 +08:00
parent 1b2d443697
commit 4eba5b27b7
13 changed files with 39 additions and 36 deletions

View File

@ -122,6 +122,14 @@ ZEND_BEGIN_ARG_INFO(binary_search_by_arginfo, 0)
ZEND_ARG_TYPE_INFO(0, to_index, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(binary_search_with_arginfo, 0)
ZEND_ARG_INFO(0, element)
ZEND_ARG_CALLABLE_INFO(0, comparator, 0)
ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, from_index, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, to_index, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(chuncked_arginfo, 0)
ZEND_ARG_TYPE_INFO(0, size, IS_LONG, 0)
ZEND_ARG_CALLABLE_INFO(0, transform, 0)
@ -148,6 +156,7 @@ const zend_function_entry collection_methods[] = {
PHP_ME(Collection, average, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Collection, binarySearch, binary_search_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(Collection, binarySearchBy, binary_search_by_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(Collection, binarySearchWith, binary_search_with_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(Collection, chunked, chuncked_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(Collection, containsAll, other_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(Collection, containsAllKeys, other_arginfo, ZEND_ACC_PUBLIC)

View File

@ -1000,6 +1000,11 @@ PHP_METHOD(Collection, binarySearchBy)
free(ref);
}
PHP_METHOD(Collection, binarySearchWith)
{
}
PHP_METHOD(Collection, chunked)
{
zend_long size;
@ -2655,11 +2660,7 @@ PHP_METHOD(Collection, reverse)
zend_hash_index_add(reversed, bucket->h, &bucket->val);
}
ZEND_HASH_FOREACH_END();
if (GC_REFCOUNT(current) > 1) {
GC_DELREF(current);
} else {
zend_array_destroy(current);
}
array_release(current);
THIS_COLLECTION = reversed;
}
@ -2696,11 +2697,7 @@ PHP_METHOD(Collection, shuffle)
zend_long rand_idx = php_mt_rand_range(offset, num_elements - 1);
zend_hash_bucket_renum_swap(&bucket[offset], &bucket[rand_idx]);
}
if (GC_REFCOUNT(current) > 1) {
GC_DELREF(current);
} else {
zend_array_destroy(current);
}
array_release(current);
THIS_COLLECTION = shuffled;
}
@ -2933,11 +2930,7 @@ PHP_METHOD(Collection, sortWith)
ZVAL_COPY_VALUE(val, PAIR_SECOND(pair));
GC_DELREF(pair);
ZEND_HASH_FOREACH_END();
if (GC_REFCOUNT(current) > 1) {
GC_DELREF(current);
} else {
zend_array_destroy(current);
}
array_release(current);
THIS_COLLECTION = sorted_with;
}

View File

@ -20,6 +20,7 @@ PHP_METHOD(Collection, associateByTo);
PHP_METHOD(Collection, average);
PHP_METHOD(Collection, binarySearch);
PHP_METHOD(Collection, binarySearchBy);
PHP_METHOD(Collection, binarySearchWith);
PHP_METHOD(Collection, chunked);
PHP_METHOD(Collection, containsAll);
PHP_METHOD(Collection, containsAllKeys);

View File

@ -4,7 +4,7 @@ Test Collection::sort(), Collection::sortDescending(), Collection::sorted(), Col
<?php
$array = [];
for ($i = 0; $i < 100; ++$i) {
$array[] = random_int(-200, 200);
$array[] = mt_rand(-200, 200);
}
$collection = Collection::init($array);

View File

@ -4,7 +4,7 @@ Test Collection::sortBy(), Collection::sortByDescending(), Collection::sortedBy(
<?php
$array = [];
for ($i = 0; $i < 4; ++$i) {
$array[random_bytes(2)] = [random_int(10, 99)];
$array[random_bytes(2)] = [mt_rand(10, 99)];
}
$sort_by = function ($value) {

View File

@ -4,7 +4,7 @@ Test Collection::distinct().
<?php
$array = [];
for ($i = 0; $i < 100; ++$i) {
$array[] = random_int(1, 10);
$array[] = mt_rand(1, 10);
}
$collection = Collection::init($array)->distinct();
if ($collection->toArray() != array_values(array_unique($array))) {

View File

@ -4,7 +4,7 @@ Test Collection::distinctBy().
<?php
$array = [];
for ($i = 0; $i < 100; ++$i) {
$array[] = random_bytes(random_int(1, 10));
$array[] = random_bytes(mt_rand(1, 10));
}
$get_len = function ($value) {
return strlen($value);

View File

@ -4,11 +4,11 @@ Test Collection::union().
<?php
$array = [];
for ($i = 0; $i < 50; ++$i) {
$array[] = random_int(1, 50);
$array[] = mt_rand(1, 50);
}
$array1 = [];
for ($i = 0; $i < 50; ++$i) {
$array1[] = random_int(1, 50);
$array1[] = mt_rand(1, 50);
}
$union = array_values(array_unique(array_merge($array, $array1)));
$collection = Collection::init($array);

View File

@ -4,7 +4,7 @@ Test Collection::sum() and Collection::sumBy().
<?php
$array = [];
for ($i = 0; $i < 50; ++$i) {
$array[] = random_int(1, 50);
$array[] = mt_rand(1, 50);
}
$collection = Collection::init($array);
$sum = array_sum($array);
@ -12,7 +12,7 @@ if ($collection->sum() != $sum) {
echo 'Collection::sum() failed.', PHP_EOL;
}
$array = array_map(function ($value) {
return [$value, floatval($value / random_int(3, 7))];
return [$value, floatval($value / mt_rand(3, 7))];
}, $array);
$collection = Collection::init($array);
$sum = array_sum(array_column($array, 1));

View File

@ -4,11 +4,11 @@ Test Collection::intersectValues() and Collection::subtract().
<?php
$array = [];
for ($i = 0; $i < 50; ++$i) {
$array[] = random_int(1, 50);
$array[] = mt_rand(1, 50);
}
$array1 = [];
for ($i = 0; $i < 50; ++$i) {
$array1[] = random_int(1, 50);
$array1[] = mt_rand(1, 50);
}
$collection = Collection::init($array);
$collection1 = Collection::init($array1);

View File

@ -4,11 +4,11 @@ Test Collection::removeAll() and Collection::retainAll().
<?php
$array = [];
for ($i = 0; $i < 50; ++$i) {
$array[] = random_int(1, 50);
$array[] = mt_rand(1, 50);
}
$array1 = [];
for ($i = 0; $i < 50; ++$i) {
$array1[] = random_int(1, 50);
$array1[] = mt_rand(1, 50);
}
$collection = Collection::init($array);

View File

@ -3,11 +3,11 @@ Test Collection::chunked().
--FILE--
<?php
$array = [];
$size = random_int(20, 30);
$size = mt_rand(20, 30);
for ($i = 0; $i < 50; ++$i) {
$array[] = random_int(100, 200);
$array[] = mt_rand(100, 200);
}
$chunk_size = random_int(4, 9);
$chunk_size = mt_rand(4, 9);
$chunked = array_chunk($array, $chunk_size, false);
$collection = Collection::init($array);
if ($collection->chunked($chunk_size)->toArray() != $chunked) {

View File

@ -1,18 +1,18 @@
--TEST--
Test Collection::binarySearch().
Test Collection::binarySearch() and Collection::binarySearchBy().
--FILE--
<?php
$array = [];
$size = random_int(20, 30);
$size = mt_rand(20, 30);
for ($i = 0; $i < 50; ++$i) {
$array[] = random_int(100, 200);
$array[] = mt_rand(100, 200);
}
$array = array_unique($array);
sort($array);
$idx = random_int(0, count($array) - 1);
$idx = mt_rand(0, count($array) - 1);
$which = $array[$idx];
$from = random_int(0, $idx);
$to = random_int($idx, count($array));
$from = mt_rand(0, $idx);
$to = mt_rand($idx, count($array));
$collection = Collection::init($array);
if ($collection->binarySearch($which, 0, $from, $to) != $idx) {
echo 'Collection::binarySearch() failed.', PHP_EOL;