Thursday 5 June 2014

PHP DataStructures

Data structure is a way of storing and organizing data in a computer so that it can be used efficiently. It provide a efficient way to manage large amounts of data efficiently, such as large databases and Internet indexing services.


SPL provides a following set of standard datastructures.
SplDoublyLinkedList: The SplDoublyLinkedList class provides the main functionalities of a doubly linked list.

SplStack
The SplStack class provides the main functionalities of a stack implemented using a doubly linked list.

SplQueue
The SplQueue class provides the main functionalities of a queue implemented using a doubly linked list.
See Example
echo "Create Object of SplQueue:";
$obj = new SplQueue();

echo "
Check for Queue, is it Empty:";
if($obj->isEmpty())
{
    $obj->enqueue("Simple");
    $obj->enqueue("Example");
    $obj->enqueue("Of");
    $obj->enqueue("PHP");
}
echo "
View queue:";
print_r($obj);
if(! $obj->offsetExists(4))
{
    $obj->enqueue(10);
}
print_r($obj);
echo "
"; echo " Get the value of the offset at 3 "; if($obj->offsetGet(3)) { echo $obj->offsetGet(3); echo " Resetting the value of a node:"; $obj->offsetSet(4,6); }
SplHeap The SplHeap class provides the main functionalities of a Heap. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm, and in the sorting algorithm heapsort.

SplMaxHeap The SplMaxHeap class provides the main functionalities of a heap, keeping the maximum on the top.

SplMinHeap The SplMinHeap class provides the main functionalities of a heap, keeping the minimum on the top.


SplPriorityQueue The SplPriorityQueue class provides the main functionalists of an functionalists of an prioritized queue, implemented using a max heap.

SplFixedArray The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation. See Example Below
$array = new SplFixedArray(5);
$array[1] = 2;
$array[4] = "foo";
var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)

var_dump($array["5"]); // Fatal error: Uncaught exception 'RuntimeException' with message 'Index invalid or out of range'

SplObjectStorage The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.