Search Algorithm

The Fossil Algorithm Search module provides a flexible, runtime-configurable search interface for arrays of arbitrary data types. By identifying both the element type and the desired search strategy through string identifiers, the API supports linear, binary, jump, interpolation, exponential, and Fibonacci searches in ascending or descending order. The module automatically selects compactors based on the declared type and resolves algorithm behavior dynamically, enabling consistent searching across numeric, floating-point, and string data.

HEADER REFERENCE #

#include "fossil/algorithm/search.h"
#include <iostream>
#include <vector>

using fossil::algorithm::Search;

int main() {
    std::vector<double> data = { 0.5, 1.2, 3.8, 7.4, 9.9 };
    double key = 7.4;

    int idx = Search::exec(
        data.data(),
        data.size(),
        &key,
        "f64",
        "binary",
        "asc"
    );

    if (idx >= 0)
        std::cout << "Found at index " << idx << "\n";
    else
        std::cout << "Error: " << idx << "\n";

    std::cout << "f64 size = " << Search::type_sizeof("f64") << "\n";
    std::cout << "supports i32? "
              << (Search::type_supported("i32") ? "yes" : "no") << "\n";

    return 0;
}

SAMPLE CODE C #

#include "fossil/algorithm/search.h"
#include <iostream>
#include <vector>

using fossil::algorithm::Search;

int main() {
    std::vector<double> data = { 0.5, 1.2, 3.8, 7.4, 9.9 };
    double key = 7.4;

    int idx = Search::exec(
        data.data(),
        data.size(),
        &key,
        "f64",
        "binary",
        "asc"
    );

    if (idx >= 0)
        std::cout << "Found at index " << idx << "\n";
    else
        std::cout << "Error: " << idx << "\n";

    std::cout << "f64 size = " << Search::type_sizeof("f64") << "\n";
    std::cout << "supports i32? "
              << (Search::type_supported("i32") ? "yes" : "no") << "\n";

    return 0;
}

SAMPLE CODE C++ #

#include "fossil/algorithm/search.h"
#include <iostream>
#include <vector>

using fossil::algorithm::Search;

int main() {
    std::vector<double> data = { 0.5, 1.2, 3.8, 7.4, 9.9 };
    double key = 7.4;

    int idx = Search::exec(
        data.data(),
        data.size(),
        &key,
        "f64",
        "binary",
        "asc"
    );

    if (idx >= 0)
        std::cout << "Found at index " << idx << "\n";
    else
        std::cout << "Error: " << idx << "\n";

    std::cout << "f64 size = " << Search::type_sizeof("f64") << "\n";
    std::cout << "supports i32? "
              << (Search::type_supported("i32") ? "yes" : "no") << "\n";

    return 0;
}

What are your feelings

Updated on December 4, 2025