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_ARG_TYPE_INFO(0, to_index, IS_LONG, 0)
ZEND_END_ARG_INFO() 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_BEGIN_ARG_INFO(chuncked_arginfo, 0)
ZEND_ARG_TYPE_INFO(0, size, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, size, IS_LONG, 0)
ZEND_ARG_CALLABLE_INFO(0, transform, 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, average, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Collection, binarySearch, binary_search_arginfo, 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, 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, chunked, chuncked_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(Collection, containsAll, other_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, containsAll, other_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(Collection, containsAllKeys, 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); free(ref);
} }
PHP_METHOD(Collection, binarySearchWith)
{
}
PHP_METHOD(Collection, chunked) PHP_METHOD(Collection, chunked)
{ {
zend_long size; zend_long size;
@ -2655,11 +2660,7 @@ PHP_METHOD(Collection, reverse)
zend_hash_index_add(reversed, bucket->h, &bucket->val); zend_hash_index_add(reversed, bucket->h, &bucket->val);
} }
ZEND_HASH_FOREACH_END(); ZEND_HASH_FOREACH_END();
if (GC_REFCOUNT(current) > 1) { array_release(current);
GC_DELREF(current);
} else {
zend_array_destroy(current);
}
THIS_COLLECTION = reversed; THIS_COLLECTION = reversed;
} }
@ -2696,11 +2697,7 @@ PHP_METHOD(Collection, shuffle)
zend_long rand_idx = php_mt_rand_range(offset, num_elements - 1); zend_long rand_idx = php_mt_rand_range(offset, num_elements - 1);
zend_hash_bucket_renum_swap(&bucket[offset], &bucket[rand_idx]); zend_hash_bucket_renum_swap(&bucket[offset], &bucket[rand_idx]);
} }
if (GC_REFCOUNT(current) > 1) { array_release(current);
GC_DELREF(current);
} else {
zend_array_destroy(current);
}
THIS_COLLECTION = shuffled; THIS_COLLECTION = shuffled;
} }
@ -2933,11 +2930,7 @@ PHP_METHOD(Collection, sortWith)
ZVAL_COPY_VALUE(val, PAIR_SECOND(pair)); ZVAL_COPY_VALUE(val, PAIR_SECOND(pair));
GC_DELREF(pair); GC_DELREF(pair);
ZEND_HASH_FOREACH_END(); ZEND_HASH_FOREACH_END();
if (GC_REFCOUNT(current) > 1) { array_release(current);
GC_DELREF(current);
} else {
zend_array_destroy(current);
}
THIS_COLLECTION = sorted_with; THIS_COLLECTION = sorted_with;
} }

View File

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

View File

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

View File

@ -4,7 +4,7 @@ Test Collection::sortBy(), Collection::sortByDescending(), Collection::sortedBy(
<?php <?php
$array = []; $array = [];
for ($i = 0; $i < 4; ++$i) { 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) { $sort_by = function ($value) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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