Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99344 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63684 invoked from network); 3 Jun 2017 14:34:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jun 2017 14:34:52 -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:47038] helo=28.ip-149-56-142.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3D/B0-12681-509C2395 for ; Sat, 03 Jun 2017 10:34:46 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: thruska@cubiclesoft.com) with ESMTPSA id 2BF903E848 To: Pawel Por , internals@lists.php.net References: Message-ID: Date: Sat, 3 Jun 2017 07:34:40 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Why hashtable keeps insertion order by default ? From: thruska@cubiclesoft.com (Thomas Hruska) On 6/3/2017 1:31 AM, Pawel Por wrote: > Hi, > > AFAIK the hashtable in PHP by default keeps the order in which the > elements are inserted. Could you please write me why it keeps the > insertion order by default ? > > Thanks in advance for clarification. Because it is a brilliant, high performance solution to extremely annoying classical data structures (aka unordered hashes) found in other programming and scripting languages. It's the only sane option to present to any language's users and the cost is minimal. PHP nailed it very early on and the PHP array implementation is the primary reason I use PHP today. If you want pseudorandom-ish order back in your userland PHP arrays, then you could try using array_rand() or similar. Keys can also be a mixture of integers and strings. Most hash table implementations require choosing one key type and sticking with that. The only weakness in PHP's implementation is that there aren't built-in userland functions to insert elements before/after specific keys. Users instead have to rebuild the whole array just to insert one element at a desired position while maintaining keys and order. It's occasionally useful to do so and rebuilding an array can be an expensive memory-churning operation when a much more performant solution is just slightly out of reach of userland. From: https://github.com/cubiclesoft/cross-platform-cpp "The detachable node ordered hash is similar to PHP arrays. It accepts both integer and string keys in the same hash, has almost constant time insert (anywhere!), lookup, delete, and iteration operations (both directions), and, most importantly, maintains the desired order of elements." All the potential benefits of marrying a hash table to a linked list in a single paragraph. -- 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/