Skip to content

Latest commit

 

History

History
94 lines (68 loc) · 4.17 KB

tpl.readme.md

File metadata and controls

94 lines (68 loc) · 4.17 KB

About

{{pkg.description}}

{{meta.status}}

{{repo.supportPackages}}

{{repo.relatedPackages}}

{{meta.blogPosts}}

Installation

{{pkg.install}}

{{pkg.size}}

Dependencies

{{pkg.deps}}

{{repo.examples}}

API

{{pkg.docs}}

Binary search result predicates

The following predicates can be used to perform predecessor / successor queries using binarySearch().

  • bsLT() - Returns index of last item less than search value or -1 if no such values exist
  • bsLE() - Similar to bsLT(), but for less-than-equals queries
  • bsGT() - Returns index of first item greater than search value or -1 if no such values exist
  • bsGE() - Similar to bsGT(), but for less-than-equals queries
  • bsEQ() - Merely syntax sugar, casting any non-found result indices to -1
import { binarySearch, bsGT, bsLT } from "@thi.ng/arrays";

const src = [10, 20, 30, 40];

bsLT(binarySearch(src, 25))
// 1

// greater-than queries also require the array length

bsGT(binarySearch(src, 25), src.length)
// 2

bsGT(binarySearch(src, 40), src.length)
// -1