@@ -36,246 +36,246 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class ReversedMap extends AbstractMap |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * @var SortedMap Internal map |
|
| 41 | - * |
|
| 42 | - * @since 1.0.0 |
|
| 43 | - */ |
|
| 44 | - private $map; |
|
| 39 | + /** |
|
| 40 | + * @var SortedMap Internal map |
|
| 41 | + * |
|
| 42 | + * @since 1.0.0 |
|
| 43 | + */ |
|
| 44 | + private $map; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @var callable Comparator function |
|
| 48 | - * |
|
| 49 | - * @param mixed $key1 First key |
|
| 50 | - * @param mixed $key2 Second key |
|
| 51 | - * |
|
| 52 | - * @return integer negative if $key1 is lesser than $key2, |
|
| 53 | - * 0 if $key1 is equal to $key2, |
|
| 54 | - * positive if $key1 is greater than $key2 |
|
| 55 | - * |
|
| 56 | - * @since 1.0.0 |
|
| 57 | - */ |
|
| 58 | - private $comparator; |
|
| 46 | + /** |
|
| 47 | + * @var callable Comparator function |
|
| 48 | + * |
|
| 49 | + * @param mixed $key1 First key |
|
| 50 | + * @param mixed $key2 Second key |
|
| 51 | + * |
|
| 52 | + * @return integer negative if $key1 is lesser than $key2, |
|
| 53 | + * 0 if $key1 is equal to $key2, |
|
| 54 | + * positive if $key1 is greater than $key2 |
|
| 55 | + * |
|
| 56 | + * @since 1.0.0 |
|
| 57 | + */ |
|
| 58 | + private $comparator; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Constructor |
|
| 62 | - * |
|
| 63 | - * @param SortedMap $map Internal map |
|
| 64 | - * |
|
| 65 | - * @since 1.0.0 |
|
| 66 | - */ |
|
| 67 | - protected function __construct(SortedMap $map) |
|
| 68 | - { |
|
| 69 | - $this->map = $map; |
|
| 70 | - $this->comparator = function ($key1, $key2) { |
|
| 71 | - return - call_user_func($this->map->comparator, $key1, $key2); |
|
| 72 | - }; |
|
| 73 | - } |
|
| 60 | + /** |
|
| 61 | + * Constructor |
|
| 62 | + * |
|
| 63 | + * @param SortedMap $map Internal map |
|
| 64 | + * |
|
| 65 | + * @since 1.0.0 |
|
| 66 | + */ |
|
| 67 | + protected function __construct(SortedMap $map) |
|
| 68 | + { |
|
| 69 | + $this->map = $map; |
|
| 70 | + $this->comparator = function ($key1, $key2) { |
|
| 71 | + return - call_user_func($this->map->comparator, $key1, $key2); |
|
| 72 | + }; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Create |
|
| 77 | - * |
|
| 78 | - * @param SortedMap $map Internal map |
|
| 79 | - * |
|
| 80 | - * @return ReversedMap A new reversed map |
|
| 81 | - * |
|
| 82 | - * @since 1.0.0 |
|
| 83 | - */ |
|
| 84 | - public static function create(SortedMap $map) |
|
| 85 | - { |
|
| 86 | - return new static($map); |
|
| 87 | - } |
|
| 75 | + /** |
|
| 76 | + * Create |
|
| 77 | + * |
|
| 78 | + * @param SortedMap $map Internal map |
|
| 79 | + * |
|
| 80 | + * @return ReversedMap A new reversed map |
|
| 81 | + * |
|
| 82 | + * @since 1.0.0 |
|
| 83 | + */ |
|
| 84 | + public static function create(SortedMap $map) |
|
| 85 | + { |
|
| 86 | + return new static($map); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Magic get method |
|
| 91 | - * |
|
| 92 | - * @param string $property The property |
|
| 93 | - * |
|
| 94 | - * @return mixed The value associated to the property |
|
| 95 | - * |
|
| 96 | - * @since 1.0.0 |
|
| 97 | - */ |
|
| 98 | - public function __get($property) |
|
| 99 | - { |
|
| 100 | - switch ($property) { |
|
| 101 | - case 'map': |
|
| 102 | - return $this->map; |
|
| 103 | - default: |
|
| 104 | - return parent::__get($property); |
|
| 105 | - } |
|
| 106 | - } |
|
| 89 | + /** |
|
| 90 | + * Magic get method |
|
| 91 | + * |
|
| 92 | + * @param string $property The property |
|
| 93 | + * |
|
| 94 | + * @return mixed The value associated to the property |
|
| 95 | + * |
|
| 96 | + * @since 1.0.0 |
|
| 97 | + */ |
|
| 98 | + public function __get($property) |
|
| 99 | + { |
|
| 100 | + switch ($property) { |
|
| 101 | + case 'map': |
|
| 102 | + return $this->map; |
|
| 103 | + default: |
|
| 104 | + return parent::__get($property); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get the comparator |
|
| 110 | - * |
|
| 111 | - * @return callable The comparator |
|
| 112 | - * |
|
| 113 | - * @since 1.0.0 |
|
| 114 | - */ |
|
| 115 | - public function comparator() |
|
| 116 | - { |
|
| 117 | - return $this->comparator; |
|
| 118 | - } |
|
| 108 | + /** |
|
| 109 | + * Get the comparator |
|
| 110 | + * |
|
| 111 | + * @return callable The comparator |
|
| 112 | + * |
|
| 113 | + * @since 1.0.0 |
|
| 114 | + */ |
|
| 115 | + public function comparator() |
|
| 116 | + { |
|
| 117 | + return $this->comparator; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Get the first element |
|
| 122 | - * |
|
| 123 | - * @return mixed The first element |
|
| 124 | - * |
|
| 125 | - * @throws \OutOfBoundsException If there is no element |
|
| 126 | - * |
|
| 127 | - * @since 1.0.0 |
|
| 128 | - */ |
|
| 129 | - public function first() |
|
| 130 | - { |
|
| 131 | - return $this->map->last(); |
|
| 132 | - } |
|
| 120 | + /** |
|
| 121 | + * Get the first element |
|
| 122 | + * |
|
| 123 | + * @return mixed The first element |
|
| 124 | + * |
|
| 125 | + * @throws \OutOfBoundsException If there is no element |
|
| 126 | + * |
|
| 127 | + * @since 1.0.0 |
|
| 128 | + */ |
|
| 129 | + public function first() |
|
| 130 | + { |
|
| 131 | + return $this->map->last(); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Get the last element |
|
| 136 | - * |
|
| 137 | - * @return mixed The last element |
|
| 138 | - * |
|
| 139 | - * @throws \OutOfBoundsException If there is no element |
|
| 140 | - * |
|
| 141 | - * @since 1.0.0 |
|
| 142 | - */ |
|
| 143 | - public function last() |
|
| 144 | - { |
|
| 145 | - return $this->map->first(); |
|
| 146 | - } |
|
| 134 | + /** |
|
| 135 | + * Get the last element |
|
| 136 | + * |
|
| 137 | + * @return mixed The last element |
|
| 138 | + * |
|
| 139 | + * @throws \OutOfBoundsException If there is no element |
|
| 140 | + * |
|
| 141 | + * @since 1.0.0 |
|
| 142 | + */ |
|
| 143 | + public function last() |
|
| 144 | + { |
|
| 145 | + return $this->map->first(); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Get the predecessor element |
|
| 150 | - * |
|
| 151 | - * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 152 | - * |
|
| 153 | - * @return mixed The predecessor element |
|
| 154 | - * |
|
| 155 | - * @throws \OutOfBoundsException If there is no predecessor |
|
| 156 | - * |
|
| 157 | - * @since 1.0.0 |
|
| 158 | - */ |
|
| 159 | - public function predecessor($element) |
|
| 160 | - { |
|
| 161 | - return $this->map->successor($element); |
|
| 162 | - } |
|
| 148 | + /** |
|
| 149 | + * Get the predecessor element |
|
| 150 | + * |
|
| 151 | + * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 152 | + * |
|
| 153 | + * @return mixed The predecessor element |
|
| 154 | + * |
|
| 155 | + * @throws \OutOfBoundsException If there is no predecessor |
|
| 156 | + * |
|
| 157 | + * @since 1.0.0 |
|
| 158 | + */ |
|
| 159 | + public function predecessor($element) |
|
| 160 | + { |
|
| 161 | + return $this->map->successor($element); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Get the successor element |
|
| 166 | - * |
|
| 167 | - * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 168 | - * |
|
| 169 | - * @return mixed The successor element |
|
| 170 | - * |
|
| 171 | - * @throws \OutOfBoundsException If there is no successor |
|
| 172 | - */ |
|
| 173 | - public function successor($element) |
|
| 174 | - { |
|
| 175 | - return $this->map->predecessor($element); |
|
| 176 | - } |
|
| 164 | + /** |
|
| 165 | + * Get the successor element |
|
| 166 | + * |
|
| 167 | + * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 168 | + * |
|
| 169 | + * @return mixed The successor element |
|
| 170 | + * |
|
| 171 | + * @throws \OutOfBoundsException If there is no successor |
|
| 172 | + */ |
|
| 173 | + public function successor($element) |
|
| 174 | + { |
|
| 175 | + return $this->map->predecessor($element); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Returns the element whose key is the greatest key lesser than the given key |
|
| 180 | - * |
|
| 181 | - * @param mixed $key The searched key |
|
| 182 | - * |
|
| 183 | - * @return mixed The found element |
|
| 184 | - * |
|
| 185 | - * @throws \OutOfBoundsException If there is no lower element |
|
| 186 | - * |
|
| 187 | - * @since 1.0.0 |
|
| 188 | - */ |
|
| 189 | - public function lower($key) |
|
| 190 | - { |
|
| 191 | - return $this->map->higher($key); |
|
| 192 | - } |
|
| 178 | + /** |
|
| 179 | + * Returns the element whose key is the greatest key lesser than the given key |
|
| 180 | + * |
|
| 181 | + * @param mixed $key The searched key |
|
| 182 | + * |
|
| 183 | + * @return mixed The found element |
|
| 184 | + * |
|
| 185 | + * @throws \OutOfBoundsException If there is no lower element |
|
| 186 | + * |
|
| 187 | + * @since 1.0.0 |
|
| 188 | + */ |
|
| 189 | + public function lower($key) |
|
| 190 | + { |
|
| 191 | + return $this->map->higher($key); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Returns the element whose key is the greatest key lesser than or equal to the given key |
|
| 196 | - * |
|
| 197 | - * @param mixed $key The searched key |
|
| 198 | - * |
|
| 199 | - * @return mixed The found element |
|
| 200 | - * |
|
| 201 | - * @throws \OutOfBoundsException If there is no floor element |
|
| 202 | - * |
|
| 203 | - * @since 1.0.0 |
|
| 204 | - */ |
|
| 205 | - public function floor($key) |
|
| 206 | - { |
|
| 207 | - return $this->map->ceiling($key); |
|
| 208 | - } |
|
| 194 | + /** |
|
| 195 | + * Returns the element whose key is the greatest key lesser than or equal to the given key |
|
| 196 | + * |
|
| 197 | + * @param mixed $key The searched key |
|
| 198 | + * |
|
| 199 | + * @return mixed The found element |
|
| 200 | + * |
|
| 201 | + * @throws \OutOfBoundsException If there is no floor element |
|
| 202 | + * |
|
| 203 | + * @since 1.0.0 |
|
| 204 | + */ |
|
| 205 | + public function floor($key) |
|
| 206 | + { |
|
| 207 | + return $this->map->ceiling($key); |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * Returns the element whose key is equal to the given key |
|
| 212 | - * |
|
| 213 | - * @param mixed $key The searched key |
|
| 214 | - * |
|
| 215 | - * @return mixed The found element |
|
| 216 | - * |
|
| 217 | - * @throws \OutOfBoundsException If there is no such element |
|
| 218 | - * |
|
| 219 | - * @since 1.0.0 |
|
| 220 | - */ |
|
| 221 | - public function find($key) |
|
| 222 | - { |
|
| 223 | - return $this->map->find($key); |
|
| 224 | - } |
|
| 210 | + /** |
|
| 211 | + * Returns the element whose key is equal to the given key |
|
| 212 | + * |
|
| 213 | + * @param mixed $key The searched key |
|
| 214 | + * |
|
| 215 | + * @return mixed The found element |
|
| 216 | + * |
|
| 217 | + * @throws \OutOfBoundsException If there is no such element |
|
| 218 | + * |
|
| 219 | + * @since 1.0.0 |
|
| 220 | + */ |
|
| 221 | + public function find($key) |
|
| 222 | + { |
|
| 223 | + return $this->map->find($key); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Returns the element whose key is the lowest key greater than or equal to the given key |
|
| 228 | - * |
|
| 229 | - * @param mixed $key The searched key |
|
| 230 | - * |
|
| 231 | - * @return mixed The found element |
|
| 232 | - * |
|
| 233 | - * @throws \OutOfBoundsException If there is no ceiling element |
|
| 234 | - * |
|
| 235 | - * @since 1.0.0 |
|
| 236 | - */ |
|
| 237 | - public function ceiling($key) |
|
| 238 | - { |
|
| 239 | - return $this->map->floor($key); |
|
| 240 | - } |
|
| 226 | + /** |
|
| 227 | + * Returns the element whose key is the lowest key greater than or equal to the given key |
|
| 228 | + * |
|
| 229 | + * @param mixed $key The searched key |
|
| 230 | + * |
|
| 231 | + * @return mixed The found element |
|
| 232 | + * |
|
| 233 | + * @throws \OutOfBoundsException If there is no ceiling element |
|
| 234 | + * |
|
| 235 | + * @since 1.0.0 |
|
| 236 | + */ |
|
| 237 | + public function ceiling($key) |
|
| 238 | + { |
|
| 239 | + return $this->map->floor($key); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * Returns the element whose key is the lowest key greater than to the given key |
|
| 244 | - * |
|
| 245 | - * @param mixed $key The searched key |
|
| 246 | - * |
|
| 247 | - * @return mixed The found element |
|
| 248 | - * |
|
| 249 | - * @throws \OutOfBoundsException If there is no higher element |
|
| 250 | - * |
|
| 251 | - * @since 1.0.0 |
|
| 252 | - */ |
|
| 253 | - public function higher($key) |
|
| 254 | - { |
|
| 255 | - return $this->map->lower($key); |
|
| 256 | - } |
|
| 242 | + /** |
|
| 243 | + * Returns the element whose key is the lowest key greater than to the given key |
|
| 244 | + * |
|
| 245 | + * @param mixed $key The searched key |
|
| 246 | + * |
|
| 247 | + * @return mixed The found element |
|
| 248 | + * |
|
| 249 | + * @throws \OutOfBoundsException If there is no higher element |
|
| 250 | + * |
|
| 251 | + * @since 1.0.0 |
|
| 252 | + */ |
|
| 253 | + public function higher($key) |
|
| 254 | + { |
|
| 255 | + return $this->map->lower($key); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * Serialize the object |
|
| 260 | - * |
|
| 261 | - * @return array Array of values |
|
| 262 | - * |
|
| 263 | - * @since 1.0.0 |
|
| 264 | - */ |
|
| 265 | - public function jsonSerialize() |
|
| 266 | - { |
|
| 267 | - return array('ReversedMap' => $this->map->jsonSerialize()); |
|
| 268 | - } |
|
| 258 | + /** |
|
| 259 | + * Serialize the object |
|
| 260 | + * |
|
| 261 | + * @return array Array of values |
|
| 262 | + * |
|
| 263 | + * @since 1.0.0 |
|
| 264 | + */ |
|
| 265 | + public function jsonSerialize() |
|
| 266 | + { |
|
| 267 | + return array('ReversedMap' => $this->map->jsonSerialize()); |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Count the number of key/value pairs |
|
| 272 | - * |
|
| 273 | - * @return integer |
|
| 274 | - * |
|
| 275 | - * @since 1.0.0 |
|
| 276 | - */ |
|
| 277 | - public function count() |
|
| 278 | - { |
|
| 279 | - return $this->map->count(); |
|
| 280 | - } |
|
| 270 | + /** |
|
| 271 | + * Count the number of key/value pairs |
|
| 272 | + * |
|
| 273 | + * @return integer |
|
| 274 | + * |
|
| 275 | + * @since 1.0.0 |
|
| 276 | + */ |
|
| 277 | + public function count() |
|
| 278 | + { |
|
| 279 | + return $this->map->count(); |
|
| 280 | + } |
|
| 281 | 281 | } |
@@ -67,8 +67,8 @@ |
||
| 67 | 67 | protected function __construct(SortedMap $map) |
| 68 | 68 | { |
| 69 | 69 | $this->map = $map; |
| 70 | - $this->comparator = function ($key1, $key2) { |
|
| 71 | - return - call_user_func($this->map->comparator, $key1, $key2); |
|
| 70 | + $this->comparator = function($key1, $key2) { |
|
| 71 | + return -call_user_func($this->map->comparator, $key1, $key2); |
|
| 72 | 72 | }; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -29,304 +29,304 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | abstract class AbstractSet implements SortedSet |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * @var SortedMap Underlying map |
|
| 34 | - * |
|
| 35 | - * @since 1.0.0 |
|
| 36 | - */ |
|
| 37 | - private $map; |
|
| 32 | + /** |
|
| 33 | + * @var SortedMap Underlying map |
|
| 34 | + * |
|
| 35 | + * @since 1.0.0 |
|
| 36 | + */ |
|
| 37 | + private $map; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Get the map |
|
| 41 | - * |
|
| 42 | - * @return SortedMap The underlying map |
|
| 43 | - * |
|
| 44 | - * @since 1.0.0 |
|
| 45 | - */ |
|
| 46 | - protected function getMap() |
|
| 47 | - { |
|
| 48 | - return $this->map; |
|
| 49 | - } |
|
| 39 | + /** |
|
| 40 | + * Get the map |
|
| 41 | + * |
|
| 42 | + * @return SortedMap The underlying map |
|
| 43 | + * |
|
| 44 | + * @since 1.0.0 |
|
| 45 | + */ |
|
| 46 | + protected function getMap() |
|
| 47 | + { |
|
| 48 | + return $this->map; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Set the map |
|
| 53 | - * |
|
| 54 | - * @param SortedMap $map The underlying map |
|
| 55 | - * |
|
| 56 | - * @return AbstractSet $this for chaining |
|
| 57 | - * |
|
| 58 | - * @since 1.0.0 |
|
| 59 | - */ |
|
| 60 | - protected function setMap(SortedMap $map) |
|
| 61 | - { |
|
| 62 | - $this->map = $map; |
|
| 51 | + /** |
|
| 52 | + * Set the map |
|
| 53 | + * |
|
| 54 | + * @param SortedMap $map The underlying map |
|
| 55 | + * |
|
| 56 | + * @return AbstractSet $this for chaining |
|
| 57 | + * |
|
| 58 | + * @since 1.0.0 |
|
| 59 | + */ |
|
| 60 | + protected function setMap(SortedMap $map) |
|
| 61 | + { |
|
| 62 | + $this->map = $map; |
|
| 63 | 63 | |
| 64 | - return $this; |
|
| 65 | - } |
|
| 64 | + return $this; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Magic get method |
|
| 69 | - * |
|
| 70 | - * @param string $property The property |
|
| 71 | - * |
|
| 72 | - * @throws \RuntimeException If the property does not exist |
|
| 73 | - * |
|
| 74 | - * @return mixed The value associated to the property |
|
| 75 | - * |
|
| 76 | - * @since 1.0.0 |
|
| 77 | - */ |
|
| 78 | - public function __get($property) |
|
| 79 | - { |
|
| 80 | - switch ($property) { |
|
| 81 | - case 'comparator': |
|
| 82 | - return $this->comparator(); |
|
| 83 | - case 'first': |
|
| 84 | - return $this->first(); |
|
| 85 | - case 'last': |
|
| 86 | - return $this->last(); |
|
| 87 | - case 'count': |
|
| 88 | - return $this->count(); |
|
| 89 | - default: |
|
| 90 | - throw new \RuntimeException('Undefined property'); |
|
| 91 | - } |
|
| 92 | - } |
|
| 67 | + /** |
|
| 68 | + * Magic get method |
|
| 69 | + * |
|
| 70 | + * @param string $property The property |
|
| 71 | + * |
|
| 72 | + * @throws \RuntimeException If the property does not exist |
|
| 73 | + * |
|
| 74 | + * @return mixed The value associated to the property |
|
| 75 | + * |
|
| 76 | + * @since 1.0.0 |
|
| 77 | + */ |
|
| 78 | + public function __get($property) |
|
| 79 | + { |
|
| 80 | + switch ($property) { |
|
| 81 | + case 'comparator': |
|
| 82 | + return $this->comparator(); |
|
| 83 | + case 'first': |
|
| 84 | + return $this->first(); |
|
| 85 | + case 'last': |
|
| 86 | + return $this->last(); |
|
| 87 | + case 'count': |
|
| 88 | + return $this->count(); |
|
| 89 | + default: |
|
| 90 | + throw new \RuntimeException('Undefined property'); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Get the comparator |
|
| 96 | - * |
|
| 97 | - * @return callable The comparator |
|
| 98 | - * |
|
| 99 | - * @since 1.0.0 |
|
| 100 | - */ |
|
| 101 | - public function comparator() |
|
| 102 | - { |
|
| 103 | - return $this->map->comparator(); |
|
| 104 | - } |
|
| 94 | + /** |
|
| 95 | + * Get the comparator |
|
| 96 | + * |
|
| 97 | + * @return callable The comparator |
|
| 98 | + * |
|
| 99 | + * @since 1.0.0 |
|
| 100 | + */ |
|
| 101 | + public function comparator() |
|
| 102 | + { |
|
| 103 | + return $this->map->comparator(); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Get the first element |
|
| 108 | - * |
|
| 109 | - * @return mixed The first element |
|
| 110 | - * |
|
| 111 | - * @throws \OutOfBoundsException If there is no element |
|
| 112 | - * |
|
| 113 | - * @since 1.0.0 |
|
| 114 | - */ |
|
| 115 | - public function first() |
|
| 116 | - { |
|
| 117 | - return $this->map->firstKey(); |
|
| 118 | - } |
|
| 106 | + /** |
|
| 107 | + * Get the first element |
|
| 108 | + * |
|
| 109 | + * @return mixed The first element |
|
| 110 | + * |
|
| 111 | + * @throws \OutOfBoundsException If there is no element |
|
| 112 | + * |
|
| 113 | + * @since 1.0.0 |
|
| 114 | + */ |
|
| 115 | + public function first() |
|
| 116 | + { |
|
| 117 | + return $this->map->firstKey(); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Get the last element |
|
| 122 | - * |
|
| 123 | - * @return mixed The last element |
|
| 124 | - * |
|
| 125 | - * @throws \OutOfBoundsException If there is no element |
|
| 126 | - * |
|
| 127 | - * @since 1.0.0 |
|
| 128 | - */ |
|
| 129 | - public function last() |
|
| 130 | - { |
|
| 131 | - return $this->map->lastKey(); |
|
| 132 | - } |
|
| 120 | + /** |
|
| 121 | + * Get the last element |
|
| 122 | + * |
|
| 123 | + * @return mixed The last element |
|
| 124 | + * |
|
| 125 | + * @throws \OutOfBoundsException If there is no element |
|
| 126 | + * |
|
| 127 | + * @since 1.0.0 |
|
| 128 | + */ |
|
| 129 | + public function last() |
|
| 130 | + { |
|
| 131 | + return $this->map->lastKey(); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Returns the greatest element lesser than the given element |
|
| 136 | - * |
|
| 137 | - * @param mixed $element The searched element |
|
| 138 | - * |
|
| 139 | - * @return mixed The found element |
|
| 140 | - * |
|
| 141 | - * @throws \OutOfBoundsException If there is no lower element |
|
| 142 | - * |
|
| 143 | - * @since 1.0.0 |
|
| 144 | - */ |
|
| 145 | - public function lower($element) |
|
| 146 | - { |
|
| 147 | - return $this->map->lowerKey($element); |
|
| 148 | - } |
|
| 134 | + /** |
|
| 135 | + * Returns the greatest element lesser than the given element |
|
| 136 | + * |
|
| 137 | + * @param mixed $element The searched element |
|
| 138 | + * |
|
| 139 | + * @return mixed The found element |
|
| 140 | + * |
|
| 141 | + * @throws \OutOfBoundsException If there is no lower element |
|
| 142 | + * |
|
| 143 | + * @since 1.0.0 |
|
| 144 | + */ |
|
| 145 | + public function lower($element) |
|
| 146 | + { |
|
| 147 | + return $this->map->lowerKey($element); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Returns the greatest element lesser than or equal to the given element |
|
| 152 | - * |
|
| 153 | - * @param mixed $element The searched element |
|
| 154 | - * |
|
| 155 | - * @return mixed The found element |
|
| 156 | - * |
|
| 157 | - * @throws \OutOfBoundsException If there is no floor element |
|
| 158 | - * |
|
| 159 | - * @since 1.0.0 |
|
| 160 | - */ |
|
| 161 | - public function floor($element) |
|
| 162 | - { |
|
| 163 | - return $this->map->floorKey($element); |
|
| 164 | - } |
|
| 150 | + /** |
|
| 151 | + * Returns the greatest element lesser than or equal to the given element |
|
| 152 | + * |
|
| 153 | + * @param mixed $element The searched element |
|
| 154 | + * |
|
| 155 | + * @return mixed The found element |
|
| 156 | + * |
|
| 157 | + * @throws \OutOfBoundsException If there is no floor element |
|
| 158 | + * |
|
| 159 | + * @since 1.0.0 |
|
| 160 | + */ |
|
| 161 | + public function floor($element) |
|
| 162 | + { |
|
| 163 | + return $this->map->floorKey($element); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * Returns the element equal to the given element |
|
| 168 | - * |
|
| 169 | - * @param mixed $element The searched element |
|
| 170 | - * |
|
| 171 | - * @return mixed The found element |
|
| 172 | - * |
|
| 173 | - * @throws \OutOfBoundsException If there is no such element |
|
| 174 | - * |
|
| 175 | - * @since 1.0.0 |
|
| 176 | - */ |
|
| 177 | - public function find($element) |
|
| 178 | - { |
|
| 179 | - return $this->map->findKey($element); |
|
| 180 | - } |
|
| 166 | + /** |
|
| 167 | + * Returns the element equal to the given element |
|
| 168 | + * |
|
| 169 | + * @param mixed $element The searched element |
|
| 170 | + * |
|
| 171 | + * @return mixed The found element |
|
| 172 | + * |
|
| 173 | + * @throws \OutOfBoundsException If there is no such element |
|
| 174 | + * |
|
| 175 | + * @since 1.0.0 |
|
| 176 | + */ |
|
| 177 | + public function find($element) |
|
| 178 | + { |
|
| 179 | + return $this->map->findKey($element); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Returns the lowest element greater than or equal to the given element |
|
| 184 | - * |
|
| 185 | - * @param mixed $element The searched element |
|
| 186 | - * |
|
| 187 | - * @return mixed The found element |
|
| 188 | - * |
|
| 189 | - * @throws \OutOfBoundsException If there is no ceiling element |
|
| 190 | - * |
|
| 191 | - * @since 1.0.0 |
|
| 192 | - */ |
|
| 193 | - public function ceiling($element) |
|
| 194 | - { |
|
| 195 | - return $this->map->ceilingKey($element); |
|
| 196 | - } |
|
| 182 | + /** |
|
| 183 | + * Returns the lowest element greater than or equal to the given element |
|
| 184 | + * |
|
| 185 | + * @param mixed $element The searched element |
|
| 186 | + * |
|
| 187 | + * @return mixed The found element |
|
| 188 | + * |
|
| 189 | + * @throws \OutOfBoundsException If there is no ceiling element |
|
| 190 | + * |
|
| 191 | + * @since 1.0.0 |
|
| 192 | + */ |
|
| 193 | + public function ceiling($element) |
|
| 194 | + { |
|
| 195 | + return $this->map->ceilingKey($element); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * Returns the lowest element greater than to the given element |
|
| 200 | - * |
|
| 201 | - * @param mixed $element The searched element |
|
| 202 | - * |
|
| 203 | - * @return mixed The found element |
|
| 204 | - * |
|
| 205 | - * @throws \OutOfBoundsException If there is no higher element |
|
| 206 | - * |
|
| 207 | - * @since 1.0.0 |
|
| 208 | - */ |
|
| 209 | - public function higher($element) |
|
| 210 | - { |
|
| 211 | - return $this->map->higherKey($element); |
|
| 212 | - } |
|
| 198 | + /** |
|
| 199 | + * Returns the lowest element greater than to the given element |
|
| 200 | + * |
|
| 201 | + * @param mixed $element The searched element |
|
| 202 | + * |
|
| 203 | + * @return mixed The found element |
|
| 204 | + * |
|
| 205 | + * @throws \OutOfBoundsException If there is no higher element |
|
| 206 | + * |
|
| 207 | + * @since 1.0.0 |
|
| 208 | + */ |
|
| 209 | + public function higher($element) |
|
| 210 | + { |
|
| 211 | + return $this->map->higherKey($element); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Convert the object to a string |
|
| 216 | - * |
|
| 217 | - * @return string String representation of the object |
|
| 218 | - * |
|
| 219 | - * @since 1.0.0 |
|
| 220 | - */ |
|
| 221 | - public function __toString() |
|
| 222 | - { |
|
| 223 | - return json_encode($this->toArray()); |
|
| 224 | - } |
|
| 214 | + /** |
|
| 215 | + * Convert the object to a string |
|
| 216 | + * |
|
| 217 | + * @return string String representation of the object |
|
| 218 | + * |
|
| 219 | + * @since 1.0.0 |
|
| 220 | + */ |
|
| 221 | + public function __toString() |
|
| 222 | + { |
|
| 223 | + return json_encode($this->toArray()); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Convert the object to an array |
|
| 228 | - * |
|
| 229 | - * @return array Array representation of the object |
|
| 230 | - * |
|
| 231 | - * @since 1.0.0 |
|
| 232 | - */ |
|
| 233 | - public function toArray() |
|
| 234 | - { |
|
| 235 | - $array = array(); |
|
| 226 | + /** |
|
| 227 | + * Convert the object to an array |
|
| 228 | + * |
|
| 229 | + * @return array Array representation of the object |
|
| 230 | + * |
|
| 231 | + * @since 1.0.0 |
|
| 232 | + */ |
|
| 233 | + public function toArray() |
|
| 234 | + { |
|
| 235 | + $array = array(); |
|
| 236 | 236 | |
| 237 | - foreach ($this as $value) { |
|
| 238 | - $array[] = $value; |
|
| 239 | - } |
|
| 237 | + foreach ($this as $value) { |
|
| 238 | + $array[] = $value; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - return $array; |
|
| 242 | - } |
|
| 241 | + return $array; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * Create an iterator |
|
| 246 | - * |
|
| 247 | - * @return Iterator A new iterator |
|
| 248 | - * |
|
| 249 | - * @since 1.0.0 |
|
| 250 | - */ |
|
| 251 | - public function getIterator() |
|
| 252 | - { |
|
| 253 | - return Iterator::keys($this->map); |
|
| 254 | - } |
|
| 244 | + /** |
|
| 245 | + * Create an iterator |
|
| 246 | + * |
|
| 247 | + * @return Iterator A new iterator |
|
| 248 | + * |
|
| 249 | + * @since 1.0.0 |
|
| 250 | + */ |
|
| 251 | + public function getIterator() |
|
| 252 | + { |
|
| 253 | + return Iterator::keys($this->map); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * Get the value for an element |
|
| 258 | - * |
|
| 259 | - * @param mixed $element The element |
|
| 260 | - * |
|
| 261 | - * @return mixed The found value |
|
| 262 | - * |
|
| 263 | - * @since 1.0.0 |
|
| 264 | - */ |
|
| 265 | - public function offsetGet($element) |
|
| 266 | - { |
|
| 267 | - try { |
|
| 268 | - return (bool) $this->map->find($element); |
|
| 269 | - } catch (\OutOfBoundsException $e) { |
|
| 270 | - return false; |
|
| 271 | - } |
|
| 272 | - } |
|
| 256 | + /** |
|
| 257 | + * Get the value for an element |
|
| 258 | + * |
|
| 259 | + * @param mixed $element The element |
|
| 260 | + * |
|
| 261 | + * @return mixed The found value |
|
| 262 | + * |
|
| 263 | + * @since 1.0.0 |
|
| 264 | + */ |
|
| 265 | + public function offsetGet($element) |
|
| 266 | + { |
|
| 267 | + try { |
|
| 268 | + return (bool) $this->map->find($element); |
|
| 269 | + } catch (\OutOfBoundsException $e) { |
|
| 270 | + return false; |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Test the existence of an element |
|
| 276 | - * |
|
| 277 | - * @param mixed $element The element |
|
| 278 | - * |
|
| 279 | - * @return boolean TRUE if the element exists, false otherwise |
|
| 280 | - * |
|
| 281 | - * @since 1.0.0 |
|
| 282 | - */ |
|
| 283 | - public function offsetExists($element) |
|
| 284 | - { |
|
| 285 | - return $this->offsetGet($element); |
|
| 286 | - } |
|
| 274 | + /** |
|
| 275 | + * Test the existence of an element |
|
| 276 | + * |
|
| 277 | + * @param mixed $element The element |
|
| 278 | + * |
|
| 279 | + * @return boolean TRUE if the element exists, false otherwise |
|
| 280 | + * |
|
| 281 | + * @since 1.0.0 |
|
| 282 | + */ |
|
| 283 | + public function offsetExists($element) |
|
| 284 | + { |
|
| 285 | + return $this->offsetGet($element); |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - /** |
|
| 289 | - * Set the value for an element |
|
| 290 | - * |
|
| 291 | - * @param mixed $element The element |
|
| 292 | - * @param mixed $value The value |
|
| 293 | - * |
|
| 294 | - * @return void |
|
| 295 | - * |
|
| 296 | - * @throws \RuntimeOperation The operation is not supported by this class |
|
| 297 | - * |
|
| 298 | - * @since 1.0.0 |
|
| 299 | - */ |
|
| 300 | - public function offsetSet($element, $value) |
|
| 301 | - { |
|
| 302 | - throw new \RuntimeException('Unsupported operation'); |
|
| 303 | - } |
|
| 288 | + /** |
|
| 289 | + * Set the value for an element |
|
| 290 | + * |
|
| 291 | + * @param mixed $element The element |
|
| 292 | + * @param mixed $value The value |
|
| 293 | + * |
|
| 294 | + * @return void |
|
| 295 | + * |
|
| 296 | + * @throws \RuntimeOperation The operation is not supported by this class |
|
| 297 | + * |
|
| 298 | + * @since 1.0.0 |
|
| 299 | + */ |
|
| 300 | + public function offsetSet($element, $value) |
|
| 301 | + { |
|
| 302 | + throw new \RuntimeException('Unsupported operation'); |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | - /** |
|
| 306 | - * Unset the existence of an element |
|
| 307 | - * |
|
| 308 | - * @param mixed $element The element |
|
| 309 | - * |
|
| 310 | - * @return void |
|
| 311 | - * |
|
| 312 | - * @throws \RuntimeOperation The operation is not supported by this class |
|
| 313 | - * |
|
| 314 | - * @since 1.0.0 |
|
| 315 | - */ |
|
| 316 | - public function offsetUnset($element) |
|
| 317 | - { |
|
| 318 | - throw new \RuntimeException('Unsupported operation'); |
|
| 319 | - } |
|
| 305 | + /** |
|
| 306 | + * Unset the existence of an element |
|
| 307 | + * |
|
| 308 | + * @param mixed $element The element |
|
| 309 | + * |
|
| 310 | + * @return void |
|
| 311 | + * |
|
| 312 | + * @throws \RuntimeOperation The operation is not supported by this class |
|
| 313 | + * |
|
| 314 | + * @since 1.0.0 |
|
| 315 | + */ |
|
| 316 | + public function offsetUnset($element) |
|
| 317 | + { |
|
| 318 | + throw new \RuntimeException('Unsupported operation'); |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - /** |
|
| 322 | - * Count the number of elements |
|
| 323 | - * |
|
| 324 | - * @return integer |
|
| 325 | - * |
|
| 326 | - * @since 1.0.0 |
|
| 327 | - */ |
|
| 328 | - public function count() |
|
| 329 | - { |
|
| 330 | - return count($this->map); |
|
| 331 | - } |
|
| 321 | + /** |
|
| 322 | + * Count the number of elements |
|
| 323 | + * |
|
| 324 | + * @return integer |
|
| 325 | + * |
|
| 326 | + * @since 1.0.0 |
|
| 327 | + */ |
|
| 328 | + public function count() |
|
| 329 | + { |
|
| 330 | + return count($this->map); |
|
| 331 | + } |
|
| 332 | 332 | } |
@@ -35,414 +35,414 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class TreeMap extends AbstractMap |
| 37 | 37 | { |
| 38 | - /** |
|
| 39 | - * @var TreeNode Root of the tree |
|
| 40 | - * |
|
| 41 | - * @since 1.0.0 |
|
| 42 | - */ |
|
| 43 | - private $root; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var callable Comparator function |
|
| 47 | - * |
|
| 48 | - * @param mixed $key1 First key |
|
| 49 | - * @param mixed $key2 Second key |
|
| 50 | - * |
|
| 51 | - * @return integer negative if $key1 is lesser than $key2, |
|
| 52 | - * 0 if $key1 is equal to $key2, |
|
| 53 | - * positive if $key1 is greater than $key2 |
|
| 54 | - * |
|
| 55 | - * @since 1.0.0 |
|
| 56 | - */ |
|
| 57 | - private $comparator; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Constructor |
|
| 61 | - * |
|
| 62 | - * @param callable $comparator Comparison function |
|
| 63 | - * |
|
| 64 | - * @since 1.0.0 |
|
| 65 | - */ |
|
| 66 | - protected function __construct($comparator = null) |
|
| 67 | - { |
|
| 68 | - if ($comparator == null) { |
|
| 69 | - $this->comparator = function ($key1, $key2) { |
|
| 70 | - return $key1 - $key2; |
|
| 71 | - }; |
|
| 72 | - } else { |
|
| 73 | - $this->comparator = $comparator; |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Create |
|
| 79 | - * |
|
| 80 | - * @param callable $comparator Comparison function |
|
| 81 | - * |
|
| 82 | - * @return TreeMap A new TreeMap |
|
| 83 | - * |
|
| 84 | - * @since 1.0.0 |
|
| 85 | - */ |
|
| 86 | - public static function create($comparator = null) |
|
| 87 | - { |
|
| 88 | - return new static($comparator); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the comparator |
|
| 93 | - * |
|
| 94 | - * @return callable The comparator |
|
| 95 | - * |
|
| 96 | - * @since 1.0.0 |
|
| 97 | - */ |
|
| 98 | - public function comparator() |
|
| 99 | - { |
|
| 100 | - return $this->comparator; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Get the first element |
|
| 105 | - * |
|
| 106 | - * @return mixed The first element |
|
| 107 | - * |
|
| 108 | - * @throws \OutOfBoundsException If there is no element |
|
| 109 | - * |
|
| 110 | - * @since 1.0.0 |
|
| 111 | - */ |
|
| 112 | - public function first() |
|
| 113 | - { |
|
| 114 | - if ($this->root) { |
|
| 115 | - return $this->root->first; |
|
| 116 | - } else { |
|
| 117 | - throw new \OutOfBoundsException('First element unexisting'); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get the last element |
|
| 123 | - * |
|
| 124 | - * @return mixed The last element |
|
| 125 | - * |
|
| 126 | - * @throws \OutOfBoundsException If there is no element |
|
| 127 | - * |
|
| 128 | - * @since 1.0.0 |
|
| 129 | - */ |
|
| 130 | - public function last() |
|
| 131 | - { |
|
| 132 | - if ($this->root) { |
|
| 133 | - return $this->root->last; |
|
| 134 | - } else { |
|
| 135 | - throw new \OutOfBoundsException('Last element unexisting'); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Get the predecessor element |
|
| 141 | - * |
|
| 142 | - * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 143 | - * |
|
| 144 | - * @return mixed The predecessor element |
|
| 145 | - * |
|
| 146 | - * @throws \OutOfBoundsException If there is no predecessor |
|
| 147 | - * |
|
| 148 | - * @since 1.0.0 |
|
| 149 | - */ |
|
| 150 | - public function predecessor($element) |
|
| 151 | - { |
|
| 152 | - $predecessor = $element->predecessor; |
|
| 153 | - |
|
| 154 | - if ($predecessor) { |
|
| 155 | - return $predecessor; |
|
| 156 | - } else { |
|
| 157 | - throw new \OutOfBoundsException('Predecessor element unexisting'); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Get the successor element |
|
| 163 | - * |
|
| 164 | - * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 165 | - * |
|
| 166 | - * @return mixed The successor element |
|
| 167 | - * |
|
| 168 | - * @throws \OutOfBoundsException If there is no successor |
|
| 169 | - * |
|
| 170 | - * @since 1.0.0 |
|
| 171 | - */ |
|
| 172 | - public function successor($element) |
|
| 173 | - { |
|
| 174 | - $successor = $element->successor; |
|
| 175 | - |
|
| 176 | - if ($successor) { |
|
| 177 | - return $successor; |
|
| 178 | - } else { |
|
| 179 | - throw new \OutOfBoundsException('Successor element unexisting'); |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Returns the element whose key is the greatest key lesser than the given key |
|
| 185 | - * |
|
| 186 | - * @param mixed $key The searched key |
|
| 187 | - * |
|
| 188 | - * @return mixed The found element |
|
| 189 | - * |
|
| 190 | - * @throws \OutOfBoundsException If there is no lower element |
|
| 191 | - * |
|
| 192 | - * @since 1.0.0 |
|
| 193 | - */ |
|
| 194 | - public function lower($key) |
|
| 195 | - { |
|
| 196 | - if ($this->root) { |
|
| 197 | - $lower = $this->root->find($key, $this->comparator, -2); |
|
| 198 | - } else { |
|
| 199 | - $lower = null; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if ($lower) { |
|
| 203 | - return $lower; |
|
| 204 | - } else { |
|
| 205 | - throw new \OutOfBoundsException('Lower element unexisting'); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Returns the element whose key is the greatest key lesser than or equal to the given key |
|
| 211 | - * |
|
| 212 | - * @param mixed $key The searched key |
|
| 213 | - * |
|
| 214 | - * @return mixed The found element |
|
| 215 | - * |
|
| 216 | - * @throws \OutOfBoundsException If there is no floor element |
|
| 217 | - * |
|
| 218 | - * @since 1.0.0 |
|
| 219 | - */ |
|
| 220 | - public function floor($key) |
|
| 221 | - { |
|
| 222 | - if ($this->root) { |
|
| 223 | - $floor = $this->root->find($key, $this->comparator, -1); |
|
| 224 | - } else { |
|
| 225 | - $floor = null; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - if ($floor) { |
|
| 229 | - return $floor; |
|
| 230 | - } else { |
|
| 231 | - throw new \OutOfBoundsException('Floor element unexisting'); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Returns the element whose key is equal to the given key |
|
| 237 | - * |
|
| 238 | - * @param mixed $key The searched key |
|
| 239 | - * |
|
| 240 | - * @return mixed The found element |
|
| 241 | - * |
|
| 242 | - * @throws \OutOfBoundsException If there is no such element |
|
| 243 | - * |
|
| 244 | - * @since 1.0.0 |
|
| 245 | - */ |
|
| 246 | - public function find($key) |
|
| 247 | - { |
|
| 248 | - if ($this->root) { |
|
| 249 | - $find = $this->root->find($key, $this->comparator, 0); |
|
| 250 | - } else { |
|
| 251 | - $find = null; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if ($find) { |
|
| 255 | - return $find; |
|
| 256 | - } else { |
|
| 257 | - throw new \OutOfBoundsException('Element unexisting'); |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Returns the element whose key is the lowest key greater than or equal to the given key |
|
| 263 | - * |
|
| 264 | - * @param mixed $key The searched key |
|
| 265 | - * |
|
| 266 | - * @return mixed The found element |
|
| 267 | - * |
|
| 268 | - * @throws \OutOfBoundsException If there is no ceiling element |
|
| 269 | - * |
|
| 270 | - * @since 1.0.0 |
|
| 271 | - */ |
|
| 272 | - public function ceiling($key) |
|
| 273 | - { |
|
| 274 | - if ($this->root) { |
|
| 275 | - $ceiling = $this->root->find($key, $this->comparator, 1); |
|
| 276 | - } else { |
|
| 277 | - $ceiling = null; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - if ($ceiling) { |
|
| 281 | - return $ceiling; |
|
| 282 | - } else { |
|
| 283 | - throw new \OutOfBoundsException('Ceiling element unexisting'); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Returns the element whose key is the lowest key greater than to the given key |
|
| 289 | - * |
|
| 290 | - * @param mixed $key The searched key |
|
| 291 | - * |
|
| 292 | - * @return mixed The found element |
|
| 293 | - * |
|
| 294 | - * @throws \OutOfBoundsException If there is no higher element |
|
| 295 | - * |
|
| 296 | - * @since 1.0.0 |
|
| 297 | - */ |
|
| 298 | - public function higher($key) |
|
| 299 | - { |
|
| 300 | - if ($this->root) { |
|
| 301 | - $higher = $this->root->find($key, $this->comparator, 2); |
|
| 302 | - } else { |
|
| 303 | - $higher = null; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - if ($higher) { |
|
| 307 | - return $higher; |
|
| 308 | - } else { |
|
| 309 | - throw new \OutOfBoundsException('Higher element unexisting'); |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Put values in the map |
|
| 315 | - * |
|
| 316 | - * @param \Traversable $traversable Values to put in the map |
|
| 317 | - * |
|
| 318 | - * @return TreeMap $this for chaining |
|
| 319 | - * |
|
| 320 | - * @since 1.0.0 |
|
| 321 | - */ |
|
| 322 | - public function put($traversable = array()) |
|
| 323 | - { |
|
| 324 | - foreach ($traversable as $key => $value) { |
|
| 325 | - $this[$key] = $value; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return $this; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Clear the map |
|
| 333 | - * |
|
| 334 | - * @return TreeMap $this for chaining |
|
| 335 | - * |
|
| 336 | - * @since 1.0.0 |
|
| 337 | - */ |
|
| 338 | - public function clear() |
|
| 339 | - { |
|
| 340 | - $this->root = null; |
|
| 341 | - |
|
| 342 | - return $this; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Initialise the map |
|
| 347 | - * |
|
| 348 | - * @param \Traversable $traversable Values to initialise the map |
|
| 349 | - * |
|
| 350 | - * @return TreeMap $this for chaining |
|
| 351 | - * |
|
| 352 | - * @since 1.0.0 |
|
| 353 | - */ |
|
| 354 | - public function initialise($traversable = array()) |
|
| 355 | - { |
|
| 356 | - return $this->clear()->put($traversable); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Clone the map |
|
| 361 | - * |
|
| 362 | - * @return void |
|
| 363 | - * |
|
| 364 | - * @since 1.0.0 |
|
| 365 | - */ |
|
| 366 | - public function __clone() |
|
| 367 | - { |
|
| 368 | - if ($this->root != null) { |
|
| 369 | - $root = $this->root; |
|
| 370 | - $this->root = null; |
|
| 371 | - $node = $root->first; |
|
| 372 | - |
|
| 373 | - while ($node != null) { |
|
| 374 | - $this[$node->key] = $node->value; |
|
| 375 | - $node = $node->successor; |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Serialize the object |
|
| 382 | - * |
|
| 383 | - * @return array Array of values |
|
| 384 | - * |
|
| 385 | - * @since 1.0.0 |
|
| 386 | - */ |
|
| 387 | - public function jsonSerialize() |
|
| 388 | - { |
|
| 389 | - $array = array(); |
|
| 390 | - |
|
| 391 | - foreach ($this as $key => $value) { |
|
| 392 | - $array[$key] = $value; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - return array('TreeMap' => $array); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Set the value for a key |
|
| 400 | - * |
|
| 401 | - * @param mixed $key The key |
|
| 402 | - * @param mixed $value The value |
|
| 403 | - * |
|
| 404 | - * @return void |
|
| 405 | - * |
|
| 406 | - * @since 1.0.0 |
|
| 407 | - */ |
|
| 408 | - public function offsetSet($key, $value) |
|
| 409 | - { |
|
| 410 | - if ($this->root) { |
|
| 411 | - $this->root = $this->root->insert($key, $value, $this->comparator); |
|
| 412 | - } else { |
|
| 413 | - $this->root = TreeNode::create($key, $value); |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Unset the existence of a key |
|
| 419 | - * |
|
| 420 | - * @param mixed $key The key |
|
| 421 | - * |
|
| 422 | - * @return void |
|
| 423 | - * |
|
| 424 | - * @since 1.0.0 |
|
| 425 | - */ |
|
| 426 | - public function offsetUnset($key) |
|
| 427 | - { |
|
| 428 | - if ($this->root) { |
|
| 429 | - $this->root = $this->root->remove($key, $this->comparator); |
|
| 430 | - } |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Count the number of key/value pairs |
|
| 435 | - * |
|
| 436 | - * @return integer |
|
| 437 | - * |
|
| 438 | - * @since 1.0.0 |
|
| 439 | - */ |
|
| 440 | - public function count() |
|
| 441 | - { |
|
| 442 | - if ($this->root) { |
|
| 443 | - return count($this->root); |
|
| 444 | - } else { |
|
| 445 | - return 0; |
|
| 446 | - } |
|
| 447 | - } |
|
| 38 | + /** |
|
| 39 | + * @var TreeNode Root of the tree |
|
| 40 | + * |
|
| 41 | + * @since 1.0.0 |
|
| 42 | + */ |
|
| 43 | + private $root; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var callable Comparator function |
|
| 47 | + * |
|
| 48 | + * @param mixed $key1 First key |
|
| 49 | + * @param mixed $key2 Second key |
|
| 50 | + * |
|
| 51 | + * @return integer negative if $key1 is lesser than $key2, |
|
| 52 | + * 0 if $key1 is equal to $key2, |
|
| 53 | + * positive if $key1 is greater than $key2 |
|
| 54 | + * |
|
| 55 | + * @since 1.0.0 |
|
| 56 | + */ |
|
| 57 | + private $comparator; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Constructor |
|
| 61 | + * |
|
| 62 | + * @param callable $comparator Comparison function |
|
| 63 | + * |
|
| 64 | + * @since 1.0.0 |
|
| 65 | + */ |
|
| 66 | + protected function __construct($comparator = null) |
|
| 67 | + { |
|
| 68 | + if ($comparator == null) { |
|
| 69 | + $this->comparator = function ($key1, $key2) { |
|
| 70 | + return $key1 - $key2; |
|
| 71 | + }; |
|
| 72 | + } else { |
|
| 73 | + $this->comparator = $comparator; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Create |
|
| 79 | + * |
|
| 80 | + * @param callable $comparator Comparison function |
|
| 81 | + * |
|
| 82 | + * @return TreeMap A new TreeMap |
|
| 83 | + * |
|
| 84 | + * @since 1.0.0 |
|
| 85 | + */ |
|
| 86 | + public static function create($comparator = null) |
|
| 87 | + { |
|
| 88 | + return new static($comparator); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the comparator |
|
| 93 | + * |
|
| 94 | + * @return callable The comparator |
|
| 95 | + * |
|
| 96 | + * @since 1.0.0 |
|
| 97 | + */ |
|
| 98 | + public function comparator() |
|
| 99 | + { |
|
| 100 | + return $this->comparator; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Get the first element |
|
| 105 | + * |
|
| 106 | + * @return mixed The first element |
|
| 107 | + * |
|
| 108 | + * @throws \OutOfBoundsException If there is no element |
|
| 109 | + * |
|
| 110 | + * @since 1.0.0 |
|
| 111 | + */ |
|
| 112 | + public function first() |
|
| 113 | + { |
|
| 114 | + if ($this->root) { |
|
| 115 | + return $this->root->first; |
|
| 116 | + } else { |
|
| 117 | + throw new \OutOfBoundsException('First element unexisting'); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get the last element |
|
| 123 | + * |
|
| 124 | + * @return mixed The last element |
|
| 125 | + * |
|
| 126 | + * @throws \OutOfBoundsException If there is no element |
|
| 127 | + * |
|
| 128 | + * @since 1.0.0 |
|
| 129 | + */ |
|
| 130 | + public function last() |
|
| 131 | + { |
|
| 132 | + if ($this->root) { |
|
| 133 | + return $this->root->last; |
|
| 134 | + } else { |
|
| 135 | + throw new \OutOfBoundsException('Last element unexisting'); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Get the predecessor element |
|
| 141 | + * |
|
| 142 | + * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 143 | + * |
|
| 144 | + * @return mixed The predecessor element |
|
| 145 | + * |
|
| 146 | + * @throws \OutOfBoundsException If there is no predecessor |
|
| 147 | + * |
|
| 148 | + * @since 1.0.0 |
|
| 149 | + */ |
|
| 150 | + public function predecessor($element) |
|
| 151 | + { |
|
| 152 | + $predecessor = $element->predecessor; |
|
| 153 | + |
|
| 154 | + if ($predecessor) { |
|
| 155 | + return $predecessor; |
|
| 156 | + } else { |
|
| 157 | + throw new \OutOfBoundsException('Predecessor element unexisting'); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Get the successor element |
|
| 163 | + * |
|
| 164 | + * @param TreeNode $element A tree node member of the underlying TreeMap |
|
| 165 | + * |
|
| 166 | + * @return mixed The successor element |
|
| 167 | + * |
|
| 168 | + * @throws \OutOfBoundsException If there is no successor |
|
| 169 | + * |
|
| 170 | + * @since 1.0.0 |
|
| 171 | + */ |
|
| 172 | + public function successor($element) |
|
| 173 | + { |
|
| 174 | + $successor = $element->successor; |
|
| 175 | + |
|
| 176 | + if ($successor) { |
|
| 177 | + return $successor; |
|
| 178 | + } else { |
|
| 179 | + throw new \OutOfBoundsException('Successor element unexisting'); |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Returns the element whose key is the greatest key lesser than the given key |
|
| 185 | + * |
|
| 186 | + * @param mixed $key The searched key |
|
| 187 | + * |
|
| 188 | + * @return mixed The found element |
|
| 189 | + * |
|
| 190 | + * @throws \OutOfBoundsException If there is no lower element |
|
| 191 | + * |
|
| 192 | + * @since 1.0.0 |
|
| 193 | + */ |
|
| 194 | + public function lower($key) |
|
| 195 | + { |
|
| 196 | + if ($this->root) { |
|
| 197 | + $lower = $this->root->find($key, $this->comparator, -2); |
|
| 198 | + } else { |
|
| 199 | + $lower = null; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if ($lower) { |
|
| 203 | + return $lower; |
|
| 204 | + } else { |
|
| 205 | + throw new \OutOfBoundsException('Lower element unexisting'); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Returns the element whose key is the greatest key lesser than or equal to the given key |
|
| 211 | + * |
|
| 212 | + * @param mixed $key The searched key |
|
| 213 | + * |
|
| 214 | + * @return mixed The found element |
|
| 215 | + * |
|
| 216 | + * @throws \OutOfBoundsException If there is no floor element |
|
| 217 | + * |
|
| 218 | + * @since 1.0.0 |
|
| 219 | + */ |
|
| 220 | + public function floor($key) |
|
| 221 | + { |
|
| 222 | + if ($this->root) { |
|
| 223 | + $floor = $this->root->find($key, $this->comparator, -1); |
|
| 224 | + } else { |
|
| 225 | + $floor = null; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + if ($floor) { |
|
| 229 | + return $floor; |
|
| 230 | + } else { |
|
| 231 | + throw new \OutOfBoundsException('Floor element unexisting'); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Returns the element whose key is equal to the given key |
|
| 237 | + * |
|
| 238 | + * @param mixed $key The searched key |
|
| 239 | + * |
|
| 240 | + * @return mixed The found element |
|
| 241 | + * |
|
| 242 | + * @throws \OutOfBoundsException If there is no such element |
|
| 243 | + * |
|
| 244 | + * @since 1.0.0 |
|
| 245 | + */ |
|
| 246 | + public function find($key) |
|
| 247 | + { |
|
| 248 | + if ($this->root) { |
|
| 249 | + $find = $this->root->find($key, $this->comparator, 0); |
|
| 250 | + } else { |
|
| 251 | + $find = null; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if ($find) { |
|
| 255 | + return $find; |
|
| 256 | + } else { |
|
| 257 | + throw new \OutOfBoundsException('Element unexisting'); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Returns the element whose key is the lowest key greater than or equal to the given key |
|
| 263 | + * |
|
| 264 | + * @param mixed $key The searched key |
|
| 265 | + * |
|
| 266 | + * @return mixed The found element |
|
| 267 | + * |
|
| 268 | + * @throws \OutOfBoundsException If there is no ceiling element |
|
| 269 | + * |
|
| 270 | + * @since 1.0.0 |
|
| 271 | + */ |
|
| 272 | + public function ceiling($key) |
|
| 273 | + { |
|
| 274 | + if ($this->root) { |
|
| 275 | + $ceiling = $this->root->find($key, $this->comparator, 1); |
|
| 276 | + } else { |
|
| 277 | + $ceiling = null; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + if ($ceiling) { |
|
| 281 | + return $ceiling; |
|
| 282 | + } else { |
|
| 283 | + throw new \OutOfBoundsException('Ceiling element unexisting'); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Returns the element whose key is the lowest key greater than to the given key |
|
| 289 | + * |
|
| 290 | + * @param mixed $key The searched key |
|
| 291 | + * |
|
| 292 | + * @return mixed The found element |
|
| 293 | + * |
|
| 294 | + * @throws \OutOfBoundsException If there is no higher element |
|
| 295 | + * |
|
| 296 | + * @since 1.0.0 |
|
| 297 | + */ |
|
| 298 | + public function higher($key) |
|
| 299 | + { |
|
| 300 | + if ($this->root) { |
|
| 301 | + $higher = $this->root->find($key, $this->comparator, 2); |
|
| 302 | + } else { |
|
| 303 | + $higher = null; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + if ($higher) { |
|
| 307 | + return $higher; |
|
| 308 | + } else { |
|
| 309 | + throw new \OutOfBoundsException('Higher element unexisting'); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Put values in the map |
|
| 315 | + * |
|
| 316 | + * @param \Traversable $traversable Values to put in the map |
|
| 317 | + * |
|
| 318 | + * @return TreeMap $this for chaining |
|
| 319 | + * |
|
| 320 | + * @since 1.0.0 |
|
| 321 | + */ |
|
| 322 | + public function put($traversable = array()) |
|
| 323 | + { |
|
| 324 | + foreach ($traversable as $key => $value) { |
|
| 325 | + $this[$key] = $value; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return $this; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Clear the map |
|
| 333 | + * |
|
| 334 | + * @return TreeMap $this for chaining |
|
| 335 | + * |
|
| 336 | + * @since 1.0.0 |
|
| 337 | + */ |
|
| 338 | + public function clear() |
|
| 339 | + { |
|
| 340 | + $this->root = null; |
|
| 341 | + |
|
| 342 | + return $this; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Initialise the map |
|
| 347 | + * |
|
| 348 | + * @param \Traversable $traversable Values to initialise the map |
|
| 349 | + * |
|
| 350 | + * @return TreeMap $this for chaining |
|
| 351 | + * |
|
| 352 | + * @since 1.0.0 |
|
| 353 | + */ |
|
| 354 | + public function initialise($traversable = array()) |
|
| 355 | + { |
|
| 356 | + return $this->clear()->put($traversable); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Clone the map |
|
| 361 | + * |
|
| 362 | + * @return void |
|
| 363 | + * |
|
| 364 | + * @since 1.0.0 |
|
| 365 | + */ |
|
| 366 | + public function __clone() |
|
| 367 | + { |
|
| 368 | + if ($this->root != null) { |
|
| 369 | + $root = $this->root; |
|
| 370 | + $this->root = null; |
|
| 371 | + $node = $root->first; |
|
| 372 | + |
|
| 373 | + while ($node != null) { |
|
| 374 | + $this[$node->key] = $node->value; |
|
| 375 | + $node = $node->successor; |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Serialize the object |
|
| 382 | + * |
|
| 383 | + * @return array Array of values |
|
| 384 | + * |
|
| 385 | + * @since 1.0.0 |
|
| 386 | + */ |
|
| 387 | + public function jsonSerialize() |
|
| 388 | + { |
|
| 389 | + $array = array(); |
|
| 390 | + |
|
| 391 | + foreach ($this as $key => $value) { |
|
| 392 | + $array[$key] = $value; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + return array('TreeMap' => $array); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Set the value for a key |
|
| 400 | + * |
|
| 401 | + * @param mixed $key The key |
|
| 402 | + * @param mixed $value The value |
|
| 403 | + * |
|
| 404 | + * @return void |
|
| 405 | + * |
|
| 406 | + * @since 1.0.0 |
|
| 407 | + */ |
|
| 408 | + public function offsetSet($key, $value) |
|
| 409 | + { |
|
| 410 | + if ($this->root) { |
|
| 411 | + $this->root = $this->root->insert($key, $value, $this->comparator); |
|
| 412 | + } else { |
|
| 413 | + $this->root = TreeNode::create($key, $value); |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Unset the existence of a key |
|
| 419 | + * |
|
| 420 | + * @param mixed $key The key |
|
| 421 | + * |
|
| 422 | + * @return void |
|
| 423 | + * |
|
| 424 | + * @since 1.0.0 |
|
| 425 | + */ |
|
| 426 | + public function offsetUnset($key) |
|
| 427 | + { |
|
| 428 | + if ($this->root) { |
|
| 429 | + $this->root = $this->root->remove($key, $this->comparator); |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Count the number of key/value pairs |
|
| 435 | + * |
|
| 436 | + * @return integer |
|
| 437 | + * |
|
| 438 | + * @since 1.0.0 |
|
| 439 | + */ |
|
| 440 | + public function count() |
|
| 441 | + { |
|
| 442 | + if ($this->root) { |
|
| 443 | + return count($this->root); |
|
| 444 | + } else { |
|
| 445 | + return 0; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | 448 | } |
@@ -66,7 +66,7 @@ |
||
| 66 | 66 | protected function __construct($comparator = null) |
| 67 | 67 | { |
| 68 | 68 | if ($comparator == null) { |
| 69 | - $this->comparator = function ($key1, $key2) { |
|
| 69 | + $this->comparator = function($key1, $key2) { |
|
| 70 | 70 | return $key1 - $key2; |
| 71 | 71 | }; |
| 72 | 72 | } else { |
@@ -23,189 +23,189 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Iterator implements \Iterator |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Iterate on pairs |
|
| 28 | - * |
|
| 29 | - * @since 1.0.0 |
|
| 30 | - */ |
|
| 31 | - private const PAIRS = 0; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Iterate on keys |
|
| 35 | - * |
|
| 36 | - * @since 1.0.0 |
|
| 37 | - */ |
|
| 38 | - private const KEYS = 1; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Iterate on values |
|
| 42 | - * |
|
| 43 | - * @since 1.0.0 |
|
| 44 | - */ |
|
| 45 | - private const VALUES = 2; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var integer Type: self::PAIRS, self::KEYS or self::VALUES |
|
| 49 | - * |
|
| 50 | - * @since 1.0.0 |
|
| 51 | - */ |
|
| 52 | - private $type; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var integer Index |
|
| 56 | - * |
|
| 57 | - * @since 1.0.0 |
|
| 58 | - */ |
|
| 59 | - private $index; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var SortedMap Map |
|
| 63 | - * |
|
| 64 | - * @since 1.0.0 |
|
| 65 | - */ |
|
| 66 | - private $map; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Constructor |
|
| 70 | - * |
|
| 71 | - * @param SortedMap $map Sorted map |
|
| 72 | - * @param integer $type Iterator type |
|
| 73 | - * |
|
| 74 | - * @since 1.0.0 |
|
| 75 | - */ |
|
| 76 | - protected function __construct(SortedMap $map, $type) |
|
| 77 | - { |
|
| 78 | - $this->map = $map; |
|
| 79 | - $this->type = $type; |
|
| 80 | - $this->rewind(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Create a new iterator on pairs |
|
| 85 | - * |
|
| 86 | - * @param SortedMap $map Sorted map |
|
| 87 | - * |
|
| 88 | - * @return Iterator A new iterator on pairs |
|
| 89 | - * |
|
| 90 | - * @since 1.0.0 |
|
| 91 | - */ |
|
| 92 | - public static function create(SortedMap $map) |
|
| 93 | - { |
|
| 94 | - return new static($map, self::PAIRS); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Create a new iterator on keys |
|
| 99 | - * |
|
| 100 | - * @param SortedMap $map Sorted map |
|
| 101 | - * |
|
| 102 | - * @return Iterator A new iterator on keys |
|
| 103 | - * |
|
| 104 | - * @since 1.0.0 |
|
| 105 | - */ |
|
| 106 | - public static function keys(SortedMap $map) |
|
| 107 | - { |
|
| 108 | - return new static($map, self::KEYS); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Create a new iterator on values |
|
| 113 | - * |
|
| 114 | - * @param SortedMap $map Sorted map |
|
| 115 | - * |
|
| 116 | - * @return Iterator A new iterator on values |
|
| 117 | - * |
|
| 118 | - * @since 1.0.0 |
|
| 119 | - */ |
|
| 120 | - public static function values(SortedMap $map) |
|
| 121 | - { |
|
| 122 | - return new static($map, self::VALUES); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @var TreeNode The current node |
|
| 127 | - * |
|
| 128 | - * @since 1.0.0 |
|
| 129 | - */ |
|
| 130 | - protected $current; |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Rewind the Iterator to the first element |
|
| 134 | - * |
|
| 135 | - * @return void |
|
| 136 | - * |
|
| 137 | - * @since 1.0.0 |
|
| 138 | - */ |
|
| 139 | - public function rewind() |
|
| 140 | - { |
|
| 141 | - $this->index = 0; |
|
| 142 | - |
|
| 143 | - try { |
|
| 144 | - $this->current = $this->map->first(); |
|
| 145 | - } catch (\OutOfBoundsException $e) { |
|
| 146 | - $this->current = null; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Return the current key |
|
| 152 | - * |
|
| 153 | - * @return mixed The current key |
|
| 154 | - * |
|
| 155 | - * @since 1.0.0 |
|
| 156 | - */ |
|
| 157 | - public function key() |
|
| 158 | - { |
|
| 159 | - if ($this->type == self::PAIRS) { |
|
| 160 | - return $this->current->key; |
|
| 161 | - } else { |
|
| 162 | - return $this->index; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Return the current value |
|
| 168 | - * |
|
| 169 | - * @return mixed The current value |
|
| 170 | - * |
|
| 171 | - * @since 1.0.0 |
|
| 172 | - */ |
|
| 173 | - public function current() |
|
| 174 | - { |
|
| 175 | - if ($this->type == self::KEYS) { |
|
| 176 | - return $this->current->key; |
|
| 177 | - } else { |
|
| 178 | - return $this->current->value; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Move forward to the next element |
|
| 184 | - * |
|
| 185 | - * @return void |
|
| 186 | - * |
|
| 187 | - * @since 1.0.0 |
|
| 188 | - */ |
|
| 189 | - public function next() |
|
| 190 | - { |
|
| 191 | - try { |
|
| 192 | - $this->current = $this->map->successor($this->current); |
|
| 193 | - } catch (\OutOfBoundsException $e) { |
|
| 194 | - $this->current = null; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $this->index++; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Checks if current position is valid |
|
| 202 | - * |
|
| 203 | - * @return boolean |
|
| 204 | - * |
|
| 205 | - * @since 1.0.0 |
|
| 206 | - */ |
|
| 207 | - public function valid() |
|
| 208 | - { |
|
| 209 | - return (bool) $this->current; |
|
| 210 | - } |
|
| 26 | + /** |
|
| 27 | + * Iterate on pairs |
|
| 28 | + * |
|
| 29 | + * @since 1.0.0 |
|
| 30 | + */ |
|
| 31 | + private const PAIRS = 0; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Iterate on keys |
|
| 35 | + * |
|
| 36 | + * @since 1.0.0 |
|
| 37 | + */ |
|
| 38 | + private const KEYS = 1; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Iterate on values |
|
| 42 | + * |
|
| 43 | + * @since 1.0.0 |
|
| 44 | + */ |
|
| 45 | + private const VALUES = 2; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var integer Type: self::PAIRS, self::KEYS or self::VALUES |
|
| 49 | + * |
|
| 50 | + * @since 1.0.0 |
|
| 51 | + */ |
|
| 52 | + private $type; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var integer Index |
|
| 56 | + * |
|
| 57 | + * @since 1.0.0 |
|
| 58 | + */ |
|
| 59 | + private $index; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var SortedMap Map |
|
| 63 | + * |
|
| 64 | + * @since 1.0.0 |
|
| 65 | + */ |
|
| 66 | + private $map; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Constructor |
|
| 70 | + * |
|
| 71 | + * @param SortedMap $map Sorted map |
|
| 72 | + * @param integer $type Iterator type |
|
| 73 | + * |
|
| 74 | + * @since 1.0.0 |
|
| 75 | + */ |
|
| 76 | + protected function __construct(SortedMap $map, $type) |
|
| 77 | + { |
|
| 78 | + $this->map = $map; |
|
| 79 | + $this->type = $type; |
|
| 80 | + $this->rewind(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Create a new iterator on pairs |
|
| 85 | + * |
|
| 86 | + * @param SortedMap $map Sorted map |
|
| 87 | + * |
|
| 88 | + * @return Iterator A new iterator on pairs |
|
| 89 | + * |
|
| 90 | + * @since 1.0.0 |
|
| 91 | + */ |
|
| 92 | + public static function create(SortedMap $map) |
|
| 93 | + { |
|
| 94 | + return new static($map, self::PAIRS); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Create a new iterator on keys |
|
| 99 | + * |
|
| 100 | + * @param SortedMap $map Sorted map |
|
| 101 | + * |
|
| 102 | + * @return Iterator A new iterator on keys |
|
| 103 | + * |
|
| 104 | + * @since 1.0.0 |
|
| 105 | + */ |
|
| 106 | + public static function keys(SortedMap $map) |
|
| 107 | + { |
|
| 108 | + return new static($map, self::KEYS); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Create a new iterator on values |
|
| 113 | + * |
|
| 114 | + * @param SortedMap $map Sorted map |
|
| 115 | + * |
|
| 116 | + * @return Iterator A new iterator on values |
|
| 117 | + * |
|
| 118 | + * @since 1.0.0 |
|
| 119 | + */ |
|
| 120 | + public static function values(SortedMap $map) |
|
| 121 | + { |
|
| 122 | + return new static($map, self::VALUES); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @var TreeNode The current node |
|
| 127 | + * |
|
| 128 | + * @since 1.0.0 |
|
| 129 | + */ |
|
| 130 | + protected $current; |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Rewind the Iterator to the first element |
|
| 134 | + * |
|
| 135 | + * @return void |
|
| 136 | + * |
|
| 137 | + * @since 1.0.0 |
|
| 138 | + */ |
|
| 139 | + public function rewind() |
|
| 140 | + { |
|
| 141 | + $this->index = 0; |
|
| 142 | + |
|
| 143 | + try { |
|
| 144 | + $this->current = $this->map->first(); |
|
| 145 | + } catch (\OutOfBoundsException $e) { |
|
| 146 | + $this->current = null; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Return the current key |
|
| 152 | + * |
|
| 153 | + * @return mixed The current key |
|
| 154 | + * |
|
| 155 | + * @since 1.0.0 |
|
| 156 | + */ |
|
| 157 | + public function key() |
|
| 158 | + { |
|
| 159 | + if ($this->type == self::PAIRS) { |
|
| 160 | + return $this->current->key; |
|
| 161 | + } else { |
|
| 162 | + return $this->index; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Return the current value |
|
| 168 | + * |
|
| 169 | + * @return mixed The current value |
|
| 170 | + * |
|
| 171 | + * @since 1.0.0 |
|
| 172 | + */ |
|
| 173 | + public function current() |
|
| 174 | + { |
|
| 175 | + if ($this->type == self::KEYS) { |
|
| 176 | + return $this->current->key; |
|
| 177 | + } else { |
|
| 178 | + return $this->current->value; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Move forward to the next element |
|
| 184 | + * |
|
| 185 | + * @return void |
|
| 186 | + * |
|
| 187 | + * @since 1.0.0 |
|
| 188 | + */ |
|
| 189 | + public function next() |
|
| 190 | + { |
|
| 191 | + try { |
|
| 192 | + $this->current = $this->map->successor($this->current); |
|
| 193 | + } catch (\OutOfBoundsException $e) { |
|
| 194 | + $this->current = null; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $this->index++; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Checks if current position is valid |
|
| 202 | + * |
|
| 203 | + * @return boolean |
|
| 204 | + * |
|
| 205 | + * @since 1.0.0 |
|
| 206 | + */ |
|
| 207 | + public function valid() |
|
| 208 | + { |
|
| 209 | + return (bool) $this->current; |
|
| 210 | + } |
|
| 211 | 211 | } |
@@ -31,99 +31,99 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | interface SortedCollection extends \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * Get the comparator |
|
| 36 | - * |
|
| 37 | - * @return callable The comparator |
|
| 38 | - * |
|
| 39 | - * @since 1.0.0 |
|
| 40 | - */ |
|
| 41 | - public function comparator(); |
|
| 34 | + /** |
|
| 35 | + * Get the comparator |
|
| 36 | + * |
|
| 37 | + * @return callable The comparator |
|
| 38 | + * |
|
| 39 | + * @since 1.0.0 |
|
| 40 | + */ |
|
| 41 | + public function comparator(); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get the first element |
|
| 45 | - * |
|
| 46 | - * @return mixed The first element |
|
| 47 | - * |
|
| 48 | - * @throws \OutOfBoundsException If there is no element |
|
| 49 | - * |
|
| 50 | - * @since 1.0.0 |
|
| 51 | - */ |
|
| 52 | - public function first(); |
|
| 43 | + /** |
|
| 44 | + * Get the first element |
|
| 45 | + * |
|
| 46 | + * @return mixed The first element |
|
| 47 | + * |
|
| 48 | + * @throws \OutOfBoundsException If there is no element |
|
| 49 | + * |
|
| 50 | + * @since 1.0.0 |
|
| 51 | + */ |
|
| 52 | + public function first(); |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Get the last element |
|
| 56 | - * |
|
| 57 | - * @return mixed The last element |
|
| 58 | - * |
|
| 59 | - * @throws \OutOfBoundsException If there is no element |
|
| 60 | - * |
|
| 61 | - * @since 1.0.0 |
|
| 62 | - */ |
|
| 63 | - public function last(); |
|
| 54 | + /** |
|
| 55 | + * Get the last element |
|
| 56 | + * |
|
| 57 | + * @return mixed The last element |
|
| 58 | + * |
|
| 59 | + * @throws \OutOfBoundsException If there is no element |
|
| 60 | + * |
|
| 61 | + * @since 1.0.0 |
|
| 62 | + */ |
|
| 63 | + public function last(); |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Returns the greatest element lesser than the given key |
|
| 67 | - * |
|
| 68 | - * @param mixed $key The searched key |
|
| 69 | - * |
|
| 70 | - * @return mixed The found node |
|
| 71 | - * |
|
| 72 | - * @throws \OutOfBoundsException If there is no lower element |
|
| 73 | - * |
|
| 74 | - * @since 1.0.0 |
|
| 75 | - */ |
|
| 76 | - public function lower($key); |
|
| 65 | + /** |
|
| 66 | + * Returns the greatest element lesser than the given key |
|
| 67 | + * |
|
| 68 | + * @param mixed $key The searched key |
|
| 69 | + * |
|
| 70 | + * @return mixed The found node |
|
| 71 | + * |
|
| 72 | + * @throws \OutOfBoundsException If there is no lower element |
|
| 73 | + * |
|
| 74 | + * @since 1.0.0 |
|
| 75 | + */ |
|
| 76 | + public function lower($key); |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Returns the greatest element lesser than or equal to the given key |
|
| 80 | - * |
|
| 81 | - * @param mixed $key The searched key |
|
| 82 | - * |
|
| 83 | - * @return mixed The found node |
|
| 84 | - * |
|
| 85 | - * @throws \OutOfBoundsException If there is no floor element |
|
| 86 | - * |
|
| 87 | - * @since 1.0.0 |
|
| 88 | - */ |
|
| 89 | - public function floor($key); |
|
| 78 | + /** |
|
| 79 | + * Returns the greatest element lesser than or equal to the given key |
|
| 80 | + * |
|
| 81 | + * @param mixed $key The searched key |
|
| 82 | + * |
|
| 83 | + * @return mixed The found node |
|
| 84 | + * |
|
| 85 | + * @throws \OutOfBoundsException If there is no floor element |
|
| 86 | + * |
|
| 87 | + * @since 1.0.0 |
|
| 88 | + */ |
|
| 89 | + public function floor($key); |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Returns the element equal to the given key |
|
| 93 | - * |
|
| 94 | - * @param mixed $key The searched key |
|
| 95 | - * |
|
| 96 | - * @return mixed The found node |
|
| 97 | - * |
|
| 98 | - * @throws \OutOfBoundsException If there is no such element |
|
| 99 | - * |
|
| 100 | - * @since 1.0.0 |
|
| 101 | - */ |
|
| 102 | - public function find($key); |
|
| 91 | + /** |
|
| 92 | + * Returns the element equal to the given key |
|
| 93 | + * |
|
| 94 | + * @param mixed $key The searched key |
|
| 95 | + * |
|
| 96 | + * @return mixed The found node |
|
| 97 | + * |
|
| 98 | + * @throws \OutOfBoundsException If there is no such element |
|
| 99 | + * |
|
| 100 | + * @since 1.0.0 |
|
| 101 | + */ |
|
| 102 | + public function find($key); |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Returns the lowest element greater than or equal to the given key |
|
| 106 | - * |
|
| 107 | - * @param mixed $key The searched key |
|
| 108 | - * |
|
| 109 | - * @return mixed The found node |
|
| 110 | - * |
|
| 111 | - * @throws \OutOfBoundsException If there is no ceiling element |
|
| 112 | - * |
|
| 113 | - * @since 1.0.0 |
|
| 114 | - */ |
|
| 115 | - public function ceiling($key); |
|
| 104 | + /** |
|
| 105 | + * Returns the lowest element greater than or equal to the given key |
|
| 106 | + * |
|
| 107 | + * @param mixed $key The searched key |
|
| 108 | + * |
|
| 109 | + * @return mixed The found node |
|
| 110 | + * |
|
| 111 | + * @throws \OutOfBoundsException If there is no ceiling element |
|
| 112 | + * |
|
| 113 | + * @since 1.0.0 |
|
| 114 | + */ |
|
| 115 | + public function ceiling($key); |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Returns the lowest element greater than to the given key |
|
| 119 | - * |
|
| 120 | - * @param mixed $key The searched key |
|
| 121 | - * |
|
| 122 | - * @return mixed The found node |
|
| 123 | - * |
|
| 124 | - * @throws \OutOfBoundsException If there is no higher element |
|
| 125 | - * |
|
| 126 | - * @since 1.0.0 |
|
| 127 | - */ |
|
| 128 | - public function higher($key); |
|
| 117 | + /** |
|
| 118 | + * Returns the lowest element greater than to the given key |
|
| 119 | + * |
|
| 120 | + * @param mixed $key The searched key |
|
| 121 | + * |
|
| 122 | + * @return mixed The found node |
|
| 123 | + * |
|
| 124 | + * @throws \OutOfBoundsException If there is no higher element |
|
| 125 | + * |
|
| 126 | + * @since 1.0.0 |
|
| 127 | + */ |
|
| 128 | + public function higher($key); |
|
| 129 | 129 | } |
@@ -30,67 +30,67 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class ReversedSet extends AbstractSet |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * @var SortedSet Internal set |
|
| 35 | - * |
|
| 36 | - * @since 1.0.0 |
|
| 37 | - */ |
|
| 38 | - private $set; |
|
| 33 | + /** |
|
| 34 | + * @var SortedSet Internal set |
|
| 35 | + * |
|
| 36 | + * @since 1.0.0 |
|
| 37 | + */ |
|
| 38 | + private $set; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Constructor |
|
| 42 | - * |
|
| 43 | - * @param SortedSet $set Internal set |
|
| 44 | - * |
|
| 45 | - * @since 1.0.0 |
|
| 46 | - */ |
|
| 47 | - protected function __construct(SortedSet $set) |
|
| 48 | - { |
|
| 49 | - $this->setMap(ReversedMap::create($set->getMap()))->set = $set; |
|
| 50 | - } |
|
| 40 | + /** |
|
| 41 | + * Constructor |
|
| 42 | + * |
|
| 43 | + * @param SortedSet $set Internal set |
|
| 44 | + * |
|
| 45 | + * @since 1.0.0 |
|
| 46 | + */ |
|
| 47 | + protected function __construct(SortedSet $set) |
|
| 48 | + { |
|
| 49 | + $this->setMap(ReversedMap::create($set->getMap()))->set = $set; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Create |
|
| 54 | - * |
|
| 55 | - * @param SortedSet $set Internal set |
|
| 56 | - * |
|
| 57 | - * @return ReversedSet A new reversed set |
|
| 58 | - * |
|
| 59 | - * @since 1.0.0 |
|
| 60 | - */ |
|
| 61 | - public static function create(SortedSet $set) |
|
| 62 | - { |
|
| 63 | - return new static($set); |
|
| 64 | - } |
|
| 52 | + /** |
|
| 53 | + * Create |
|
| 54 | + * |
|
| 55 | + * @param SortedSet $set Internal set |
|
| 56 | + * |
|
| 57 | + * @return ReversedSet A new reversed set |
|
| 58 | + * |
|
| 59 | + * @since 1.0.0 |
|
| 60 | + */ |
|
| 61 | + public static function create(SortedSet $set) |
|
| 62 | + { |
|
| 63 | + return new static($set); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Magic get method |
|
| 68 | - * |
|
| 69 | - * @param string $property The property |
|
| 70 | - * |
|
| 71 | - * @return mixed The value associated to the property |
|
| 72 | - * |
|
| 73 | - * @since 1.0.0 |
|
| 74 | - */ |
|
| 75 | - public function __get($property) |
|
| 76 | - { |
|
| 77 | - switch ($property) { |
|
| 78 | - case 'set': |
|
| 79 | - return $this->set; |
|
| 80 | - default: |
|
| 81 | - return parent::__get($property); |
|
| 82 | - } |
|
| 83 | - } |
|
| 66 | + /** |
|
| 67 | + * Magic get method |
|
| 68 | + * |
|
| 69 | + * @param string $property The property |
|
| 70 | + * |
|
| 71 | + * @return mixed The value associated to the property |
|
| 72 | + * |
|
| 73 | + * @since 1.0.0 |
|
| 74 | + */ |
|
| 75 | + public function __get($property) |
|
| 76 | + { |
|
| 77 | + switch ($property) { |
|
| 78 | + case 'set': |
|
| 79 | + return $this->set; |
|
| 80 | + default: |
|
| 81 | + return parent::__get($property); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Serialize the object |
|
| 87 | - * |
|
| 88 | - * @return array Array of values |
|
| 89 | - * |
|
| 90 | - * @since 1.0.0 |
|
| 91 | - */ |
|
| 92 | - public function jsonSerialize() |
|
| 93 | - { |
|
| 94 | - return array('ReversedSet' => $this->set->jsonSerialize()); |
|
| 95 | - } |
|
| 85 | + /** |
|
| 86 | + * Serialize the object |
|
| 87 | + * |
|
| 88 | + * @return array Array of values |
|
| 89 | + * |
|
| 90 | + * @since 1.0.0 |
|
| 91 | + */ |
|
| 92 | + public function jsonSerialize() |
|
| 93 | + { |
|
| 94 | + return array('ReversedSet' => $this->set->jsonSerialize()); |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -25,184 +25,184 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class TreeMapBench |
| 27 | 27 | { |
| 28 | - /** |
|
| 29 | - * @var TreeMap The tree map |
|
| 30 | - * |
|
| 31 | - * @since 1.0.5 |
|
| 32 | - */ |
|
| 33 | - protected $tree; |
|
| 28 | + /** |
|
| 29 | + * @var TreeMap The tree map |
|
| 30 | + * |
|
| 31 | + * @since 1.0.5 |
|
| 32 | + */ |
|
| 33 | + protected $tree; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var SortedMap The sorted map |
|
| 37 | - * |
|
| 38 | - * @since 1.0.5 |
|
| 39 | - */ |
|
| 40 | - protected $data; |
|
| 35 | + /** |
|
| 36 | + * @var SortedMap The sorted map |
|
| 37 | + * |
|
| 38 | + * @since 1.0.5 |
|
| 39 | + */ |
|
| 40 | + protected $data; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Provider for counts |
|
| 44 | - * |
|
| 45 | - * @return iterator Iterator on count |
|
| 46 | - * |
|
| 47 | - * @since 1.0.5 |
|
| 48 | - */ |
|
| 49 | - public function provideCounts() |
|
| 50 | - { |
|
| 51 | - yield array('count' => 100); |
|
| 52 | - yield array('count' => 1000); |
|
| 53 | - yield array('count' => 10000); |
|
| 54 | - yield array('count' => 100000); |
|
| 55 | - } |
|
| 42 | + /** |
|
| 43 | + * Provider for counts |
|
| 44 | + * |
|
| 45 | + * @return iterator Iterator on count |
|
| 46 | + * |
|
| 47 | + * @since 1.0.5 |
|
| 48 | + */ |
|
| 49 | + public function provideCounts() |
|
| 50 | + { |
|
| 51 | + yield array('count' => 100); |
|
| 52 | + yield array('count' => 1000); |
|
| 53 | + yield array('count' => 10000); |
|
| 54 | + yield array('count' => 100000); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Provider for counts |
|
| 59 | - * |
|
| 60 | - * @return iterator Iterator on type |
|
| 61 | - * |
|
| 62 | - * @since 1.0.5 |
|
| 63 | - */ |
|
| 64 | - public function provideTypes() |
|
| 65 | - { |
|
| 66 | - yield array('type' => 'tree'); |
|
| 67 | - yield array('type' => 'reversed'); |
|
| 68 | - yield array('type' => 'sub', 'from' => 0.30, 'to' => 0.70); |
|
| 69 | - yield array('type' => 'sub', 'from' => 0.40, 'to' => 0.80); |
|
| 70 | - } |
|
| 57 | + /** |
|
| 58 | + * Provider for counts |
|
| 59 | + * |
|
| 60 | + * @return iterator Iterator on type |
|
| 61 | + * |
|
| 62 | + * @since 1.0.5 |
|
| 63 | + */ |
|
| 64 | + public function provideTypes() |
|
| 65 | + { |
|
| 66 | + yield array('type' => 'tree'); |
|
| 67 | + yield array('type' => 'reversed'); |
|
| 68 | + yield array('type' => 'sub', 'from' => 0.30, 'to' => 0.70); |
|
| 69 | + yield array('type' => 'sub', 'from' => 0.40, 'to' => 0.80); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Create the tree map. |
|
| 74 | - * |
|
| 75 | - * @param array $params Array of parameters |
|
| 76 | - * |
|
| 77 | - * @return void |
|
| 78 | - * |
|
| 79 | - * @since 1.0.5 |
|
| 80 | - */ |
|
| 81 | - public function init($params) |
|
| 82 | - { |
|
| 83 | - $this->tree = TreeMap::create(); |
|
| 84 | - } |
|
| 72 | + /** |
|
| 73 | + * Create the tree map. |
|
| 74 | + * |
|
| 75 | + * @param array $params Array of parameters |
|
| 76 | + * |
|
| 77 | + * @return void |
|
| 78 | + * |
|
| 79 | + * @since 1.0.5 |
|
| 80 | + */ |
|
| 81 | + public function init($params) |
|
| 82 | + { |
|
| 83 | + $this->tree = TreeMap::create(); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Create the sorted map. |
|
| 88 | - * |
|
| 89 | - * @param array $params Array of parameters |
|
| 90 | - * |
|
| 91 | - * @return void |
|
| 92 | - * |
|
| 93 | - * @since 1.0.5 |
|
| 94 | - */ |
|
| 95 | - public function data($params) |
|
| 96 | - { |
|
| 97 | - if (isset($params['type'])) { |
|
| 98 | - switch ($params['type']) { |
|
| 99 | - case 'tree': |
|
| 100 | - $this->data = $this->tree; |
|
| 101 | - break; |
|
| 102 | - case 'reversed': |
|
| 103 | - $this->data = ReversedMap::create($this->tree); |
|
| 104 | - break; |
|
| 105 | - case 'sub': |
|
| 106 | - if (isset($params['from']) && isset($params['to'])) { |
|
| 107 | - $this->data = SubMap::create( |
|
| 108 | - $this->tree, |
|
| 109 | - (int) ($params['from'] * $params['count']), |
|
| 110 | - (int) ($params['to'] * $params['count']) |
|
| 111 | - ); |
|
| 112 | - } else { |
|
| 113 | - $this->data = SubMap::create( |
|
| 114 | - $this->tree, |
|
| 115 | - null, |
|
| 116 | - null |
|
| 117 | - ); |
|
| 118 | - } |
|
| 119 | - break; |
|
| 120 | - } |
|
| 121 | - } else { |
|
| 122 | - $this->data = $this->tree; |
|
| 123 | - } |
|
| 124 | - } |
|
| 86 | + /** |
|
| 87 | + * Create the sorted map. |
|
| 88 | + * |
|
| 89 | + * @param array $params Array of parameters |
|
| 90 | + * |
|
| 91 | + * @return void |
|
| 92 | + * |
|
| 93 | + * @since 1.0.5 |
|
| 94 | + */ |
|
| 95 | + public function data($params) |
|
| 96 | + { |
|
| 97 | + if (isset($params['type'])) { |
|
| 98 | + switch ($params['type']) { |
|
| 99 | + case 'tree': |
|
| 100 | + $this->data = $this->tree; |
|
| 101 | + break; |
|
| 102 | + case 'reversed': |
|
| 103 | + $this->data = ReversedMap::create($this->tree); |
|
| 104 | + break; |
|
| 105 | + case 'sub': |
|
| 106 | + if (isset($params['from']) && isset($params['to'])) { |
|
| 107 | + $this->data = SubMap::create( |
|
| 108 | + $this->tree, |
|
| 109 | + (int) ($params['from'] * $params['count']), |
|
| 110 | + (int) ($params['to'] * $params['count']) |
|
| 111 | + ); |
|
| 112 | + } else { |
|
| 113 | + $this->data = SubMap::create( |
|
| 114 | + $this->tree, |
|
| 115 | + null, |
|
| 116 | + null |
|
| 117 | + ); |
|
| 118 | + } |
|
| 119 | + break; |
|
| 120 | + } |
|
| 121 | + } else { |
|
| 122 | + $this->data = $this->tree; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Clear the tree map. |
|
| 128 | - * |
|
| 129 | - * @param array $params Array of parameters |
|
| 130 | - * |
|
| 131 | - * @return void |
|
| 132 | - * |
|
| 133 | - * @since 1.0.5 |
|
| 134 | - */ |
|
| 135 | - public function finish($params) |
|
| 136 | - { |
|
| 137 | - $this->tree->clear(); |
|
| 138 | - } |
|
| 126 | + /** |
|
| 127 | + * Clear the tree map. |
|
| 128 | + * |
|
| 129 | + * @param array $params Array of parameters |
|
| 130 | + * |
|
| 131 | + * @return void |
|
| 132 | + * |
|
| 133 | + * @since 1.0.5 |
|
| 134 | + */ |
|
| 135 | + public function finish($params) |
|
| 136 | + { |
|
| 137 | + $this->tree->clear(); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * @BeforeMethods({"init", "data"}) |
|
| 142 | - * @AfterMethods({"finish"}) |
|
| 143 | - * @Revs(5) |
|
| 144 | - * @ParamProviders({"provideCounts"}) |
|
| 145 | - * |
|
| 146 | - * @param array $params Array of parameters |
|
| 147 | - * |
|
| 148 | - * @return void |
|
| 149 | - * |
|
| 150 | - * @since 1.0.5 |
|
| 151 | - */ |
|
| 152 | - public function benchFill($params) |
|
| 153 | - { |
|
| 154 | - for ($i = 0; $i < $params['count']; $i++) { |
|
| 155 | - $this->tree[$i] = $i; |
|
| 156 | - } |
|
| 157 | - } |
|
| 140 | + /** |
|
| 141 | + * @BeforeMethods({"init", "data"}) |
|
| 142 | + * @AfterMethods({"finish"}) |
|
| 143 | + * @Revs(5) |
|
| 144 | + * @ParamProviders({"provideCounts"}) |
|
| 145 | + * |
|
| 146 | + * @param array $params Array of parameters |
|
| 147 | + * |
|
| 148 | + * @return void |
|
| 149 | + * |
|
| 150 | + * @since 1.0.5 |
|
| 151 | + */ |
|
| 152 | + public function benchFill($params) |
|
| 153 | + { |
|
| 154 | + for ($i = 0; $i < $params['count']; $i++) { |
|
| 155 | + $this->tree[$i] = $i; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * @BeforeMethods({"init", "benchFill", "data"}) |
|
| 161 | - * @AfterMethods({"finish"}) |
|
| 162 | - * @Revs(5) |
|
| 163 | - * @ParamProviders({"provideCounts", "provideTypes"}) |
|
| 164 | - * |
|
| 165 | - * @param array $params Array of parameters |
|
| 166 | - * |
|
| 167 | - * @return void |
|
| 168 | - * |
|
| 169 | - * @since 1.0.5 |
|
| 170 | - */ |
|
| 171 | - public function benchSearch($params) |
|
| 172 | - { |
|
| 173 | - if (isset($params['from'])) { |
|
| 174 | - $min = (int) ($params['from'] * $params['count']); |
|
| 175 | - } else { |
|
| 176 | - $min = 0; |
|
| 177 | - } |
|
| 159 | + /** |
|
| 160 | + * @BeforeMethods({"init", "benchFill", "data"}) |
|
| 161 | + * @AfterMethods({"finish"}) |
|
| 162 | + * @Revs(5) |
|
| 163 | + * @ParamProviders({"provideCounts", "provideTypes"}) |
|
| 164 | + * |
|
| 165 | + * @param array $params Array of parameters |
|
| 166 | + * |
|
| 167 | + * @return void |
|
| 168 | + * |
|
| 169 | + * @since 1.0.5 |
|
| 170 | + */ |
|
| 171 | + public function benchSearch($params) |
|
| 172 | + { |
|
| 173 | + if (isset($params['from'])) { |
|
| 174 | + $min = (int) ($params['from'] * $params['count']); |
|
| 175 | + } else { |
|
| 176 | + $min = 0; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - if (isset($params['to'])) { |
|
| 180 | - $max = (int) ($params['to'] * $params['count']); |
|
| 181 | - } else { |
|
| 182 | - $max = $params['count']; |
|
| 183 | - } |
|
| 179 | + if (isset($params['to'])) { |
|
| 180 | + $max = (int) ($params['to'] * $params['count']); |
|
| 181 | + } else { |
|
| 182 | + $max = $params['count']; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - for ($i = $min; $i < $max; $i++) { |
|
| 186 | - $value = $this->data[$i]; |
|
| 187 | - } |
|
| 188 | - } |
|
| 185 | + for ($i = $min; $i < $max; $i++) { |
|
| 186 | + $value = $this->data[$i]; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * @BeforeMethods({"init", "benchFill", "data"}) |
|
| 192 | - * @AfterMethods({"finish"}) |
|
| 193 | - * @Revs(5) |
|
| 194 | - * @ParamProviders({"provideCounts"}) |
|
| 195 | - * |
|
| 196 | - * @param array $params Array of parameters |
|
| 197 | - * |
|
| 198 | - * @return void |
|
| 199 | - * |
|
| 200 | - * @since 1.0.5 |
|
| 201 | - */ |
|
| 202 | - public function benchClean($params) |
|
| 203 | - { |
|
| 204 | - for ($i = 0; $i < $params['count']; $i++) { |
|
| 205 | - unset($this->tree[$i]); |
|
| 206 | - } |
|
| 207 | - } |
|
| 190 | + /** |
|
| 191 | + * @BeforeMethods({"init", "benchFill", "data"}) |
|
| 192 | + * @AfterMethods({"finish"}) |
|
| 193 | + * @Revs(5) |
|
| 194 | + * @ParamProviders({"provideCounts"}) |
|
| 195 | + * |
|
| 196 | + * @param array $params Array of parameters |
|
| 197 | + * |
|
| 198 | + * @return void |
|
| 199 | + * |
|
| 200 | + * @since 1.0.5 |
|
| 201 | + */ |
|
| 202 | + public function benchClean($params) |
|
| 203 | + { |
|
| 204 | + for ($i = 0; $i < $params['count']; $i++) { |
|
| 205 | + unset($this->tree[$i]); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | 208 | } |
@@ -49,7 +49,7 @@ |
||
| 49 | 49 | |
| 50 | 50 | // Print 0-0;1-1;2-2;3-3;4-4;5-7;6-8;7-9; |
| 51 | 51 | foreach ($set as $key => $value) { |
| 52 | - echo $key . '-' . $value . ';'; |
|
| 52 | + echo $key . '-' . $value . ';'; |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | echo PHP_EOL; |
@@ -14,42 +14,42 @@ |
||
| 14 | 14 | * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections |
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | -require __DIR__ . '/../vendor/autoload.php'; |
|
| 17 | +require __DIR__.'/../vendor/autoload.php'; |
|
| 18 | 18 | |
| 19 | 19 | use chdemko\SortedCollection\TreeSet; |
| 20 | 20 | |
| 21 | 21 | $set = TreeSet::create()->put(array(1, 9, 5, 2, 6, 3, 0, 8, 7, 4)); |
| 22 | 22 | |
| 23 | 23 | // Print [0,1,2,3,4,5,6,7,8,9] |
| 24 | -echo $set . PHP_EOL; |
|
| 24 | +echo $set.PHP_EOL; |
|
| 25 | 25 | |
| 26 | 26 | // Print 0 |
| 27 | -echo $set->first . PHP_EOL; |
|
| 27 | +echo $set->first.PHP_EOL; |
|
| 28 | 28 | |
| 29 | 29 | // Print 9 |
| 30 | -echo $set->last . PHP_EOL; |
|
| 30 | +echo $set->last.PHP_EOL; |
|
| 31 | 31 | |
| 32 | 32 | // Print 10 |
| 33 | -echo count($set) . PHP_EOL; |
|
| 33 | +echo count($set).PHP_EOL; |
|
| 34 | 34 | |
| 35 | 35 | // Print 1 |
| 36 | -echo $set[5] . PHP_EOL; |
|
| 36 | +echo $set[5].PHP_EOL; |
|
| 37 | 37 | |
| 38 | 38 | // Change value for $set[5] |
| 39 | 39 | $set[5] = false; |
| 40 | 40 | |
| 41 | 41 | // Print [0,1,2,3,4,6,7,8,9] |
| 42 | -echo $set . PHP_EOL; |
|
| 42 | +echo $set.PHP_EOL; |
|
| 43 | 43 | |
| 44 | 44 | // Unset $set[6] |
| 45 | 45 | unset($set[6]); |
| 46 | 46 | |
| 47 | 47 | // Print [0,1,2,3,4,7,8,9] |
| 48 | -echo $set . PHP_EOL; |
|
| 48 | +echo $set.PHP_EOL; |
|
| 49 | 49 | |
| 50 | 50 | // Print 0-0;1-1;2-2;3-3;4-4;5-7;6-8;7-9; |
| 51 | 51 | foreach ($set as $key => $value) { |
| 52 | - echo $key . '-' . $value . ';'; |
|
| 52 | + echo $key.'-'.$value.';'; |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | echo PHP_EOL; |
@@ -49,7 +49,7 @@ |
||
| 49 | 49 | |
| 50 | 50 | // Print 0-0;1-1;2-2;3-3;4-4;6-6;7-7;8-8;9-9; |
| 51 | 51 | foreach ($tree as $key => $value) { |
| 52 | - echo $key . '-' . $value . ';'; |
|
| 52 | + echo $key . '-' . $value . ';'; |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | echo PHP_EOL; |
@@ -14,42 +14,42 @@ |
||
| 14 | 14 | * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections |
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | -require __DIR__ . '/../vendor/autoload.php'; |
|
| 17 | +require __DIR__.'/../vendor/autoload.php'; |
|
| 18 | 18 | |
| 19 | 19 | use chdemko\SortedCollection\TreeMap; |
| 20 | 20 | |
| 21 | 21 | $tree = TreeMap::create()->put(array(1 => 1, 9 => 9, 5 => 5, 2 => 2, 6 => 6, 3 => 3, 0 => 0, 8 => 8, 7 => 7, 4 => 4)); |
| 22 | 22 | |
| 23 | 23 | // Print [0,1,2,3,4,5,6,7,8,9] |
| 24 | -echo $tree . PHP_EOL; |
|
| 24 | +echo $tree.PHP_EOL; |
|
| 25 | 25 | |
| 26 | 26 | // Print 0 |
| 27 | -echo $tree->firstKey . PHP_EOL; |
|
| 27 | +echo $tree->firstKey.PHP_EOL; |
|
| 28 | 28 | |
| 29 | 29 | // Print 9 |
| 30 | -echo $tree->lastValue . PHP_EOL; |
|
| 30 | +echo $tree->lastValue.PHP_EOL; |
|
| 31 | 31 | |
| 32 | 32 | // Print 10 |
| 33 | -echo count($tree) . PHP_EOL; |
|
| 33 | +echo count($tree).PHP_EOL; |
|
| 34 | 34 | |
| 35 | 35 | // Print 5 |
| 36 | -echo $tree[5] . PHP_EOL; |
|
| 36 | +echo $tree[5].PHP_EOL; |
|
| 37 | 37 | |
| 38 | 38 | // Change value for $tree[5] |
| 39 | 39 | $tree[5] = 10; |
| 40 | 40 | |
| 41 | 41 | // Print [0,1,2,3,4,10,6,7,8,9] |
| 42 | -echo $tree . PHP_EOL; |
|
| 42 | +echo $tree.PHP_EOL; |
|
| 43 | 43 | |
| 44 | 44 | // Unset $tree[5] |
| 45 | 45 | unset($tree[5]); |
| 46 | 46 | |
| 47 | 47 | // Print {"0":0,"1":1,"2":2,"3":3,"4":4,"6":6,"7":7,"8":8,"9":9} |
| 48 | -echo $tree . PHP_EOL; |
|
| 48 | +echo $tree.PHP_EOL; |
|
| 49 | 49 | |
| 50 | 50 | // Print 0-0;1-1;2-2;3-3;4-4;6-6;7-7;8-8;9-9; |
| 51 | 51 | foreach ($tree as $key => $value) { |
| 52 | - echo $key . '-' . $value . ';'; |
|
| 52 | + echo $key.'-'.$value.';'; |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | echo PHP_EOL; |