1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\Containers\Structs; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Countable; |
7
|
|
|
use Exception; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use Smoren\Containers\Exceptions\LinkedListException; |
10
|
|
|
use Smoren\Containers\Exceptions\SortedLinkedListException; |
11
|
|
|
|
12
|
|
|
abstract class SortedLinkedList implements IteratorAggregate, Countable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var LinkedList data source |
16
|
|
|
*/ |
17
|
|
|
protected LinkedList $list; |
18
|
|
|
/** |
19
|
|
|
* @var Closure comparator |
20
|
|
|
*/ |
21
|
|
|
protected Closure $comparator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* SortedLinkedList constructor. |
25
|
|
|
* @param array|LinkedList $input default data list |
26
|
|
|
* @throws Exception |
27
|
|
|
*/ |
28
|
|
|
public function __construct($input = []) |
29
|
|
|
{ |
30
|
|
|
if($input instanceof LinkedList) { |
31
|
|
|
$this->list = $input; |
32
|
|
|
} elseif(is_array($input)) { |
|
|
|
|
33
|
|
|
$this->list = new LinkedList($input); |
34
|
|
|
} else { |
35
|
|
|
$linkedListType = LinkedList::class; |
36
|
|
|
$givenType = get_class($input); |
37
|
|
|
throw new SortedLinkedListException( |
38
|
|
|
"input must be instance of array or $linkedListType, given {$givenType}", |
39
|
|
|
SortedLinkedListException::STATUS_BAD_LINKED_LIST_TYPE |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->comparator = $this->getComparator(); |
44
|
|
|
$this->refresh(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Inserts element into collection |
49
|
|
|
* @param mixed $data element data value |
50
|
|
|
* @return LinkedListItem |
51
|
|
|
*/ |
52
|
|
|
public function insert($data): LinkedListItem |
53
|
|
|
{ |
54
|
|
|
return $this->list->pushAfter($this->findLeftPosition($data), $data); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Converts collection to array |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function toArray(): array |
62
|
|
|
{ |
63
|
|
|
return $this->list->toArray(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function count(): int |
70
|
|
|
{ |
71
|
|
|
return $this->list->count(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
* @return LinkedListIterator |
77
|
|
|
*/ |
78
|
|
|
public function getIterator(): LinkedListIterator |
79
|
|
|
{ |
80
|
|
|
return $this->list->getIterator(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns source list |
85
|
|
|
* @return LinkedList |
86
|
|
|
*/ |
87
|
|
|
public function getList(): LinkedList |
88
|
|
|
{ |
89
|
|
|
return $this->list; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Removes element from the front of list |
94
|
|
|
* @return mixed data value of removed element |
95
|
|
|
* @throws LinkedListException |
96
|
|
|
*/ |
97
|
|
|
public function popFront() |
98
|
|
|
{ |
99
|
|
|
return $this->list->popFront(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Removes element from the back of list |
104
|
|
|
* @return mixed data value of removed element |
105
|
|
|
* @throws LinkedListException |
106
|
|
|
*/ |
107
|
|
|
public function popBack() |
108
|
|
|
{ |
109
|
|
|
return $this->list->popBack(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Removes element from target element position |
114
|
|
|
* @param LinkedListItem $item target element position |
115
|
|
|
* @return LinkedListItem old position of element |
116
|
|
|
*/ |
117
|
|
|
public function delete(LinkedListItem $item): LinkedListItem |
118
|
|
|
{ |
119
|
|
|
return $this->list->delete($item); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Clears list |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function clear(): self |
127
|
|
|
{ |
128
|
|
|
$this->list->clear(); |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Refreshes items order |
134
|
|
|
* @return $this |
135
|
|
|
* @throws Exception |
136
|
|
|
*/ |
137
|
|
|
public function refresh(): self |
138
|
|
|
{ |
139
|
|
|
$this->list->sort($this->comparator); |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns comparator function for sorting and position search |
145
|
|
|
* @return callable |
146
|
|
|
*/ |
147
|
|
|
abstract protected function getComparator(): callable; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns position max element which is less than argument (using comparator) |
151
|
|
|
* @param mixed $data element data value |
152
|
|
|
* @return LinkedListItem|null |
153
|
|
|
*/ |
154
|
|
|
protected function findLeftPosition($data): ?LinkedListItem |
155
|
|
|
{ |
156
|
|
|
$position = null; |
157
|
|
|
foreach($this->list as $pos => $val) { |
158
|
|
|
if(!($this->comparator)($data, $val)) { |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
$position = $pos; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $position; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Clones object |
169
|
|
|
*/ |
170
|
|
|
public function __clone() |
171
|
|
|
{ |
172
|
|
|
$this->list = clone $this->list; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|