SlideShare une entreprise Scribd logo
1  sur  75
Télécharger pour lire hors ligne
Masterizing PHP Data Structure 102
          Patrick Allaert




PHPBenelux Conference Antwerp 2012
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/
Masterizing =
 Mastering +
      Rising
PHP native datatypes
●   NULL (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)
Wikipedia datatypes
●
    2-3-4 tree             ●   Bit field           ●
                                                       Directed acyclic      ●   Hashed array tree Map/Associative
                                                                                                    ●                       ●
                                                                                                                                Rapidly-exploring      ●
                                                                                                                                                           Suffix tree
                                                       graph                                            array/Dictionary        random tree                Symbol table
●
    2-3 heap               ●
                               Bitmap                                            Hash list                                                             ●
                                                                             ●
                                                       Directed graph                                   Matrix                  Record (also called        Syntax tree
    2-3 tree                   BK-tree             ●                                                                        ●                          ●

                                                                                 Hash table
                                                                                                    ●
●
                           ●
                                                       Disjoint-set                                                             tuple or struct)           Tagged union (variant
                                                                             ●
                                                                                                        Metric tree
    AA tree
                                                                                                                                                       ●

                               Bloom filter                                                                                                                record, discriminated
                                                   ●                                                ●
●
                                                       Distributed hash          Hash tree                                      Red-black tree
                           ●
                                                                             ●                          Minimax tree        ●
                                                                                                                                                           union, disjoint union)
    Abstract syntax tree                           ●                                                ●
●
                           ●   Boolean                 table                     Hash trie              Min/max kd-tree     ●
                                                                                                                                Rope                       Tango tree
    (a,b)-tree
                                                                             ●                      ●                                                  ●
●
                               Bounding interval       Double                                                                   Routing table              Ternary heap
                           ●                       ●
                                                                                 Heap                   M-tree              ●

    Adaptive k-d tree
                                                                                                                                                       ●

                               hierarchy                                     ●                      ●
●                                                      Doubly connected                                                         R-tree                     Ternary search tree
                                                   ●
                                                                                 Heightmap              Multigraph          ●                          ●


●
    Adjacency list             B sharp tree            edge list             ●
                                                                                                    ●
                                                                                                                                R* tree                    Threaded binary tree
                           ●
                                                                                                        Multimap            ●                          ●


●
    Adjacency matrix           BSP tree            ●
                                                       Doubly linked list    ●   Hilbert R-tree     ●
                                                                                                                                R+ tree                    Top tree
                                                                                                        Multiset
                                                                                                                                                       ●
                           ●                                                                                                ●

    AF-heap                                            Dynamic array                                                                                       Treap
                                                                                 Hypergraph
                                                                                                    ●

                               B-tree                                                                                           Scapegoat tree         ●

                                                                                                        Octree
●                                                  ●                         ●                                              ●
                           ●
                                                                                                                                                           Tree
    Alternating decision                               Enfilade                                     ●
                                                                                                                                Scene graph
                                                                                 Iliffe vector
                                                                                                                                                       ●
●

    tree                   ●
                               B*-tree             ●
                                                                             ●                          Pagoda              ●
                                                                                                                                                           Trees
                                                       Enumerated type                              ●
                                                                                                                                Segment tree
                                                                                                                                                       ●


                               B+ tree
                                                   ●
                                                                                 Image                  Pairing heap        ●                              Trie
    And-inverter graph     ●
                                                       Expectiminimax tree
                                                                             ●                      ●                                                  ●
●
                                                                                                                                Self-balancing             T-tree
                                                                                 Implicit kd-tree       Parallel array
                                                   ●

    And–or tree            ●
                               B-trie                  Exponential tree      ●                      ●
                                                                                                                            ●

                                                                                                                                binary search tree
                                                                                                                                                       ●

●
                                                                                                                                                           UB-tree
                                                                                                        Parse tree
                                                   ●


●
    Array                  ●
                               Bx-tree                 Fenwick tree          ●   Interval tree      ●
                                                                                                                            ●
                                                                                                                                Self-organizing list
                                                                                                                                                       ●

                                                                                                                                                           Union
                                                                                                        Plain old data
                                                   ●
                                                                                                                                                       ●

    AVL tree                   Cartesian tree          Fibonacci heap            Int                ●
                                                                                                                                Set                        Unrolled linked list
●                          ●                       ●                         ●
                                                                                                        structure           ●                          ●



●
    Beap                   ●
                               Char                ●
                                                       Finger tree           ●   Judy array             Prefix hash tree    ●
                                                                                                                                Skew heap              ●
                                                                                                                                                           Van Emde Boas tree
                                                                                                    ●
                                                                                                                                Skip list                  Variable-length array
●
    Bidirectional map      ●
                               Circular buffer     ●
                                                       Float                     Kdb tree               Priority queue      ●
                                                                                                                                                       ●

                                                                             ●                      ●
                                                                                                                                                           VList
    Bin                        Compressed suffix FM-index                                                                       Soft heap              ●
●                          ●                       ●
                                                                                 Kd-tree            ●
                                                                                                        Propositional       ●
                                                                                                                                                           VP-tree
                                                                             ●
    Binary decision            array             Fusion tree                                            directed acyclic        Sorted array           ●

●                                                  ●
                                                                                 Koorde
                                                                                                                            ●
                                                                                                                                                           Weight-balanced tree
    diagram                                            Gap buffer                                       graph                   Spaghetti stack
                                                                                                                                                       ●

                           ●
                               Container           ●
                                                                             ●
                                                                                                                            ●
                                                                                                                                                           Winged edge
    Binary heap                                        Generalised suffix        Leftist heap           Quad-edge               Sparse array
                                                                                                                                                       ●

●
                           ●   Control table       ●
                                                                             ●                      ●
                                                                                                                            ●                              X-fast trie
                                                                                                        Quadtree
                                                                                                                                                       ●

    Binary search tree                                 tree                      Lightmap                                       Sparse matrix              Xor linked list
                               Cover tree
                                                                                                    ●
●                                                                            ●                                              ●                          ●


    Binary tree
                           ●
                                                       Graph                                            Queap                   Splay tree                 X-tree
●
                               Ctrie
                                                   ●
                                                                             ●   Linear octree      ●
                                                                                                                            ●                          ●


    Binomial heap
                           ●
                                                       Graph-structured                                 Queue                   SPQR-tree                  Y-fast trie
●
                               Dancing tree
                                                   ●

                                                       stack                     Link/cut tree      ●
                                                                                                                            ●
                                                                                                                                                       ●
                                                                             ●
                                                                                                        Radix tree                                         Zero suppressed
                                                                                                                                Stack
                           ●
    Bit array                                                                                       ●                                                  ●
●
                               D-ary heap              Hash                  ●   Linked list            Randomized binary
                                                                                                                            ●
                                                                                                                                                           decision diagram
    Bitboard
                           ●                       ●
                                                                                                    ●                           String                     Zipper
                                                       Hash array mapped         Lookup table           search tree
●                                                                                                                           ●
                               Decision tree       ●                         ●
                                                                                                                                Suffix array
                                                                                                                                                       ●
                           ●
                                                       trie                                                                                                Z-order
                               Deque                                                                ●
                                                                                                        Range tree          ●                          ●

                           ●
Game:
Can you recognize some structures?
Array: PHP's untruthfulness
PHP “Arrays” are not true Arrays!
Array: PHP's untruthfulness
PHP “Arrays” are not true Arrays!
An array is typically implemented like this:




               Data   Data   Data   Data   Data   Data
Array: PHP's untruthfulness
PHP “Arrays” can be iterated both directions (reset(),
next(), prev(), end()), exclusively with O(1) operations.
Array: PHP's untruthfulness
PHP “Arrays” can be iterated both directions (reset(),
next(), prev(), end()), exclusively with O(1) operations.
Implementation based on a Doubly Linked List (DLL):
                Head                    Tail

        Data      Data      Data      Data      Data



Enables List, Deque, Queue and Stack
implementations
Array: PHP's untruthfulness
PHP “Arrays” elements are always accessible using a
key (index).
Array: PHP's untruthfulness
PHP “Arrays” elements are always accessible using a
key (index).
Implementation based on a Hash Table:
      Head                              Bucket pointers array                                       Tail
                  0        1        2        3            4    5            ...   nTableSize -1
               Bucket * Bucket * Bucket * Bucket * Bucket * Bucket *                Bucket *




              Bucket           Bucket            Bucket            Bucket              Bucket




       Data               Data                   Data                  Data                       Data
Array: PHP's untruthfulness
http://php.net/manual/en/language.types.array.php:


        “This type is optimized for several
        different uses; it can be treated as an
        array, list (vector), hash table (an
        implementation of a map),
        dictionary, collection, stack, queue,
        and probably more.”
Optimized for anything ≈ Optimized for nothing!
Array: PHP's untruthfulness
●   In C: 100 000 integers (using long on 64bits => 8
    bytes) can be stored in 0.76 Mb.
●   In PHP: it will take ≅ 13.97 Mb!
●   A PHP variable (containing an integer) takes 48
    bytes.
●   The overhead of buckets for every “array” entries is
    about 96 bytes.
●   More details:
    http://nikic.github.com/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html
Data Structure
Structs (or records, tuples,...)
●   A struct is a value containing other values which
    are typically accessed using a name.
●   Example:
    Person => firstName / lastName
    ComplexNumber => realPart / imaginaryPart
Structs – Using array
$person = array(
    "firstName" => "Patrick",
    "lastName" => "Allaert"
);
Structs – Using a class
$person = new PersonStruct(
    "Patrick", "Allaert"
);
Structs – Using a class
              (Implementation)
class PersonStruct
{
    public $firstName;
    public $lastName;

    public function __construct($firstName, $lastName)
    {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}
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
    }
}
Structs – Pros and Cons
              Array                            Class
+ 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 OO
+ Slightly faster                - Slightly slower
“true” Arrays
●   An array is a fixed size collection where elements
    are each identified by a numeric index.
“true” Arrays
●   An array is a fixed size collection where elements
    are each identified by a numeric index.

                 0      1      2      3      4      5


                Data   Data   Data   Data   Data   Data
“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
“true” Arrays – Pros and Cons
              Array           SplFixedArray
- Uses more memory     + Uses less memory
+|- Less OO            +|- More OO
+ Slightly faster      - Slightly slower
Queues
●   A queue is an ordered collection respecting First
    In, First Out (FIFO) order.
●   Elements are inserted at one end and removed at
    the other.
Queues
●   A queue is an ordered collection respecting First
    In, First Out (FIFO) order.
●   Elements are inserted at one end and removed at
    the other.

          Data

                                                           Dequeue
                 Data   Data   Data   Data   Data   Data
       Enqueue

                                                            Data
Queues – Using array
$queue = array();
$queue[] = 1; // or array_push()
$queue[] = 2; // or array_push()
$queue[] = 3; // or array_push()
array_shift($queue); // gives 1
array_shift($queue); // gives 2
array_shift($queue); // gives 3
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
Queues – Pros and Cons
              Array                     SplQueue
- Uses more memory             + Uses less memory
(overhead / entry: 96 bytes)   (overhead / entry: 48 bytes)
- No type hinting              + Type hinting possible
+|- Less OO                    +|- More OO
Stacks
●   A stack is an ordered collection respecting Last In,
    First Out (LIFO) order.
●   Elements are inserted and removed on the same
    end.
Stacks
●   A stack is an ordered collection respecting Last In,
    First Out (LIFO) order.
●   Elements are inserted and removed on the same
    end.

          Data

        Push
                 Data   Data   Data   Data   Data   Data
        Pop


          Data
Stacks – Using array
$stack = array();
$stack[] = 1; // or array_push()
$stack[] = 2; // or array_push()
$stack[] = 3; // or array_push()
array_pop($stack); // gives 3
array_pop($stack); // gives 2
array_pop($stack); // gives 1
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
Stacks – Pros and Cons
              Array                          Class
- Uses more memory             + Uses less memory
(overhead / entry: 96 bytes)   (overhead / entry: 48 bytes)
- No type hinting              + Type hinting possible
+|- Less OO                    +|- More OO
Sets
●   A set is a collection with no particular ordering
    especially suited for testing the membership of a
    value against a collection or to perform
    union/intersection/complement operations
    between them.
Sets
●   A set is a collection with no particular ordering
    especially suited for testing the membership of a
    value against a collection or to perform
    union/intersection/complement operations
    between them.
                                     Data

                     Data
                                   Data

                                            Data
                            Data
Sets – Using array
$set =   array();
$set[]   = 1;
$set[]   = 2;
$set[]   = 3;

in_array(2, $set); // true
in_array(5, $set); // false

array_merge($set1, $set2); // union
array_intersect($set1, $set2); // intersection
array_diff($set1, $set2); // complement
Sets – Using array
$set =   array();
$set[]   = 1;
$set[]   = 2;
$set[]   = 3;

in_array(2, $set); // true     True
in_array(5, $set); // false
                               performance
                               killers!
array_merge($set1, $set2); // union
array_intersect($set1, $set2); // intersection
array_diff($set1, $set2); // complement
Sets – Using array (simple types)
$set = array();
$set[1] = true; // Any dummy value
$set[2] = true; // is good but NULL!
$set[3] = true;

isset($set[2]); // true
isset($set[5]); // false

$set1 + $set2; // union
array_intersect_key($set1, $set2); // intersection
array_diff_key($set1, $set2); // complement
Sets – Using array (simple types)
$set = array();
$set[1] = true; // Any dummy value
$set[2] = true; // is good but NULL!
$set[3] = true;

isset($set[2]); // true
isset($set[5]); // false

$set1 + $set2; // union
array_intersect_key($set1, $set2); // intersection
array_diff_key($set1, $set2); // complement

●   Remember that PHP Array keys can be integers or
    strings only!
Sets – Using array (objects)
$set = array();
$set[spl_object_hash($object1)] = $object1;
$set[spl_object_hash($object2)] = $object2;
$set[spl_object_hash($object3)] = $object3;

isset($set[spl_object_hash($object2)]); // true
isset($set[spl_object_hash($object5)]); // false

$set1 + $set2; // union
array_intersect_key($set1, $set2); // intersection
array_diff_key($set1, $set2); // complement
Sets – Using SplObjectStorage
                 (objects)
$set = new SplObjectStorage();
$set->attach($object1); // or $set[$object1] = null;
$set->attach($object2); // or $set[$object2] = null;
$set->attach($object3); // or $set[$object3] = null;

isset($set[$object2]); // true
isset($set[$object2]); // false

$set1->addAll($set2); // union
$set1->removeAllExcept($set2); // intersection
$set1->removeAll($set2); // complement
Sets – Using QuickHash (int)
$set = new QuickHashIntSet(64,
QuickHashIntSet::CHECK_FOR_DUPES);
$set->add(1);
$set->add(2);
$set->add(3);

$set->exists(2); // true
$set->exists(5); // false

●   No union/intersection/complement operations
    (yet?)
●   Yummy features like (loadFrom|saveTo)(String|File)
Sets – With finite possible values
define("E_ERROR", 1); // or 1<<0
define("E_WARNING", 2); // or 1<<1
define("E_PARSE", 4); // or 1<<2
define("E_NOTICE", 8); // or 1<<3

$set   = 0;
$set   |= E_ERROR;
$set   |= E_WARNING;
$set   |= E_PARSE;

$set & E_ERROR; // true
$set & E_NOTICE; // false

$set1 | $set2; // union
$set1 & $set2; // intersection
$set1 ^ $set2; // complement
Sets – With finite possible values
            (function features)
Instead of:
function remove($path, $files = true, $directories = true, $links = true,
$executable = true)
{
    if (!$files && is_file($path))
        return false;

    if (!$directories && is_dir($path))
        return false;

    if (!$links && is_link($path))
        return false;

    if (!$executable && is_executable($path))
        return false;

    // ...
}

remove("/tmp/removeMe", true, false, true, false); // WTF ?!
Sets – With finite possible values
             (function features)
Instead of:
define("REMOVE_FILES", 1 << 0);
define("REMOVE_DIRS", 1 << 1);
define("REMOVE_LINKS", 1 << 2);
define("REMOVE_EXEC", 1 << 3);
define("REMOVE_ALL", ~0); // Setting all bits

function remove($path, $options = REMOVE_ALL)
{
    if (~$options & REMOVE_FILES && is_file($path))
        return false;

    if (~$options & REMOVE_DIRS && is_dir($path))
        return false;

    if (~$options & REMOVE_LINKS && is_link($path))
        return false;

    if (~$options & REMOVE_EXEC && is_executable($path))
        return false;

    // ...
}

remove("/tmp/removeMe", REMOVE_FILES | REMOVE_LINKS); // Much better :)
Sets: Conclusions
●   Use the key and not the value when using PHP
    Arrays.
●   Use QuickHash for set of integers if possible.
●   Use SplObjectStorage as soon as you are playing
    with objects.
●   Don't use array_unique() when you need a set!
Bloom filters
●   A bloom filter is a space-efficient probabilistic data
    structure used to test whether an element is
    member of a set.
●   False positives are possible, but false negatives are
    not!
Bloom filters – Using bloomy
// BloomFilter::__construct(int capacity [, double
error_rate [, int random_seed ] ])
$bloomFilter = new BloomFilter(10000, 0.001);

$bloomFilter->add("An element");

$bloomFilter->has("An element"); // true for sure
$bloomFilter->has("Foo"); // false, most probably
Maps
●   A map is a collection of key/value pairs where all
    keys are unique.
Maps – Using array
$map = array();
$map["ONE"] = 1;
$map["TWO"] = 2;
$map["THREE"] = 3;

// Merging maps:
array_merge($map1, $map2); // SLOW!
$map2 + $map1; // Fast :)
●   Don't use array_merge() on maps.
Multikey Maps – Using array
$map = array();
$map["ONE"] = 1;
$map["UN"] =& $map["ONE"];
$map["UNO"] =& $map["ONE"];
$map["TWO"] = 2;
$map["DEUX"] =& $map["TWO"];
$map["DUE"] =& $map["TWO"];

$map["UNO"] = "once";
$map["DEUX"] = "twice";

var_dump($map);
/*
array(6) {
["ONE"] => &string(4) "once"
● Don't use array_merge() on maps.
["UN"] => &string(4) "once"
["UNO"] => &string(4) "once"
["TWO"] => &string(5) "twice"
["DEUX"] => &string(5) "twice"
["DUE"] => &string(5) "twice"
}
*/
Heap
●   A heap is a tree-based structure in which all
    elements are ordered with largest key at the top,
    and the smallest one as leafs.
Heap
●   A heap is a tree-based structure in which all
    elements are ordered with largest key at the top,
    and the smallest one as leafs.
Heap – Using array
$heap = array();
$heap[] = 3;
sort($heap);
$heap[] = 1;
sort($heap);
$heap[] = 2;
sort($heap);
Heap – Using Spl(Min|Max)Heap
$heap = new SplMinHeap;
$heap->insert(3);
$heap->insert(1);
$heap->insert(2);
Heaps: Conclusions
●   MUCH faster than having to re-sort() an array at
    every insertion.
●   If you don't require a collection to be sorted at
    every single step and can insert all data at once
    and then sort(). Array is a much better/faster
    approach.
●   SplPriorityQueue is very similar, consider it is the
    same as SplHeap but where the sorting is made on
    the key rather than the value.
Other related projects
●   SPL Types: Various types implemented as object:
    SplInt, SplFloat, SplEnum, SplBool and SplString
    http://pecl.php.net/package/SPL_Types
Other related projects
●   SPL Types: Various types implemented as object:
    SplInt, SplFloat, SplEnum, SplBool and SplString
    http://pecl.php.net/package/SPL_Types
●   Judy: Sparse dynamic arrays implementation
    http://pecl.php.net/package/Judy
Other related projects
●   SPL Types: Various types implemented as object:
    SplInt, SplFloat, SplEnum, SplBool and SplString
    http://pecl.php.net/package/SPL_Types
●   Judy: Sparse dynamic arrays implementation
    http://pecl.php.net/package/Judy
●   Weakref: Weak references implementation.
    Provides a gateway to an object without
    preventing that object from being collected by the
    garbage collector.
Conclusions
●   Use appropriate data structure. It will keep your
    code clean and fast.
Conclusions
●   Use appropriate data structure. It will keep your
    code clean and fast.
●   Think about the time and space complexity
    involved by your algorithms.
Conclusions
●   Use appropriate data structure. It will keep your
    code clean and fast.
●   Think about the time and space complexity
    involved by your algorithms.
●   Name your variables accordingly: use “Map”, “Set”,
    “List”, “Queue”,... to describe them instead of using
    something like: $ordersArray.
Questions?
Thanks
●   Don't forget to rate this talk on https://joind.in/4753
Photo Credits
●   Northstar Ski Jump:
    http://www.flickr.com/photos/renotahoe/5593248965
●   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

Contenu connexe

En vedette

Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modifiedInbok Lee
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresAmrinder Arora
 
Integrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML DatabaseIntegrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML Databaselucenerevolution
 
Gesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniquesGesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniquesDong-Bach Vo
 
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)Yusuke Iwasawa
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...go_oh
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithmKamal Nayan
 
data structure and algorithms
data structure and algorithmsdata structure and algorithms
data structure and algorithmsibrar ahmad
 
Building and deploying PHP applications with Phing
Building and deploying PHP applications with PhingBuilding and deploying PHP applications with Phing
Building and deploying PHP applications with PhingMichiel Rook
 
Semi supervised learning
Semi supervised learningSemi supervised learning
Semi supervised learningAhmed Taha
 
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...Spark Summit
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)Nikita Popov
 

En vedette (19)

Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modified
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data Structures
 
Integrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML DatabaseIntegrating Lucene into a Transactional XML Database
Integrating Lucene into a Transactional XML Database
 
Xml databases
Xml databasesXml databases
Xml databases
 
Gesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniquesGesture-aware remote controls: guidelines and interaction techniques
Gesture-aware remote controls: guidelines and interaction techniques
 
Algorithms and Data Structures~hmftj
Algorithms and Data Structures~hmftjAlgorithms and Data Structures~hmftj
Algorithms and Data Structures~hmftj
 
XML In My Database!
XML In My Database!XML In My Database!
XML In My Database!
 
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
[DL Hacks輪読] Semi-Supervised Learning with Ladder Networks (NIPS2015)
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...
 
06. string matching
06. string matching06. string matching
06. string matching
 
XML Databases
XML DatabasesXML Databases
XML Databases
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithm
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
data structure and algorithms
data structure and algorithmsdata structure and algorithms
data structure and algorithms
 
Building and deploying PHP applications with Phing
Building and deploying PHP applications with PhingBuilding and deploying PHP applications with Phing
Building and deploying PHP applications with Phing
 
Semi supervised learning
Semi supervised learningSemi supervised learning
Semi supervised learning
 
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
Extending Word2Vec for Performance and Semi-Supervised Learning-(Michael Mala...
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
 
Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
 

Plus de Patrick Allaert

La métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devopsLa métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devopsPatrick Allaert
 
Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012Patrick Allaert
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaPatrick Allaert
 
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 VeronaMastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 VeronaPatrick Allaert
 
Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012Patrick Allaert
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPatrick Allaert
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaPatrick Allaert
 
MVC = Make Venerated Code?
MVC = Make Venerated Code?MVC = Make Venerated Code?
MVC = Make Venerated Code?Patrick Allaert
 

Plus de Patrick Allaert (8)

La métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devopsLa métrique, ce n'est pas que pour le devops
La métrique, ce n'est pas que pour le devops
 
Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012Maitriser les structures de données PHP 102 - Forum Paris 2012
Maitriser les structures de données PHP 102 - Forum Paris 2012
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 VeronaMastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
 
Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012Masterizing PHP Data Structure 102 - PHPUK 2012
Masterizing PHP Data Structure 102 - PHPUK 2012
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & Pinba
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et Pinba
 
MVC = Make Venerated Code?
MVC = Make Venerated Code?MVC = Make Venerated Code?
MVC = Make Venerated Code?
 

Dernier

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Dernier (20)

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

Mastering PHP Data Structures 102 Conference

  • 1. Masterizing PHP Data Structure 102 Patrick Allaert PHPBenelux Conference Antwerp 2012
  • 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/
  • 4. PHP native datatypes ● NULL (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)
  • 5. Wikipedia datatypes ● 2-3-4 tree ● Bit field ● Directed acyclic ● Hashed array tree Map/Associative ● ● Rapidly-exploring ● Suffix tree graph array/Dictionary random tree Symbol table ● 2-3 heap ● Bitmap Hash list ● ● Directed graph Matrix Record (also called Syntax tree 2-3 tree BK-tree ● ● ● Hash table ● ● ● Disjoint-set tuple or struct) Tagged union (variant ● Metric tree AA tree ● Bloom filter record, discriminated ● ● ● Distributed hash Hash tree Red-black tree ● ● Minimax tree ● union, disjoint union) Abstract syntax tree ● ● ● ● Boolean table Hash trie Min/max kd-tree ● Rope Tango tree (a,b)-tree ● ● ● ● Bounding interval Double Routing table Ternary heap ● ● Heap M-tree ● Adaptive k-d tree ● hierarchy ● ● ● Doubly connected R-tree Ternary search tree ● Heightmap Multigraph ● ● ● Adjacency list B sharp tree edge list ● ● R* tree Threaded binary tree ● Multimap ● ● ● Adjacency matrix BSP tree ● Doubly linked list ● Hilbert R-tree ● R+ tree Top tree Multiset ● ● ● AF-heap Dynamic array Treap Hypergraph ● B-tree Scapegoat tree ● Octree ● ● ● ● ● Tree Alternating decision Enfilade ● Scene graph Iliffe vector ● ● tree ● B*-tree ● ● Pagoda ● Trees Enumerated type ● Segment tree ● B+ tree ● Image Pairing heap ● Trie And-inverter graph ● Expectiminimax tree ● ● ● ● Self-balancing T-tree Implicit kd-tree Parallel array ● And–or tree ● B-trie Exponential tree ● ● ● binary search tree ● ● UB-tree Parse tree ● ● Array ● Bx-tree Fenwick tree ● Interval tree ● ● Self-organizing list ● Union Plain old data ● ● AVL tree Cartesian tree Fibonacci heap Int ● Set Unrolled linked list ● ● ● ● structure ● ● ● Beap ● Char ● Finger tree ● Judy array Prefix hash tree ● Skew heap ● Van Emde Boas tree ● Skip list Variable-length array ● Bidirectional map ● Circular buffer ● Float Kdb tree Priority queue ● ● ● ● VList Bin Compressed suffix FM-index Soft heap ● ● ● ● Kd-tree ● Propositional ● VP-tree ● Binary decision array Fusion tree directed acyclic Sorted array ● ● ● Koorde ● Weight-balanced tree diagram Gap buffer graph Spaghetti stack ● ● Container ● ● ● Winged edge Binary heap Generalised suffix Leftist heap Quad-edge Sparse array ● ● ● Control table ● ● ● ● X-fast trie Quadtree ● Binary search tree tree Lightmap Sparse matrix Xor linked list Cover tree ● ● ● ● ● Binary tree ● Graph Queap Splay tree X-tree ● Ctrie ● ● Linear octree ● ● ● Binomial heap ● Graph-structured Queue SPQR-tree Y-fast trie ● Dancing tree ● stack Link/cut tree ● ● ● ● Radix tree Zero suppressed Stack ● Bit array ● ● ● D-ary heap Hash ● Linked list Randomized binary ● decision diagram Bitboard ● ● ● String Zipper Hash array mapped Lookup table search tree ● ● Decision tree ● ● Suffix array ● ● trie Z-order Deque ● Range tree ● ● ●
  • 6. Game: Can you recognize some structures?
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. Array: PHP's untruthfulness PHP “Arrays” are not true Arrays!
  • 15. Array: PHP's untruthfulness PHP “Arrays” are not true Arrays! An array is typically implemented like this: Data Data Data Data Data Data
  • 16. Array: PHP's untruthfulness PHP “Arrays” can be iterated both directions (reset(), next(), prev(), end()), exclusively with O(1) operations.
  • 17. Array: PHP's untruthfulness PHP “Arrays” can be iterated both directions (reset(), next(), prev(), end()), exclusively with O(1) operations. Implementation based on a Doubly Linked List (DLL): Head Tail Data Data Data Data Data Enables List, Deque, Queue and Stack implementations
  • 18. Array: PHP's untruthfulness PHP “Arrays” elements are always accessible using a key (index).
  • 19. Array: PHP's untruthfulness PHP “Arrays” elements are always accessible using a key (index). Implementation based on a Hash Table: Head Bucket pointers array Tail 0 1 2 3 4 5 ... nTableSize -1 Bucket * Bucket * Bucket * Bucket * Bucket * Bucket * Bucket * Bucket Bucket Bucket Bucket Bucket Data Data Data Data Data
  • 20. Array: PHP's untruthfulness http://php.net/manual/en/language.types.array.php: “This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.”
  • 21. Optimized for anything ≈ Optimized for nothing!
  • 22. Array: PHP's untruthfulness ● In C: 100 000 integers (using long on 64bits => 8 bytes) can be stored in 0.76 Mb. ● In PHP: it will take ≅ 13.97 Mb! ● A PHP variable (containing an integer) takes 48 bytes. ● The overhead of buckets for every “array” entries is about 96 bytes. ● More details: http://nikic.github.com/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html
  • 24. Structs (or records, tuples,...) ● A struct is a value containing other values which are typically accessed using a name. ● Example: Person => firstName / lastName ComplexNumber => realPart / imaginaryPart
  • 25. Structs – Using array $person = array( "firstName" => "Patrick", "lastName" => "Allaert" );
  • 26. Structs – Using a class $person = new PersonStruct( "Patrick", "Allaert" );
  • 27. Structs – Using a class (Implementation) class PersonStruct { public $firstName; public $lastName; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } }
  • 28. 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 } }
  • 29. Structs – Pros and Cons Array Class + 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 OO + Slightly faster - Slightly slower
  • 30. “true” Arrays ● An array is a fixed size collection where elements are each identified by a numeric index.
  • 31. “true” Arrays ● An array is a fixed size collection where elements are each identified by a numeric index. 0 1 2 3 4 5 Data Data Data Data Data Data
  • 32. “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
  • 33. “true” Arrays – Pros and Cons Array SplFixedArray - Uses more memory + Uses less memory +|- Less OO +|- More OO + Slightly faster - Slightly slower
  • 34. Queues ● A queue is an ordered collection respecting First In, First Out (FIFO) order. ● Elements are inserted at one end and removed at the other.
  • 35. Queues ● A queue is an ordered collection respecting First In, First Out (FIFO) order. ● Elements are inserted at one end and removed at the other. Data Dequeue Data Data Data Data Data Data Enqueue Data
  • 36. Queues – Using array $queue = array(); $queue[] = 1; // or array_push() $queue[] = 2; // or array_push() $queue[] = 3; // or array_push() array_shift($queue); // gives 1 array_shift($queue); // gives 2 array_shift($queue); // gives 3
  • 37. 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
  • 38. Queues – Pros and Cons Array SplQueue - Uses more memory + Uses less memory (overhead / entry: 96 bytes) (overhead / entry: 48 bytes) - No type hinting + Type hinting possible +|- Less OO +|- More OO
  • 39. Stacks ● A stack is an ordered collection respecting Last In, First Out (LIFO) order. ● Elements are inserted and removed on the same end.
  • 40. Stacks ● A stack is an ordered collection respecting Last In, First Out (LIFO) order. ● Elements are inserted and removed on the same end. Data Push Data Data Data Data Data Data Pop Data
  • 41. Stacks – Using array $stack = array(); $stack[] = 1; // or array_push() $stack[] = 2; // or array_push() $stack[] = 3; // or array_push() array_pop($stack); // gives 3 array_pop($stack); // gives 2 array_pop($stack); // gives 1
  • 42. 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
  • 43. Stacks – Pros and Cons Array Class - Uses more memory + Uses less memory (overhead / entry: 96 bytes) (overhead / entry: 48 bytes) - No type hinting + Type hinting possible +|- Less OO +|- More OO
  • 44. Sets ● A set is a collection with no particular ordering especially suited for testing the membership of a value against a collection or to perform union/intersection/complement operations between them.
  • 45. Sets ● A set is a collection with no particular ordering especially suited for testing the membership of a value against a collection or to perform union/intersection/complement operations between them. Data Data Data Data Data
  • 46. Sets – Using array $set = array(); $set[] = 1; $set[] = 2; $set[] = 3; in_array(2, $set); // true in_array(5, $set); // false array_merge($set1, $set2); // union array_intersect($set1, $set2); // intersection array_diff($set1, $set2); // complement
  • 47. Sets – Using array $set = array(); $set[] = 1; $set[] = 2; $set[] = 3; in_array(2, $set); // true True in_array(5, $set); // false performance killers! array_merge($set1, $set2); // union array_intersect($set1, $set2); // intersection array_diff($set1, $set2); // complement
  • 48. Sets – Using array (simple types) $set = array(); $set[1] = true; // Any dummy value $set[2] = true; // is good but NULL! $set[3] = true; isset($set[2]); // true isset($set[5]); // false $set1 + $set2; // union array_intersect_key($set1, $set2); // intersection array_diff_key($set1, $set2); // complement
  • 49. Sets – Using array (simple types) $set = array(); $set[1] = true; // Any dummy value $set[2] = true; // is good but NULL! $set[3] = true; isset($set[2]); // true isset($set[5]); // false $set1 + $set2; // union array_intersect_key($set1, $set2); // intersection array_diff_key($set1, $set2); // complement ● Remember that PHP Array keys can be integers or strings only!
  • 50. Sets – Using array (objects) $set = array(); $set[spl_object_hash($object1)] = $object1; $set[spl_object_hash($object2)] = $object2; $set[spl_object_hash($object3)] = $object3; isset($set[spl_object_hash($object2)]); // true isset($set[spl_object_hash($object5)]); // false $set1 + $set2; // union array_intersect_key($set1, $set2); // intersection array_diff_key($set1, $set2); // complement
  • 51. Sets – Using SplObjectStorage (objects) $set = new SplObjectStorage(); $set->attach($object1); // or $set[$object1] = null; $set->attach($object2); // or $set[$object2] = null; $set->attach($object3); // or $set[$object3] = null; isset($set[$object2]); // true isset($set[$object2]); // false $set1->addAll($set2); // union $set1->removeAllExcept($set2); // intersection $set1->removeAll($set2); // complement
  • 52. Sets – Using QuickHash (int) $set = new QuickHashIntSet(64, QuickHashIntSet::CHECK_FOR_DUPES); $set->add(1); $set->add(2); $set->add(3); $set->exists(2); // true $set->exists(5); // false ● No union/intersection/complement operations (yet?) ● Yummy features like (loadFrom|saveTo)(String|File)
  • 53. Sets – With finite possible values define("E_ERROR", 1); // or 1<<0 define("E_WARNING", 2); // or 1<<1 define("E_PARSE", 4); // or 1<<2 define("E_NOTICE", 8); // or 1<<3 $set = 0; $set |= E_ERROR; $set |= E_WARNING; $set |= E_PARSE; $set & E_ERROR; // true $set & E_NOTICE; // false $set1 | $set2; // union $set1 & $set2; // intersection $set1 ^ $set2; // complement
  • 54. Sets – With finite possible values (function features) Instead of: function remove($path, $files = true, $directories = true, $links = true, $executable = true) { if (!$files && is_file($path)) return false; if (!$directories && is_dir($path)) return false; if (!$links && is_link($path)) return false; if (!$executable && is_executable($path)) return false; // ... } remove("/tmp/removeMe", true, false, true, false); // WTF ?!
  • 55. Sets – With finite possible values (function features) Instead of: define("REMOVE_FILES", 1 << 0); define("REMOVE_DIRS", 1 << 1); define("REMOVE_LINKS", 1 << 2); define("REMOVE_EXEC", 1 << 3); define("REMOVE_ALL", ~0); // Setting all bits function remove($path, $options = REMOVE_ALL) { if (~$options & REMOVE_FILES && is_file($path)) return false; if (~$options & REMOVE_DIRS && is_dir($path)) return false; if (~$options & REMOVE_LINKS && is_link($path)) return false; if (~$options & REMOVE_EXEC && is_executable($path)) return false; // ... } remove("/tmp/removeMe", REMOVE_FILES | REMOVE_LINKS); // Much better :)
  • 56. Sets: Conclusions ● Use the key and not the value when using PHP Arrays. ● Use QuickHash for set of integers if possible. ● Use SplObjectStorage as soon as you are playing with objects. ● Don't use array_unique() when you need a set!
  • 57. Bloom filters ● A bloom filter is a space-efficient probabilistic data structure used to test whether an element is member of a set. ● False positives are possible, but false negatives are not!
  • 58. Bloom filters – Using bloomy // BloomFilter::__construct(int capacity [, double error_rate [, int random_seed ] ]) $bloomFilter = new BloomFilter(10000, 0.001); $bloomFilter->add("An element"); $bloomFilter->has("An element"); // true for sure $bloomFilter->has("Foo"); // false, most probably
  • 59. Maps ● A map is a collection of key/value pairs where all keys are unique.
  • 60. Maps – Using array $map = array(); $map["ONE"] = 1; $map["TWO"] = 2; $map["THREE"] = 3; // Merging maps: array_merge($map1, $map2); // SLOW! $map2 + $map1; // Fast :) ● Don't use array_merge() on maps.
  • 61. Multikey Maps – Using array $map = array(); $map["ONE"] = 1; $map["UN"] =& $map["ONE"]; $map["UNO"] =& $map["ONE"]; $map["TWO"] = 2; $map["DEUX"] =& $map["TWO"]; $map["DUE"] =& $map["TWO"]; $map["UNO"] = "once"; $map["DEUX"] = "twice"; var_dump($map); /* array(6) { ["ONE"] => &string(4) "once" ● Don't use array_merge() on maps. ["UN"] => &string(4) "once" ["UNO"] => &string(4) "once" ["TWO"] => &string(5) "twice" ["DEUX"] => &string(5) "twice" ["DUE"] => &string(5) "twice" } */
  • 62. Heap ● A heap is a tree-based structure in which all elements are ordered with largest key at the top, and the smallest one as leafs.
  • 63. Heap ● A heap is a tree-based structure in which all elements are ordered with largest key at the top, and the smallest one as leafs.
  • 64. Heap – Using array $heap = array(); $heap[] = 3; sort($heap); $heap[] = 1; sort($heap); $heap[] = 2; sort($heap);
  • 65. Heap – Using Spl(Min|Max)Heap $heap = new SplMinHeap; $heap->insert(3); $heap->insert(1); $heap->insert(2);
  • 66. Heaps: Conclusions ● MUCH faster than having to re-sort() an array at every insertion. ● If you don't require a collection to be sorted at every single step and can insert all data at once and then sort(). Array is a much better/faster approach. ● SplPriorityQueue is very similar, consider it is the same as SplHeap but where the sorting is made on the key rather than the value.
  • 67. Other related projects ● SPL Types: Various types implemented as object: SplInt, SplFloat, SplEnum, SplBool and SplString http://pecl.php.net/package/SPL_Types
  • 68. Other related projects ● SPL Types: Various types implemented as object: SplInt, SplFloat, SplEnum, SplBool and SplString http://pecl.php.net/package/SPL_Types ● Judy: Sparse dynamic arrays implementation http://pecl.php.net/package/Judy
  • 69. Other related projects ● SPL Types: Various types implemented as object: SplInt, SplFloat, SplEnum, SplBool and SplString http://pecl.php.net/package/SPL_Types ● Judy: Sparse dynamic arrays implementation http://pecl.php.net/package/Judy ● Weakref: Weak references implementation. Provides a gateway to an object without preventing that object from being collected by the garbage collector.
  • 70. Conclusions ● Use appropriate data structure. It will keep your code clean and fast.
  • 71. Conclusions ● Use appropriate data structure. It will keep your code clean and fast. ● Think about the time and space complexity involved by your algorithms.
  • 72. Conclusions ● Use appropriate data structure. It will keep your code clean and fast. ● Think about the time and space complexity involved by your algorithms. ● Name your variables accordingly: use “Map”, “Set”, “List”, “Queue”,... to describe them instead of using something like: $ordersArray.
  • 74. Thanks ● Don't forget to rate this talk on https://joind.in/4753
  • 75. Photo Credits ● Northstar Ski Jump: http://www.flickr.com/photos/renotahoe/5593248965 ● 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