Completed
Branch develop (5889c9)
by Christophe
21s
created
examples/SubSet.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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;
Please login to merge, or discard this patch.
examples/ReversedSet.php 1 patch
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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;
Please login to merge, or discard this patch.
examples/ReversedMap.php 1 patch
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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;
Please login to merge, or discard this patch.
examples/SubMap.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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;
Please login to merge, or discard this patch.
src/SortedCollection/SubSet.php 1 patch
Indentation   +276 added lines, -276 removed lines patch added patch discarded remove patch
@@ -34,294 +34,294 @@
 block discarded – undo
34 34
  */
35 35
 class SubSet extends AbstractSet
36 36
 {
37
-    /**
38
-     * When the from or to value is unused
39
-     *
40
-     * @since 1.0.0
41
-     */
42
-    private const UNUSED = 0;
37
+	/**
38
+	 * When the from or to value is unused
39
+	 *
40
+	 * @since 1.0.0
41
+	 */
42
+	private const UNUSED = 0;
43 43
 
44
-    /**
45
-     * When the from or to value is inclusive
46
-     *
47
-     * @since 1.0.0
48
-     */
49
-    private const INCLUSIVE = 1;
44
+	/**
45
+	 * When the from or to value is inclusive
46
+	 *
47
+	 * @since 1.0.0
48
+	 */
49
+	private const INCLUSIVE = 1;
50 50
 
51
-    /**
52
-     * When the from or to value is exclusive
53
-     *
54
-     * @since 1.0.0
55
-     */
56
-    private const EXCLUSIVE = 2;
51
+	/**
52
+	 * When the from or to value is exclusive
53
+	 *
54
+	 * @since 1.0.0
55
+	 */
56
+	private const EXCLUSIVE = 2;
57 57
 
58
-    /**
59
-     * @var SortedSet  Internal set
60
-     *
61
-     * @since 1.0.0
62
-     */
63
-    private $set;
58
+	/**
59
+	 * @var SortedSet  Internal set
60
+	 *
61
+	 * @since 1.0.0
62
+	 */
63
+	private $set;
64 64
 
65
-    /**
66
-     * Magic get method
67
-     *
68
-     * @param string $property The property
69
-     *
70
-     * @return mixed The value associated to the property
71
-     *
72
-     * @since 1.0.0
73
-     */
74
-    public function __get($property)
75
-    {
76
-        switch ($property) {
77
-            case 'from':
78
-                return $this->getMap()->fromKey;
79
-            case 'to':
80
-                return $this->getMap()->toKey;
81
-            case 'fromInclusive':
82
-                return $this->getMap()->fromInclusive;
83
-            case 'toInclusive':
84
-                return $this->getMap()->toInclusive;
85
-            case 'set':
86
-                return $this->set;
87
-            default:
88
-                return parent::__get($property);
89
-        }
90
-    }
65
+	/**
66
+	 * Magic get method
67
+	 *
68
+	 * @param string $property The property
69
+	 *
70
+	 * @return mixed The value associated to the property
71
+	 *
72
+	 * @since 1.0.0
73
+	 */
74
+	public function __get($property)
75
+	{
76
+		switch ($property) {
77
+			case 'from':
78
+				return $this->getMap()->fromKey;
79
+			case 'to':
80
+				return $this->getMap()->toKey;
81
+			case 'fromInclusive':
82
+				return $this->getMap()->fromInclusive;
83
+			case 'toInclusive':
84
+				return $this->getMap()->toInclusive;
85
+			case 'set':
86
+				return $this->set;
87
+			default:
88
+				return parent::__get($property);
89
+		}
90
+	}
91 91
 
92
-    /**
93
-     * Magic set method
94
-     *
95
-     * @param string $property The property
96
-     * @param mixed  $value    The new value
97
-     *
98
-     * @throws \RuntimeException If the property does not exist
99
-     *
100
-     * @return void
101
-     *
102
-     * @since 1.0.0
103
-     */
104
-    public function __set($property, $value)
105
-    {
106
-        switch ($property) {
107
-            case 'from':
108
-                $this->getMap()->fromKey = $value;
109
-                break;
110
-            case 'to':
111
-                $this->getMap()->toKey = $value;
112
-                break;
113
-            case 'fromInclusive':
114
-                $this->getMap()->fromInclusive = $value;
115
-                break;
116
-            case 'toInclusive':
117
-                $this->getMap()->toInclusive = $value;
118
-                break;
119
-            default:
120
-                throw new \RuntimeException('Undefined property');
121
-        }
122
-    }
92
+	/**
93
+	 * Magic set method
94
+	 *
95
+	 * @param string $property The property
96
+	 * @param mixed  $value    The new value
97
+	 *
98
+	 * @throws \RuntimeException If the property does not exist
99
+	 *
100
+	 * @return void
101
+	 *
102
+	 * @since 1.0.0
103
+	 */
104
+	public function __set($property, $value)
105
+	{
106
+		switch ($property) {
107
+			case 'from':
108
+				$this->getMap()->fromKey = $value;
109
+				break;
110
+			case 'to':
111
+				$this->getMap()->toKey = $value;
112
+				break;
113
+			case 'fromInclusive':
114
+				$this->getMap()->fromInclusive = $value;
115
+				break;
116
+			case 'toInclusive':
117
+				$this->getMap()->toInclusive = $value;
118
+				break;
119
+			default:
120
+				throw new \RuntimeException('Undefined property');
121
+		}
122
+	}
123 123
 
124
-    /**
125
-     * Magic unset method
126
-     *
127
-     * @param string $property The property
128
-     *
129
-     * @throws \RuntimeException If the property does not exist
130
-     *
131
-     * @return void
132
-     *
133
-     * @since 1.0.0
134
-     */
135
-    public function __unset($property)
136
-    {
137
-        switch ($property) {
138
-            case 'from':
139
-                unset($this->getMap()->fromKey);
140
-                break;
141
-            case 'to':
142
-                unset($this->getMap()->toKey);
143
-                break;
144
-            case 'fromInclusive':
145
-                unset($this->getMap()->fromInclusive);
146
-                break;
147
-            case 'toInclusive':
148
-                unset($this->getMap()->toInclusive);
149
-                break;
150
-            default:
151
-                throw new \RuntimeException('Undefined property');
152
-        }
153
-    }
124
+	/**
125
+	 * Magic unset method
126
+	 *
127
+	 * @param string $property The property
128
+	 *
129
+	 * @throws \RuntimeException If the property does not exist
130
+	 *
131
+	 * @return void
132
+	 *
133
+	 * @since 1.0.0
134
+	 */
135
+	public function __unset($property)
136
+	{
137
+		switch ($property) {
138
+			case 'from':
139
+				unset($this->getMap()->fromKey);
140
+				break;
141
+			case 'to':
142
+				unset($this->getMap()->toKey);
143
+				break;
144
+			case 'fromInclusive':
145
+				unset($this->getMap()->fromInclusive);
146
+				break;
147
+			case 'toInclusive':
148
+				unset($this->getMap()->toInclusive);
149
+				break;
150
+			default:
151
+				throw new \RuntimeException('Undefined property');
152
+		}
153
+	}
154 154
 
155
-    /**
156
-     * Magic isset method
157
-     *
158
-     * @param string $property The property
159
-     *
160
-     * @return boolean
161
-     *
162
-     * @since 1.0.0
163
-     */
164
-    public function __isset($property)
165
-    {
166
-        switch ($property) {
167
-            case 'from':
168
-                return isset($this->getMap()->fromKey);
169
-            case 'to':
170
-                return isset($this->getMap()->toKey);
171
-            case 'fromInclusive':
172
-                return isset($this->getMap()->fromInclusive);
173
-            case 'toInclusive':
174
-                return isset($this->getMap()->toInclusive);
175
-            default:
176
-                return false;
177
-        }
178
-    }
155
+	/**
156
+	 * Magic isset method
157
+	 *
158
+	 * @param string $property The property
159
+	 *
160
+	 * @return boolean
161
+	 *
162
+	 * @since 1.0.0
163
+	 */
164
+	public function __isset($property)
165
+	{
166
+		switch ($property) {
167
+			case 'from':
168
+				return isset($this->getMap()->fromKey);
169
+			case 'to':
170
+				return isset($this->getMap()->toKey);
171
+			case 'fromInclusive':
172
+				return isset($this->getMap()->fromInclusive);
173
+			case 'toInclusive':
174
+				return isset($this->getMap()->toInclusive);
175
+			default:
176
+				return false;
177
+		}
178
+	}
179 179
 
180
-    /**
181
-     * Constructor
182
-     *
183
-     * @param SortedSet $set        Internal set
184
-     * @param mixed     $from       The from element
185
-     * @param integer   $fromOption The option for from (SubSet::UNUSED, SubSet::INCLUSIVE or SubSet::EXCLUSIVE)
186
-     * @param mixed     $to         The to element
187
-     * @param integer   $toOption   The option for to (SubSet::UNUSED, SubSet::INCLUSIVE or SubSet::EXCLUSIVE)
188
-     *
189
-     * @since 1.0.0
190
-     */
191
-    protected function __construct(SortedSet $set, $from, $fromOption, $to, $toOption)
192
-    {
193
-        if ($fromOption == self::UNUSED) {
194
-            if ($toOption == self::UNUSED) {
195
-                $this->setMap(SubMap::view($set->getMap()));
196
-            } else {
197
-                $this->setMap(SubMap::head($set->getMap(), $to, $toOption == self::INCLUSIVE));
198
-            }
199
-        } elseif ($toOption == self::UNUSED) {
200
-            $this->setMap(SubMap::tail($set->getMap(), $from, $fromOption == self::INCLUSIVE));
201
-        } else {
202
-            $this->setMap(
203
-                SubMap::create($set->getMap(), $from, $to, $fromOption == self::INCLUSIVE, $toOption == self::INCLUSIVE)
204
-            );
205
-        }
180
+	/**
181
+	 * Constructor
182
+	 *
183
+	 * @param SortedSet $set        Internal set
184
+	 * @param mixed     $from       The from element
185
+	 * @param integer   $fromOption The option for from (SubSet::UNUSED, SubSet::INCLUSIVE or SubSet::EXCLUSIVE)
186
+	 * @param mixed     $to         The to element
187
+	 * @param integer   $toOption   The option for to (SubSet::UNUSED, SubSet::INCLUSIVE or SubSet::EXCLUSIVE)
188
+	 *
189
+	 * @since 1.0.0
190
+	 */
191
+	protected function __construct(SortedSet $set, $from, $fromOption, $to, $toOption)
192
+	{
193
+		if ($fromOption == self::UNUSED) {
194
+			if ($toOption == self::UNUSED) {
195
+				$this->setMap(SubMap::view($set->getMap()));
196
+			} else {
197
+				$this->setMap(SubMap::head($set->getMap(), $to, $toOption == self::INCLUSIVE));
198
+			}
199
+		} elseif ($toOption == self::UNUSED) {
200
+			$this->setMap(SubMap::tail($set->getMap(), $from, $fromOption == self::INCLUSIVE));
201
+		} else {
202
+			$this->setMap(
203
+				SubMap::create($set->getMap(), $from, $to, $fromOption == self::INCLUSIVE, $toOption == self::INCLUSIVE)
204
+			);
205
+		}
206 206
 
207
-        $this->set = $set;
208
-    }
207
+		$this->set = $set;
208
+	}
209 209
 
210
-    /**
211
-     * Create
212
-     *
213
-     * @param SortedSet $set           Internal set
214
-     * @param mixed     $from          The from element
215
-     * @param mixed     $to            The to element
216
-     * @param boolean   $fromInclusive The inclusive flag for from
217
-     * @param boolean   $toInclusive   The inclusive flag for to
218
-     *
219
-     * @return SubSet A new sub set
220
-     *
221
-     * @since 1.0.0
222
-     */
223
-    public static function create(SortedSet $set, $from, $to, $fromInclusive = true, $toInclusive = false)
224
-    {
225
-        return new static(
226
-            $set,
227
-            $from,
228
-            $fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE,
229
-            $to,
230
-            $toInclusive ? self::INCLUSIVE : self::EXCLUSIVE
231
-        );
232
-    }
210
+	/**
211
+	 * Create
212
+	 *
213
+	 * @param SortedSet $set           Internal set
214
+	 * @param mixed     $from          The from element
215
+	 * @param mixed     $to            The to element
216
+	 * @param boolean   $fromInclusive The inclusive flag for from
217
+	 * @param boolean   $toInclusive   The inclusive flag for to
218
+	 *
219
+	 * @return SubSet A new sub set
220
+	 *
221
+	 * @since 1.0.0
222
+	 */
223
+	public static function create(SortedSet $set, $from, $to, $fromInclusive = true, $toInclusive = false)
224
+	{
225
+		return new static(
226
+			$set,
227
+			$from,
228
+			$fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE,
229
+			$to,
230
+			$toInclusive ? self::INCLUSIVE : self::EXCLUSIVE
231
+		);
232
+	}
233 233
 
234
-    /**
235
-     * Head
236
-     *
237
-     * @param SortedSet $set         Internal set
238
-     * @param mixed     $to          The to element
239
-     * @param boolean   $toInclusive The inclusive flag for to
240
-     *
241
-     * @return SubSet A new head set
242
-     *
243
-     * @since 1.0.0
244
-     */
245
-    public static function head(SortedSet $set, $to, $toInclusive = false)
246
-    {
247
-        return new static($set, null, self::UNUSED, $to, $toInclusive ? self::INCLUSIVE : self::EXCLUSIVE);
248
-    }
234
+	/**
235
+	 * Head
236
+	 *
237
+	 * @param SortedSet $set         Internal set
238
+	 * @param mixed     $to          The to element
239
+	 * @param boolean   $toInclusive The inclusive flag for to
240
+	 *
241
+	 * @return SubSet A new head set
242
+	 *
243
+	 * @since 1.0.0
244
+	 */
245
+	public static function head(SortedSet $set, $to, $toInclusive = false)
246
+	{
247
+		return new static($set, null, self::UNUSED, $to, $toInclusive ? self::INCLUSIVE : self::EXCLUSIVE);
248
+	}
249 249
 
250
-    /**
251
-     * Tail
252
-     *
253
-     * @param SortedSet $set           Internal set
254
-     * @param mixed     $from          The from element
255
-     * @param boolean   $fromInclusive The inclusive flag for from
256
-     *
257
-     * @return SubSet A new tail set
258
-     *
259
-     * @since 1.0.0
260
-     */
261
-    public static function tail(SortedSet $set, $from, $fromInclusive = true)
262
-    {
263
-        return new static($set, $from, $fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE, null, self::UNUSED);
264
-    }
250
+	/**
251
+	 * Tail
252
+	 *
253
+	 * @param SortedSet $set           Internal set
254
+	 * @param mixed     $from          The from element
255
+	 * @param boolean   $fromInclusive The inclusive flag for from
256
+	 *
257
+	 * @return SubSet A new tail set
258
+	 *
259
+	 * @since 1.0.0
260
+	 */
261
+	public static function tail(SortedSet $set, $from, $fromInclusive = true)
262
+	{
263
+		return new static($set, $from, $fromInclusive ? self::INCLUSIVE : self::EXCLUSIVE, null, self::UNUSED);
264
+	}
265 265
 
266
-    /**
267
-     * View
268
-     *
269
-     * @param SortedSet $set Internal set
270
-     *
271
-     * @return SubSet A new sub set
272
-     *
273
-     * @since 1.0.0
274
-     */
275
-    public static function view(SortedSet $set)
276
-    {
277
-        return new static($set, null, self::UNUSED, null, self::UNUSED);
278
-    }
266
+	/**
267
+	 * View
268
+	 *
269
+	 * @param SortedSet $set Internal set
270
+	 *
271
+	 * @return SubSet A new sub set
272
+	 *
273
+	 * @since 1.0.0
274
+	 */
275
+	public static function view(SortedSet $set)
276
+	{
277
+		return new static($set, null, self::UNUSED, null, self::UNUSED);
278
+	}
279 279
 
280
-    /**
281
-     * Serialize the object
282
-     *
283
-     * @return array Array of values
284
-     *
285
-     * @since 1.0.0
286
-     */
287
-    public function jsonSerialize()
288
-    {
289
-        if (isset($this->from)) {
290
-            if (isset($this->to)) {
291
-                return array(
292
-                 'SubSet' => array(
293
-                  'set' => $this->set->jsonSerialize(),
294
-                  'from' => $this->from,
295
-                  'fromInclusive' => $this->fromInclusive,
296
-                  'to' => $this->to,
297
-                  'toInclusive' => $this->toInclusive,
298
-                 )
299
-                );
300
-            } else {
301
-                return array(
302
-                'TailSet' => array(
303
-                'set' => $this->set->jsonSerialize(),
304
-                'from' => $this->from,
305
-                'fromInclusive' => $this->fromInclusive,
306
-                )
307
-                );
308
-            }
309
-        } else {
310
-            if (isset($this->to)) {
311
-                return array(
312
-                'HeadSet' => array(
313
-                'set' => $this->set->jsonSerialize(),
314
-                'to' => $this->to,
315
-                'toInclusive' => $this->toInclusive,
316
-                )
317
-                );
318
-            } else {
319
-                return array(
320
-                'ViewSet' => array(
321
-                'set' => $this->set->jsonSerialize(),
322
-                )
323
-                );
324
-            }
325
-        }
326
-    }
280
+	/**
281
+	 * Serialize the object
282
+	 *
283
+	 * @return array Array of values
284
+	 *
285
+	 * @since 1.0.0
286
+	 */
287
+	public function jsonSerialize()
288
+	{
289
+		if (isset($this->from)) {
290
+			if (isset($this->to)) {
291
+				return array(
292
+				 'SubSet' => array(
293
+				  'set' => $this->set->jsonSerialize(),
294
+				  'from' => $this->from,
295
+				  'fromInclusive' => $this->fromInclusive,
296
+				  'to' => $this->to,
297
+				  'toInclusive' => $this->toInclusive,
298
+				 )
299
+				);
300
+			} else {
301
+				return array(
302
+				'TailSet' => array(
303
+				'set' => $this->set->jsonSerialize(),
304
+				'from' => $this->from,
305
+				'fromInclusive' => $this->fromInclusive,
306
+				)
307
+				);
308
+			}
309
+		} else {
310
+			if (isset($this->to)) {
311
+				return array(
312
+				'HeadSet' => array(
313
+				'set' => $this->set->jsonSerialize(),
314
+				'to' => $this->to,
315
+				'toInclusive' => $this->toInclusive,
316
+				)
317
+				);
318
+			} else {
319
+				return array(
320
+				'ViewSet' => array(
321
+				'set' => $this->set->jsonSerialize(),
322
+				)
323
+				);
324
+			}
325
+		}
326
+	}
327 327
 }
Please login to merge, or discard this patch.
src/SortedCollection/TreeSet.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -29,141 +29,141 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/SortedCollection/AbstractMap.php 1 patch
Indentation   +366 added lines, -366 removed lines patch added patch discarded remove patch
@@ -35,395 +35,395 @@
 block discarded – undo
35 35
  */
36 36
 abstract class AbstractMap implements SortedMap
37 37
 {
38
-    /**
39
-     * Magic get method
40
-     *
41
-     * @param string $property The property
42
-     *
43
-     * @throws \RuntimeException If the property does not exist
44
-     *
45
-     * @return mixed The value associated to the property
46
-     *
47
-     * @since 1.0.0
48
-     */
49
-    public function __get($property)
50
-    {
51
-        switch ($property) {
52
-            case 'comparator':
53
-                return $this->comparator();
54
-            case 'firstKey':
55
-                return $this->firstKey();
56
-            case 'lastKey':
57
-                return $this->lastKey();
58
-            case 'firstValue':
59
-                return $this->firstValue();
60
-            case 'lastValue':
61
-                return $this->lastValue();
62
-            case 'first':
63
-                return $this->first();
64
-            case 'last':
65
-                return $this->last();
66
-            case 'keys':
67
-                return $this->keys();
68
-            case 'values':
69
-                return $this->values();
70
-            case 'count':
71
-                return $this->count();
72
-            default:
73
-                throw new \RuntimeException('Undefined property');
74
-        }
75
-    }
38
+	/**
39
+	 * Magic get method
40
+	 *
41
+	 * @param string $property The property
42
+	 *
43
+	 * @throws \RuntimeException If the property does not exist
44
+	 *
45
+	 * @return mixed The value associated to the property
46
+	 *
47
+	 * @since 1.0.0
48
+	 */
49
+	public function __get($property)
50
+	{
51
+		switch ($property) {
52
+			case 'comparator':
53
+				return $this->comparator();
54
+			case 'firstKey':
55
+				return $this->firstKey();
56
+			case 'lastKey':
57
+				return $this->lastKey();
58
+			case 'firstValue':
59
+				return $this->firstValue();
60
+			case 'lastValue':
61
+				return $this->lastValue();
62
+			case 'first':
63
+				return $this->first();
64
+			case 'last':
65
+				return $this->last();
66
+			case 'keys':
67
+				return $this->keys();
68
+			case 'values':
69
+				return $this->values();
70
+			case 'count':
71
+				return $this->count();
72
+			default:
73
+				throw new \RuntimeException('Undefined property');
74
+		}
75
+	}
76 76
 
77
-    /**
78
-     * Get the first key
79
-     *
80
-     * @return mixed The first key
81
-     *
82
-     * @throws \OutOfBoundsException If there is no element
83
-     *
84
-     * @since 1.0.0
85
-     */
86
-    public function firstKey()
87
-    {
88
-        return $this->first()->key;
89
-    }
77
+	/**
78
+	 * Get the first key
79
+	 *
80
+	 * @return mixed The first key
81
+	 *
82
+	 * @throws \OutOfBoundsException If there is no element
83
+	 *
84
+	 * @since 1.0.0
85
+	 */
86
+	public function firstKey()
87
+	{
88
+		return $this->first()->key;
89
+	}
90 90
 
91
-    /**
92
-     * Get the first value
93
-     *
94
-     * @return mixed The first value
95
-     *
96
-     * @throws \OutOfBoundsException If there is no element
97
-     *
98
-     * @since 1.0.0
99
-     */
100
-    public function firstValue()
101
-    {
102
-        return $this->first()->value;
103
-    }
91
+	/**
92
+	 * Get the first value
93
+	 *
94
+	 * @return mixed The first value
95
+	 *
96
+	 * @throws \OutOfBoundsException If there is no element
97
+	 *
98
+	 * @since 1.0.0
99
+	 */
100
+	public function firstValue()
101
+	{
102
+		return $this->first()->value;
103
+	}
104 104
 
105
-    /**
106
-     * Get the last key
107
-     *
108
-     * @return mixed The last key
109
-     *
110
-     * @throws \OutOfBoundsException If there is no element
111
-     *
112
-     * @since 1.0.0
113
-     */
114
-    public function lastKey()
115
-    {
116
-        return $this->last()->key;
117
-    }
105
+	/**
106
+	 * Get the last key
107
+	 *
108
+	 * @return mixed The last key
109
+	 *
110
+	 * @throws \OutOfBoundsException If there is no element
111
+	 *
112
+	 * @since 1.0.0
113
+	 */
114
+	public function lastKey()
115
+	{
116
+		return $this->last()->key;
117
+	}
118 118
 
119
-    /**
120
-     * Get the last value
121
-     *
122
-     * @return mixed The last value
123
-     *
124
-     * @throws \OutOfBoundsException If there is no element
125
-     *
126
-     * @since 1.0.0
127
-     */
128
-    public function lastValue()
129
-    {
130
-        return $this->last()->value;
131
-    }
119
+	/**
120
+	 * Get the last value
121
+	 *
122
+	 * @return mixed The last value
123
+	 *
124
+	 * @throws \OutOfBoundsException If there is no element
125
+	 *
126
+	 * @since 1.0.0
127
+	 */
128
+	public function lastValue()
129
+	{
130
+		return $this->last()->value;
131
+	}
132 132
 
133
-    /**
134
-     * Returns the greatest key lesser than the given key
135
-     *
136
-     * @param mixed $key The searched key
137
-     *
138
-     * @return mixed The found key
139
-     *
140
-     * @throws \OutOfBoundsException If there is no lower element
141
-     *
142
-     * @since 1.0.0
143
-     */
144
-    public function lowerKey($key)
145
-    {
146
-        return $this->lower($key)->key;
147
-    }
133
+	/**
134
+	 * Returns the greatest key lesser than the given key
135
+	 *
136
+	 * @param mixed $key The searched key
137
+	 *
138
+	 * @return mixed The found key
139
+	 *
140
+	 * @throws \OutOfBoundsException If there is no lower element
141
+	 *
142
+	 * @since 1.0.0
143
+	 */
144
+	public function lowerKey($key)
145
+	{
146
+		return $this->lower($key)->key;
147
+	}
148 148
 
149
-    /**
150
-     * Returns the value whose key is the greatest key lesser than the given key
151
-     *
152
-     * @param mixed $key The searched key
153
-     *
154
-     * @return mixed The found value
155
-     *
156
-     * @throws \OutOfBoundsException If there is no lower element
157
-     *
158
-     * @since 1.0.0
159
-     */
160
-    public function lowerValue($key)
161
-    {
162
-        return $this->lower($key)->value;
163
-    }
149
+	/**
150
+	 * Returns the value whose key is the greatest key lesser than the given key
151
+	 *
152
+	 * @param mixed $key The searched key
153
+	 *
154
+	 * @return mixed The found value
155
+	 *
156
+	 * @throws \OutOfBoundsException If there is no lower element
157
+	 *
158
+	 * @since 1.0.0
159
+	 */
160
+	public function lowerValue($key)
161
+	{
162
+		return $this->lower($key)->value;
163
+	}
164 164
 
165
-    /**
166
-     * Returns the greatest key lesser than or equal to the given key
167
-     *
168
-     * @param mixed $key The searched key
169
-     *
170
-     * @return mixed The found key
171
-     *
172
-     * @throws \OutOfBoundsException If there is no floor element
173
-     *
174
-     * @since 1.0.0
175
-     */
176
-    public function floorKey($key)
177
-    {
178
-        return $this->floor($key)->key;
179
-    }
165
+	/**
166
+	 * Returns the greatest key lesser than or equal to the given key
167
+	 *
168
+	 * @param mixed $key The searched key
169
+	 *
170
+	 * @return mixed The found key
171
+	 *
172
+	 * @throws \OutOfBoundsException If there is no floor element
173
+	 *
174
+	 * @since 1.0.0
175
+	 */
176
+	public function floorKey($key)
177
+	{
178
+		return $this->floor($key)->key;
179
+	}
180 180
 
181
-    /**
182
-     * Returns the value whose key is the greatest key lesser than or equal to the given key
183
-     *
184
-     * @param mixed $key The searched key
185
-     *
186
-     * @return mixed The found value
187
-     *
188
-     * @throws \OutOfBoundsException If there is no floor element
189
-     *
190
-     * @since 1.0.0
191
-     */
192
-    public function floorValue($key)
193
-    {
194
-        return $this->floor($key)->value;
195
-    }
181
+	/**
182
+	 * Returns the value whose key is the greatest key lesser than or equal to the given key
183
+	 *
184
+	 * @param mixed $key The searched key
185
+	 *
186
+	 * @return mixed The found value
187
+	 *
188
+	 * @throws \OutOfBoundsException If there is no floor element
189
+	 *
190
+	 * @since 1.0.0
191
+	 */
192
+	public function floorValue($key)
193
+	{
194
+		return $this->floor($key)->value;
195
+	}
196 196
 
197
-    /**
198
-     * Returns the key equal to the given key
199
-     *
200
-     * @param mixed $key The searched key
201
-     *
202
-     * @return mixed The found key
203
-     *
204
-     * @throws \OutOfBoundsException If there is no such element
205
-     *
206
-     * @since 1.0.0
207
-     */
208
-    public function findKey($key)
209
-    {
210
-        return $this->find($key)->key;
211
-    }
197
+	/**
198
+	 * Returns the key equal to the given key
199
+	 *
200
+	 * @param mixed $key The searched key
201
+	 *
202
+	 * @return mixed The found key
203
+	 *
204
+	 * @throws \OutOfBoundsException If there is no such element
205
+	 *
206
+	 * @since 1.0.0
207
+	 */
208
+	public function findKey($key)
209
+	{
210
+		return $this->find($key)->key;
211
+	}
212 212
 
213
-    /**
214
-     * Returns the value whose key equal to the given key
215
-     *
216
-     * @param mixed $key The searched key
217
-     *
218
-     * @return mixed The found value
219
-     *
220
-     * @throws \OutOfBoundsException If there is no such element
221
-     *
222
-     * @since 1.0.0
223
-     */
224
-    public function findValue($key)
225
-    {
226
-        return $this->find($key)->value;
227
-    }
213
+	/**
214
+	 * Returns the value whose key equal to the given key
215
+	 *
216
+	 * @param mixed $key The searched key
217
+	 *
218
+	 * @return mixed The found value
219
+	 *
220
+	 * @throws \OutOfBoundsException If there is no such element
221
+	 *
222
+	 * @since 1.0.0
223
+	 */
224
+	public function findValue($key)
225
+	{
226
+		return $this->find($key)->value;
227
+	}
228 228
 
229
-    /**
230
-     * Returns the lowest key greater than or equal to the given key
231
-     *
232
-     * @param mixed $key The searched key
233
-     *
234
-     * @return mixed The found key
235
-     *
236
-     * @throws \OutOfBoundsException If there is no ceiling element
237
-     *
238
-     * @since 1.0.0
239
-     */
240
-    public function ceilingKey($key)
241
-    {
242
-        return $this->ceiling($key)->key;
243
-    }
229
+	/**
230
+	 * Returns the lowest key greater than or equal to the given key
231
+	 *
232
+	 * @param mixed $key The searched key
233
+	 *
234
+	 * @return mixed The found key
235
+	 *
236
+	 * @throws \OutOfBoundsException If there is no ceiling element
237
+	 *
238
+	 * @since 1.0.0
239
+	 */
240
+	public function ceilingKey($key)
241
+	{
242
+		return $this->ceiling($key)->key;
243
+	}
244 244
 
245
-    /**
246
-     * Returns the value whose key is the lowest key greater than or equal to the given key
247
-     *
248
-     * @param mixed $key The searched key
249
-     *
250
-     * @return mixed The found value
251
-     *
252
-     * @throws \OutOfBoundsException If there is no ceiling element
253
-     *
254
-     * @since 1.0.0
255
-     */
256
-    public function ceilingValue($key)
257
-    {
258
-        return $this->ceiling($key)->value;
259
-    }
245
+	/**
246
+	 * Returns the value whose key is the lowest key greater than or equal to the given key
247
+	 *
248
+	 * @param mixed $key The searched key
249
+	 *
250
+	 * @return mixed The found value
251
+	 *
252
+	 * @throws \OutOfBoundsException If there is no ceiling element
253
+	 *
254
+	 * @since 1.0.0
255
+	 */
256
+	public function ceilingValue($key)
257
+	{
258
+		return $this->ceiling($key)->value;
259
+	}
260 260
 
261
-    /**
262
-     * Returns the lowest key greater than to the given key
263
-     *
264
-     * @param mixed $key The searched key
265
-     *
266
-     * @return mixed The found key
267
-     *
268
-     * @throws \OutOfBoundsException If there is no higher element
269
-     *
270
-     * @since 1.0.0
271
-     */
272
-    public function higherKey($key)
273
-    {
274
-        return $this->higher($key)->key;
275
-    }
261
+	/**
262
+	 * Returns the lowest key greater than to the given key
263
+	 *
264
+	 * @param mixed $key The searched key
265
+	 *
266
+	 * @return mixed The found key
267
+	 *
268
+	 * @throws \OutOfBoundsException If there is no higher element
269
+	 *
270
+	 * @since 1.0.0
271
+	 */
272
+	public function higherKey($key)
273
+	{
274
+		return $this->higher($key)->key;
275
+	}
276 276
 
277
-    /**
278
-     * Returns the value whose key is the lowest key greater than to the given key
279
-     *
280
-     * @param mixed $key The searched key
281
-     *
282
-     * @return mixed The found value
283
-     *
284
-     * @throws \OutOfBoundsException If there is no higher element
285
-     *
286
-     * @since 1.0.0
287
-     */
288
-    public function higherValue($key)
289
-    {
290
-        return $this->higher($key)->value;
291
-    }
277
+	/**
278
+	 * Returns the value whose key is the lowest key greater than to the given key
279
+	 *
280
+	 * @param mixed $key The searched key
281
+	 *
282
+	 * @return mixed The found value
283
+	 *
284
+	 * @throws \OutOfBoundsException If there is no higher element
285
+	 *
286
+	 * @since 1.0.0
287
+	 */
288
+	public function higherValue($key)
289
+	{
290
+		return $this->higher($key)->value;
291
+	}
292 292
 
293
-    /**
294
-     * Keys iterator
295
-     *
296
-     * @return Iterator The keys iterator
297
-     *
298
-     * @since 1.0.0
299
-     */
300
-    public function keys()
301
-    {
302
-        return Iterator::keys($this);
303
-    }
293
+	/**
294
+	 * Keys iterator
295
+	 *
296
+	 * @return Iterator The keys iterator
297
+	 *
298
+	 * @since 1.0.0
299
+	 */
300
+	public function keys()
301
+	{
302
+		return Iterator::keys($this);
303
+	}
304 304
 
305
-    /**
306
-     * Values iterator
307
-     *
308
-     * @return Iterator The values iterator
309
-     *
310
-     * @since 1.0.0
311
-     */
312
-    public function values()
313
-    {
314
-        return Iterator::values($this);
315
-    }
305
+	/**
306
+	 * Values iterator
307
+	 *
308
+	 * @return Iterator The values iterator
309
+	 *
310
+	 * @since 1.0.0
311
+	 */
312
+	public function values()
313
+	{
314
+		return Iterator::values($this);
315
+	}
316 316
 
317
-    /**
318
-     * Convert the object to a string
319
-     *
320
-     * @return string String representation of the object
321
-     *
322
-     * @since 1.0.0
323
-     */
324
-    public function __toString()
325
-    {
326
-        return json_encode($this->toArray());
327
-    }
317
+	/**
318
+	 * Convert the object to a string
319
+	 *
320
+	 * @return string String representation of the object
321
+	 *
322
+	 * @since 1.0.0
323
+	 */
324
+	public function __toString()
325
+	{
326
+		return json_encode($this->toArray());
327
+	}
328 328
 
329
-    /**
330
-     * Convert the object to an array
331
-     *
332
-     * @return array Array representation of the object
333
-     *
334
-     * @since 1.0.0
335
-     */
336
-    public function toArray()
337
-    {
338
-        $array = array();
329
+	/**
330
+	 * Convert the object to an array
331
+	 *
332
+	 * @return array Array representation of the object
333
+	 *
334
+	 * @since 1.0.0
335
+	 */
336
+	public function toArray()
337
+	{
338
+		$array = array();
339 339
 
340
-        foreach ($this as $key => $value) {
341
-            $array[$key] = $value;
342
-        }
340
+		foreach ($this as $key => $value) {
341
+			$array[$key] = $value;
342
+		}
343 343
 
344
-        return $array;
345
-    }
344
+		return $array;
345
+	}
346 346
 
347
-    /**
348
-     * Create an iterator
349
-     *
350
-     * @return Iterator A new iterator
351
-     *
352
-     * @since 1.0.0
353
-     */
354
-    public function getIterator()
355
-    {
356
-        return Iterator::create($this);
357
-    }
347
+	/**
348
+	 * Create an iterator
349
+	 *
350
+	 * @return Iterator A new iterator
351
+	 *
352
+	 * @since 1.0.0
353
+	 */
354
+	public function getIterator()
355
+	{
356
+		return Iterator::create($this);
357
+	}
358 358
 
359
-    /**
360
-     * Get the value for a key
361
-     *
362
-     * @param mixed $key The key
363
-     *
364
-     * @return mixed The found value
365
-     *
366
-     * @throws \OutOfRangeException If there is no such element
367
-     *
368
-     * @since 1.0.0
369
-     */
370
-    public function offsetGet($key)
371
-    {
372
-        try {
373
-            return $this->find($key)->value;
374
-        } catch (\OutOfBoundsException $e) {
375
-            throw new \OutOfRangeException('Undefined offset');
376
-        }
377
-    }
359
+	/**
360
+	 * Get the value for a key
361
+	 *
362
+	 * @param mixed $key The key
363
+	 *
364
+	 * @return mixed The found value
365
+	 *
366
+	 * @throws \OutOfRangeException If there is no such element
367
+	 *
368
+	 * @since 1.0.0
369
+	 */
370
+	public function offsetGet($key)
371
+	{
372
+		try {
373
+			return $this->find($key)->value;
374
+		} catch (\OutOfBoundsException $e) {
375
+			throw new \OutOfRangeException('Undefined offset');
376
+		}
377
+	}
378 378
 
379
-    /**
380
-     * Test the existence of a key
381
-     *
382
-     * @param mixed $key The key
383
-     *
384
-     * @return boolean TRUE if the key exists, false otherwise
385
-     *
386
-     * @since 1.0.0
387
-     */
388
-    public function offsetExists($key)
389
-    {
390
-        try {
391
-            return (bool) $this->find($key);
392
-        } catch (\OutOfBoundsException $e) {
393
-            return false;
394
-        }
395
-    }
379
+	/**
380
+	 * Test the existence of a key
381
+	 *
382
+	 * @param mixed $key The key
383
+	 *
384
+	 * @return boolean TRUE if the key exists, false otherwise
385
+	 *
386
+	 * @since 1.0.0
387
+	 */
388
+	public function offsetExists($key)
389
+	{
390
+		try {
391
+			return (bool) $this->find($key);
392
+		} catch (\OutOfBoundsException $e) {
393
+			return false;
394
+		}
395
+	}
396 396
 
397
-    /**
398
-     * Set the value for a key
399
-     *
400
-     * @param mixed $key   The key
401
-     * @param mixed $value The value
402
-     *
403
-     * @return void
404
-     *
405
-     * @throws \RuntimeOperation The operation is not supported by this class
406
-     *
407
-     * @since 1.0.0
408
-     */
409
-    public function offsetSet($key, $value)
410
-    {
411
-        throw new \RuntimeException('Unsupported operation');
412
-    }
397
+	/**
398
+	 * Set the value for a key
399
+	 *
400
+	 * @param mixed $key   The key
401
+	 * @param mixed $value The value
402
+	 *
403
+	 * @return void
404
+	 *
405
+	 * @throws \RuntimeOperation The operation is not supported by this class
406
+	 *
407
+	 * @since 1.0.0
408
+	 */
409
+	public function offsetSet($key, $value)
410
+	{
411
+		throw new \RuntimeException('Unsupported operation');
412
+	}
413 413
 
414
-    /**
415
-     * Unset the existence of a key
416
-     *
417
-     * @param mixed $key The key
418
-     *
419
-     * @return void
420
-     *
421
-     * @throws \RuntimeOperation The operation is not supported by this class
422
-     *
423
-     * @since 1.0.0
424
-     */
425
-    public function offsetUnset($key)
426
-    {
427
-        throw new \RuntimeException('Unsupported operation');
428
-    }
414
+	/**
415
+	 * Unset the existence of a key
416
+	 *
417
+	 * @param mixed $key The key
418
+	 *
419
+	 * @return void
420
+	 *
421
+	 * @throws \RuntimeOperation The operation is not supported by this class
422
+	 *
423
+	 * @since 1.0.0
424
+	 */
425
+	public function offsetUnset($key)
426
+	{
427
+		throw new \RuntimeException('Unsupported operation');
428
+	}
429 429
 }
Please login to merge, or discard this patch.
src/SortedCollection/TreeNode.php 1 patch
Indentation   +539 added lines, -539 removed lines patch added patch discarded remove patch
@@ -30,543 +30,543 @@
 block discarded – undo
30 30
  */
31 31
 class TreeNode implements \Countable
32 32
 {
33
-    /**
34
-     * @var integer  Information associated to that node.
35
-     *                   Bits of order 0 and 1 are reserved for the existence of left and right tree.
36
-     *                   Other bits are for the balance
37
-     *
38
-     * @since 1.0.0
39
-     */
40
-    private $information = 0;
41
-
42
-    /**
43
-     * @var TreeNode  Left|Predecessor node
44
-     *
45
-     * @since 1.0.0
46
-     */
47
-    private $left;
48
-
49
-    /**
50
-     * @var TreeNode  Right|Successor node
51
-     *
52
-     * @since 1.0.0
53
-     */
54
-    private $right;
55
-
56
-    /**
57
-     * @var mixed  Node key
58
-     *
59
-     * @since 1.0.0
60
-     */
61
-    private $key;
62
-
63
-    /**
64
-     * @var mixed  Node value
65
-     *
66
-     * @since 1.0.0
67
-     */
68
-    public $value;
69
-
70
-    /**
71
-     * Create a node
72
-     *
73
-     * @param mixed $key   The node key
74
-     * @param mixed $value The node value
75
-     *
76
-     * @return A new node
77
-     *
78
-     * @since 1.0.0
79
-     */
80
-    public static function create($key, $value)
81
-    {
82
-        return new static($key, $value);
83
-    }
84
-
85
-    /**
86
-     * Constructor
87
-     *
88
-     * @param mixed    $key         The node key
89
-     * @param mixed    $value       The node value
90
-     * @param TreeNode $predecessor The left node
91
-     * @param TreeNode $successor   The right node
92
-     *
93
-     * @since 1.0.0
94
-     */
95
-    protected function __construct($key, $value, $predecessor = null, $successor = null)
96
-    {
97
-        $this->key = $key;
98
-        $this->value = $value;
99
-        $this->left = $predecessor;
100
-        $this->right = $successor;
101
-    }
102
-
103
-    /**
104
-     * Magic get method
105
-     *
106
-     * @param string $property The node property
107
-     *
108
-     * @return mixed The value associated to the property
109
-     *
110
-     * @throws \RuntimeException If the property is undefined
111
-     *
112
-     * @since 1.0.0
113
-     */
114
-    public function __get($property)
115
-    {
116
-        switch ($property) {
117
-            case 'first':
118
-                return $this->first();
119
-            case 'last':
120
-                return $this->last();
121
-            case 'predecessor':
122
-                return $this->predecessor();
123
-            case 'successor':
124
-                return $this->successor();
125
-            case 'key':
126
-                return $this->key;
127
-            case 'count':
128
-                return $this->count();
129
-            default:
130
-                throw new \RuntimeException('Undefined property');
131
-        }
132
-    }
133
-
134
-    /**
135
-     * Get the first node
136
-     *
137
-     * @return the first node
138
-     *
139
-     * @since 1.0.0
140
-     */
141
-    public function first()
142
-    {
143
-        $node = $this;
144
-
145
-        while ($node->information & 2) {
146
-            $node = $node->left;
147
-        }
148
-
149
-        return $node;
150
-    }
151
-
152
-    /**
153
-     * Get the last node
154
-     *
155
-     * @return the last node
156
-     *
157
-     * @since 1.0.0
158
-     */
159
-    public function last()
160
-    {
161
-        $node = $this;
162
-
163
-        while ($node->information & 1) {
164
-            $node = $node->right;
165
-        }
166
-
167
-        return $node;
168
-    }
169
-
170
-    /**
171
-     * Get the predecessor
172
-     *
173
-     * @return the predecessor node
174
-     *
175
-     * @since 1.0.0
176
-     */
177
-    public function predecessor()
178
-    {
179
-        if ($this->information & 2) {
180
-            $node = $this->left;
181
-
182
-            while ($node->information & 1) {
183
-                $node = $node->right;
184
-            }
185
-
186
-            return $node;
187
-        } else {
188
-            return $this->left;
189
-        }
190
-    }
191
-
192
-    /**
193
-     * Get the successor
194
-     *
195
-     * @return the successor node
196
-     *
197
-     * @since 1.0.0
198
-     */
199
-    public function successor()
200
-    {
201
-        if ($this->information & 1) {
202
-            $node = $this->right;
203
-
204
-            while ($node->information & 2) {
205
-                $node = $node->left;
206
-            }
207
-
208
-            return $node;
209
-        } else {
210
-            return $this->right;
211
-        }
212
-    }
213
-
214
-    /**
215
-     * Count the number of key/value pair
216
-     *
217
-     * @return integer
218
-     *
219
-     * @since 1.0.0
220
-     */
221
-    public function count()
222
-    {
223
-        $count = 1;
224
-
225
-        if ($this->information & 2) {
226
-            $count += $this->left->count;
227
-        }
228
-
229
-        if ($this->information & 1) {
230
-            $count += $this->right->count;
231
-        }
232
-
233
-        return $count;
234
-    }
235
-
236
-    /**
237
-     * Get the node for a key
238
-     *
239
-     * @param mixed    $key        The key
240
-     * @param callable $comparator The comparator function
241
-     * @param integer  $type       The operation type
242
-     *                             -2 for the
243
-     *                             greatest key
244
-     *                             lesser than the
245
-     *                             given key -1 for
246
-     *                             the greatest key
247
-     *                             lesser than or
248
-     *                             equal to the given
249
-     *                             key 0 for the
250
-     *                             given key +1 for
251
-     *                             the lowest key
252
-     *                             greater than or
253
-     *                             equal to the given
254
-     *                             key +2 for the
255
-     *                             lowest key greater
256
-     *                             than the given key
257
-     *
258
-     * @return mixed The node or null if not found
259
-     *
260
-     * @since 1.0.0
261
-     */
262
-    public function find($key, $comparator, $type = 0)
263
-    {
264
-        $node = $this;
265
-
266
-        while (true) {
267
-            $cmp = call_user_func($comparator, $key, $node->key);
268
-
269
-            if ($cmp < 0 && $node->information & 2) {
270
-                $node = $node->left;
271
-            } elseif ($cmp > 0 && $node->information & 1) {
272
-                $node = $node->right;
273
-            } else {
274
-                break;
275
-            }
276
-        }
277
-
278
-        if ($cmp < 0) {
279
-            if ($type < 0) {
280
-                return $node->left;
281
-            } elseif ($type > 0) {
282
-                return $node;
283
-            } else {
284
-                return null;
285
-            }
286
-        } elseif ($cmp > 0) {
287
-            if ($type < 0) {
288
-                return $node;
289
-            } elseif ($type > 0) {
290
-                return $node->right;
291
-            } else {
292
-                return null;
293
-            }
294
-        } else {
295
-            if ($type < -1) {
296
-                return $node->predecessor;
297
-            } elseif ($type > 1) {
298
-                return $node->successor;
299
-            } else {
300
-                return $node;
301
-            }
302
-        }
303
-    }
304
-
305
-    /**
306
-     * Rotate the node to the left
307
-     *
308
-     * @return TreeNode The rotated node
309
-     *
310
-     * @since 1.0.0
311
-     */
312
-    private function rotateLeft()
313
-    {
314
-        $right = $this->right;
315
-
316
-        if ($right->information & 2) {
317
-            $this->right = $right->left;
318
-            $right->left = $this;
319
-        } else {
320
-            $right->information |= 2;
321
-            $this->information &= ~ 1;
322
-        }
323
-
324
-        $this->information -= 4;
325
-
326
-        if ($right->information >= 4) {
327
-            $this->information -= $right->information & ~3;
328
-        }
329
-
330
-        $right->information -= 4;
331
-
332
-        if ($this->information < 0) {
333
-            $right->information += $this->information & ~3;
334
-        }
335
-
336
-        return $right;
337
-    }
338
-
339
-    /**
340
-     * Rotate the node to the right
341
-     *
342
-     * @return TreeNode The rotated node
343
-     *
344
-     * @since 1.0.0
345
-     */
346
-    private function rotateRight()
347
-    {
348
-        $left = $this->left;
349
-
350
-        if ($left->information & 1) {
351
-            $this->left = $left->right;
352
-            $left->right = $this;
353
-        } else {
354
-            $this->information &= ~ 2;
355
-            $left->information |= 1;
356
-        }
357
-
358
-        $this->information += 4;
359
-
360
-        if ($left->information < 0) {
361
-            $this->information -= $left->information & ~3;
362
-        }
363
-
364
-        $left->information += 4;
365
-
366
-        if ($this->information >= 4) {
367
-            $left->information += $this->information & ~3;
368
-        }
369
-
370
-        return $left;
371
-    }
372
-
373
-    /**
374
-     * Increment the balance of the node
375
-     *
376
-     * @return TreeNode $this or a rotated version of $this
377
-     *
378
-     * @since 1.0.0
379
-     */
380
-    private function incBalance()
381
-    {
382
-        $this->information += 4;
383
-
384
-        if ($this->information >= 8) {
385
-            if ($this->right->information < 0) {
386
-                $this->right = $this->right->rotateRight();
387
-            }
388
-
389
-            return $this->rotateLeft();
390
-        }
391
-
392
-        return $this;
393
-    }
394
-
395
-    /**
396
-     * Decrement the balance of the node
397
-     *
398
-     * @return TreeNode $this or a rotated version of $this
399
-     *
400
-     * @since 1.0.0
401
-     */
402
-    private function decBalance()
403
-    {
404
-        $this->information -= 4;
405
-
406
-        if ($this->information < - 4) {
407
-            if ($this->left->information >= 4) {
408
-                $this->left = $this->left->rotateLeft();
409
-            }
410
-
411
-            return $this->rotateRight();
412
-        }
413
-
414
-        return $this;
415
-    }
416
-
417
-    /**
418
-     * Insert a key/value pair
419
-     *
420
-     * @param mixed    $key        The key
421
-     * @param mixed    $value      The value
422
-     * @param callable $comparator The comparator function
423
-     *
424
-     * @return TreeNode The new root
425
-     *
426
-     * @since 1.0.0
427
-     */
428
-    public function insert($key, $value, $comparator)
429
-    {
430
-        $node = $this;
431
-        $cmp = call_user_func($comparator, $key, $this->key);
432
-
433
-        if ($cmp < 0) {
434
-            if ($this->information & 2) {
435
-                $leftBalance = $this->left->information & ~3;
436
-                $this->left = $this->left->insert($key, $value, $comparator);
437
-
438
-                if (($this->left->information & ~3) && ($this->left->information & ~3) != $leftBalance) {
439
-                    $node = $this->decBalance();
440
-                }
441
-            } else {
442
-                $this->left = new static($key, $value, $this->left, $this);
443
-                $this->information |= 2;
444
-                $node = $this->decBalance();
445
-            }
446
-        } elseif ($cmp > 0) {
447
-            if ($this->information & 1) {
448
-                $rightBalance = $this->right->information & ~3;
449
-                $this->right = $this->right->insert($key, $value, $comparator);
450
-
451
-                if (($this->right->information & ~3) && ($this->right->information & ~3) != $rightBalance) {
452
-                    $node = $this->incBalance();
453
-                }
454
-            } else {
455
-                $this->right = new static($key, $value, $this, $this->right);
456
-                $this->information |= 1;
457
-                $node = $this->incBalance();
458
-            }
459
-        } else {
460
-            $this->value = $value;
461
-        }
462
-
463
-        return $node;
464
-    }
465
-
466
-    /**
467
-     * Pull up the left most node of a node
468
-     *
469
-     * @return TreeNode The new root
470
-     *
471
-     * @since 1.0.0
472
-     */
473
-    private function pullUpLeftMost()
474
-    {
475
-        if ($this->information & 2) {
476
-            $leftBalance = $this->left->information & ~3;
477
-            $this->left = $this->left->pullUpLeftMost();
478
-
479
-            if (!($this->information & 2) || $leftBalance != 0 && ($this->left->information & ~3) == 0) {
480
-                return $this->incBalance();
481
-            } else {
482
-                return $this;
483
-            }
484
-        } else {
485
-            $this->left->key = $this->key;
486
-            $this->left->value = $this->value;
487
-
488
-            if ($this->information & 1) {
489
-                $this->right->left = $this->left;
490
-
491
-                return $this->right;
492
-            } else {
493
-                if ($this->left->right == $this) {
494
-                    $this->left->information &= ~ 1;
495
-
496
-                    return $this->right;
497
-                } else {
498
-                    $this->right->information &= ~ 2;
499
-
500
-                    return $this->left;
501
-                }
502
-            }
503
-        }
504
-    }
505
-
506
-    /**
507
-     * Remove a key
508
-     *
509
-     * @param mixed    $key        The key
510
-     * @param callable $comparator The comparator function
511
-     *
512
-     * @return TreeNode The new root
513
-     *
514
-     * @since 1.0.0
515
-     */
516
-    public function remove($key, $comparator)
517
-    {
518
-        $cmp = call_user_func($comparator, $key, $this->key);
519
-
520
-        if ($cmp < 0) {
521
-            if ($this->information & 2) {
522
-                $leftBalance = $this->left->information & ~3;
523
-                $this->left = $this->left->remove($key, $comparator);
524
-
525
-                if (!($this->information & 2) || $leftBalance != 0 && ($this->left->information & ~3) == 0) {
526
-                    return $this->incBalance();
527
-                }
528
-            }
529
-        } elseif ($cmp > 0) {
530
-            if ($this->information & 1) {
531
-                $rightBalance = $this->right->information & ~3;
532
-                $this->right = $this->right->remove($key, $comparator);
533
-
534
-                if (!($this->information & 1) || $rightBalance != 0 && ($this->right->information & ~3) == 0) {
535
-                    return $this->decBalance();
536
-                }
537
-            }
538
-        } else {
539
-            if ($this->information & 1) {
540
-                $rightBalance = $this->right->information & ~3;
541
-                $this->right = $this->right->pullUpLeftMost();
542
-
543
-                if (!($this->information & 1) || $rightBalance != 0 && ($this->right->information & ~3) == 0) {
544
-                    return $this->decBalance();
545
-                }
546
-            } else {
547
-                $left = $this->left;
548
-                $right = $this->right;
549
-
550
-                if ($this->information & 2) {
551
-                    $left->right = $right;
552
-
553
-                    return $left;
554
-                } else {
555
-                    if ($left && $left->right == $this) {
556
-                        $left->information &= ~ 1;
557
-
558
-                        return $right;
559
-                    } elseif ($right && $right->left == $this) {
560
-                        $right->information &= ~ 2;
561
-
562
-                        return $left;
563
-                    } else {
564
-                        return null;
565
-                    }
566
-                }
567
-            }
568
-        }
569
-
570
-        return $this;
571
-    }
33
+	/**
34
+	 * @var integer  Information associated to that node.
35
+	 *                   Bits of order 0 and 1 are reserved for the existence of left and right tree.
36
+	 *                   Other bits are for the balance
37
+	 *
38
+	 * @since 1.0.0
39
+	 */
40
+	private $information = 0;
41
+
42
+	/**
43
+	 * @var TreeNode  Left|Predecessor node
44
+	 *
45
+	 * @since 1.0.0
46
+	 */
47
+	private $left;
48
+
49
+	/**
50
+	 * @var TreeNode  Right|Successor node
51
+	 *
52
+	 * @since 1.0.0
53
+	 */
54
+	private $right;
55
+
56
+	/**
57
+	 * @var mixed  Node key
58
+	 *
59
+	 * @since 1.0.0
60
+	 */
61
+	private $key;
62
+
63
+	/**
64
+	 * @var mixed  Node value
65
+	 *
66
+	 * @since 1.0.0
67
+	 */
68
+	public $value;
69
+
70
+	/**
71
+	 * Create a node
72
+	 *
73
+	 * @param mixed $key   The node key
74
+	 * @param mixed $value The node value
75
+	 *
76
+	 * @return A new node
77
+	 *
78
+	 * @since 1.0.0
79
+	 */
80
+	public static function create($key, $value)
81
+	{
82
+		return new static($key, $value);
83
+	}
84
+
85
+	/**
86
+	 * Constructor
87
+	 *
88
+	 * @param mixed    $key         The node key
89
+	 * @param mixed    $value       The node value
90
+	 * @param TreeNode $predecessor The left node
91
+	 * @param TreeNode $successor   The right node
92
+	 *
93
+	 * @since 1.0.0
94
+	 */
95
+	protected function __construct($key, $value, $predecessor = null, $successor = null)
96
+	{
97
+		$this->key = $key;
98
+		$this->value = $value;
99
+		$this->left = $predecessor;
100
+		$this->right = $successor;
101
+	}
102
+
103
+	/**
104
+	 * Magic get method
105
+	 *
106
+	 * @param string $property The node property
107
+	 *
108
+	 * @return mixed The value associated to the property
109
+	 *
110
+	 * @throws \RuntimeException If the property is undefined
111
+	 *
112
+	 * @since 1.0.0
113
+	 */
114
+	public function __get($property)
115
+	{
116
+		switch ($property) {
117
+			case 'first':
118
+				return $this->first();
119
+			case 'last':
120
+				return $this->last();
121
+			case 'predecessor':
122
+				return $this->predecessor();
123
+			case 'successor':
124
+				return $this->successor();
125
+			case 'key':
126
+				return $this->key;
127
+			case 'count':
128
+				return $this->count();
129
+			default:
130
+				throw new \RuntimeException('Undefined property');
131
+		}
132
+	}
133
+
134
+	/**
135
+	 * Get the first node
136
+	 *
137
+	 * @return the first node
138
+	 *
139
+	 * @since 1.0.0
140
+	 */
141
+	public function first()
142
+	{
143
+		$node = $this;
144
+
145
+		while ($node->information & 2) {
146
+			$node = $node->left;
147
+		}
148
+
149
+		return $node;
150
+	}
151
+
152
+	/**
153
+	 * Get the last node
154
+	 *
155
+	 * @return the last node
156
+	 *
157
+	 * @since 1.0.0
158
+	 */
159
+	public function last()
160
+	{
161
+		$node = $this;
162
+
163
+		while ($node->information & 1) {
164
+			$node = $node->right;
165
+		}
166
+
167
+		return $node;
168
+	}
169
+
170
+	/**
171
+	 * Get the predecessor
172
+	 *
173
+	 * @return the predecessor node
174
+	 *
175
+	 * @since 1.0.0
176
+	 */
177
+	public function predecessor()
178
+	{
179
+		if ($this->information & 2) {
180
+			$node = $this->left;
181
+
182
+			while ($node->information & 1) {
183
+				$node = $node->right;
184
+			}
185
+
186
+			return $node;
187
+		} else {
188
+			return $this->left;
189
+		}
190
+	}
191
+
192
+	/**
193
+	 * Get the successor
194
+	 *
195
+	 * @return the successor node
196
+	 *
197
+	 * @since 1.0.0
198
+	 */
199
+	public function successor()
200
+	{
201
+		if ($this->information & 1) {
202
+			$node = $this->right;
203
+
204
+			while ($node->information & 2) {
205
+				$node = $node->left;
206
+			}
207
+
208
+			return $node;
209
+		} else {
210
+			return $this->right;
211
+		}
212
+	}
213
+
214
+	/**
215
+	 * Count the number of key/value pair
216
+	 *
217
+	 * @return integer
218
+	 *
219
+	 * @since 1.0.0
220
+	 */
221
+	public function count()
222
+	{
223
+		$count = 1;
224
+
225
+		if ($this->information & 2) {
226
+			$count += $this->left->count;
227
+		}
228
+
229
+		if ($this->information & 1) {
230
+			$count += $this->right->count;
231
+		}
232
+
233
+		return $count;
234
+	}
235
+
236
+	/**
237
+	 * Get the node for a key
238
+	 *
239
+	 * @param mixed    $key        The key
240
+	 * @param callable $comparator The comparator function
241
+	 * @param integer  $type       The operation type
242
+	 *                             -2 for the
243
+	 *                             greatest key
244
+	 *                             lesser than the
245
+	 *                             given key -1 for
246
+	 *                             the greatest key
247
+	 *                             lesser than or
248
+	 *                             equal to the given
249
+	 *                             key 0 for the
250
+	 *                             given key +1 for
251
+	 *                             the lowest key
252
+	 *                             greater than or
253
+	 *                             equal to the given
254
+	 *                             key +2 for the
255
+	 *                             lowest key greater
256
+	 *                             than the given key
257
+	 *
258
+	 * @return mixed The node or null if not found
259
+	 *
260
+	 * @since 1.0.0
261
+	 */
262
+	public function find($key, $comparator, $type = 0)
263
+	{
264
+		$node = $this;
265
+
266
+		while (true) {
267
+			$cmp = call_user_func($comparator, $key, $node->key);
268
+
269
+			if ($cmp < 0 && $node->information & 2) {
270
+				$node = $node->left;
271
+			} elseif ($cmp > 0 && $node->information & 1) {
272
+				$node = $node->right;
273
+			} else {
274
+				break;
275
+			}
276
+		}
277
+
278
+		if ($cmp < 0) {
279
+			if ($type < 0) {
280
+				return $node->left;
281
+			} elseif ($type > 0) {
282
+				return $node;
283
+			} else {
284
+				return null;
285
+			}
286
+		} elseif ($cmp > 0) {
287
+			if ($type < 0) {
288
+				return $node;
289
+			} elseif ($type > 0) {
290
+				return $node->right;
291
+			} else {
292
+				return null;
293
+			}
294
+		} else {
295
+			if ($type < -1) {
296
+				return $node->predecessor;
297
+			} elseif ($type > 1) {
298
+				return $node->successor;
299
+			} else {
300
+				return $node;
301
+			}
302
+		}
303
+	}
304
+
305
+	/**
306
+	 * Rotate the node to the left
307
+	 *
308
+	 * @return TreeNode The rotated node
309
+	 *
310
+	 * @since 1.0.0
311
+	 */
312
+	private function rotateLeft()
313
+	{
314
+		$right = $this->right;
315
+
316
+		if ($right->information & 2) {
317
+			$this->right = $right->left;
318
+			$right->left = $this;
319
+		} else {
320
+			$right->information |= 2;
321
+			$this->information &= ~ 1;
322
+		}
323
+
324
+		$this->information -= 4;
325
+
326
+		if ($right->information >= 4) {
327
+			$this->information -= $right->information & ~3;
328
+		}
329
+
330
+		$right->information -= 4;
331
+
332
+		if ($this->information < 0) {
333
+			$right->information += $this->information & ~3;
334
+		}
335
+
336
+		return $right;
337
+	}
338
+
339
+	/**
340
+	 * Rotate the node to the right
341
+	 *
342
+	 * @return TreeNode The rotated node
343
+	 *
344
+	 * @since 1.0.0
345
+	 */
346
+	private function rotateRight()
347
+	{
348
+		$left = $this->left;
349
+
350
+		if ($left->information & 1) {
351
+			$this->left = $left->right;
352
+			$left->right = $this;
353
+		} else {
354
+			$this->information &= ~ 2;
355
+			$left->information |= 1;
356
+		}
357
+
358
+		$this->information += 4;
359
+
360
+		if ($left->information < 0) {
361
+			$this->information -= $left->information & ~3;
362
+		}
363
+
364
+		$left->information += 4;
365
+
366
+		if ($this->information >= 4) {
367
+			$left->information += $this->information & ~3;
368
+		}
369
+
370
+		return $left;
371
+	}
372
+
373
+	/**
374
+	 * Increment the balance of the node
375
+	 *
376
+	 * @return TreeNode $this or a rotated version of $this
377
+	 *
378
+	 * @since 1.0.0
379
+	 */
380
+	private function incBalance()
381
+	{
382
+		$this->information += 4;
383
+
384
+		if ($this->information >= 8) {
385
+			if ($this->right->information < 0) {
386
+				$this->right = $this->right->rotateRight();
387
+			}
388
+
389
+			return $this->rotateLeft();
390
+		}
391
+
392
+		return $this;
393
+	}
394
+
395
+	/**
396
+	 * Decrement the balance of the node
397
+	 *
398
+	 * @return TreeNode $this or a rotated version of $this
399
+	 *
400
+	 * @since 1.0.0
401
+	 */
402
+	private function decBalance()
403
+	{
404
+		$this->information -= 4;
405
+
406
+		if ($this->information < - 4) {
407
+			if ($this->left->information >= 4) {
408
+				$this->left = $this->left->rotateLeft();
409
+			}
410
+
411
+			return $this->rotateRight();
412
+		}
413
+
414
+		return $this;
415
+	}
416
+
417
+	/**
418
+	 * Insert a key/value pair
419
+	 *
420
+	 * @param mixed    $key        The key
421
+	 * @param mixed    $value      The value
422
+	 * @param callable $comparator The comparator function
423
+	 *
424
+	 * @return TreeNode The new root
425
+	 *
426
+	 * @since 1.0.0
427
+	 */
428
+	public function insert($key, $value, $comparator)
429
+	{
430
+		$node = $this;
431
+		$cmp = call_user_func($comparator, $key, $this->key);
432
+
433
+		if ($cmp < 0) {
434
+			if ($this->information & 2) {
435
+				$leftBalance = $this->left->information & ~3;
436
+				$this->left = $this->left->insert($key, $value, $comparator);
437
+
438
+				if (($this->left->information & ~3) && ($this->left->information & ~3) != $leftBalance) {
439
+					$node = $this->decBalance();
440
+				}
441
+			} else {
442
+				$this->left = new static($key, $value, $this->left, $this);
443
+				$this->information |= 2;
444
+				$node = $this->decBalance();
445
+			}
446
+		} elseif ($cmp > 0) {
447
+			if ($this->information & 1) {
448
+				$rightBalance = $this->right->information & ~3;
449
+				$this->right = $this->right->insert($key, $value, $comparator);
450
+
451
+				if (($this->right->information & ~3) && ($this->right->information & ~3) != $rightBalance) {
452
+					$node = $this->incBalance();
453
+				}
454
+			} else {
455
+				$this->right = new static($key, $value, $this, $this->right);
456
+				$this->information |= 1;
457
+				$node = $this->incBalance();
458
+			}
459
+		} else {
460
+			$this->value = $value;
461
+		}
462
+
463
+		return $node;
464
+	}
465
+
466
+	/**
467
+	 * Pull up the left most node of a node
468
+	 *
469
+	 * @return TreeNode The new root
470
+	 *
471
+	 * @since 1.0.0
472
+	 */
473
+	private function pullUpLeftMost()
474
+	{
475
+		if ($this->information & 2) {
476
+			$leftBalance = $this->left->information & ~3;
477
+			$this->left = $this->left->pullUpLeftMost();
478
+
479
+			if (!($this->information & 2) || $leftBalance != 0 && ($this->left->information & ~3) == 0) {
480
+				return $this->incBalance();
481
+			} else {
482
+				return $this;
483
+			}
484
+		} else {
485
+			$this->left->key = $this->key;
486
+			$this->left->value = $this->value;
487
+
488
+			if ($this->information & 1) {
489
+				$this->right->left = $this->left;
490
+
491
+				return $this->right;
492
+			} else {
493
+				if ($this->left->right == $this) {
494
+					$this->left->information &= ~ 1;
495
+
496
+					return $this->right;
497
+				} else {
498
+					$this->right->information &= ~ 2;
499
+
500
+					return $this->left;
501
+				}
502
+			}
503
+		}
504
+	}
505
+
506
+	/**
507
+	 * Remove a key
508
+	 *
509
+	 * @param mixed    $key        The key
510
+	 * @param callable $comparator The comparator function
511
+	 *
512
+	 * @return TreeNode The new root
513
+	 *
514
+	 * @since 1.0.0
515
+	 */
516
+	public function remove($key, $comparator)
517
+	{
518
+		$cmp = call_user_func($comparator, $key, $this->key);
519
+
520
+		if ($cmp < 0) {
521
+			if ($this->information & 2) {
522
+				$leftBalance = $this->left->information & ~3;
523
+				$this->left = $this->left->remove($key, $comparator);
524
+
525
+				if (!($this->information & 2) || $leftBalance != 0 && ($this->left->information & ~3) == 0) {
526
+					return $this->incBalance();
527
+				}
528
+			}
529
+		} elseif ($cmp > 0) {
530
+			if ($this->information & 1) {
531
+				$rightBalance = $this->right->information & ~3;
532
+				$this->right = $this->right->remove($key, $comparator);
533
+
534
+				if (!($this->information & 1) || $rightBalance != 0 && ($this->right->information & ~3) == 0) {
535
+					return $this->decBalance();
536
+				}
537
+			}
538
+		} else {
539
+			if ($this->information & 1) {
540
+				$rightBalance = $this->right->information & ~3;
541
+				$this->right = $this->right->pullUpLeftMost();
542
+
543
+				if (!($this->information & 1) || $rightBalance != 0 && ($this->right->information & ~3) == 0) {
544
+					return $this->decBalance();
545
+				}
546
+			} else {
547
+				$left = $this->left;
548
+				$right = $this->right;
549
+
550
+				if ($this->information & 2) {
551
+					$left->right = $right;
552
+
553
+					return $left;
554
+				} else {
555
+					if ($left && $left->right == $this) {
556
+						$left->information &= ~ 1;
557
+
558
+						return $right;
559
+					} elseif ($right && $right->left == $this) {
560
+						$right->information &= ~ 2;
561
+
562
+						return $left;
563
+					} else {
564
+						return null;
565
+					}
566
+				}
567
+			}
568
+		}
569
+
570
+		return $this;
571
+	}
572 572
 }
Please login to merge, or discard this patch.
src/SortedCollection/SortedMap.php 1 patch
Indentation   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -24,130 +24,130 @@
 block discarded – undo
24 24
  */
25 25
 interface SortedMap extends SortedCollection
26 26
 {
27
-    /**
28
-     * Get the first key or throw an exception if there is no element
29
-     *
30
-     * @return mixed The first key
31
-     *
32
-     * @throws \OutOfBoundsException If there is no element
33
-     *
34
-     * @since 1.0.0
35
-     */
36
-    public function firstKey();
27
+	/**
28
+	 * Get the first key or throw an exception if there is no element
29
+	 *
30
+	 * @return mixed The first key
31
+	 *
32
+	 * @throws \OutOfBoundsException If there is no element
33
+	 *
34
+	 * @since 1.0.0
35
+	 */
36
+	public function firstKey();
37 37
 
38
-    /**
39
-     * Get the last key or throw an exception if there is no element
40
-     *
41
-     * @return mixed The last key
42
-     *
43
-     * @throws \OutOfBoundsException If there is no element
44
-     *
45
-     * @since 1.0.0
46
-     */
47
-    public function lastKey();
38
+	/**
39
+	 * Get the last key or throw an exception if there is no element
40
+	 *
41
+	 * @return mixed The last key
42
+	 *
43
+	 * @throws \OutOfBoundsException If there is no element
44
+	 *
45
+	 * @since 1.0.0
46
+	 */
47
+	public function lastKey();
48 48
 
49
-    /**
50
-     * Returns the greatest key lesser than the given key or throw an exception if there is no such key
51
-     *
52
-     * @param mixed $key The searched key
53
-     *
54
-     * @return mixed The found key
55
-     *
56
-     * @throws \OutOfBoundsException If there is no lower element
57
-     *
58
-     * @since 1.0.0
59
-     */
60
-    public function lowerKey($key);
49
+	/**
50
+	 * Returns the greatest key lesser than the given key or throw an exception if there is no such key
51
+	 *
52
+	 * @param mixed $key The searched key
53
+	 *
54
+	 * @return mixed The found key
55
+	 *
56
+	 * @throws \OutOfBoundsException If there is no lower element
57
+	 *
58
+	 * @since 1.0.0
59
+	 */
60
+	public function lowerKey($key);
61 61
 
62
-    /**
63
-     * Returns the greatest key lesser than or equal to the given key or throw an exception if there is no such key
64
-     *
65
-     * @param mixed $key The searched key
66
-     *
67
-     * @return mixed The found key
68
-     *
69
-     * @throws \OutOfBoundsException If there is no floor element
70
-     *
71
-     * @since 1.0.0
72
-     */
73
-    public function floorKey($key);
62
+	/**
63
+	 * Returns the greatest key lesser than or equal to the given key or throw an exception if there is no such key
64
+	 *
65
+	 * @param mixed $key The searched key
66
+	 *
67
+	 * @return mixed The found key
68
+	 *
69
+	 * @throws \OutOfBoundsException If there is no floor element
70
+	 *
71
+	 * @since 1.0.0
72
+	 */
73
+	public function floorKey($key);
74 74
 
75
-    /**
76
-     * Returns the key equal to the given key or throw an exception if there is no such key
77
-     *
78
-     * @param mixed $key The searched key
79
-     *
80
-     * @return mixed The found key
81
-     *
82
-     * @throws \OutOfBoundsException If there is no such element
83
-     *
84
-     * @since 1.0.0
85
-     */
86
-    public function findKey($key);
75
+	/**
76
+	 * Returns the key equal to the given key or throw an exception if there is no such key
77
+	 *
78
+	 * @param mixed $key The searched key
79
+	 *
80
+	 * @return mixed The found key
81
+	 *
82
+	 * @throws \OutOfBoundsException If there is no such element
83
+	 *
84
+	 * @since 1.0.0
85
+	 */
86
+	public function findKey($key);
87 87
 
88
-    /**
89
-     * Returns the lowest key greater than or equal to the given key or throw an exception if there is no such key
90
-     *
91
-     * @param mixed $key The searched key
92
-     *
93
-     * @return mixed The found key
94
-     *
95
-     * @throws \OutOfBoundsException If there is no ceiling element
96
-     *
97
-     * @since 1.0.0
98
-     */
99
-    public function ceilingKey($key);
88
+	/**
89
+	 * Returns the lowest key greater than or equal to the given key or throw an exception if there is no such key
90
+	 *
91
+	 * @param mixed $key The searched key
92
+	 *
93
+	 * @return mixed The found key
94
+	 *
95
+	 * @throws \OutOfBoundsException If there is no ceiling element
96
+	 *
97
+	 * @since 1.0.0
98
+	 */
99
+	public function ceilingKey($key);
100 100
 
101
-    /**
102
-     * Returns the lowest key greater than to the given key or throw an exception if there is no such key
103
-     *
104
-     * @param mixed $key The searched key
105
-     *
106
-     * @return mixed The found key
107
-     *
108
-     * @throws \OutOfBoundsException If there is no higher element
109
-     *
110
-     * @since 1.0.0
111
-     */
112
-    public function higherKey($key);
101
+	/**
102
+	 * Returns the lowest key greater than to the given key or throw an exception if there is no such key
103
+	 *
104
+	 * @param mixed $key The searched key
105
+	 *
106
+	 * @return mixed The found key
107
+	 *
108
+	 * @throws \OutOfBoundsException If there is no higher element
109
+	 *
110
+	 * @since 1.0.0
111
+	 */
112
+	public function higherKey($key);
113 113
 
114
-    /**
115
-     * Get the predecessor node
116
-     *
117
-     * @param TreeNode $node A tree node member of the underlying TreeMap
118
-     *
119
-     * @return mixed The predecessor node
120
-     *
121
-     * @since 1.0.0
122
-     */
123
-    public function predecessor($node);
114
+	/**
115
+	 * Get the predecessor node
116
+	 *
117
+	 * @param TreeNode $node A tree node member of the underlying TreeMap
118
+	 *
119
+	 * @return mixed The predecessor node
120
+	 *
121
+	 * @since 1.0.0
122
+	 */
123
+	public function predecessor($node);
124 124
 
125
-    /**
126
-     * Get the successor node
127
-     *
128
-     * @param TreeNode $node A tree node member of the underlying TreeMap
129
-     *
130
-     * @return mixed The successor node
131
-     *
132
-     * @since 1.0.0
133
-     */
134
-    public function successor($node);
125
+	/**
126
+	 * Get the successor node
127
+	 *
128
+	 * @param TreeNode $node A tree node member of the underlying TreeMap
129
+	 *
130
+	 * @return mixed The successor node
131
+	 *
132
+	 * @since 1.0.0
133
+	 */
134
+	public function successor($node);
135 135
 
136
-    /**
137
-     * Keys generator
138
-     *
139
-     * @return mixed The keys generator
140
-     *
141
-     * @since 1.0.0
142
-     */
143
-    public function keys();
136
+	/**
137
+	 * Keys generator
138
+	 *
139
+	 * @return mixed The keys generator
140
+	 *
141
+	 * @since 1.0.0
142
+	 */
143
+	public function keys();
144 144
 
145
-    /**
146
-     * Values generator
147
-     *
148
-     * @return mixed The values generator
149
-     *
150
-     * @since 1.0.0
151
-     */
152
-    public function values();
145
+	/**
146
+	 * Values generator
147
+	 *
148
+	 * @return mixed The values generator
149
+	 *
150
+	 * @since 1.0.0
151
+	 */
152
+	public function values();
153 153
 }
Please login to merge, or discard this patch.