17
17
18
18
import { Path } from './util/Path' ;
19
19
import { PRIORITY_INDEX } from './snap/indexes/PriorityIndex' ;
20
- import { CountedSet } from './util/CountedSet' ;
21
20
import { Node } from './snap/Node' ;
22
21
23
22
/**
24
23
* Helper class to store a sparse set of snapshots.
25
- *
26
- * @constructor
27
24
*/
28
25
export class SparseSnapshotTree {
29
- /**
30
- * @private
31
- * @type {Node }
32
- */
33
- private value_ : Node | null = null ;
26
+ private value : Node | null = null ;
34
27
35
- /**
36
- * @private
37
- * @type {CountedSet }
38
- */
39
- private children_ : CountedSet < string , SparseSnapshotTree > | null = null ;
28
+ private readonly children : Map < string , SparseSnapshotTree > = new Map ( ) ;
40
29
41
30
/**
42
31
* Gets the node stored at the given path if one exists.
43
32
*
44
- * @param { !Path } path Path to look up snapshot for.
45
- * @return { ?Node } The retrieved node, or null.
33
+ * @param path Path to look up snapshot for.
34
+ * @return The retrieved node, or null.
46
35
*/
47
36
find ( path : Path ) : Node | null {
48
- if ( this . value_ != null ) {
49
- return this . value_ . getChild ( path ) ;
50
- } else if ( ! path . isEmpty ( ) && this . children_ != null ) {
37
+ if ( this . value != null ) {
38
+ return this . value . getChild ( path ) ;
39
+ } else if ( ! path . isEmpty ( ) && this . children . size > 0 ) {
51
40
const childKey = path . getFront ( ) ;
52
41
path = path . popFront ( ) ;
53
- if ( this . children_ . contains ( childKey ) ) {
54
- const childTree = this . children_ . get ( childKey ) as SparseSnapshotTree ;
42
+ if ( this . children . has ( childKey ) ) {
43
+ const childTree = this . children . get ( childKey ) ;
55
44
return childTree . find ( path ) ;
56
45
} else {
57
46
return null ;
@@ -65,26 +54,22 @@ export class SparseSnapshotTree {
65
54
* Stores the given node at the specified path. If there is already a node
66
55
* at a shallower path, it merges the new data into that snapshot node.
67
56
*
68
- * @param { !Path } path Path to look up snapshot for.
69
- * @param { !Node } data The new data, or null.
57
+ * @param path Path to look up snapshot for.
58
+ * @param data The new data, or null.
70
59
*/
71
60
remember ( path : Path , data : Node ) {
72
61
if ( path . isEmpty ( ) ) {
73
- this . value_ = data ;
74
- this . children_ = null ;
75
- } else if ( this . value_ !== null ) {
76
- this . value_ = this . value_ . updateChild ( path , data ) ;
62
+ this . value = data ;
63
+ this . children . clear ( ) ;
64
+ } else if ( this . value !== null ) {
65
+ this . value = this . value . updateChild ( path , data ) ;
77
66
} else {
78
- if ( this . children_ == null ) {
79
- this . children_ = new CountedSet < string , SparseSnapshotTree > ( ) ;
80
- }
81
-
82
67
const childKey = path . getFront ( ) ;
83
- if ( ! this . children_ . contains ( childKey ) ) {
84
- this . children_ . add ( childKey , new SparseSnapshotTree ( ) ) ;
68
+ if ( ! this . children . has ( childKey ) ) {
69
+ this . children . set ( childKey , new SparseSnapshotTree ( ) ) ;
85
70
}
86
71
87
- const child = this . children_ . get ( childKey ) as SparseSnapshotTree ;
72
+ const child = this . children . get ( childKey ) ;
88
73
path = path . popFront ( ) ;
89
74
child . remember ( path , data ) ;
90
75
}
@@ -93,22 +78,22 @@ export class SparseSnapshotTree {
93
78
/**
94
79
* Purge the data at path from the cache.
95
80
*
96
- * @param { !Path } path Path to look up snapshot for.
97
- * @return { boolean } True if this node should now be removed.
81
+ * @param path Path to look up snapshot for.
82
+ * @return True if this node should now be removed.
98
83
*/
99
84
forget ( path : Path ) : boolean {
100
85
if ( path . isEmpty ( ) ) {
101
- this . value_ = null ;
102
- this . children_ = null ;
86
+ this . value = null ;
87
+ this . children . clear ( ) ;
103
88
return true ;
104
89
} else {
105
- if ( this . value_ !== null ) {
106
- if ( this . value_ . isLeafNode ( ) ) {
90
+ if ( this . value !== null ) {
91
+ if ( this . value . isLeafNode ( ) ) {
107
92
// We're trying to forget a node that doesn't exist
108
93
return false ;
109
94
} else {
110
- const value = this . value_ ;
111
- this . value_ = null ;
95
+ const value = this . value ;
96
+ this . value = null ;
112
97
113
98
const self = this ;
114
99
value . forEachChild ( PRIORITY_INDEX , function ( key , tree ) {
@@ -117,24 +102,17 @@ export class SparseSnapshotTree {
117
102
118
103
return this . forget ( path ) ;
119
104
}
120
- } else if ( this . children_ !== null ) {
105
+ } else if ( this . children . size > 0 ) {
121
106
const childKey = path . getFront ( ) ;
122
107
path = path . popFront ( ) ;
123
- if ( this . children_ . contains ( childKey ) ) {
124
- const safeToRemove = ( this . children_ . get (
125
- childKey
126
- ) as SparseSnapshotTree ) . forget ( path ) ;
108
+ if ( this . children . has ( childKey ) ) {
109
+ const safeToRemove = this . children . get ( childKey ) . forget ( path ) ;
127
110
if ( safeToRemove ) {
128
- this . children_ . remove ( childKey ) ;
111
+ this . children . delete ( childKey ) ;
129
112
}
130
113
}
131
114
132
- if ( this . children_ . isEmpty ( ) ) {
133
- this . children_ = null ;
134
- return true ;
135
- } else {
136
- return false ;
137
- }
115
+ return this . children . size === 0 ;
138
116
} else {
139
117
return true ;
140
118
}
@@ -145,12 +123,12 @@ export class SparseSnapshotTree {
145
123
* Recursively iterates through all of the stored tree and calls the
146
124
* callback on each one.
147
125
*
148
- * @param { !Path } prefixPath Path to look up node for.
149
- * @param { !Function } func The function to invoke for each tree.
126
+ * @param prefixPath Path to look up node for.
127
+ * @param func The function to invoke for each tree.
150
128
*/
151
129
forEachTree ( prefixPath : Path , func : ( a : Path , b : Node ) => any ) {
152
- if ( this . value_ !== null ) {
153
- func ( prefixPath , this . value_ ) ;
130
+ if ( this . value !== null ) {
131
+ func ( prefixPath , this . value ) ;
154
132
} else {
155
133
this . forEachChild ( ( key , tree ) => {
156
134
const path = new Path ( prefixPath . toString ( ) + '/' + key ) ;
@@ -162,13 +140,11 @@ export class SparseSnapshotTree {
162
140
/**
163
141
* Iterates through each immediate child and triggers the callback.
164
142
*
165
- * @param { !Function } func The function to invoke for each child.
143
+ * @param func The function to invoke for each child.
166
144
*/
167
145
forEachChild ( func : ( a : string , b : SparseSnapshotTree ) => void ) {
168
- if ( this . children_ !== null ) {
169
- this . children_ . each ( ( key , tree ) => {
170
- func ( key , tree ) ;
171
- } ) ;
172
- }
146
+ this . children . forEach ( ( tree , key ) => {
147
+ func ( key , tree ) ;
148
+ } ) ;
173
149
}
174
150
}
0 commit comments