| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library index.b_plus_tree; | 5 library index.b_plus_tree; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 return _readIndexNode(id); | 147 return _readIndexNode(id); |
| 148 } else { | 148 } else { |
| 149 return _readLeafNode(id); | 149 return _readLeafNode(id); |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 /** | 153 /** |
| 154 * Writes [node] into the manager. | 154 * Writes [node] into the manager. |
| 155 */ | 155 */ |
| 156 void _writeIndexNode(_IndexNode<K, V, N> node) { | 156 void _writeIndexNode(_IndexNode<K, V, N> node) { |
| 157 _manager.writeIndex(node.id, new IndexNodeData(node.keys, node.children)); | 157 _manager.writeIndex(node.id, new IndexNodeData<K, N>(node.keys, node.childre
n)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 /** | 160 /** |
| 161 * Writes [node] into the manager. | 161 * Writes [node] into the manager. |
| 162 */ | 162 */ |
| 163 void _writeLeafNode(_LeafNode<K, V, N> node) { | 163 void _writeLeafNode(_LeafNode<K, V, N> node) { |
| 164 _manager.writeLeaf(node.id, new LeafNodeData(node.keys, node.values)); | 164 _manager.writeLeaf(node.id, new LeafNodeData<K, V>(node.keys, node.values)); |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 | 168 |
| 169 /** | 169 /** |
| 170 * A container with information about an index node. | 170 * A container with information about an index node. |
| 171 */ | 171 */ |
| 172 class IndexNodeData<K, N> { | 172 class IndexNodeData<K, N> { |
| 173 final List<N> children; | 173 final List<N> children; |
| 174 final List<K> keys; | 174 final List<K> keys; |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 left.keys.add(anchor); | 433 left.keys.add(anchor); |
| 434 left.keys.addAll(keys); | 434 left.keys.addAll(keys); |
| 435 left.children.addAll(children); | 435 left.children.addAll(children); |
| 436 tree._writeIndexNode(this); | 436 tree._writeIndexNode(this); |
| 437 tree._writeIndexNode(left); | 437 tree._writeIndexNode(left); |
| 438 return new _Remove<K, V>.mergeLeft(value); | 438 return new _Remove<K, V>.mergeLeft(value); |
| 439 } | 439 } |
| 440 // Try right sibling. | 440 // Try right sibling. |
| 441 if (right is _IndexNode<K, V, N>) { | 441 if (right is _IndexNode<K, V, N>) { |
| 442 // Try to redistribute. | 442 // Try to redistribute. |
| 443 var rightLength = right.keys.length; | 443 int rightLength = right.keys.length; |
| 444 if (rightLength > minKeys) { | 444 if (rightLength > minKeys) { |
| 445 int halfExcess = (rightLength - minKeys + 1) ~/ 2; | 445 int halfExcess = (rightLength - minKeys + 1) ~/ 2; |
| 446 keys.add(anchor); | 446 keys.add(anchor); |
| 447 keys.addAll(right.keys.getRange(0, halfExcess - 1)); | 447 keys.addAll(right.keys.getRange(0, halfExcess - 1)); |
| 448 children.addAll(right.children.getRange(0, halfExcess)); | 448 children.addAll(right.children.getRange(0, halfExcess)); |
| 449 K newAnchor = right.keys[halfExcess - 1]; | 449 K newAnchor = right.keys[halfExcess - 1]; |
| 450 right.keys.removeRange(0, halfExcess); | 450 right.keys.removeRange(0, halfExcess); |
| 451 right.children.removeRange(0, halfExcess); | 451 right.children.removeRange(0, halfExcess); |
| 452 tree._writeIndexNode(this); | 452 tree._writeIndexNode(this); |
| 453 tree._writeIndexNode(right); | 453 tree._writeIndexNode(right); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 // Do merge. | 604 // Do merge. |
| 605 left.keys.addAll(keys); | 605 left.keys.addAll(keys); |
| 606 left.values.addAll(values); | 606 left.values.addAll(values); |
| 607 tree._writeLeafNode(this); | 607 tree._writeLeafNode(this); |
| 608 tree._writeLeafNode(left); | 608 tree._writeLeafNode(left); |
| 609 return new _Remove<K, V>.mergeLeft(value); | 609 return new _Remove<K, V>.mergeLeft(value); |
| 610 } | 610 } |
| 611 // Try right sibling. | 611 // Try right sibling. |
| 612 if (right is _LeafNode<K, V, N>) { | 612 if (right is _LeafNode<K, V, N>) { |
| 613 // Try to redistribute. | 613 // Try to redistribute. |
| 614 var rightLength = right.keys.length; | 614 int rightLength = right.keys.length; |
| 615 if (rightLength > minKeys) { | 615 if (rightLength > minKeys) { |
| 616 int halfExcess = (rightLength - minKeys + 1) ~/ 2; | 616 int halfExcess = (rightLength - minKeys + 1) ~/ 2; |
| 617 keys.addAll(right.keys.getRange(0, halfExcess)); | 617 keys.addAll(right.keys.getRange(0, halfExcess)); |
| 618 values.addAll(right.values.getRange(0, halfExcess)); | 618 values.addAll(right.values.getRange(0, halfExcess)); |
| 619 right.keys.removeRange(0, halfExcess); | 619 right.keys.removeRange(0, halfExcess); |
| 620 right.values.removeRange(0, halfExcess); | 620 right.values.removeRange(0, halfExcess); |
| 621 tree._writeLeafNode(this); | 621 tree._writeLeafNode(this); |
| 622 tree._writeLeafNode(right); | 622 tree._writeLeafNode(right); |
| 623 return new _Remove<K, V>.borrowRight(value, right.keys.first); | 623 return new _Remove<K, V>.borrowRight(value, right.keys.first); |
| 624 } | 624 } |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 | 765 |
| 766 /** | 766 /** |
| 767 * A container with information about split during insert. | 767 * A container with information about split during insert. |
| 768 */ | 768 */ |
| 769 class _Split<K, N> { | 769 class _Split<K, N> { |
| 770 final K key; | 770 final K key; |
| 771 final N left; | 771 final N left; |
| 772 final N right; | 772 final N right; |
| 773 _Split(this.key, this.left, this.right); | 773 _Split(this.key, this.left, this.right); |
| 774 } | 774 } |
| OLD | NEW |