1#ifndef TERNARY_SEARCH_H
2#define TERNARY_SEARCH_H
30template <
typename Iter,
typename T> Iter ternary_search(Iter first, Iter last, T value) {
31 while (first < last) {
32 typedef typename std::iterator_traits<Iter>::difference_type diff_t;
33 diff_t diff = std::distance(first, last) / 3;
34 Iter mid1 = first + diff;
35 Iter mid2 = mid1 + diff;
39 else if (*mid2 == value)
44 else if (value > *mid2)
70template <
typename Container,
typename T> int64_t ternary_search(Container& c,
const T& value) {
71 auto it = ternary_search(c.begin(), c.end(), value);
72 if (it != c.end() && *it == value)
73 return static_cast<int64_t
>(std::distance(c.begin(), it));