diff --git a/src/collections_me.c b/src/collections_me.c index 9bb2430..7db055e 100644 --- a/src/collections_me.c +++ b/src/collections_me.c @@ -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) diff --git a/src/collections_methods.c b/src/collections_methods.c index 2699655..7964a83 100644 --- a/src/collections_methods.c +++ b/src/collections_methods.c @@ -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; } diff --git a/src/php_collections_me.h b/src/php_collections_me.h index c2344b5..89867b9 100644 --- a/src/php_collections_me.h +++ b/src/php_collections_me.h @@ -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); diff --git a/tests/045-sort-sorted.phpt b/tests/045-sort-sorted.phpt index 2ed03e4..2504dc6 100644 --- a/tests/045-sort-sorted.phpt +++ b/tests/045-sort-sorted.phpt @@ -4,7 +4,7 @@ Test Collection::sort(), Collection::sortDescending(), Collection::sorted(), Col distinct(); if ($collection->toArray() != array_values(array_unique($array))) { diff --git a/tests/048-distinct-by.phpt b/tests/048-distinct-by.phpt index ce14197..dcda9b6 100644 --- a/tests/048-distinct-by.phpt +++ b/tests/048-distinct-by.phpt @@ -4,7 +4,7 @@ Test Collection::distinctBy(). 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)); diff --git a/tests/055-intersect-values-subtract.phpt b/tests/055-intersect-values-subtract.phpt index fcf4be1..13d7757 100644 --- a/tests/055-intersect-values-subtract.phpt +++ b/tests/055-intersect-values-subtract.phpt @@ -4,11 +4,11 @@ Test Collection::intersectValues() and Collection::subtract(). chunked($chunk_size)->toArray() != $chunked) { diff --git a/tests/058-binary-search-by.phpt b/tests/058-binary-search-by.phpt index 07830ff..de91d05 100644 --- a/tests/058-binary-search-by.phpt +++ b/tests/058-binary-search-by.phpt @@ -1,18 +1,18 @@ --TEST-- -Test Collection::binarySearch(). +Test Collection::binarySearch() and Collection::binarySearchBy(). --FILE-- binarySearch($which, 0, $from, $to) != $idx) { echo 'Collection::binarySearch() failed.', PHP_EOL;