Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98976 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93448 invoked from network); 5 May 2017 21:19:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 May 2017 21:19:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=derokorian@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=derokorian@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.47 as permitted sender) X-PHP-List-Original-Sender: derokorian@gmail.com X-Host-Fingerprint: 209.85.218.47 mail-oi0-f47.google.com Received: from [209.85.218.47] ([209.85.218.47:35283] helo=mail-oi0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/EB-02776-A7CEC095 for ; Fri, 05 May 2017 17:19:55 -0400 Received: by mail-oi0-f47.google.com with SMTP id l18so1084652oig.2 for ; Fri, 05 May 2017 14:19:54 -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=ou7D1Wh+/X+ycNRD1nRgT9+UquoZReoZuwQkVNwPE7w=; b=sYZSMhQU4u8z1KifgboJjQ/FXZXraw6WK/lu4fpH+uB2LtL9ab7t4qlil0oAmMDXTi 986/yoUyNl10kW0yUi8Uts7CoJmtGkg9+hMSzjD0at0C1GG3gnKOiZpqYPEgl/h4jd9p I/5sZdrzOw43uVyslF9eXmlj6BkPb3zVAtItNdeBYvRUlLlR61bGw648C1UZBRN/XVub /5K+egO+UfHiUHI1s2JYqzdSwN8Vqi8SogDjvjMTX2MhYslgj5LUBCt8yXLJ0uVe2R1p d40ZuoFCYfk7Wq6XRJipNAIvrisl7WQE+T/jOxUj3QBX41lBz89qdnaHITRmW00ofNY9 QTVQ== 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=ou7D1Wh+/X+ycNRD1nRgT9+UquoZReoZuwQkVNwPE7w=; b=towARkhLDlZiibDzXKWLyfz68ZMwDjJNdaUUFZUpwyXBbGpT/CJ76yP3ZIQia7pygw 67ucpY4DfqKKAQNUxUPEODqk4SP5ucOKeIJcQEVEBrGPFiN6KUVtWiQwwURWzzzdgomm 5ukt9KIwg2swTqZy+qp+KmjPiKn02bAgCJZgJeUQBGt4rOvu4POzb/SKG8puKMR+9Ddv IAAxqiUCMOh2BYRtnBSOQANyabpkCRndH/xzUONpzffgA4xOS4lwZzqB+CKz/0m216z+ CC2BSCZjEoNpogu6TD9ADKCOHegAm1TtkFBdyf0Br39u9eG1oAA2R3fYCxG8nSQtQCyo TbZA== X-Gm-Message-State: AN3rC/48M35Wi7wlK8fxjDMQ0id+qba5WbnBGdtA4hNLl1Zrqp5boFbh BKyS8F6SWXQzVITRzmmchF2yn/GhSC3B X-Received: by 10.202.199.10 with SMTP id x10mr6030660oif.48.1494019192285; Fri, 05 May 2017 14:19:52 -0700 (PDT) MIME-Version: 1.0 Received: by 10.157.40.87 with HTTP; Fri, 5 May 2017 14:19:51 -0700 (PDT) In-Reply-To: References: Date: Fri, 5 May 2017 15:19:51 -0600 Message-ID: To: Jesse Schalken Cc: PHP internals Content-Type: multipart/alternative; boundary=001a1134f8b03e70ca054ecd73f5 Subject: Re: [PHP-DEV] Add is_vectorlike($array) function From: derokorian@gmail.com (Ryan Pallas) --001a1134f8b03e70ca054ecd73f5 Content-Type: text/plain; charset=UTF-8 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; } --001a1134f8b03e70ca054ecd73f5--