Mastering PHP Data Structure 102 - phpDay 2012 Verona

  • Published on
    17-May-2015

  • View
    5.627

  • Download
    0

Embed Size (px)

DESCRIPTION

We all have certainly learned data structures at school: arrays, lists, sets, stacks, queues (LIFO/FIFO), heaps, associative arrays, trees, ... and what do we mostly use in PHP? The "array"! In most cases, we do everything and anything with it but we stumble upon it when profiling code. During this session, we'll learn again to use the structures appropriately, leaning closer on the way to employ arrays, the SPL and other structures from PHP extensions as well.

Transcript

  • 1. Mastering PHP Data Structure 102 Patrick Allaert phpDay 2012 Verona, Italy

2. About me Patrick Allaert Founder of Libereco Playing with PHP/Linux for +10 years eZ Publish core developer Author of the APM PHP extension @patrick_allaert patrickallaert@php.net http://github.com/patrickallaert/ http://patrickallaert.blogspot.com/ 3. APM 4. APM 5. PHP native datatypesNULL (IS_NULL)Booleans (IS_BOOL)Integers (IS_LONG)Floating point numbers(IS_DOUBLE)Strings (IS_STRING)Arrays (IS_ARRAY,IS_CONSTANT_ARRAY)Objects (IS_OBJECT)Resources (IS_RESOURCE)Callable (IS_CALLABLE) 6. Wikipedia datatypes2-3-4 tree Bit field Directed acyclic Hashed array tree Map/Associative Rapidly-exploring Suffix tree grapharray/Dictionaryrandom treeSymbol table2-3 heap Bitmap Hash list Directed graph MatrixRecord (also called Syntax tree2-3 tree BK-tree Disjoint-set Hash table tuple or struct) Tagged union (variantMetric tree AA treeBloom filterrecord, discriminated Distributed hash Hash treeRed-black tree Minimax tree union, disjoint union)Abstract syntax tree Boolean table Hash trieMin/max kd-treeRope Tango tree(a,b)-tree Double Routing table Bounding interval M-tree Ternary heap Heap Adaptive k-d treehierarchy Doubly connected R-tree Ternary search tree HeightmapMultigraphAdjacency list B sharp treeedge list R* tree Threaded binary treeMultimapAdjacency matrix BSP tree Doubly linked list Hilbert R-tree R+ tree Top treeMultiset TreapAF-heap Dynamic array Hypergraph Scapegoat tree B-tree Octree TreeAlternating decision Enfilade Scene graph Iliffe vector B*-tree tree Pagoda Trees Enumerated typeSegment tree Image B+ tree Pairing heap Trie And-inverter graph Expectiminimax treeSelf-balancing T-tree Implicit kd-tree Parallel array Andor tree B-trie Exponential tree binary search tree UB-tree Bx-tree Interval treeParse treeArray Fenwick tree Self-organizing list UnionPlain old dataAVL tree Cartesian tree Fibonacci heap IntSetUnrolled linked liststructure Beap Char Finger tree Judy array Prefix hash treeSkew heap Van Emde Boas treeSkip listVariable-length arrayBidirectional mapCircular buffer Float Kdb treePriority queue VListBinCompressed suffix FM-index Soft heapPropositional Kd-tree VP-treeBinary decisionarray Fusion tree directed acyclicSorted array Koorde Weight-balanced treediagramGap buffer graph Spaghetti stack Container Winged edgeBinary heapGeneralised suffix Leftist heap Quad-edge Sparse array Control table X-fast trieBinary search tree tree Lightmap QuadtreeSparse matrixXor linked list Cover tree Binary tree GraphQueap Splay tree X-tree Ctrie Linear octreeBinomial heap Graph-structured Queue SPQR-tree Y-fast trie Link/cut tree Dancing tree stackRadix tree Zero suppressedStackBit array D-ary heap Hash Linked listRandomized binary decision diagramStringBitboard Zipper Decision tree Hash array mapped Lookup table search tree trieSuffix array Z-order DequeRange tree 7. Game:Can you recognize some structures? 8. Array: PHPs untruthfulnessPHP Arrays are not true Arrays! 9. Array: PHPs untruthfulnessPHP Arrays are not true Arrays!An array typically looks like this: 012345Data Data Data Data Data Data 10. Array: PHPs untruthfulnessPHP Arrays can dynamically grow and be iteratedboth directions (reset(), next(), prev(), end()),exclusively with O(1) operations. 11. Array: PHPs untruthfulnessPHP Arrays can dynamically grow and be iteratedboth directions (reset(), next(), prev(), end()),exclusively with O(1) operations.Lets have a Doubly Linked List (DLL): HeadTail DataDataData DataDataEnables List, Deque, Queue and Stackimplementations 12. Array: PHPs untruthfulnessPHP Arrays elements are always accessible using akey (index). 13. Array: PHPs untruthfulnessPHP Arrays elements are always accessible using akey (index).Lets have an Hash Table:HeadBucket pointers array Tail012345... nTableSize -1 Bucket * Bucket * Bucket * Bucket * Bucket * Bucket *Bucket *Bucket BucketBucketBucketBucket Data Data DataData Data 14. Array: PHPs untruthfulnesshttp://php.net/manual/en/language.types.array.php:This type is optimized for severaldifferent uses; it can be treated as anarray, list (vector), hash table (animplementation of a map),dictionary, collection, stack, queue,and probably more. 15. Optimized for anything Optimized for nothing! 16. Array: PHPs untruthfulness In C: 100 000 integers (using long on 64bits => 8bytes) can be stored in 0.76 Mb. In PHP: it will take 13.97 Mb! A PHP variable (containing an integer) takes 48bytes. The overhead of buckets for every array entries isabout 96 bytes. More details:http://nikic.github.com/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html 17. Data Structure 18. Structs (or records, tuples,...) 19. Structs (or records, tuples,...) A struct is a value containing other values whichare typically accessed using a name. Example:Person => firstName / lastNameComplexNumber => realPart / imaginaryPart 20. Structs Using array$person = array("firstName" => "Patrick","lastName" => "Allaert"); 21. Structs Using a class$person = new PersonStruct("Patrick", "Allaert"); 22. Structs Using a class(Implementation)class PersonStruct{public $firstName;public $lastName;public function __construct($firstName, $lastName){$this->firstName = $firstName;$this->lastName = $lastName;}} 23. Structs Using a class(Implementation)class PersonStruct{public $firstName;public $lastName;public function __construct($firstName, $lastName){$this->firstName = $firstName;$this->lastName = $lastName;}public function __set($key, $value){// a. Do nothing// b. trigger_error()// c. Throws an exception}} 24. Structs Pros and ConsArrayClass+ Uses less memory (PHP < 5.4) - Uses more memory (PHP < 5.4)- Uses more memory (PHP = 5.4) + Uses less memory (PHP = 5.4)- No type hinting+ Type hinting possible- Flexible structure + Rigid structure+|- Less OO+|- More OOSlightly faster?Slightly slower? 25. (true) Arrays 26. (true) Arrays An array is a fixed size collection where elementsare each identified by a numeric index. 27. (true) Arrays An array is a fixed size collection where elementsare each identified by a numeric index. 012345Data Data Data Data Data Data 28. (true) Arrays Using SplFixedArray$array = new SplFixedArray(3);$array[0] = 1; // or $array->offsetSet()$array[1] = 2; // or $array->offsetSet()$array[2] = 3; // or $array->offsetSet()$array[0]; // gives 1$array[1]; // gives 2$array[2]; // gives 3 29. (true) Arrays Pros and ConsArray SplFixedArray- Uses more memory+ Uses less memory+|- Less OO +|- More OO 30. Queues 31. Queues A queue is an ordered collection respecting FirstIn, First Out (FIFO) order. Elements are inserted at one end and removed atthe other. 32. Queues A queue is an ordered collection respecting FirstIn, First Out (FIFO) order. Elements are inserted at one end and removed atthe other.Data Dequeue Data Data Data Data Data Data EnqueueData 33. Queues Using array$queue = array();$queue[] = 1; // or array_push()$queue[] = 2; // or array_push()$queue[] = 3; // or array_push()array_shift($queue); // gives 1array_shift($queue); // gives 2array_shift($queue); // gives 3 34. Queues Using SplQueue$queue = new SplQueue();$queue[] = 1; // or $queue->enqueue()$queue[] = 2; // or $queue->enqueue()$queue[] = 3; // or $queue->enqueue()$queue->dequeue(); // gives 1$queue->dequeue(); // gives 2$queue->dequeue(); // gives 3 35. Stacks 36. Stacks A stack is an ordered collection respecting Last In,First Out (LIFO) order. Elements are inserted and removed on the sameend. 37. Stacks A stack is an ordered collection respecting Last In,First Out (LIFO) order. Elements are inserted and removed on the sameend.DataPush Data Data Data Data Data DataPopData 38. Stacks Using array$stack = array();$stack[] = 1; // or array_push()$stack[] = 2; // or array_push()$stack[] = 3; // or array_push()array_pop($stack); // gives 3array_pop($stack); // gives 2array_pop($stack); // gives 1 39. Stacks Using SplStack$stack = new SplStack();$stack[] = 1; // or $stack->push()$stack[] = 2; // or $stack->push()$stack[] = 3; // or $stack->push()$stack->pop(); // gives 3$stack->pop(); // gives 2$stack->pop(); // gives 1 40. Queues/Stacks Pros and ConsArraySplQueue / SplStack- Uses more memory + Uses less memory(overhead / entry: 96 bytes) (overhead / entry: 48 bytes)- No type hinting+ Type hinting possible+|- Less OO+|- More OO 41. SetsGeeksNerdsPeople withstrong views on the distinctionbetween geeks and nerds 42. Sets A set is a collection with no particular orderingespecially suited for testing the membership of avalue against a collection or to performunion/intersection/complement operationsbetween them. 43. Sets A set is a collection with no particular orderingespecially suited for testing the membership of avalue against a collection or to performunion/intersection/complement operationsbetween them. Data Data DataDataData 44. Sets Using array$set = array();// Adding elements to a set$set[] = 1;$set[] = 2;$set[] = 3;// Checking presence in a setin_array(2, $set); // truein_array(5, $set); // falsearray_merge($set1, $set2); // unionarray_intersect($set1, $set2); // intersectionarray_diff($set1, $set2); // complement 45. Sets Using array$set = array();// Adding elements to a set$set[] = 1;$set[] = 2;$set[] = 3; True// Checking presence in a set performancein_array(2, $set); // truein_array(5, $set); // falsekillers!array_merge($set1, $set2); // unionarray_intersect($set1, $set2); // intersectionarray_diff($set1, $set2); // complement 46. Sets Mis-usageif ($value === "val1" || $value === "val2" || $value ==="val3"))){// ...} 47. Sets Mis-usageif (in_array($value, array("val1", "val2", "val3"))){// ...} 48. Sets Mis-usageswitch ($value){case "val1":case "val2":case "val3":// ...} 49. Sets Using array (simple types)$set = array();// Adding elements to a set$set[1] = true; // Any dummy value$set[2] = true; // is good but NULL!$set[3] = true;// Checking presence in a setisset($set[2]); // trueisset($set[5]); // false$set1 + $set2; // unionarray_intersect_key($set1, $set2); // intersectionarray_diff_key($set1, $set2); // complement 50. Sets Using array (simple types)$set = array();// Adding elements to a set$set[1] = true; // Any dummy value$set[2] = true; // is good but NULL!$set[3] = true;// Checking presence in a setisset($set[2]); // trueisset($set[5]); // false$set1 + $set2; // unionarray_intersect_key($set1, $set2); // intersectionarray_diff_key($set1, $set2); // complement Remember that PHP Array keys can be integers orstrings only! 51. Sets Using array (objects)$set = array();// Adding elements to a set$set[spl_object_hash($object1)] = $object1;$set[spl_object_hash($object2)] = $object2;$set[spl_object_hash($object3)] = $object3;// Checking presence in a setisset($set[spl_object_hash($object2)]); // trueisset($set[spl_object_hash($object5)]); // false$set1 + $set2; // unionarray_intersect_key($set1, $set2); // intersectionarray_diff_key($set1, $set2); // complement 52. Sets Using array (objects)$set = array();// Adding elements to a set$set[spl_object_hash($object1)] = $object1; Store a$set[spl_object_hash($object2)] = $object2; reference of$set[spl_object_hash($object3)] = $object3; the object!// Checking presence in a setisset($set[spl_object_hash($object2)]); // trueisset($set[spl_object_hash($object5)]); // false$set1 + $set2; // unionarray_intersect_key($set1, $set2); // intersectionarray_diff_key($set1, $set2); // complement 53. Sets Using SplObjectStorage (objects)$set = new SplObjectStorage();// Adding elements to a set$set->attach($object1); // or $set[$object1] = null;$set->attach($object2); // or $set[$object2] = null;$set->attach($object3); // or $set[$object3] = null;// Checking presence in a setisset($set[$object2]); // trueisset($set[$object2]); // false$set1->addAll($set2); // union$set1->removeAllExcept($set2); // intersection$set1->removeAll($set2); // complement 54. Sets Using QuickHash (int)$set = new QuickHashIntSet(64,QuickHashIntSet::CHECK_FOR_DUPES);// Adding elements to a set$set->add(1);$set->add(2);$set->add(3);// Checking presence in a set$set->exists(2); // true$set->exists(5); // false// Soonish: isset($set[2]); No union/intersection/complement operations(yet?) Yummy features like (loadFrom|saveTo)(String|File) 55. Sets Using bitsetsdefine("E_ERROR", 1); // or 1has("Foo"); // false, most probably 69. Other related projects SPL Types: Various types implemented as object:SplInt, SplFloat, SplEnum, SplBool and SplStringhttp://pecl.php.net/package/SPL_Types 70. Other related projects SPL Types: Various types implemented as object:SplInt, SplFloat, SplEnum, SplBool and SplStringhttp://pecl.php.net/package/SPL_Types Judy: Sparse dynamic arrays implementationhttp://pecl.php.net/package/Judy 71. Other related projects SPL Types: Various types implemented as object:SplInt, SplFloat, SplEnum, SplBool and SplStringhttp://pecl.php.net/package/SPL_Types Judy: Sparse dynamic arrays implementationhttp://pecl.php.net/package/Judy Weakref: Weak references implementation.Provides a gateway to an object withoutpreventing that object from being collected by thegarbage collector. 72. Conclusions Use appropriate data structure. It will keep yourcode clean and fast. 73. Conclusions Use appropriate data structure. It will keep yourcode clean and fast. Think about the time and space complexityinvolved by your algorithms. 74. Conclusions Use appropriate data structure. It will keep yourcode clean and fast. Think about the time and space complexityinvolved by your algorithms. Name your variables accordingly: use Map, Set,List, Queue,... to describe them instead of usingsomething like: $ordersArray. 75. Questions? 76. Thanks Dont forget to rate this talk on http://joind.in/6371 77. Photo Credits Tuned car:http://www.flickr.com/photos/gioxxswall/5783867752 London Eye Structure:http://www.flickr.com/photos/photographygal123/4883546484 Cigarette:http://www.flickr.com/photos/superfantastic/166215927 Heap structure:http://en.wikipedia.org/wiki/File:Max-Heap.svg Drawers:http://www.flickr.com/photos/jamesclay/2312912612 Stones stack:http://www.flickr.com/photos/silent_e/2282729987 Tree:http://www.flickr.com/photos/drewbandy/6002204996