MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / binarySearch

Function binarySearch

web/pgadmin/browser/static/js/browser.js:596–649  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

594 return false;
595 },
596 binarySearch = function() {
597 let d, m;
598 // Binary search only outperforms Linear search for n > 44.
599 // Reference:
600 // https://en.wikipedia.org/wiki/Binary_search_algorithm#cite_note-30
601 //
602 // We will try until it's half.
603 while (e - s > 22) {
604 i = items[s];
605 d = __ctx.t.itemData(i);
606 if (d._type === 'column') {
607 if (pgAdmin.numeric_comparator(d._id, _data._id) != -1)
608 return true;
609 } else if (pgAdmin.natural_sort(d._label, _data._label) != -1)
610 return true;
611 i = items[e];
612 d = __ctx.t.itemData(i);
613 let result;
614 if (d._type === 'column') {
615 result = pgAdmin.numeric_comparator(d._id, _data._id);
616 } else {
617 result = pgAdmin.natural_sort(d._label, _data._label);
618 }
619 if (result !=1) {
620 if (e != items.length - 1) {
621 i = items[e+1];
622 return true;
623 }
624 i = null;
625 return false;
626 }
627 m = s + Math.round((e - s) / 2);
628 i = items[m];
629 d = __ctx.t.itemData(i);
630 if(d._type === 'column'){
631 result = pgAdmin.numeric_comparator(d._id, _data._id);
632 } else {
633 result = pgAdmin.natural_sort(d._label, _data._label);
634 }
635 //result will never become 0 because of remove operation
636 //which happens with updateTreeNode
637 if (result == 0)
638 return true;
639
640 if (result == -1) {
641 s = m + 1;
642 e--;
643 } else {
644 s++;
645 e = m - 1;
646 }
647 }
648 return linearSearch();
649 };
650
651 if (binarySearch()) {
652 __ctx.t.before(i, _data).then((_item) => {

Callers 1

browser.jsFile · 0.85

Calls 2

linearSearchFunction · 0.85
itemDataMethod · 0.65

Tested by

no test coverage detected