Comparing version 1.1.6 to 1.1.7
@@ -17,11 +17,20 @@ 'use strict'; | ||
function BPlusTree(options) { | ||
function BPlusTree() { | ||
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var _ref$order = _ref.order; | ||
var order = _ref$order === undefined ? 6 : _ref$order; | ||
var _ref$debug = _ref.debug; | ||
var debug = _ref$debug === undefined ? false : _ref$debug; | ||
var _ref$cmpFn = _ref.cmpFn; | ||
var cmpFn = _ref$cmpFn === undefined ? function (a, b) { | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
} : _ref$cmpFn; | ||
_classCallCheck(this, BPlusTree); | ||
options || (options = {}); | ||
this.order = options.order || 6; | ||
this.debug = options.debug || false; | ||
this.cmpFn = options.cmpFn || function (a, b) { | ||
return a < b ? -1 : a > b ? 1 : 0; // eslint-disable-line | ||
}; | ||
// eslint-disable-line | ||
this.order = order; | ||
this.debug = debug; | ||
this.cmpFn = cmpFn; | ||
@@ -51,6 +60,16 @@ if (this.order % 2 !== 0 || this.order < 4) { | ||
key: 'repr', | ||
value: function repr(options) { | ||
options || (options = {}); | ||
var tree = options.root || this.tree; | ||
var result = options.getKeys || options.getValues ? [] : {}; | ||
value: function repr() { | ||
var _ref2 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var _ref2$root = _ref2.root; | ||
var root = _ref2$root === undefined ? this.tree : _ref2$root; | ||
var _ref2$getKeys = _ref2.getKeys; | ||
var getKeys = _ref2$getKeys === undefined ? false : _ref2$getKeys; | ||
var _ref2$getValues = _ref2.getValues; | ||
var getValues = _ref2$getValues === undefined ? false : _ref2$getValues; | ||
var _ref2$descending = _ref2.descending; | ||
var descending = _ref2$descending === undefined ? false : _ref2$descending; | ||
var tree = root; | ||
var result = getKeys || getValues ? [] : {}; | ||
function walk(node) { | ||
@@ -64,5 +83,5 @@ if (node.t === 'branch') { | ||
for (var i = 0, nkl = node.k.length; i < nkl; i++) { | ||
if (options.getKeys) { | ||
if (getKeys) { | ||
result.push(node.k[i]); | ||
} else if (options.getValues) { | ||
} else if (getValues) { | ||
result.push(node.v[i]); | ||
@@ -78,3 +97,3 @@ } else { | ||
if ((options.getKeys || options.getValues) && options.descending) { | ||
if ((getKeys || getValues) && descending) { | ||
return out.reverse(); | ||
@@ -96,4 +115,8 @@ } | ||
key: 'fetchRange', | ||
value: function fetchRange(lowerBound, upperBound, options) { | ||
options || (options = {}); | ||
value: function fetchRange(lowerBound, upperBound) { | ||
var _ref3 = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var _ref3$descending = _ref3.descending; | ||
var descending = _ref3$descending === undefined ? false : _ref3$descending; | ||
var hi = upperBound; | ||
@@ -162,3 +185,3 @@ var lo = lowerBound; | ||
if (options.descending) { | ||
if (descending) { | ||
result.reverse(); | ||
@@ -179,5 +202,9 @@ } | ||
key: 'depth', | ||
value: function depth(options) { | ||
options || (options = {}); | ||
var tree = options.root || this.tree; | ||
value: function depth() { | ||
var _ref4 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var _ref4$root = _ref4.root; | ||
var root = _ref4$root === undefined ? this.tree : _ref4$root; | ||
var tree = root; | ||
var d = 0; | ||
@@ -200,7 +227,10 @@ while (tree.t === 'branch') { | ||
key: 'check', | ||
value: function check(options) { | ||
options || (options = {}); | ||
var tree = options.root || this.tree; | ||
var depth = this.depth({ root: tree }); | ||
value: function check() { | ||
var _ref5 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var _ref5$root = _ref5.root; | ||
var root = _ref5$root === undefined ? this.tree : _ref5$root; | ||
var depth = this.depth({ root: root }); | ||
function assert(expr, msg) { | ||
@@ -255,7 +285,3 @@ if (!expr) { | ||
if (!options.root) { | ||
assert(this.repr({ getKeys: true }).length === this.numKeys, 'leaf count does not match'); | ||
} | ||
return checking(this, tree, 0, [], []); | ||
return checking(this, root, 0, [], []); | ||
} | ||
@@ -275,6 +301,14 @@ | ||
key: 'fetch', | ||
value: function fetch(key, options) { | ||
options || (options = {}); | ||
var node = options.root || this.tree; | ||
value: function fetch(key) { | ||
var _ref6 = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var _ref6$root = _ref6.root; | ||
var root = _ref6$root === undefined ? this.tree : _ref6$root; | ||
var _ref6$getLeaf = _ref6.getLeaf; | ||
var getLeaf = _ref6$getLeaf === undefined ? false : _ref6$getLeaf; | ||
var _ref6$getPath = _ref6.getPath; | ||
var getPath = _ref6$getPath === undefined ? false : _ref6$getPath; | ||
var node = root; | ||
var index = undefined; | ||
@@ -301,6 +335,6 @@ var path = []; | ||
var val = node.v[j]; | ||
if (options.getPath) { | ||
if (getPath) { | ||
return { val: val, leaf: node, path: path }; | ||
} | ||
if (options.getLeaf) { | ||
if (getLeaf) { | ||
return node; | ||
@@ -307,0 +341,0 @@ } |
@@ -9,9 +9,6 @@ /** Class representing a B+ Tree. */ | ||
*/ | ||
constructor(options) { | ||
options || (options = {}); | ||
this.order = options.order || 6; | ||
this.debug = options.debug || false; | ||
this.cmpFn = options.cmpFn || ((a, b) => { | ||
return (a < b) ? -1 : ((a > b) ? 1 : 0); // eslint-disable-line | ||
}); | ||
constructor({ order = 6, debug = false, cmpFn = ((a, b) => (a < b) ? -1 : ((a > b) ? 1 : 0)) } = {}) { // eslint-disable-line | ||
this.order = order; | ||
this.debug = debug; | ||
this.cmpFn = cmpFn; | ||
@@ -38,6 +35,5 @@ if (this.order % 2 !== 0 || this.order < 4) { | ||
*/ | ||
repr(options) { | ||
options || (options = {}); | ||
const tree = options.root || this.tree; | ||
const result = (options.getKeys || options.getValues) ? [] : {}; | ||
repr({ root = this.tree, getKeys = false, getValues = false, descending = false } = {}) { | ||
const tree = root; | ||
const result = (getKeys || getValues) ? [] : {}; | ||
function walk(node) { | ||
@@ -51,5 +47,5 @@ if (node.t === 'branch') { | ||
for (let i = 0, nkl = node.k.length; i < nkl; i++) { | ||
if (options.getKeys) { | ||
if (getKeys) { | ||
result.push(node.k[i]); | ||
} else if (options.getValues) { | ||
} else if (getValues) { | ||
result.push(node.v[i]); | ||
@@ -66,3 +62,3 @@ } else { | ||
if ((options.getKeys || options.getValues) && options.descending) { | ||
if ((getKeys || getValues) && descending) { | ||
return out.reverse(); | ||
@@ -81,4 +77,3 @@ } | ||
*/ | ||
fetchRange(lowerBound, upperBound, options) { | ||
options || (options = {}); | ||
fetchRange(lowerBound, upperBound, { descending = false } = {}) { | ||
const hi = upperBound; | ||
@@ -146,3 +141,3 @@ let lo = lowerBound; | ||
if (options.descending) { | ||
if (descending) { | ||
result.reverse(); | ||
@@ -160,5 +155,4 @@ } | ||
*/ | ||
depth(options) { | ||
options || (options = {}); | ||
let tree = options.root || this.tree; | ||
depth({ root = this.tree } = {}) { | ||
let tree = root; | ||
let d = 0; | ||
@@ -178,6 +172,4 @@ while (tree.t === 'branch') { | ||
*/ | ||
check(options) { | ||
options || (options = {}); | ||
const tree = options.root || this.tree; | ||
const depth = this.depth({ root: tree }); | ||
check({ root = this.tree } = {}) { | ||
const depth = this.depth({ root }); | ||
@@ -233,7 +225,3 @@ function assert(expr, msg) { | ||
if (!options.root) { | ||
assert(this.repr({ getKeys: true }).length === this.numKeys, 'leaf count does not match'); | ||
} | ||
return checking(this, tree, 0, [], []); | ||
return checking(this, root, 0, [], []); | ||
} | ||
@@ -250,5 +238,4 @@ | ||
*/ | ||
fetch(key, options) { | ||
options || (options = {}); | ||
let node = options.root || this.tree; | ||
fetch(key, { root = this.tree, getLeaf = false, getPath = false } = {}) { | ||
let node = root; | ||
@@ -276,6 +263,6 @@ let index; | ||
const val = node.v[j]; | ||
if (options.getPath) { | ||
if (getPath) { | ||
return { val, leaf: node, path }; | ||
} | ||
if (options.getLeaf) { | ||
if (getLeaf) { | ||
return node; | ||
@@ -282,0 +269,0 @@ } |
{ | ||
"name": "bplustree", | ||
"version": "1.1.6", | ||
"version": "1.1.7", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "test": "mocha --compilers js:babel-core/register --check-leaks test/bplustree.js && npm run build", |
@@ -24,1 +24,15 @@ # bplustree | ||
- `npm run doc` generates the jsdoc documentation | ||
# Dependencies | ||
None | ||
# License | ||
MIT | ||
# Acknowledgement | ||
- This implementation is based on @darius' work: [bplustree.py](https://github.com/darius/sketchbook/blob/master/trees/bplustrees.py) | ||
- @tehgeekmeister's notes on [B+ Trees](https://github.com/tehgeekmeister/dadabass/blob/master/notes/b_plus_tree.md) were also very helpful | ||
- The [`_genGetKeyFn`](https://github.com/vhf/bplustree/blob/9e0192dd8d591a7e1a29370edbe5a119a038e0db/lib/bplustree.js#L374-L379) function is courtesy of @martinmaillard |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1129094
2102
38