spl

P3369 【模板】普通平衡树

眉间皱痕 提交于 2019-12-05 19:28:31
题目链接: 参考于AgOH大佬的递归版splay做法!!! #include <bits/stdc++.h> using namespace std; #define ll long long #define re register const int N=1e6+10; void read(int &a) { a=0;int d=1;char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } struct note{int l,r,siz,cnt,val;}spl[N]; int rt,cnt; void newnode(int &now,int val) { spl[now=++cnt].val=val; spl[now].siz=1; spl[now].cnt=1; } void update(int now){spl[now].siz=spl[spl[now].l].siz+spl[spl[now].r].siz+spl[now].cnt;} void zig(int &now) { int l=spl[now].l; spl[now].l=spl[l]

PHP Recursive Iterator: Parent key of current array iteration?

北战南征 提交于 2019-12-05 14:46:52
问题 I have an array like this: $arr = array( $foo = array( 'donuts' => array( 'name' => 'lionel ritchie', 'animal' => 'manatee', ) ) ); Using that magic of the 'SPL Recursive Iterator' and this code: $bar = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)); foreach($bar as $key => $value) { echo $key . ": " . $value . "<br>"; } I can traverse the multidimensional array and return the key => value pairs, such as: name: lionel ritchie animal: manatee However, I need to return the

Sorting files per directory using SPL's DirectoryTreeIterator

那年仲夏 提交于 2019-12-05 08:07:59
问题 I found a couple of questions (this one and this question) related to the SPL iterators, but I'm not sure if they're helpful in my case, as I'm using a rather high level extension of the RecursiveIteratorIterator ; the DirectoryTreeIterator . Could somebody perhaps show me how to alter the DirectoryTreeIterator or how to sort the returned array per directory after it has been outputted by the iterator? A method of sorting the files correctly directly on the Apache server is also an option for

PHP RecursiveIterator traversing

丶灬走出姿态 提交于 2019-12-05 03:31:51
I have a structure representing a form and I want to iterate it using RecursiveIterator. The problem is this only returns the top-level questions. What am I doing wrong? Whole form: class Form implements RecursiveIterator{ private $id; private $caption; private $other_text; private $questions = array(); private $current; private function __construct(DibiRow $row){ $this->id = $row->id; $this->caption = $row->caption; $this->other_text = $row->other_text; $this->loadQuestions(); } private function loadQuestions(){ $questions = dibi::query('SELECT * FROM cyp_questions WHERE form_id = %i AND

What is the point of PHP's SplDoublyLinkedList class, and more importantly, Linked Lists in general?

折月煮酒 提交于 2019-12-04 22:42:34
On a quest to expand my programming prowess, I've delved ever-so-slightly into The Standard PHP Library . This led to my discovery of the SplDoublyLinkedList class. From there I read the descriptions of Linked Lists and Doubly Linked Lists on Wikipedia. I understand how they work... But I cannot conceive of a reason WHY we need it—or better yet a practical example of SplDoublyLinkedList since we have indexed and associative arrays in PHP. How are Linked Lists normally used in-and-out of PHP? The SPL data structures reduce memory consumption and improve performance. Good explanations: Data

SPLFileInfo: get filename without extension

有些话、适合烂在心里 提交于 2019-12-04 17:54:50
问题 I'm accessing a number of files in the SPLFileInfo object. I see a way to get the path, filename, and even extension of the file. Is there a way to get the filename without extension? Here's the code I've been working with but I'm hoping to get something more elegant. Is there an out of the box solution? $file = new SplFileInfo("path/to/file.txt.zip"); echo 'basename: '.$file->getBasename(); echo PHP_EOL; echo 'filename: '.$file->getFilename(); echo PHP_EOL; echo 'extension: '.$file-

PHP lazy array mapping

六眼飞鱼酱① 提交于 2019-12-04 10:13:51
问题 Is there a way of doing array_map but as an iterator? For example: foreach (new MapIterator($array, $function) as $value) { if ($value == $required) break; } The reason to do this is that $function is hard to calculate and $array has too many elements, only need to map until I find a specific value. array_map will calculate all values before I can search for the one I want. I could implement the iterator myself, but I want to know if there is a native way of doing this. I couldn't find

Associative Array versus SplObjectStorage

一笑奈何 提交于 2019-12-04 07:57:36
问题 I'm working on code to manage a collection of unique objects. The first prototype of this code utilises an associative array, basically as that's the way I've always done it. However, I'm also keen on taking advantage of functionality that's been added to more modern versions of PHP such as SplObjectStorage for doing this instead, partly as a learning experience, partly because it's bound to offer advantages (benchmarks I've seen suggest that SplObjectStorage can be faster than arrays in a

spl_object_hash for PHP < 5.2 (unique ID for object instances)

笑着哭i 提交于 2019-12-04 07:02:43
I'm trying to get unique IDs for object instances in PHP 5+. The function, spl_object_hash() is available from PHP 5.2 but I'm wondering if there is a workaround for older PHP versions. There are a couple of functions in the comments on php.net but they're not working for me. The first (simplified): function spl_object_hash($object){ if (is_object($object)){ return md5((string)$object); } return null; } does not work with native objects (such as DOMDocument), and the second: function spl_object_hash($object){ if (is_object($object)){ ob_start(); var_dump($object); $dump = ob_get_contents(); ob

SPL vs. Array: When should we use SPL and when should we use Array in PHP?

妖精的绣舞 提交于 2019-12-04 06:39:30
In java and C++ when we don't know the size - array not used like in PHP, instead used linkedList etc. In PHP exist SPL, but most of the times programmers use array, why (because people don't know about SPL )? When we should use Array in PHP and whenSPL and what is the difference in this case between PHP and Java/C++? Every PHP request must initialize all variables and after request they are freed. Because of that not often comes situations where special data structures (like maxheap, linkedlist or queue) are more efficient than array. Also arrays are much simpler to understand and use for