1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
'\" t
.\" Copyright (c) 2025 Bruce Hill
.\" All rights reserved.
.\"
.TH List.binary_search 3 2025-04-30 "Tomo man-pages"
.SH NAME
List.binary_search \- binary search
.SH LIBRARY
Tomo Standard Library
.SH SYNOPSIS
.nf
.BI List.binary_search\ :\ func(list:\ [T],\ by:\ func(x,y:&T->Int32)\ =\ T.compare\ ->\ Int)
.fi
.SH DESCRIPTION
Performs a binary search on a sorted list.
.SH ARGUMENTS
.TS
allbox;
lb lb lbx lb
l l l l.
Name Type Description Default
list [T] The sorted list to search. -
by func(x,y:&T->Int32) The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. T.compare
.TE
.SH RETURN
Assuming the input list is sorted according to the given comparison function, return the index where the given item would be inserted to maintain the sorted order. That is, if the item is found, return its index, otherwise return the place where it would be found if it were inserted and the list were sorted.
.SH EXAMPLES
.EX
>> [1, 3, 5, 7, 9].binary_search(5)
= 3
>> [1, 3, 5, 7, 9].binary_search(-999)
= 1
>> [1, 3, 5, 7, 9].binary_search(999)
= 6
.EE
|