Files
Aerofoil/PortabilityLayer/BinarySearch.h
Diomendius 1b18a87495 Normalize line endings
This commit contains only the result of `git add --renormalize .`

`git show --ignore-space-change` can verify that this commit only
changes whitespace.
2024-07-31 20:21:47 +12:00

38 lines
919 B
C++

#pragma once
#ifndef __PL_BINARY_SEARCH_H__
#define __PL_BINARY_SEARCH_H__
#include <stdint.h>
#include <stddef.h>
namespace PortabilityLayer
{
template<class TIterator, class TItem, class TPredicate>
TIterator BinarySearch(const TIterator &startInclusive, const TIterator &endExclusive, const TItem &item, const TPredicate &pred)
{
TIterator searchStartInclusive = startInclusive;
TIterator searchEndExclusive = endExclusive;
while (searchStartInclusive != searchEndExclusive)
{
const ptrdiff_t delta = searchEndExclusive - searchStartInclusive;
const ptrdiff_t halfDelta = delta / 2;
const TIterator midPoint = searchStartInclusive + halfDelta;
const int comparison = pred(item, *midPoint);
if (comparison < 0)
searchEndExclusive = midPoint;
else if (comparison > 0)
searchStartInclusive = midPoint + 1;
else
return midPoint;
}
return endExclusive;
}
}
#endif