<commit_before>/*
 *  Copyright 2008-2011 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


/*! \file binary_search.h
 *  \brief C++ implementation of binary search algorithms.
 */

#pragma once

#include <thrust/advance.h>
#include <thrust/distance.h>
#include <thrust/iterator/iterator_traits.h>

#include <thrust/binary_search.h>
#include <thrust/detail/backend/dereference.h>
#include <thrust/system/cpp/detail/tag.h>

// TODO replace the code below with calls to thrust::detail::backend::generic::scalar::*
//      when warnings about __host__ calling __host__ __device__ are silenceable

namespace thrust
{
namespace system
{
namespace cpp
{
namespace detail
{

template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
ForwardIterator lower_bound(tag,
                            ForwardIterator first,
                            ForwardIterator last,
                            const T& val,
                            StrictWeakOrdering comp)
{
  typedef typename thrust::iterator_difference<ForwardIterator>::type difference_type;

  difference_type len = thrust::distance(first, last);

  while(len > 0)
  {
    difference_type half = len >> 1;
    ForwardIterator middle = first;

    thrust::advance(middle, half);

    if(comp(thrust::detail::backend::dereference(middle), val))
    {
      first = middle;
      ++first;
      len = len - half - 1;
    }
    else
    {
      len = half;
    }
  }

  return first;
}


template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
ForwardIterator upper_bound(tag,
                            ForwardIterator first,
                            ForwardIterator last,
                            const T& val, 
                            StrictWeakOrdering comp)
{
  typedef typename thrust::iterator_difference<ForwardIterator>::type difference_type;

  difference_type len = thrust::distance(first, last);

  while(len > 0)
  {
    difference_type half = len >> 1;
    ForwardIterator middle = first;

    thrust::advance(middle, half);

    if(comp(val, thrust::detail::backend::dereference(middle)))
    {
      len = half;
    }
    else
    {
      first = middle;
      ++first;
      len = len - half - 1;
    }
  }

  return first;
}

template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
bool binary_search(tag,
                   ForwardIterator first,
                   ForwardIterator last,
                   const T& val, 
                   StrictWeakOrdering comp)
{
  ForwardIterator iter = thrust::lower_bound(first,last,val,comp);
  return iter != last && !comp(val, thrust::detail::backend::dereference(iter));
}

} // end namespace detail
} // end namespace cpp
} // end namespace system
} // end namespace thrust
<commit_msg>Eliminate thrust::detail::backend::dereference from cpp::detail::lower_bound, cpp::detail::upper_bound, and cpp::detail::binary_search Use host_wrapped_binary_function inside cpp::detail::lower_bound, cpp::detail::upper_bound, and cpp::detail::binary_search<commit_after>/*
 *  Copyright 2008-2011 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


/*! \file binary_search.h
 *  \brief C++ implementation of binary search algorithms.
 */

#pragma once

#include <thrust/advance.h>
#include <thrust/distance.h>
#include <thrust/iterator/iterator_traits.h>

#include <thrust/binary_search.h>
#include <thrust/detail/wrapped_function.h>
#include <thrust/system/cpp/detail/tag.h>

// TODO replace the code below with calls to thrust::detail::backend::generic::scalar::*
//      when warnings about __host__ calling __host__ __device__ are silenceable

namespace thrust
{
namespace system
{
namespace cpp
{
namespace detail
{

template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
ForwardIterator lower_bound(tag,
                            ForwardIterator first,
                            ForwardIterator last,
                            const T& val,
                            StrictWeakOrdering comp)
{
  typedef typename thrust::iterator_difference<ForwardIterator>::type difference_type;

  // wrap comp
  thrust::detail::host_wrapped_binary_function<
    StrictWeakOrdering,
    typename thrust::iterator_reference<ForwardIterator>::type,
    const T&,
    bool
  > wrapped_comp(comp);

  difference_type len = thrust::distance(first, last);

  while(len > 0)
  {
    difference_type half = len >> 1;
    ForwardIterator middle = first;

    thrust::advance(middle, half);

    if(wrapped_comp(*middle, val))
    {
      first = middle;
      ++first;
      len = len - half - 1;
    }
    else
    {
      len = half;
    }
  }

  return first;
}


template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
ForwardIterator upper_bound(tag,
                            ForwardIterator first,
                            ForwardIterator last,
                            const T& val, 
                            StrictWeakOrdering comp)
{
  typedef typename thrust::iterator_difference<ForwardIterator>::type difference_type;

  // wrap comp
  thrust::detail::host_wrapped_binary_function<
    StrictWeakOrdering,
    const T&,
    typename thrust::iterator_reference<ForwardIterator>::type,
    bool
  > wrapped_comp(comp);

  difference_type len = thrust::distance(first, last);

  while(len > 0)
  {
    difference_type half = len >> 1;
    ForwardIterator middle = first;

    thrust::advance(middle, half);

    if(wrapped_comp(val, *middle))
    {
      len = half;
    }
    else
    {
      first = middle;
      ++first;
      len = len - half - 1;
    }
  }

  return first;
}

template <typename ForwardIterator, typename T, typename StrictWeakOrdering>
bool binary_search(tag,
                   ForwardIterator first,
                   ForwardIterator last,
                   const T& val, 
                   StrictWeakOrdering comp)
{
  ForwardIterator iter = thrust::lower_bound(first,last,val,comp);

  // wrap comp
  thrust::detail::host_wrapped_binary_function<
    StrictWeakOrdering,
    const T&,
    typename thrust::iterator_reference<ForwardIterator>::type,
    bool
  > wrapped_comp(comp);

  return iter != last && !wrapped_comp(val, *iter);
}

} // end namespace detail
} // end namespace cpp
} // end namespace system
} // end namespace thrust

{'fraction_non_alphanumeric': 0.09417344173441734, 'fraction_numerical': 0.004878048780487805, 'mean_word_length': 2.8066013408973696, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6633785', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2013, 'n_words': 587}