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
+ }
126
129
}
0 commit comments