Skip to content

Commit 7b4363e

Browse files
committed
PHP7.4 new feature. Add method getAllValue()
1 parent e493d63 commit 7b4363e

File tree

9 files changed

+994
-899
lines changed

9 files changed

+994
-899
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/cli/

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ $tree->setValue(50000, 60000, 'key2', new \SplQueue());
2222
$tree->setValue(99999, 100000, 'key3', 'some value');
2323

2424
// Get value of certain position.
25-
var_dump($tree->getValue(20006));
25+
var_dump($tree->getValue(20006, 'key1'));
2626

27-
// Delete value of between certain postions.
27+
// Delete value of between certain positions.
2828
$tree->delValue(30000, 100000, 'key1');
2929

30-
// Thorws exception when value not found.
31-
var_dump($tree->getValue(70000));
30+
// Throws exception when value not found.
31+
var_dump($tree->getValue(70000, 'key1'));
3232

3333
// Get segment arrays.
3434
var_dump($tree->getSegmentsOfGivenKey('key1'));
@@ -46,10 +46,18 @@ $tree = Swango\SegmentTree\Tree\Version::newTree(
4646
'1.0.0', '1.0.1', '1.0.2', '1.0.3', '1.1.0', '1.4.1', '2.0.0', '2.0.1-RC1', '3.0.0'
4747
); // Create a tree with versions. These versions will be sorted using version_compare() and remove all duplicated
4848

49-
// All methos are similar with Common tree.
50-
$tree->setValue('1.0.2', '2.0.0', 'key1', $true);
49+
// All methods are similar with Common tree.
50+
$tree->setValue('1.0.2', '2.0.0', 'key1', true);
5151
var_dump($tree->getValue('1.1.0'));
5252
```
5353
### Date compressed segment tree
54-
(todo)
54+
```php
55+
$tree = Swango\SegmentTree\Tree\Date::newTree(
56+
'2020-09-25', '2020-12-12'
57+
); // Create a tree with dates.
58+
59+
// All methods are similar with Common tree.
60+
$tree->setValue('2020-09-29', '2020-11-11', 'key1', true);
61+
var_dump($tree->getValue('2020-11-01'));
62+
```
5563

src/AbstractSegmentTree.php

Lines changed: 128 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,129 @@
1-
<?php
2-
namespace Swango\SegmentTree;
3-
abstract class AbstractSegmentTree extends Node {
4-
/**
5-
* Get all existing keys.
6-
* Some keys maybe deleted or override by others.
7-
*
8-
* @return array
9-
*/
10-
public function getAllExistingKeys(): array {
11-
$ret = [];
12-
foreach ($this->data as $key=>&$tmp)
13-
$ret[] = $key;
14-
return $key;
15-
}
16-
protected $use_double_equal_sign = true;
17-
protected function isEqual($x, $y): bool {
18-
if ($x instanceof MixtureFlag || $y instanceof MixtureFlag)
19-
return false;
20-
if ($this->use_double_equal_sign)
21-
return $x == $y;
22-
else
23-
return $x === $y;
24-
}
25-
/**
26-
* Set to use "==" when comparing values.
27-
* Two objects of different instances that have same content will be considered equal.
28-
*
29-
* This is a default setting. No need to call manualy.
30-
* Must be called before set any value or things could be corrupted.
31-
*
32-
* @return self
33-
*/
34-
public function useDoubleEqualSign(): self {
35-
$this->use_double_equal_sign = true;
36-
return $this;
37-
}
38-
/**
39-
* Set to use "===" when comparing values.
40-
* Two objects of different instances will be considered not equal no matter their content.
41-
*
42-
* Must be called before set any value or things could be corrupted.
43-
*
44-
* @return self
45-
*/
46-
public function useTripleEqualSign(): self {
47-
$this->use_double_equal_sign = false;
48-
return $this;
49-
}
50-
/**
51-
* fill $key of the whole tree by $value
52-
*
53-
* @param string $key
54-
* @param mixed $value
55-
* @return self
56-
*/
57-
public function fill(string $key, $value): self {
58-
$this->_setValue($this->l, $this->r, $key, $value);
59-
return $this;
60-
}
61-
/**
62-
* Clear all values and child nodes.
63-
* Make it a new tree.
64-
*
65-
* @return self
66-
*/
67-
public function clear(): self {
68-
$this->data = new \stdClass();
69-
$this->node_l = null;
70-
$this->node_r = null;
71-
return $this;
72-
}
73-
/**
74-
* Remove all redundant nodes in the tree to reduce memory cost.
75-
*
76-
* @return self
77-
*/
78-
public function optimize(): self {
79-
if (! $this->hasChildNode())
80-
return $this;
81-
$queue = new \SplQueue();
82-
$queue->enqueue($this);
83-
do {
84-
/**
85-
*
86-
* @var Node $node
87-
*/
88-
$node = $queue->dequeue();
89-
foreach ($node->data as $value)
90-
if ($value instanceof MixtureFlag) {
91-
if ($node->node_l->hasChildNode())
92-
$queue->enqueue($node->node_l);
93-
if ($node->node_r->hasChildNode())
94-
$queue->enqueue($node->node_r);
95-
continue 2;
96-
}
97-
98-
$node->node_l = null;
99-
$node->node_r = null;
100-
} while ( ! $queue->isEmpty() );
101-
return $this;
102-
}
103-
/**
104-
* Get number of nodes in the tree
105-
*
106-
* @return int
107-
*/
108-
public function getNodesCount(): int {
109-
$count = 0;
110-
$queue = new \SplQueue();
111-
$queue->enqueue($this);
112-
do {
113-
++ $count;
114-
/**
115-
*
116-
* @var Node $node
117-
*/
118-
$node = $queue->dequeue();
119-
if ($node->hasChildNode()) {
120-
$queue->enqueue($node->node_l);
121-
$queue->enqueue($node->node_r);
122-
}
123-
} while ( ! $queue->isEmpty() );
124-
return $count;
125-
}
1+
<?php
2+
namespace Swango\SegmentTree;
3+
abstract class AbstractSegmentTree extends Node {
4+
/**
5+
* Get all existing keys.
6+
* Some keys maybe deleted or override by others.
7+
*
8+
* @return array
9+
*/
10+
public function getAllExistingKeys(): array {
11+
$ret = [];
12+
foreach ($this->data as $key => &$tmp)
13+
$ret[] = $key;
14+
return $key;
15+
}
16+
protected bool $use_double_equal_sign = true;
17+
protected function isEqual($x, $y): bool {
18+
if ($x instanceof MixtureFlag || $y instanceof MixtureFlag) {
19+
return false;
20+
}
21+
if ($this->use_double_equal_sign) {
22+
return $x == $y;
23+
} else {
24+
return $x === $y;
25+
}
26+
}
27+
/**
28+
* Set to use "==" when comparing values.
29+
* Two objects of different instances that have same content will be considered equal.
30+
*
31+
* This is a default setting. No need to call manually.
32+
* Must be called before set any value or things could be corrupted.
33+
*
34+
* @return self
35+
*/
36+
public function useDoubleEqualSign(): self {
37+
$this->use_double_equal_sign = true;
38+
return $this;
39+
}
40+
/**
41+
* Set to use "===" when comparing values.
42+
* Two objects of different instances will be considered not equal no matter their content.
43+
*
44+
* Must be called before set any value or things could be corrupted.
45+
*
46+
* @return self
47+
*/
48+
public function useTripleEqualSign(): self {
49+
$this->use_double_equal_sign = false;
50+
return $this;
51+
}
52+
/**
53+
* fill $key of the whole tree by $value
54+
*
55+
* @param string $key
56+
* @param mixed $value
57+
* @return self
58+
*/
59+
public function fill(string $key, $value): self {
60+
$this->_setValue($this->l, $this->r, $key, $value);
61+
return $this;
62+
}
63+
/**
64+
* Clear all values and child nodes.
65+
* Make it a new tree.
66+
*
67+
* @return self
68+
*/
69+
public function clear(): self {
70+
$this->data = new \stdClass();
71+
unset($this->node_l);
72+
unset($this->node_r);
73+
return $this;
74+
}
75+
/**
76+
* Remove all redundant nodes in the tree to reduce memory cost.
77+
*
78+
* @return self
79+
*/
80+
public function optimize(): self {
81+
if (! $this->hasChildNode()) {
82+
return $this;
83+
}
84+
$queue = new \SplQueue();
85+
$queue->enqueue($this);
86+
do {
87+
/**
88+
*
89+
* @var Node $node
90+
*/
91+
$node = $queue->dequeue();
92+
foreach ($node->data as $value)
93+
if ($value instanceof MixtureFlag) {
94+
if ($node->node_l->hasChildNode()) {
95+
$queue->enqueue($node->node_l);
96+
}
97+
if ($node->node_r->hasChildNode()) {
98+
$queue->enqueue($node->node_r);
99+
}
100+
continue 2;
101+
}
102+
unset($this->node_l);
103+
unset($this->node_r);
104+
} while (! $queue->isEmpty());
105+
return $this;
106+
}
107+
/**
108+
* Get number of nodes in the tree
109+
*
110+
* @return int
111+
*/
112+
public function getNodesCount(): int {
113+
$count = 0;
114+
$queue = new \SplQueue();
115+
$queue->enqueue($this);
116+
do {
117+
++$count;
118+
/**
119+
* @var Node $node
120+
*/
121+
$node = $queue->dequeue();
122+
if ($node->hasChildNode()) {
123+
$queue->enqueue($node->node_l);
124+
$queue->enqueue($node->node_r);
125+
}
126+
} while (! $queue->isEmpty());
127+
return $count;
128+
}
126129
}

src/MixtureFlag.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
<?php
2-
namespace Swango\SegmentTree;
3-
/**
4-
* This is only a simple object used as a flag.
5-
*
6-
* @author fdrea
7-
*
8-
*/
9-
class MixtureFlag {
10-
private static $instance;
11-
public static function getInstance(): MixtureFlag {
12-
if (isset(self::$instance))
13-
return self::$instance;
14-
$instance = new self();
15-
self::$instance = $instance;
16-
return $instance;
17-
}
18-
private function __construct() {}
1+
<?php
2+
namespace Swango\SegmentTree;
3+
/**
4+
* This is only a simple object used as flag.
5+
*/
6+
final class MixtureFlag {
7+
private static MixtureFlag $instance;
8+
public static function getInstance(): MixtureFlag {
9+
if (isset(self::$instance)) {
10+
return self::$instance;
11+
}
12+
$instance = new self();
13+
self::$instance = $instance;
14+
return $instance;
15+
}
16+
private function __construct() {
17+
}
1918
}

0 commit comments

Comments
 (0)