git.delta.rocks / jrsonnet / refs/commits / a05afd32cebc

difftreelog

source

crates/jrsonnet-stdlib/src/sets.rs738 Bsourcehistory
1use std::cmp::Ordering;23use jrsonnet_evaluator::{4	error::Result,5	function::{builtin, FuncVal},6	operator::evaluate_compare_op,7	val::ArrValue,8	Val,9};10use jrsonnet_parser::BinaryOpType;1112#[builtin]13#[allow(non_snake_case)]14pub fn builtin_set_member(x: Val, arr: ArrValue, keyF: Option<FuncVal>) -> Result<bool> {15	let mut low = 0;16	let mut high = arr.len();1718	let keyF = keyF.unwrap_or(FuncVal::Id).into_native::<((Val,), Val)>();1920	let x = keyF(x)?;2122	while low < high {23		let middle = (high + low) / 2;24		match evaluate_compare_op(&arr.get(middle)?.expect("in bounds"), &x, BinaryOpType::Lt)? {25			Ordering::Less => low = middle + 1,26			Ordering::Equal => return Ok(true),27			Ordering::Greater => high = middle,28		}29	}30	Ok(false)31}