Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101041 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74770 invoked from network); 3 Nov 2017 13:52:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Nov 2017 13:52:08 -0000 Authentication-Results: pb1.pair.com smtp.mail=thruska@cubiclesoft.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=thruska@cubiclesoft.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain cubiclesoft.com designates 149.56.142.28 as permitted sender) X-PHP-List-Original-Sender: thruska@cubiclesoft.com X-Host-Fingerprint: 149.56.142.28 28.ip-149-56-142.net Received: from [149.56.142.28] ([149.56.142.28:45718] helo=28.ip-149-56-142.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5D/8A-09857-9747CF95 for ; Fri, 03 Nov 2017 08:51:54 -0500 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: thruska@cubiclesoft.com) with ESMTPSA id A73EA3E84A To: PHP Development Message-ID: Date: Fri, 3 Nov 2017 06:51:48 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Inserting string keys in arrays at any position From: thruska@cubiclesoft.com (Thomas Hruska) I've been wondering for some time why PHP does not have the ability to efficiently insert string keys before/after an existing string key. Let's say I have an array of 10,000 string key/value pairs and I want to insert five new string key/value pairs at specific positions in the existing array. Unless I've missed something, despite looking at each of the array_...() functions, this is not possible in userland without copying the entire array to insert the new elements (or some real hackery such as append with some tracking info and use a function like uksort() to somehow only move the newly added items). There are several userland functions in the PHP documentation comments and also on StackOverflow that end up recreating the array each time they are called. Several examples: https://stackoverflow.com/a/7257599/917198 http://us1.php.net/manual/en/function.array-splice.php As the author of the OrderedHash implementation here: https://github.com/cubiclesoft/cross-platform-cpp I provide an ordered hash data structure that is similar-ish to the PHP array implementation, but it also has support for inserting a node anywhere in the OrderedHash by making a few pointer adjustments. Shifting a few pointers around is usually magnitudes faster than performing memory allocations and copying data. -- Thomas Hruska CubicleSoft President I've got great, time saving software that you will find useful. http://cubiclesoft.com/ And once you find my software useful: http://cubiclesoft.com/donate/