SEARCH(3) Library Functions Manual SEARCH(3)

KS_binary_searchsearch utility functions

#include <libks/search.h>

type *
KS_binary_search(type *v, size_t n, int (*cmp)(type *haystack, needle), needle);

The search API provides functions for finding elements in data structures.

struct page {
	int id;
};

static int
page_cmp(const struct page *page, int id)
{
	if (page->id < id)
		return -1;
	if (page->id > id)
		return 1;
	return 0;
}

const struct page *
find_page_by_id(const struct page *pages, size_t n, int id)
{
	return KS_binary_search(pages, n, id);
}

Anton Lindqvist <anton@basename.se>

OpenBSD 7.8 August 28, 2025 SEARCH(3)