Php standard library Spl two-way linked list, php standard library spl

Source: Internet
Author: User
Tags bit set spl rewind

Php standard library Spl two-way linked list, php standard library spl

Spl Study Notes

What does spl do?
Spl is a set of interfaces and classes used to solve typical problems (standard problems.
If you want to implement a typical data structure, you can find the appropriate data structure in the spl class.
You can simply inherit the built-in classes to implement these data structures. Avoid repeated implementations.
Focus on your own business logic.

 

Data Structure
1. Implement bidirectional list
Spldoublyasklist implements Iterator, ArrayAccess, Countable {}


Example1:
FIFO and LIFO in spldoubly1_list // first-in-first-out

$ List = new spldoublyasklist ();
$ List-> push ('A ');
$ List-> push ('B ');
$ List-> push ('C ');
$ List-> push ('D ');

Echo "FIFO (first In First Out): \ n ";
$ List-> setIteratorMode (SplDoublyLinkedList: IT_MODE_FIFO); // you can specify the first-in-first-out iteration mode.
For ($ list-> rewind (); $ list-> valid (); $ list-> next ()){
Echo $ list-> current (). "\ n ";
}

Result:
// FIFO (First In First Out)
//
// B
// C
// D

Echo "LIFO (Last In First Out ):
$ List-> setIteratorMode (spldoublyshortlist: IT_MODE_LIFO); // sets the first-in-first-out iteration mode.
For ($ list-> rewind (); $ list-> valid (); $ list-> next ()){
Echo $ list-> current (). "\ n ";
}

Result:
// LIFO (Last In First Out ):
// D
// C
// B
//


Example2:
Php doubly link list is an amazing data structure, doubly means
You can traverse forward as well as backward, it can act
A deque (double ended queue) if you want it to, here is how it works

$ Dlist = new spldoublyinclulist () // instantiate

// Insert data push from the end of the linked list
$ Dlist-> push ('hiramariam );
$ Dlist-> push ('maaz ');
$ Dlist-> push ('zafar ');

// The linked list contains
/*
Hiramariam
Maaz
Zafar
*/

// Insert data from the beginning of the linked list
$ Dlist-> unshift (1 );
$ Dlist-> unshift (2 );
$ Dlist-> unshift (3 );

// The linked list contains
/*
3
2
1
Hiramariam
Maaz
Zafar
*/

// Delete the pop operation from the bottom of the linked list
$ Dlist-> pop ();

// The linked list contains
/*
3
2
1
Hiramariam
Maaz
*/

// Shift operation
$ Dlist-> shift ()

/**
If you want to replace an entry, an exception will be thrown if the index value exceeds the limit.
**/
$ Dlist-> add (3, 2.24 );

/**
You can use a cyclic traversal table.
**/

// Forward and backward
/**
The initial position of rewind () pointing to the linked list depends on the iterator mode which may point to the beginning or end
Is valid () available?
Next () points to next
**/

For ($ dlist-> rewind (); $ dlist-> valid (); $ dlist-> next ()){
Echo $ dlist-> current (). "<br/> ";
}

// Forward from the back
$ Dlist-> setIteratorMode (spldoubly+list: IT_MODE_LIFO );
For ($ dlist-> rewind (); $ dlist-> valid (); $ dlist-> next ()){
Echo $ dlsit-> current (). "<br/> ";
}

// Implementation of double-stranded tables
Spldoublyasklist implements Iterator, ArrayAccess, Countable {


Public _ construct (void)

// Implemented Iterator Interface
Public void rewind (void) // initialize the position pointed to by the linked list
Public bool valid (void) // whether the address pointed to by the linked list is valid
Public mixed current (void) // current element
Public mixed key (void) // index value of the current element
Public void prev (void) // The previous
Public void next (void) // next


Public void add (mixed $ index, mixed $ newval) // add

Public mixed top (void) // return the top Element
Public mixed bottom (void) // bottom Element

// Countable Interface
Public int count (void) // count

Public int getIteratorMode (void) // forward or backward of the current iteration Mode
Public void setIteratorMode (int $ mode) // sets the iteration mode.

/**
Mode Parameter
There are two orthogonal (orthogonal) sets of modes that can be set: the two sets do not affect each other.

The direction of the iteration (either one or the other): The direction of iteration
SplDoublyLinkedList: IT_MODE_LIFO (Stack style) Stack method
Spldoublyinclulist: IT_MODE_FIFO (Queue style) Queue Method


The behavior of the iterator (either one or the other): whether The direction of the next iteration of the iteration is retained
Spldoublyinclulist: IT_MODE_DELETE (Elements are deleted by the iterator)
Spldoublylists: IT_MODE_KEEP (Elements are traversed by the iterator)

Default
The default mode is: spldoubly1_list: IT_MODE_FIFO | spldoubly1_list: IT_MODE_KEEP is set to The next iteration of FIFO or this mode.

// Bitwise operation
/*
PHP ini sets error_reporting to use a bitwise value,
Provides a real example of disabling a bit. In addition to the prompt level
All other errors are used in php. ini as follows:
E_ALL &~ E_NOTICE

 

The specific operation method is to first obtain the value of E_ALL:
00000000000000000111011111111111
Then get the value of E_NOTICE:
00000000000000000000000000001000
Then pass ~ Reverse:
11111111111111111111111111110111
Finally, we use the bitwise AND (&) to get the BIT (1) set in both values:
00000000000000000111011111110111

 

Another method is to use bitwise or xor (^) to obtain
The bit set in one of the values:
E_ALL ^ E_NOTICE
Error_reporting can also be used to demonstrate how to set a location. Show only errors and recoverability
The error method is:
E_ERROR | E_RECOVERABLE_ERROR

 

That is, E_ERROR
00000000000000000000000000000001
And E_RECOVERABLE_ERROR
00000000000000000001000000000000
Use the bitwise OR (|) operator to obtain the result of being set in any value:
00000000000000000001000000000001

**/

Public bool isEmpty (void) // whether the linked list is empty

// ArrayAccess Interface
Public bool offsetExists (mixed $ index) // check whether the index exists
Public mixed offsetGet (mixed $ index) // gets the element of the specified index
Public void offsetSet (mixed $ index, mixed $ newval) // you can specify the value of the new index.
Public void offsetUnset (mixed $ index)


Public mixed pop (void) // delete from the end
Public void push (mixed $ value) // Add from the end

Public mixed shift (void) // delete
Public void unshift (mixed $ value) // Add from the beginning

Public void unserialize (string $ serialized) // deserializes the linked list
Public string serialiaze (void) // serialize

}

 

 

 

 


2. Stack is a special linear table. It can only insert or delete elements at one end of a linear table (that is, the elements are pushed to and from the Stack)
SplStack extends spldoublylists implements Iterator, ArrayAccess, Countable {}

3. A queue is like a queue in our daily life. Like a stack, a queue features FIFO ).
SplQueue extends spldoublylists implements Iterator, ArrayAccess, Countable {}

4. The priority queue SplPriorityQueue is implemented based on the heap.
SplPriorityQueue implements Iterator, Countable {}

5. Heap is a data structure designed to achieve the priority queue SplPriorityQueue. It is implemented by constructing a binary Heap (a binary tree.
Abstract SplHeap implements Iterator, Countable {}

6. The array is used to process a large number of fixed-length arrays.
SplFixedArray implements Iterator, ArrayAccess, Countable {}

7. ing is used to store a group of objects, especially when you need to uniquely identify objects.
SplObjectStorage implements Countable, Iterator, Serializable, ArrayAccess {}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.