Skip to content

Commit

Permalink
Implemented NameTree.get() using binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandermeij committed Oct 6, 2014
1 parent f814117 commit 695b3c1
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,7 @@ var Catalog = (function CatalogClosure() {
}
if (nameTreeRef) {
var nameTree = new NameTree(nameTreeRef, xref);
var names = nameTree.getAll();
for (var name in names) {
if (!names.hasOwnProperty(name)) {
continue;
}
if (name === destinationId) {
dest = fetchDestination(names[name]);
}
}
dest = fetchDestination(nameTree.get(destinationId));
}
return dest;
},
Expand Down Expand Up @@ -1398,6 +1390,46 @@ var NameTree = (function NameTreeClosure() {
}
}
return dict;
},

get: function NameTree_get(destinationId, root) {
if (!root) {
root = this.root;
}
var xref = this.xref;
root = xref.fetchIfRef(root);
var kids = root.get('Kids');
var names = root.get('Names');

if (isArray(kids)) {
var l = 0;
var r = kids.length - 1;

while (l < r) {
var m = (l + r) >> 1;
var kid = xref.fetchIfRef(kids[m]);
var limits = kid.get('Limits');

if (destinationId < limits[0]) {
r = m - 1;
} else if (destinationId > limits[1]) {
l = m + 1;
} else {
return this.get(destinationId, kids[m]);
}
}
return this.get(destinationId, kids[l]);
}

if (isArray(names)) {
for (var i = 0, n = names.length; i < n; i += 2) {
if (names[i] === destinationId) {
return xref.fetchIfRef(names[i + 1]);
}
}
}

return null;
}
};
return NameTree;
Expand Down

0 comments on commit 695b3c1

Please sign in to comment.