In the previous article, we looked at some of the more common data structures in DS extensions, but we also left some hints, such as keys() and values() in Map, which return two special data structures, respectively, which we will look at today.

Vector set

We first saw Vector in Java very early on. But even in Java, where lists and Maps dominate, this type of data structure is not used much. So let’s see how this set works.

$vector = new \Ds\Vector(["a"."b"."c"]);

$vector->push("d");
$vector->push(5);
$vector->push(6);
$vector->push(7);
$vector->push(8);

$vector->set(3."ccc");

var_dump($vector->get(3)); // string(3) "ccc"
var_dump($vector->pop()); // int(8)

$vector->unshift(1);
$vector->unshift(-1); 

var_dump($vector->shift()); // int(-1)

$vector->insert(5.'five');
var_dump($vector->get(5)); // string(4) "five"

var_dump($vector->get(6)); // int(5)
$vector->remove(6);
var_dump($vector->get(6)); // int(6)

var_dump($vector[4]); // string(3) "ccc"

$vector[4] = 'Num 4.';

var_dump($vector[4]); // string(6) "Num 4."

var_dump($vector);
// object(Ds\Vector)#1 (8) {
/ / [0] = >
// int(1)
/ / [1] = >
// string(1) "a"
/ / [2] = >
// string(1) "b"
/ / [3] = >
// string(1) "c"
/ / [4] = >
// string(6) "Num 4."
/ / [5] = >
// string(4) "five"
/ / [6] = >
// int(6)
/ / [7] = >
// int(7)
// }
Copy the code

Many of its methods are similar to Map, but it supports push(), pop(), Shift (), and unshift(), which add and remove data from the tail and head, respectively. Also, at the bottom, it uses less total memory than arrays and automatically frees memory when the allocated memory size drops low enough. Get (), set(), push(), and pop() are all O(1) efficient, while shift(), unshift(), insert(), and remove() are all O(n) efficient. It is clear in what context it can be used, large arrays can save memory, and some operations can be very efficient.

Using values() and Paris () in a Map returns a Vector.

Unique Set Set

In fact, the Set structure is quite common, not only in Java programming languages, redis also has this way of storing data, I believe you are familiar with. The most significant difference from other constructs is that the values in a Set must be unique.

$set = new \Ds\Set(["a"."b"."c"]);
$set->add("d");

$set->add("b");

var_dump($set);
// object(Ds\Set)#2 (4) {
/ / [0] = >
// string(1) "a"
/ / [1] = >
// string(1) "b"
/ / [2] = >
// string(1) "c"
/ / [3] = >
// string(1) "d"
// }
Copy the code

This is not much to explain, I believe you even if not in the code, you will be exposed to the use of Redis, business scenarios are also very many. In the previous article, the keys() returned by a Map are Set, because keys in a Map cannot be duplicated, and arrays of numeric subscripts cannot have duplicate keys.

Double-endian queue Deque

A double-endided queue behaves like a Vector. It can also add and fetch at the head and tail of the queue, but it has two Pointers to the head and tail of the queue, so its shift() and unshift() speed is O(1) fast.

$deque = new \Ds\Deque(["a"."b"."c"]);

$deque->push("d");
$deque->unshift("z");

var_dump($deque);
// object(Ds\Deque)#3 (5) {
/ / [0] = >
// string(1) "z"
/ / [1] = >
// string(1) "a"
/ / [2] = >
// string(1) "b"
/ / [3] = >
// string(1) "c"
/ / [4] = >
// string(1) "d"
/ /}

var_dump($deque->pop()); // string(1) "d"
var_dump($deque->shift()); // string(1) "z"
Copy the code

conclusion

Today’s introduction is much simpler than the previous article, but the last article really only showed more of the code results of the test. These are the data structures in the overall DataStruct extension framework, and as we said in the beginning, if there is no specific need but to use stacks or queues, you can use SPL directly. If you have a special need, such as an object type like a Map, or need a memory-saving array, these data structures can be useful.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/b…