⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.93
Server IP:
65.108.141.171
Server:
Linux server.heloix.com 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
Server Software:
Apache
PHP Version:
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
ri
/
2.7.0
/
system
/
Range
/
View File Name :
bsearch-i.ri
U:RDoc::AnyMethod[iI"bsearch:ETI"Range#bsearch;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"JBy using binary search, finds a value in range which meets the given ;TI"<condition in O(log n) where n is the size of the range.;To:RDoc::Markup::BlankLine o; ; [I"GYou can use this method in two use cases: a find-minimum mode and ;TI"Ia find-any mode. In either case, the elements of the range must be ;TI"4monotone (or sorted) with respect to the block.;T@o; ; [I"HIn find-minimum mode (this is a good choice for typical use case), ;TI"Fthe block must return true or false, and there must be a value x ;TI" so that:;T@o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o; ; [I"Dthe block returns false for any value which is less than x, and;To;;0; [o; ; [I"Cthe block returns true for any value which is greater than or ;TI"equal to x.;T@o; ; [I"@If x is within the range, this method returns the value x. ;TI"Otherwise, it returns nil.;T@o:RDoc::Markup::Verbatim; [I"ary = [0, 4, 7, 10, 12] ;TI"5(0...ary.size).bsearch {|i| ary[i] >= 4 } #=> 1 ;TI"5(0...ary.size).bsearch {|i| ary[i] >= 6 } #=> 2 ;TI"5(0...ary.size).bsearch {|i| ary[i] >= 8 } #=> 3 ;TI"9(0...ary.size).bsearch {|i| ary[i] >= 100 } #=> nil ;TI" ;TI"E(0.0...Float::INFINITY).bsearch {|x| Math.log(x) >= 0 } #=> 1.0 ;T:@format0o; ; [I"GIn find-any mode (this behaves like libc's bsearch(3)), the block ;TI"Imust return a number, and there must be two values x and y (x <= y) ;TI" so that:;T@o;; ;;[o;;0; [o; ; [I"8the block returns a positive number for v if v < x,;To;;0; [o; ; [I"4the block returns zero for v if x <= v < y, and;To;;0; [o; ; [I"9the block returns a negative number for v if y <= v.;T@o; ; [I"GThis method returns any value which is within the intersection of ;TI"Dthe given range and x...y (if any). If there is no value that ;TI"-satisfies the condition, it returns nil.;T@o;; [ I"#ary = [0, 100, 100, 100, 200] ;TI"6(0..4).bsearch {|i| 100 - ary[i] } #=> 1, 2 or 3 ;TI"0(0..4).bsearch {|i| 300 - ary[i] } #=> nil ;TI"0(0..4).bsearch {|i| 50 - ary[i] } #=> nil ;T;0o; ; [I"EYou must not mix the two modes at a time; the block must always ;TI"Areturn either true/false, or always return a number. It is ;TI"Cundefined which value is actually picked up at each iteration.;T: @fileI"range.c;T:0@omit_headings_from_table_of_contents_below0I"*rng.bsearch {|obj| block } -> value ;T0[ I"();T@ZFI" Range;TcRDoc::NormalClass00