Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98978 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46899 invoked from network); 6 May 2017 15:12:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 May 2017 15:12:17 -0000 Authentication-Results: pb1.pair.com header.from=aidantwoods@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=aidantwoods@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.175 as permitted sender) X-PHP-List-Original-Sender: aidantwoods@gmail.com X-Host-Fingerprint: 209.85.128.175 mail-wr0-f175.google.com Received: from [209.85.128.175] ([209.85.128.175:35255] helo=mail-wr0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C2/BD-02776-FC7ED095 for ; Sat, 06 May 2017 11:12:15 -0400 Received: by mail-wr0-f175.google.com with SMTP id z52so17212781wrc.2 for ; Sat, 06 May 2017 08:12:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=NZnMkMlZ111O67gNFvmAATWbP2e8A32+YB7rzGDJjfQ=; b=EDi5RHRLT6P1V4RglIPo9zVZAqlQs24fW52MlNdQ6fKwmbUZkb/Fx3yQJK9u1yQsb3 xFBUVaKx5VFI688VURbvleJnmewaZ84mnkJqKsaF2HEEBIqRQRAhkE1MUng1MTfdX3o0 4f9A4u3m2pml8pwxTL9+9OmWoM1tfjd1nYgRtZln2N892DW4/qPa42aT7/1TDps+7T7E PzeM5w1nGBe0lAVJJA6aBdy1LeqAsyvxek5DGMlITJL86mF1rvkZqPjr7wcKwG1FpTA6 jrQW73QMUE0AkDpdPyhTEW8cwIebtTRsgy49CoT17GkGfSBysWhi9BZyYxsDJGMNIS2h fZFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=NZnMkMlZ111O67gNFvmAATWbP2e8A32+YB7rzGDJjfQ=; b=PjdGZjS8CXdjE1Vbru2gI0iXVpR7Kow/2DnZhDIHRq29Muahwcx2PVxljrWnpr0Egj a3NXzSL34axY3eC+rky5ujPFpmNfPite38uAHiDgy0OVvHDwcbLjAWgOjClAFFCR+ljk wd4sIFNMT6tizlEE7FKAZ1SMzFyhuDLhc0A8yvjMzybGeoG60SvVhIulIok/oiFC/DBU uwIhVwqTNTkVVJbVZeoShHCv3t7+wqleQGr40HQqFaMSl1JK1gczoVv0eelb/edzaXyI CykGSesPcsm0HLeJH2Z2eglPR5hWT0fGu6g8ymfexSd78fSdKKUg960hZXFSRY671UPs 7uXw== X-Gm-Message-State: AN3rC/6diusVROCo/acL9jpjbubPckVeLsi5/fF86WITj/u4paG38eAN R107HEA2saXt22R2YzM7m/LV2A+xQA== X-Received: by 10.223.174.200 with SMTP id y66mr30261372wrc.79.1494083532133; Sat, 06 May 2017 08:12:12 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.164.215 with HTTP; Sat, 6 May 2017 08:12:11 -0700 (PDT) In-Reply-To: References: Date: Sat, 6 May 2017 16:12:11 +0100 Message-ID: To: Ryan Pallas Cc: Jesse Schalken , PHP internals Content-Type: multipart/alternative; boundary=001a113c998c329d50054edc6ed1 Subject: Re: [PHP-DEV] Add is_vectorlike($array) function From: aidantwoods@gmail.com (Aidan Woods) --001a113c998c329d50054edc6ed1 Content-Type: text/plain; charset=UTF-8 > Also, given the implementation above, why does the array have to be ksorted > correctly to count? Specifically, why isn't this array considered vector > like? > $arr = [ > 1=> 1, > 0=> 0, > ]; I suppose the question would be, given that array (or a similar out-of-order array), should is_vectorlike afford you the expectation that in a foreach loop: foreach ($arr as $i => $v) { // do something } that (within the length of the array) the value $arr[$i + 1] has not yet been processed by the code in the loop, and is also the next item to be processed. Or maybe more succinctly, should is_vectorlike guarantee that items are iterated over in order? On 5 May 2017 at 22:19, Ryan Pallas wrote: > On Tue, May 2, 2017 at 3:13 AM, Jesse Schalken > wrote: > > > Related to the optimisation made by Sara Golemon here: > > https://github.com/php/php-src/commit/c74bc87c74f48bc55541b3bf2fc67d > > 595f58a3b5 > > > > I often define a function like this, which checks if an array is "vector > > like" i.e. has keys 0,1,2..N: > > > > function is_vectorlike(array $a): bool { > > $i = 0; > > foreach ($a as $k => $v) { > > if ($k !== $i++) { > > return false; > > } > > } > > return true; > > } > > > > The problem is that this function is O(n), but in PHP7 an array that is > > vector-like is likely to be packed and without holes (HT_IS_PACKED(x) && > > HT_IS_WITHOUT_HOLES(x)), in which case it is known to be vector-like > > without needing to iterate over it, but PHP code can't check for this > (nor > > should it be able to, since it's really an implementation detail). > > > > Would it be a good idea to define this is_vectorlike() function in the > PHP > > runtime, so it can short circuit to return true on packed arrays without > > holes? The above code would be a suitable polyfill. > > > > > I just read this thread and am wondering what exactly is the use case? Like > are you going to do something if it is vector-like, and do something > different (or not do anything at all) if it's not vector-like? I mean, if > you have to crawl it, and need a vector, why not just call array_values and > guarantee you have a vector? > > Also, given the implementation above, why does the array have to be ksorted > correctly to count? Specifically, why isn't this array considered vector > like? > $arr = [ > 1=> 1, > 0=> 0, > ]; > > Wouldn't the following be a better test? > function is_vectorlike(array $a): bool { > $l = count($a); > for (i=0; $i<$l, $i++) { > if (!array_key_exists($i, $a)) { > return false; > } > } > return true; > } > --001a113c998c329d50054edc6ed1--