Mastering PHP Data Structure 102 - phpDay 2012 Verona

Patrick Allaert
Patrick AllaertSystem developer at Libereco Technologies
Mastering PHP Data Structure 102
         Patrick Allaert




     phpDay 2012 Verona, Italy
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/
APM
APM
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
                           ●

                                                       Disjoint-set
                                                                             ●   Hash table                                     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                                         Double                                                                   Routing table
                           ●   Bounding interval   ●
                                                                                                        M-tree
                                                                                                                            ●
                                                                                                                                                           Ternary heap
                                                                                 Heap
                                                                                                                                                       ●
                                                                             ●                      ●
●
    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                                           Treap
●
    AF-heap                                        ●
                                                       Dynamic array         ●   Hypergraph                                     Scapegoat tree
                                                                                                                                                       ●

                               B-tree
                                                                                                                            ●
                           ●                                                                        ●
                                                                                                        Octree                                         ●
                                                                                                                                                           Tree
    Alternating decision                               Enfilade                                                                 Scene graph
                                                                                 Iliffe vector
●                                                  ●

                               B*-tree
                                                                             ●                                              ●


    tree
                           ●                                                                        ●
                                                                                                        Pagoda                                         ●   Trees
                                                   ●
                                                       Enumerated type                                                          Segment tree
                                                                                 Image
                                                                                                                            ●
                           ●
                               B+ tree                                       ●                      ●
                                                                                                        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
                               Bx-tree                                           Interval tree
                                                                                                    ●
                                                                                                        Parse tree
●
    Array                  ●
                                                   ●
                                                       Fenwick 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
                                                                                                        Propositional
●
                                                                                 Kd-tree
                           ●                                                                        ●
                                                                             ●                                                                         ●   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
●
    Binary search tree                                 tree                  ●   Lightmap           ●
                                                                                                        Quadtree            ●
                                                                                                                                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
                                                                                 Link/cut tree
●                                                                                                                           ●

                               Dancing tree
                                                                             ●
                           ●
                                                       stack                                            Radix tree                                     ●
                                                                                                                                                           Zero suppressed
                                                                                                                                Stack
                                                                                                    ●
●
    Bit array                                                                                                               ●

                           ●   D-ary heap          ●
                                                       Hash                  ●
                                                                                 Linked list            Randomized binary
                                                                                                                                                           decision diagram
                                                                                                                                String
                                                                                                    ●
●
    Bitboard                                                                                                                ●
                                                                                                                                                       ●   Zipper
                           ●   Decision tree       ●
                                                       Hash array mapped     ●   Lookup table           search tree
                                                       trie
                                                                                                                            ●
                                                                                                                                Suffix array           ●
                                                                                                                                                           Z-order
                           ●   Deque
                                                                                                    ●
                                                                                                        Range tree
Game:
Can you recognize some structures?
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Mastering PHP Data Structure 102 - phpDay 2012 Verona
Array: PHP's untruthfulness
PHP “Arrays” are not true Arrays!
Array: PHP's untruthfulness
PHP “Arrays” are not true Arrays!
An array typically looks like this:



                 0      1      2      3      4      5


                Data   Data   Data   Data   Data   Data
Array: PHP's untruthfulness
PHP “Arrays” can dynamically grow and be iterated
both directions (reset(), next(), prev(), end()),
exclusively with O(1) operations.
Array: PHP's untruthfulness
PHP “Arrays” can dynamically grow and be iterated
both directions (reset(), next(), prev(), end()),
exclusively with O(1) operations.
Let's have 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).
Let's have an 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,...)
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
(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
Queues
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
Stacks
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
Queues/Stacks – Pros and Cons
              Array              SplQueue / 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
Sets

Geeks                      Nerds
          People with
        strong views on
         the distinction
        between geeks
           and nerds
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();

// Adding elements to a set
$set[] = 1;
$set[] = 2;
$set[] = 3;

// Checking presence in a set
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();

// Adding elements to a set
$set[] = 1;
$set[] = 2;
$set[] = 3;                     True
// Checking presence in a set   performance
in_array(2, $set); // true
in_array(5, $set); // false
                                killers!
array_merge($set1, $set2); // union
array_intersect($set1, $set2); // intersection
array_diff($set1, $set2); // complement
Sets – Mis-usage
if ($value === "val1" || $value === "val2" || $value ===
"val3")))
{
    // ...
}
Sets – Mis-usage
if (in_array($value, array("val1", "val2", "val3")))
{
    // ...
}
Sets – Mis-usage
switch ($value)
{
    case "val1":
    case "val2":
    case "val3":
        // ...
}
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 set
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();

// 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 set
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();

// 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 set
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 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 set
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();

// 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 set
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);

// 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)
Sets – Using bitsets
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

// Adding elements to a set
$set = 0;
$set |= E_ERROR;
$set |= E_WARNING;
$set |= E_PARSE;

// Checking presence in a set
$set & E_ERROR; // true
$set & E_NOTICE; // false

$set1 | $set2; // union
$set1 & $set2; // intersection
$set1 ^ $set2; // complement
Sets – Using bitsets (example)
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 – Using bitsets (example)
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!
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"
["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.
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
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 http://joind.in/6371
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
1 of 84

Recommended

25 php interview questions – codementor by
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementorArc & Codementor
173.1K views37 slides
Advanced debugging techniques (PHP) by
Advanced debugging techniques (PHP)Advanced debugging techniques (PHP)
Advanced debugging techniques (PHP)Patrick Allaert
3.9K views35 slides
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy by
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPatrick Allaert
20.8K views95 slides
La métrique, ce n'est pas que pour le devops by
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
1.4K views96 slides
Maitriser les structures de données PHP 102 - Forum Paris 2012 by
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
5.3K views86 slides
Create your own PHP extension, step by step - phpDay 2012 Verona by
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
41.8K views46 slides

More Related Content

Recently uploaded

Perth MeetUp November 2023 by
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023 Michael Price
15 views44 slides
Case Study Copenhagen Energy and Business Central.pdf by
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdfAitana
12 views3 slides
handbook for web 3 adoption.pdf by
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdfLiveplex
19 views16 slides
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院IttrainingIttraining
34 views8 slides
Vertical User Stories by
Vertical User StoriesVertical User Stories
Vertical User StoriesMoisés Armani Ramírez
11 views16 slides
Special_edition_innovator_2023.pdf by
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdfWillDavies22
16 views6 slides

Recently uploaded(20)

Perth MeetUp November 2023 by Michael Price
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023
Michael Price15 views
Case Study Copenhagen Energy and Business Central.pdf by Aitana
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdf
Aitana12 views
handbook for web 3 adoption.pdf by Liveplex
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdf
Liveplex19 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2216 views
1st parposal presentation.pptx by i238212
1st parposal presentation.pptx1st parposal presentation.pptx
1st parposal presentation.pptx
i2382129 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson33 views
Black and White Modern Science Presentation.pptx by maryamkhalid2916
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptx
maryamkhalid291614 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
Transcript: The Details of Description Techniques tips and tangents on altern... by BookNet Canada
Transcript: The Details of Description Techniques tips and tangents on altern...Transcript: The Details of Description Techniques tips and tangents on altern...
Transcript: The Details of Description Techniques tips and tangents on altern...
BookNet Canada130 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10209 views
Data-centric AI and the convergence of data and model engineering: opportunit... by Paolo Missier
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...
Paolo Missier34 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi120 views
6g - REPORT.pdf by Liveplex
6g - REPORT.pdf6g - REPORT.pdf
6g - REPORT.pdf
Liveplex9 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker26 views
Empathic Computing: Delivering the Potential of the Metaverse by Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst470 views

Featured

ChatGPT and the Future of Work - Clark Boyd by
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
21.3K views69 slides
Getting into the tech field. what next by
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
5.2K views22 slides
Google's Just Not That Into You: Understanding Core Updates & Search Intent by
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
5.9K views99 slides
How to have difficult conversations by
How to have difficult conversations How to have difficult conversations
How to have difficult conversations Rajiv Jayarajah, MAppComm, ACC
4.5K views19 slides
Introduction to Data Science by
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data ScienceChristy Abraham Joy
82.2K views51 slides
Time Management & Productivity - Best Practices by
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
169.7K views42 slides

Featured(20)

ChatGPT and the Future of Work - Clark Boyd by Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd21.3K views
Getting into the tech field. what next by Tessa Mero
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero5.2K views
Google's Just Not That Into You: Understanding Core Updates & Search Intent by Lily Ray
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray5.9K views
Time Management & Productivity - Best Practices by Vit Horky
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky169.7K views
The six step guide to practical project management by MindGenius
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius36.6K views
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright... by RachelPearson36
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson3612.6K views
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present... by Applitools
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools55.4K views
12 Ways to Increase Your Influence at Work by GetSmarter
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter401.6K views
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G... by DevGAMM Conference
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference3.6K views
Barbie - Brand Strategy Presentation by Erica Santiago
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago25.1K views
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well by Saba Software
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software25.2K views
Introduction to C Programming Language by Simplilearn
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn8.4K views
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr... by Palo Alto Software
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
Palo Alto Software88.3K views
9 Tips for a Work-free Vacation by Weekdone.com
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation
Weekdone.com7.2K views
How to Map Your Future by SlideShop.com
How to Map Your FutureHow to Map Your Future
How to Map Your Future
SlideShop.com275.1K views

Mastering PHP Data Structure 102 - phpDay 2012 Verona

  • 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 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)
  • 6. 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 ● Disjoint-set ● Hash table 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 Double Routing table ● Bounding interval ● M-tree ● Ternary heap Heap ● ● ● ● 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 Treap ● AF-heap ● Dynamic array ● Hypergraph Scapegoat tree ● B-tree ● ● ● Octree ● Tree Alternating decision Enfilade Scene graph Iliffe vector ● ● B*-tree ● ● tree ● ● Pagoda ● Trees ● Enumerated type Segment tree Image ● ● B+ tree ● ● 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 Bx-tree Interval tree ● Parse tree ● Array ● ● Fenwick 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 Propositional ● Kd-tree ● ● ● ● 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 ● Binary search tree tree ● Lightmap ● Quadtree ● 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 Link/cut tree ● ● Dancing tree ● ● stack Radix tree ● Zero suppressed Stack ● ● Bit array ● ● D-ary heap ● Hash ● Linked list Randomized binary decision diagram String ● ● Bitboard ● ● Zipper ● Decision tree ● Hash array mapped ● Lookup table search tree trie ● Suffix array ● Z-order ● Deque ● Range tree
  • 7. Game: Can you recognize some structures?
  • 15. Array: PHP's untruthfulness PHP “Arrays” are not true Arrays!
  • 16. Array: PHP's untruthfulness PHP “Arrays” are not true Arrays! An array typically looks like this: 0 1 2 3 4 5 Data Data Data Data Data Data
  • 17. Array: PHP's untruthfulness PHP “Arrays” can dynamically grow and be iterated both directions (reset(), next(), prev(), end()), exclusively with O(1) operations.
  • 18. Array: PHP's untruthfulness PHP “Arrays” can dynamically grow and be iterated both directions (reset(), next(), prev(), end()), exclusively with O(1) operations. Let's have a Doubly Linked List (DLL): Head Tail Data Data Data Data Data Enables List, Deque, Queue and Stack implementations
  • 19. Array: PHP's untruthfulness PHP “Arrays” elements are always accessible using a key (index).
  • 20. Array: PHP's untruthfulness PHP “Arrays” elements are always accessible using a key (index). Let's have an 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
  • 21. 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.”
  • 22. Optimized for anything ≈ Optimized for nothing!
  • 23. 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
  • 25. Structs (or records, tuples,...)
  • 26. 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
  • 27. Structs – Using array $person = array( "firstName" => "Patrick", "lastName" => "Allaert" );
  • 28. Structs – Using a class $person = new PersonStruct( "Patrick", "Allaert" );
  • 29. Structs – Using a class (Implementation) class PersonStruct { public $firstName; public $lastName; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } }
  • 30. 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 } }
  • 31. 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?
  • 33. (true) Arrays ● An array is a fixed size collection where elements are each identified by a numeric index.
  • 34. (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
  • 35. (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
  • 36. (true) Arrays – Pros and Cons Array SplFixedArray - Uses more memory + Uses less memory +|- Less OO +|- More OO
  • 38. 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.
  • 39. 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
  • 40. 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
  • 41. 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
  • 43. Stacks ● A stack is an ordered collection respecting Last In, First Out (LIFO) order. ● Elements are inserted and removed on the same end.
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. Queues/Stacks – Pros and Cons Array SplQueue / 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
  • 48. Sets Geeks Nerds People with strong views on the distinction between geeks and nerds
  • 49. 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.
  • 50. 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
  • 51. Sets – Using array $set = array(); // Adding elements to a set $set[] = 1; $set[] = 2; $set[] = 3; // Checking presence in a set 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
  • 52. Sets – Using array $set = array(); // Adding elements to a set $set[] = 1; $set[] = 2; $set[] = 3; True // Checking presence in a set performance in_array(2, $set); // true in_array(5, $set); // false killers! array_merge($set1, $set2); // union array_intersect($set1, $set2); // intersection array_diff($set1, $set2); // complement
  • 53. Sets – Mis-usage if ($value === "val1" || $value === "val2" || $value === "val3"))) { // ... }
  • 54. Sets – Mis-usage if (in_array($value, array("val1", "val2", "val3"))) { // ... }
  • 55. Sets – Mis-usage switch ($value) { case "val1": case "val2": case "val3": // ... }
  • 56. 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 set isset($set[2]); // true isset($set[5]); // false $set1 + $set2; // union array_intersect_key($set1, $set2); // intersection array_diff_key($set1, $set2); // complement
  • 57. 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 set 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!
  • 58. 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 set 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
  • 59. 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 set 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
  • 60. 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 set isset($set[$object2]); // true isset($set[$object2]); // false $set1->addAll($set2); // union $set1->removeAllExcept($set2); // intersection $set1->removeAll($set2); // complement
  • 61. 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)
  • 62. Sets – Using bitsets 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 // Adding elements to a set $set = 0; $set |= E_ERROR; $set |= E_WARNING; $set |= E_PARSE; // Checking presence in a set $set & E_ERROR; // true $set & E_NOTICE; // false $set1 | $set2; // union $set1 & $set2; // intersection $set1 ^ $set2; // complement
  • 63. Sets – Using bitsets (example) 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 ?!
  • 64. Sets – Using bitsets (example) 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 :)
  • 65. 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!
  • 66. Maps ● A map is a collection of key/value pairs where all keys are unique.
  • 67. 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.
  • 68. 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" ["UN"] => &string(4) "once" ["UNO"] => &string(4) "once" ["TWO"] => &string(5) "twice" ["DEUX"] => &string(5) "twice" ["DUE"] => &string(5) "twice" } */
  • 69. 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.
  • 70. 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.
  • 71. Heap – Using array $heap = array(); $heap[] = 3; sort($heap); $heap[] = 1; sort($heap); $heap[] = 2; sort($heap);
  • 72. Heap – Using Spl(Min|Max)Heap $heap = new SplMinHeap; $heap->insert(3); $heap->insert(1); $heap->insert(2);
  • 73. 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.
  • 74. 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!
  • 75. 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
  • 76. Other related projects ● SPL Types: Various types implemented as object: SplInt, SplFloat, SplEnum, SplBool and SplString http://pecl.php.net/package/SPL_Types
  • 77. 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
  • 78. 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.
  • 79. Conclusions ● Use appropriate data structure. It will keep your code clean and fast.
  • 80. Conclusions ● Use appropriate data structure. It will keep your code clean and fast. ● Think about the time and space complexity involved by your algorithms.
  • 81. 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.
  • 83. Thanks ● Don't forget to rate this talk on http://joind.in/6371
  • 84. 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