static int spl_ptr_pqueue_zval_cmp(zval *a, zval *b, zval *object) { /* {{{ */ zval result; zval *a_priority_p = spl_pqueue_extract_helper(a, SPL_PQUEUE_EXTR_PRIORITY); zval *b_priority_p = spl_pqueue_extract_helper(b, SPL_PQUEUE_EXTR_PRIORITY); if ((!a_priority_p) || (!b_priority_p)) { zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node"); return 0; } if (EG(exception)) { return 0; } if (object) { spl_heap_object *heap_object = Z_SPLHEAP_P(object); if (heap_object->fptr_cmp) { zend_long lval = 0; if (spl_ptr_heap_cmp_cb_helper((zval *)object, heap_object, a_priority_p, b_priority_p, &lval) == FAILURE) { /* exception or call failure */ return 0; } return lval > 0 ? 1 : (lval < 0 ? -1 : 0); } } compare_function(&result, a_priority_p, b_priority_p); return (int)Z_LVAL(result); }
/* {{{ proto mixed SplPriorityQueue::top() Peek at the top element of the priority queue */ SPL_METHOD(SplPriorityQueue, top) { zval *value, *value_out; spl_heap_object *intern; if (zend_parse_parameters_none() == FAILURE) { return; } intern = Z_SPLHEAP_P(getThis()); if (intern->heap->flags & SPL_HEAP_CORRUPTED) { zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0); return; } value = spl_ptr_heap_top(intern->heap); if (!value) { zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty heap", 0); return; } value_out = spl_pqueue_extract_helper(value, intern->flags); if (!value_out) { zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node"); return; } RETURN_ZVAL(value_out, 1, 0); }
/* {{{ proto mixed SplPriorityQueue::extract() extract the element out of the top of the priority queue */ SPL_METHOD(SplPriorityQueue, extract) { zval value, *value_out; spl_heap_object *intern; if (zend_parse_parameters_none() == FAILURE) { return; } intern = Z_SPLHEAP_P(getThis()); if (intern->heap->flags & SPL_HEAP_CORRUPTED) { zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0); return; } spl_ptr_heap_delete_top(intern->heap, &value, getThis()); if (Z_ISUNDEF(value)) { zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0); return; } value_out = spl_pqueue_extract_helper(&value, intern->flags); if (!value_out) { zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node"); zval_ptr_dtor(&value); return; } ZVAL_DEREF(value_out); ZVAL_COPY(return_value, value_out); zval_ptr_dtor(&value); }
static zval *spl_pqueue_it_get_current_data(zend_object_iterator *iter) /* {{{ */ { spl_heap_object *object = Z_SPLHEAP_P(&iter->data); zval *element = &object->heap->elements[0]; if (object->heap->flags & SPL_HEAP_CORRUPTED) { zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0); return NULL; } if (object->heap->count == 0 || Z_ISUNDEF_P(element)) { return NULL; } else { zval *data = spl_pqueue_extract_helper(element, object->flags); if (!data) { zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node"); } return data; } }
/* {{{ proto mixed|NULL SplPriorityQueue::current() Return current datastructure entry */ SPL_METHOD(SplPriorityQueue, current) { spl_heap_object *intern = Z_SPLHEAP_P(getThis()); zval *element = &intern->heap->elements[0]; if (zend_parse_parameters_none() == FAILURE) { return; } if (!intern->heap->count || Z_ISUNDEF_P(element)) { RETURN_NULL(); } else { zval *data = spl_pqueue_extract_helper(element, intern->flags); if (!data) { zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node"); RETURN_NULL(); } RETURN_ZVAL(data, 1, 0); } }