Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84275 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34383 invoked from network); 4 Mar 2015 07:13:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Mar 2015 07:13:07 -0000 Authentication-Results: pb1.pair.com header.from=thomas@gielfeldt.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=thomas@gielfeldt.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain gielfeldt.dk from 209.85.217.171 cause and error) X-PHP-List-Original-Sender: thomas@gielfeldt.dk X-Host-Fingerprint: 209.85.217.171 mail-lb0-f171.google.com Received: from [209.85.217.171] ([209.85.217.171:45392] helo=mail-lb0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 37/41-22753-180B6F45 for ; Wed, 04 Mar 2015 02:13:06 -0500 Received: by lbiz12 with SMTP id z12so14367755lbi.12 for ; Tue, 03 Mar 2015 23:13:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=lgfQoXdHi5xPNhnoys9dm23aCu6ZiWKZyRDcNYPTKlI=; b=hB30x/yQA7EzKXEy5tcsxnqdstORuYBTs7GiOM4mCMBWsCN3f4EUhI5m6NlBE+4XEr Q/3oiGKaD6qQWZqzMfh9DEt0NP1T13DkZJhcfJBaMPc+MZYuAzoZi2WImPYD9babG2Ed Gko9REUGPGDd8inZEy6gsUGaJ2UYdNuWjsNZ/2x+pFUjvv/T/4kzYY+SBeNT1FAS09i2 lRYLi8tchKIhJAI2pcbRU8z0xblG6k1/gBOCTpx8qZEhqb2Z2KmFqYq2YMlGjuh+5tR6 jWXQb/qsER2Yd5Z1jqXymjSIfky39yAbPpYzyBdZfskFmqmLH3M5xpL61dfO+z26N4+R Rvxg== X-Gm-Message-State: ALoCoQl4H7GllvpHwOF7IX7RJ0jvXvheXOG2VxCoUCcIWxAYDe3MHJsJpC557kJ+Q4vB3JHjoWhi X-Received: by 10.152.26.201 with SMTP id n9mr2078509lag.29.1425453181658; Tue, 03 Mar 2015 23:13:01 -0800 (PST) MIME-Version: 1.0 Received: by 10.152.144.194 with HTTP; Tue, 3 Mar 2015 23:12:41 -0800 (PST) In-Reply-To: <54F48108.6010000@gmail.com> References: <54F440E6.4050908@gmail.com> <54F48108.6010000@gmail.com> Date: Wed, 4 Mar 2015 08:12:41 +0100 Message-ID: To: Rowan Collins Cc: PHP Internals Content-Type: multipart/alternative; boundary=089e0160a7068986650510712e67 Subject: Re: [PHP-DEV] Re: Feature request and RFC From: thomas@gielfeldt.dk (Thomas Gielfeldt) --089e0160a7068986650510712e67 Content-Type: text/plain; charset=UTF-8 2015-03-02 16:26 GMT+01:00 Rowan Collins : > Rowan Collins wrote on 02/03/2015 10:52: > >> Thomas Gielfeldt wrote on 02/03/2015 07:43: >> >> 2015-02-24 17:17 GMT+01:00 Thomas Gielfeldt : >>> >>> 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. >>>> >>>> Thanks >>>> >>>> Br, >>>> >>>> Thomas Gielfeldt >>>> >>>> >>>> The 2nd PR I made addresses this (https://github.com/php/php- >>> src/pull/1123), >>> exposing 3 much simpler interfaces depending on what the user wants/needs >>> to implement, which combined covers all the 11 sort functions. >>> >>> Also, the point of this/these interface(s) is to provide the same power >>> that some of the other SPL interfaces have (Countable, Serializable, >>> ArrayAccess, Iterator, etc.). That is, using native functions with >>> objects >>> possessing certain capabilities. >>> >> >> >> I don't think a container should be expected to reimplement something as >> specific as a case-insensitive natural string comparison; its job should be >> to do the actual sorting of data. >> >> I suggest a single interface with 3 methods: sortValues($cmp_function), >> sortValuesAssoc($cmp_function), and sortKeys($cmp_function): >> https://gist.github.com/IMSoP/4ea904203eadf8d5859a >> >> This maintains the obvious connection between userland functions and the >> method called, and keeps the userland implementation much lower on >> boilerplate. Conveniently, a collection which internally stores data in an >> array can pass the callback directly to usort/uasort/uksort for a trivial >> implementation. >> >> Obviously, this means more implementation needed on the engine side, in >> that it needs to expose callback functions for the various different >> comparisons, but I think that's where the comparison logic belongs. >> > > > Incidentally, note that this is exactly how the sorting of real arrays is > done internally: zend_hash_sort_ex [1] takes a comparison callback, using a > global set by php_set_compare_func [2] for flags, and wrappers for things > like key vs data comparison [3]. > > Basically, to implement my suggestion, you need a way to expose the > php_array_*_compare functions in ext/standard/array.c as callbacks to > userland, presumably without actually making them named functions. > > Though I can appreciate the elegance in what you propose, I'm worried about a couple of things with this approach. 1.) Having to pass a comparison function through userland, could hurt performance for the ArrayObject, which otherwise would use the native C sort functions directly on its hashtable. 2.) This approach forces the implementor to rely on a userland comparison function. While this would not be a problem for the specific use case I had in mind, it could destroy the possibility of further abstraction. One example could be a sort in conjunction with a database class. Probably not the best example, but I hope you understand what I'm getting at. Dispatching the sort to something outside PHP. If all that the sort methods get are closures, then it's near impossible to determine the intent of the caller. 3.) I'm probably gonna the one implementing this, and I think this approach would be much more complex than the two I've already made :-) ... though, of course, that's not an excuse :-) That being said, I still like the approach you suggest wrt responsibility of the interface. I just think that there may be some practical implications. > > [1] http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_hash.c#1930 > [2] http://lxr.php.net/xref/PHP_TRUNK/ext/standard/array.c#144 > [3] http://lxr.php.net/xref/PHP_TRUNK/ext/standard/array.c#173 > > Regards, > -- > Rowan Collins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --089e0160a7068986650510712e67--