-
Notifications
You must be signed in to change notification settings - Fork 0
/
leafIterator.js
180 lines (161 loc) · 4.48 KB
/
leafIterator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function hasChildren(node) {
return node !== undefined &&
node.children !== undefined &&
Object.keys(node.children).length > 0;
}
// Expects that the node has children
function childKeys(node) {
return Object.keys(node.children);
}
function getChildKey(node, keys) {
var childKeyId = keys.indexOf(""+node.id);
if(childKeyId === -1) {
//console.log('Node has been deleted?', keys, node);
// Node has been deleted?
throw new Error('Node has been deleted?');
}
return childKeyId;
}
function moveDown(node, pickLastKey) {
while(hasChildren(node)) {
if(pickLastKey) {
var keys = childKeys(node);
var lastKeyId = keys.length - 1;
var lastChildKey = keys[lastKeyId];
node = node.children[lastChildKey]
} else {
var firstChildKey = childKeys(node)[0];
node = node.children[firstChildKey];
}
}
return node;
}
function findNextLeaf(node) {
var parent = node.parent;
if(parent !== undefined) {
var keys = childKeys(parent);
var childKeyId = getChildKey(node, keys);
var nextChildId = childKeyId + 1;
if(keys.length > nextChildId) {
node = parent.children[keys[nextChildId]];
return moveDown(node);
} else {
return findNextLeaf(parent);
}
}
return undefined;
}
function findPrevLeaf(node) {
var parent = node.parent;
if(parent !== undefined) {
var keys = childKeys(parent);
var childKeyId = getChildKey(node, keys);
var nextChildId = childKeyId - 1;
if(nextChildId >= 0) {
node = parent.children[keys[nextChildId]];
return moveDown(node, true);
} else {
return findPrevLeaf(parent);
}
}
return undefined;
}
function findClosestChildKey(node, childKey) {
var keys = node.getChildrenKeys();
var lastKey = keys[0], i = 0;
while(++i < keys.length && keys[i] < childKey) {
lastKey = keys[i];
}
return lastKey;
}
/**
Traverse the tree as long as we can with the current path, when there is no
match with the path and the children then move down to the closest leaf node.
*/
function findClosestLeaf(node, pathArray) {
if(pathArray.length === 0 ||
node.children.length === 0) {
return node;
}
var subNodeId = pathArray[0];
var childNode = node.children[subNodeId];
if(childNode === undefined) {
let key = findClosestChildKey(node, subNodeId);
// We might want the rightmost child if the requested id is larger than the given key
return moveDown(node.children[key], key < subNodeId);
}
return findClosestLeaf(childNode, pathArray.slice(1));
}
/**
Handle both next and prev
**/
function step(stepNode, findStepNode) {
if(stepNode == undefined) {
this._node = findStepNode(this._node);
} else {
this._node = stepNode;
}
if(this._node == undefined) {
return undefined;
}
return this._node.content;
}
function LeafIterator(tree, reverse) {
if(tree === undefined) {
throw new Error("Missing argument tree in leaf iterator");
}
this._tree = tree;
if(reverse === true) {
this._node = moveDown(tree.getNode(), true);
this._prevNode = this._node;
this._nextNode = undefined;
} else {
this._node = moveDown(tree.getNode());
this._nextNode = this._node;
this._prevNode = undefined;
}
}
LeafIterator.prototype.hasNext = function() {
if(this._nextNode == undefined) {
this._nextNode = findNextLeaf(this._node);
}
return this._nextNode !== undefined;
}
LeafIterator.prototype.next = function() {
var content = step.bind(this)(this._nextNode, findNextLeaf);
this._nextNode = undefined;
if(this._node !== undefined) {
this._prevNode = this._node;
}
return content;
}
LeafIterator.prototype.hasPrev = function() {
if(this._prevNode == undefined) {
this._prevNode = findPrevLeaf(this._node);
}
return this._prevNode !== undefined;
}
LeafIterator.prototype.prev = function() {
var content = step.bind(this)(this._prevNode, findPrevLeaf);
this._prevNode = undefined;
if(this._node !== undefined) {
this._nextNode = this._node;
}
return content;
}
LeafIterator.prototype.getPath = function() {
if(this._node !== undefined) {
return this._node._getPath();
}
return undefined;
}
LeafIterator.prototype.gotoPath = function(path, pickLastKey) {
this._node = this._tree.getNode.apply(this._tree, path);
if(this._node === undefined) {
this._node = findClosestLeaf(this._tree._rootNode, path);
}
this._node = moveDown(this._node, pickLastKey);
this._nextNode = this._node;
this._prevNode = this._node;
}
module.exports = LeafIterator;