buildlang 1.15.0

BuildLang (buildc): an effects-oriented compiler with a verified C path, shader output, experimental backends, opt-in linear types, and re-checkable accountability + scientific-runtime receipts
Documentation
// BuildLang Standard Library - Algorithms Module
// Sorting and searching algorithms using Vec runtime.

/// Check if a value is in a sorted range using binary search logic.
/// Returns the index if found, -1 if not.
fn binary_search_vec(data: Vec<i32>, target: i32) -> i32 {
    let len = vec_len(data);
    let mut low: i32 = 0;
    let mut high = len - 1;
    while low <= high {
        let mid = low + (high - low) / 2;
        let val = vec_get(data, mid);
        if val == target {
            return mid;
        }
        if val < target {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    -1
}

/// Sum all elements in a Vec<i32>.
fn vec_sum(data: Vec<i32>) -> i32 {
    let len = vec_len(data);
    let mut total: i32 = 0;
    let mut i: i32 = 0;
    while i < len {
        total = total + vec_get(data, i);
        i = i + 1;
    }
    total
}

/// Find the minimum value in a Vec<i32>. Returns 0 for empty.
fn vec_min(data: Vec<i32>) -> i32 {
    let len = vec_len(data);
    if len == 0 { return 0; }
    let mut result = vec_get(data, 0);
    let mut i: i32 = 1;
    while i < len {
        let val = vec_get(data, i);
        if val < result { result = val; }
        i = i + 1;
    }
    result
}

/// Find the maximum value in a Vec<i32>. Returns 0 for empty.
fn vec_max(data: Vec<i32>) -> i32 {
    let len = vec_len(data);
    if len == 0 { return 0; }
    let mut result = vec_get(data, 0);
    let mut i: i32 = 1;
    while i < len {
        let val = vec_get(data, i);
        if val > result { result = val; }
        i = i + 1;
    }
    result
}

/// Count elements matching a predicate value.
fn vec_count(data: Vec<i32>, target: i32) -> i32 {
    let len = vec_len(data);
    let mut count: i32 = 0;
    let mut i: i32 = 0;
    while i < len {
        if vec_get(data, i) == target { count = count + 1; }
        i = i + 1;
    }
    count
}

/// Check if a value exists in a Vec.
fn vec_contains(data: Vec<i32>, target: i32) -> bool {
    let len = vec_len(data);
    let mut i: i32 = 0;
    while i < len {
        if vec_get(data, i) == target { return true; }
        i = i + 1;
    }
    false
}