Skip to content
Snippets Groups Projects
Commit ed884801 authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

Remove binarySearch().

Equivalent STL call is std::binary_search(array.begin(), array.end());
parent ed7ca05a
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*
* \file binarySearch.cpp
*
* Created on 2010-09-07 by Thomas Fischer
*
*/
#include "binarySearch.h"
namespace BaseLib {
size_t searchElement (double const& val, size_t beg, size_t end, const std::vector<double>& array)
{
if (beg >= end) return std::numeric_limits<size_t>::max();
size_t m ((end+beg)/2);
if (array[m] - val < 0 && array[m+1] - val > 0) {
return m;
}
if (val < array[m]) {
return searchElement (val, beg, m, array);
}
return searchElement (val, m+1, end, array);
}
} // end namespace BaseLib
/**
* Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*
* \file binarySearch.h
*
* Created on 2010-06-07 by Thomas Fischer
*
*/
// STL
#include <limits>
#include <vector>
#include <cstddef>
#ifndef BINARYSEARCH_H_
#define BINARYSEARCH_H_
namespace BaseLib {
/**
* Binary search in a sorted vector of elements to get the
* id of an element according its key.
* @param key the key for the element
* @param beg beginning index in the sorted vector of elements
* @param end ending index in the sorted vector of elements
* @param array the vector of elements
* @return the id of the element in the vector or, if not found,
* the value std::numeric_limits<std::size_t>::max()
*/
template <class T>
std::size_t searchElement (const T& key, std::size_t beg, std::size_t end, const std::vector<T>& array)
{
if (beg >= end) return std::numeric_limits<std::size_t>::max();
std::size_t m ((end+beg)/2);
if (key == array[m]) {
return m;
}
if (key < array[m]) {
return searchElement (key, beg, m, array);
}
return searchElement (key, m+1, end, array);
}
std::size_t searchElement (double const& val, std::size_t beg, std::size_t end, const std::vector<double>& array);
} // end namespace BaseLib
#endif /* BINARYSEARCH_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment