Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83995 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46838 invoked from network); 27 Feb 2015 12:29:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Feb 2015 12:29:03 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.175 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.175 mail-we0-f175.google.com Received: from [74.125.82.175] ([74.125.82.175:44062] helo=mail-we0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 35/AD-32582-E0360F45 for ; Fri, 27 Feb 2015 07:29:03 -0500 Received: by wesk11 with SMTP id k11so20000063wes.11 for ; Fri, 27 Feb 2015 04:28:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=NxWSi2YPCBBAY2uo6HWlmYNvKO4NgDQcPvaBZXu72qI=; b=rE61xSPX/asGJuw+vrkrrkMIYpcF2VXL9mEKbWCVQhfO2nQQ8FaHpe9GYUP96Yq7rB zUDSm271BfQR4waODeWPrUSXuFuTTXD/R0AydBpiV4ADKbHHOsgPmMfUf2Ny5wOmF9Y/ 9x5WdnZyaFYLqNx+krIljYu3hYMqj3JAj3cfwcJ+e48Q1NZ9sgpzTtm09taEVb5kLerg jX7nD+qli2fuKEc0cOIjdttqpL4SGVzlVvGeLGQhH4hyBmfG+kyhdahfLaSEs00056U/ fu5JjkFfqG4U197XpjrLcw/+NDzvTxneFgXBQCxe3zlknP+2z3HCHONslHOdyyKWOZO2 tPBw== X-Received: by 10.194.240.164 with SMTP id wb4mr26786748wjc.66.1425040138671; Fri, 27 Feb 2015 04:28:58 -0800 (PST) Received: from [192.168.0.136] ([62.189.198.114]) by mx.google.com with ESMTPSA id jy7sm2576448wid.22.2015.02.27.04.28.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Feb 2015 04:28:57 -0800 (PST) Message-ID: <54F062EC.9000606@gmail.com> Date: Fri, 27 Feb 2015 12:28:28 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Feature request and RFC From: rowan.collins@gmail.com (Rowan Collins) Thomas Gielfeldt wrote on 24/02/2015 16:17: > Hi internals. > > I've made PR proposing a feature request: A new interface Sortable. > > https://github.com/php/php-src/pull/1116 > > If possible, I would like to create and RFC describing this in more detail, > and perhaps get a voting on. I think the reason this interface is proving hard to simplify is that you're putting too much of the responsibility on the implementing object. Most containers don't need or want to reimplement an entire quicksort algorithm, or be responsible for tracking the various types of comparison. Ideally, you could start by writing an OO implementation of QuickSort which takes as its input an abstract "Collection" instead of an array, and look at what methods you end up wanting to call on that Collection. That might end up rather complex in practice, though, because the underlying data structures do make a difference to an efficient implementation. However, the types of sort differ primarily in the way values are compared, so if the engine passed in an appropriate comparison callback, the interface needs only one method: public function sort(callable $comparison_callback, boolean $preserve_keys) where $comparison_callback is: function($l_key, $l_value, $r_key, $r_value): int The only flag that the object might want to care about is whether to preserve keys (asort, uasort, ksort, uksort), because it could allow optimisations rather than reindexing after the sort has completed. For everything else, it should just be repeatedly asking the engine "which of these two items should come first?" By passing both keys and values, ksort() can be implemented with the same callback signature, the only disadvantage being that the user callbacks to usort() and uksort() would need to be wrapped, with the engine basically emulating the following: // For usort($object, $usort_callback) $comparison_callback = function($l_key, $l_value, $r_key, $r_value) use ($usort_callback) { return $usort_callback($l_value, $r_value); }; // For ksort($object, $ksort_callback) $comparison_callback = function($l_key, $l_value, $r_key, $r_value) use ($ksort_callback) { return $ksort_callback($l_key, $r_key); }; Regards, -- Rowan Collins [IMSoP]