@@ -14,7 +14,7 @@ discard block |
||
| 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 | use chdemko\SortedCollection\ReversedSet; |
@@ -25,12 +25,12 @@ discard block |
||
| 25 | 25 | $sub = SubSet::create($reversed, 7, 2); |
| 26 | 26 | |
| 27 | 27 | // Print [7,6,5,4,3] |
| 28 | -echo $sub . PHP_EOL; |
|
| 28 | +echo $sub.PHP_EOL; |
|
| 29 | 29 | |
| 30 | 30 | // Print [7,6,5,3] |
| 31 | 31 | unset($set[4]); |
| 32 | -echo $sub . PHP_EOL; |
|
| 32 | +echo $sub.PHP_EOL; |
|
| 33 | 33 | |
| 34 | 34 | // Print [9,8,7,6,5,3] |
| 35 | 35 | unset($sub->from); |
| 36 | -echo $sub . PHP_EOL; |
|
| 36 | +echo $sub.PHP_EOL; |
|
@@ -14,7 +14,7 @@ discard block |
||
| 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 | use chdemko\SortedCollection\ReversedSet; |
@@ -23,8 +23,8 @@ discard block |
||
| 23 | 23 | $reversed = ReversedSet::create($set); |
| 24 | 24 | |
| 25 | 25 | // Print [9,8,7,6,5,4,3,2,1,0] |
| 26 | -echo $reversed . PHP_EOL; |
|
| 26 | +echo $reversed.PHP_EOL; |
|
| 27 | 27 | |
| 28 | 28 | // Print [8,7,6,5,4,3,2,1,0] |
| 29 | 29 | unset($set[9]); |
| 30 | -echo $reversed . PHP_EOL; |
|
| 30 | +echo $reversed.PHP_EOL; |
|
@@ -14,7 +14,7 @@ discard block |
||
| 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 | use chdemko\SortedCollection\ReversedMap; |
@@ -23,8 +23,8 @@ discard block |
||
| 23 | 23 | $reversed = ReversedMap::create($tree); |
| 24 | 24 | |
| 25 | 25 | // Print {"9":9,"8":8,"7":7,"6":6,"5":5,"4":4,"3":3,"2":2,"1":1,"0":0} |
| 26 | -echo $reversed . PHP_EOL; |
|
| 26 | +echo $reversed.PHP_EOL; |
|
| 27 | 27 | |
| 28 | 28 | // Print {"8":8,"7":7,"6":6,"5":5,"4":4,"3":3,"2":2,"1":1,"0":0} |
| 29 | 29 | unset($tree[9]); |
| 30 | -echo $reversed . PHP_EOL; |
|
| 30 | +echo $reversed.PHP_EOL; |
|
@@ -14,7 +14,7 @@ discard block |
||
| 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 | use chdemko\SortedCollection\ReversedMap; |
@@ -25,12 +25,12 @@ discard block |
||
| 25 | 25 | $sub = SubMap::create($reversed, 7, 2); |
| 26 | 26 | |
| 27 | 27 | // Print {"7":7,"6":6,"5":5,"4":4,"3":3} |
| 28 | -echo $sub . PHP_EOL; |
|
| 28 | +echo $sub.PHP_EOL; |
|
| 29 | 29 | |
| 30 | 30 | // Print {"7":7,"6":6,"5":5,"3":3} |
| 31 | 31 | unset($tree[4]); |
| 32 | -echo $sub . PHP_EOL; |
|
| 32 | +echo $sub.PHP_EOL; |
|
| 33 | 33 | |
| 34 | 34 | // Print {"9":9,"8":8,"7":7,"6":6,"5":5,"3":3} |
| 35 | 35 | unset($sub->fromKey); |
| 36 | -echo $sub . PHP_EOL; |
|
| 36 | +echo $sub.PHP_EOL; |
|
@@ -29,141 +29,141 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class TreeSet extends AbstractSet |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * Constructor |
|
| 34 | - * |
|
| 35 | - * @param callable $comparator Comparison function |
|
| 36 | - * |
|
| 37 | - * @since 1.0.0 |
|
| 38 | - */ |
|
| 39 | - protected function __construct($comparator = null) |
|
| 40 | - { |
|
| 41 | - $this->setMap(TreeMap::create($comparator)); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Create |
|
| 46 | - * |
|
| 47 | - * @param callable $comparator Comparison function |
|
| 48 | - * |
|
| 49 | - * @return TreeSet A new TreeSet |
|
| 50 | - * |
|
| 51 | - * @since 1.0.0 |
|
| 52 | - */ |
|
| 53 | - public static function create($comparator = null) |
|
| 54 | - { |
|
| 55 | - return new static($comparator); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Put values in the set |
|
| 60 | - * |
|
| 61 | - * @param \Traversable $traversable Values to put in the set |
|
| 62 | - * |
|
| 63 | - * @return TreeSet $this for chaining |
|
| 64 | - * |
|
| 65 | - * @since 1.0.0 |
|
| 66 | - */ |
|
| 67 | - public function put($traversable = array()) |
|
| 68 | - { |
|
| 69 | - foreach ($traversable as $value) { |
|
| 70 | - $this[$value] = true; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Clear the set |
|
| 78 | - * |
|
| 79 | - * @return TreeSet $this for chaining |
|
| 80 | - * |
|
| 81 | - * @since 1.0.0 |
|
| 82 | - */ |
|
| 83 | - public function clear() |
|
| 84 | - { |
|
| 85 | - $this->getMap()->clear(); |
|
| 86 | - |
|
| 87 | - return $this; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Initialise the set |
|
| 92 | - * |
|
| 93 | - * @param \Traversable $traversable Values to initialise the set |
|
| 94 | - * |
|
| 95 | - * @return TreeSet $this for chaining |
|
| 96 | - * |
|
| 97 | - * @since 1.0.0 |
|
| 98 | - */ |
|
| 99 | - public function initialise($traversable = array()) |
|
| 100 | - { |
|
| 101 | - return $this->clear()->put($traversable); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Clone the set |
|
| 106 | - * |
|
| 107 | - * @return void |
|
| 108 | - * |
|
| 109 | - * @since 1.0.0 |
|
| 110 | - */ |
|
| 111 | - public function __clone() |
|
| 112 | - { |
|
| 113 | - $this->setMap(clone $this->getMap()); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Set the value for an element |
|
| 118 | - * |
|
| 119 | - * @param mixed $element The element |
|
| 120 | - * @param mixed $value The value |
|
| 121 | - * |
|
| 122 | - * @return void |
|
| 123 | - * |
|
| 124 | - * @since 1.0.0 |
|
| 125 | - */ |
|
| 126 | - public function offsetSet($element, $value) |
|
| 127 | - { |
|
| 128 | - $map = $this->getMap(); |
|
| 129 | - |
|
| 130 | - if ($value) { |
|
| 131 | - $map[$element] = true; |
|
| 132 | - } else { |
|
| 133 | - unset($map[$element]); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Serialize the object |
|
| 139 | - * |
|
| 140 | - * @return array Array of values |
|
| 141 | - * |
|
| 142 | - * @since 1.0.0 |
|
| 143 | - */ |
|
| 144 | - public function jsonSerialize() |
|
| 145 | - { |
|
| 146 | - $array = array(); |
|
| 147 | - |
|
| 148 | - foreach ($this as $value) { |
|
| 149 | - $array[] = $value; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return array('TreeSet' => $array); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Unset the existence of an element |
|
| 157 | - * |
|
| 158 | - * @param mixed $element The element |
|
| 159 | - * |
|
| 160 | - * @return void |
|
| 161 | - * |
|
| 162 | - * @since 1.0.0 |
|
| 163 | - */ |
|
| 164 | - public function offsetUnset($element) |
|
| 165 | - { |
|
| 166 | - $map = $this->getMap(); |
|
| 167 | - unset($map[$element]); |
|
| 168 | - } |
|
| 32 | + /** |
|
| 33 | + * Constructor |
|
| 34 | + * |
|
| 35 | + * @param callable $comparator Comparison function |
|
| 36 | + * |
|
| 37 | + * @since 1.0.0 |
|
| 38 | + */ |
|
| 39 | + protected function __construct($comparator = null) |
|
| 40 | + { |
|
| 41 | + $this->setMap(TreeMap::create($comparator)); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Create |
|
| 46 | + * |
|
| 47 | + * @param callable $comparator Comparison function |
|
| 48 | + * |
|
| 49 | + * @return TreeSet A new TreeSet |
|
| 50 | + * |
|
| 51 | + * @since 1.0.0 |
|
| 52 | + */ |
|
| 53 | + public static function create($comparator = null) |
|
| 54 | + { |
|
| 55 | + return new static($comparator); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Put values in the set |
|
| 60 | + * |
|
| 61 | + * @param \Traversable $traversable Values to put in the set |
|
| 62 | + * |
|
| 63 | + * @return TreeSet $this for chaining |
|
| 64 | + * |
|
| 65 | + * @since 1.0.0 |
|
| 66 | + */ |
|
| 67 | + public function put($traversable = array()) |
|
| 68 | + { |
|
| 69 | + foreach ($traversable as $value) { |
|
| 70 | + $this[$value] = true; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Clear the set |
|
| 78 | + * |
|
| 79 | + * @return TreeSet $this for chaining |
|
| 80 | + * |
|
| 81 | + * @since 1.0.0 |
|
| 82 | + */ |
|
| 83 | + public function clear() |
|
| 84 | + { |
|
| 85 | + $this->getMap()->clear(); |
|
| 86 | + |
|
| 87 | + return $this; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Initialise the set |
|
| 92 | + * |
|
| 93 | + * @param \Traversable $traversable Values to initialise the set |
|
| 94 | + * |
|
| 95 | + * @return TreeSet $this for chaining |
|
| 96 | + * |
|
| 97 | + * @since 1.0.0 |
|
| 98 | + */ |
|
| 99 | + public function initialise($traversable = array()) |
|
| 100 | + { |
|
| 101 | + return $this->clear()->put($traversable); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Clone the set |
|
| 106 | + * |
|
| 107 | + * @return void |
|
| 108 | + * |
|
| 109 | + * @since 1.0.0 |
|
| 110 | + */ |
|
| 111 | + public function __clone() |
|
| 112 | + { |
|
| 113 | + $this->setMap(clone $this->getMap()); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Set the value for an element |
|
| 118 | + * |
|
| 119 | + * @param mixed $element The element |
|
| 120 | + * @param mixed $value The value |
|
| 121 | + * |
|
| 122 | + * @return void |
|
| 123 | + * |
|
| 124 | + * @since 1.0.0 |
|
| 125 | + */ |
|
| 126 | + public function offsetSet($element, $value) |
|
| 127 | + { |
|
| 128 | + $map = $this->getMap(); |
|
| 129 | + |
|
| 130 | + if ($value) { |
|
| 131 | + $map[$element] = true; |
|
| 132 | + } else { |
|
| 133 | + unset($map[$element]); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Serialize the object |
|
| 139 | + * |
|
| 140 | + * @return array Array of values |
|
| 141 | + * |
|
| 142 | + * @since 1.0.0 |
|
| 143 | + */ |
|
| 144 | + public function jsonSerialize() |
|
| 145 | + { |
|
| 146 | + $array = array(); |
|
| 147 | + |
|
| 148 | + foreach ($this as $value) { |
|
| 149 | + $array[] = $value; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return array('TreeSet' => $array); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Unset the existence of an element |
|
| 157 | + * |
|
| 158 | + * @param mixed $element The element |
|
| 159 | + * |
|
| 160 | + * @return void |
|
| 161 | + * |
|
| 162 | + * @since 1.0.0 |
|
| 163 | + */ |
|
| 164 | + public function offsetUnset($element) |
|
| 165 | + { |
|
| 166 | + $map = $this->getMap(); |
|
| 167 | + unset($map[$element]); |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -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 | |
@@ -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 | } |
@@ -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 { |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |