|
1
|
|
|
<?php |
|
2
|
|
|
namespace GenericCollections\Abstracts; |
|
3
|
|
|
|
|
4
|
|
|
use GenericCollections\Exceptions\ContainerIsEmptyException; |
|
5
|
|
|
use GenericCollections\Interfaces\DequeInterface; |
|
6
|
|
|
use GenericCollections\Internal\DataDoubleLinkedList; |
|
7
|
|
|
use GenericCollections\Traits\CollectionMethods; |
|
8
|
|
|
use GenericCollections\Traits\DequeCommonMethods; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractDeque extends DataDoubleLinkedList implements DequeInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use CollectionMethods; |
|
13
|
|
|
use DequeCommonMethods; |
|
14
|
|
|
|
|
15
|
43 |
|
public function add($element) |
|
16
|
|
|
{ |
|
17
|
43 |
|
$this->addLast($element); |
|
18
|
41 |
|
return true; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
6 |
|
public function offer($element) |
|
22
|
|
|
{ |
|
23
|
6 |
|
return $this->offerLast($element); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public function getLast() |
|
27
|
|
|
{ |
|
28
|
2 |
|
if ($this->isEmpty()) { |
|
29
|
1 |
|
throw new ContainerIsEmptyException($this->containerInternalName(), 'get'); |
|
30
|
|
|
} |
|
31
|
1 |
|
return $this->storage->top(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function element() |
|
35
|
|
|
{ |
|
36
|
2 |
|
return $this->getFirst(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function peekLast() |
|
40
|
|
|
{ |
|
41
|
1 |
|
if ($this->isEmpty()) { |
|
42
|
1 |
|
return null; |
|
43
|
|
|
} |
|
44
|
1 |
|
return $this->storage->top(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function peek() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $this->peekFirst(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function removeLast() |
|
53
|
|
|
{ |
|
54
|
2 |
|
if ($this->isEmpty()) { |
|
55
|
1 |
|
throw new ContainerIsEmptyException($this->containerInternalName(), 'remove'); |
|
56
|
|
|
} |
|
57
|
1 |
|
return $this->storage->pop(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
public function remove() |
|
61
|
|
|
{ |
|
62
|
2 |
|
return $this->removeFirst(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function poll() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->pollFirst(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function pollLast() |
|
71
|
|
|
{ |
|
72
|
1 |
|
if ($this->isEmpty()) { |
|
73
|
1 |
|
return null; |
|
74
|
|
|
} |
|
75
|
1 |
|
return $this->storage->pop(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
24 |
|
protected function containerInternalName() |
|
79
|
|
|
{ |
|
80
|
24 |
|
return 'deque'; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|