<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * 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.
 */
#pragma once
#ifndef ZORBA_STL_UTIL_H
#define ZORBA_STL_UTIL_H

#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <iterator>
#include <limits>
#include <set>
#include <stack>

#include <zorba/config.h>
#include <zorba/internal/ztd.h>

#include "cxx_util.h"

namespace zorba {
namespace ztd {

using internal::ztd::destroy_delete;
using internal::ztd::has_insertion_operator;
using internal::ztd::less;

///////////////////////////////////////////////////////////////////////////////

/**
 * A less-verbose way to iterate over a constant sequence.
 */
#define FOR_EACH(TYPE,IT,SEQ) \
  for ( TYPE::const_iterator IT = (SEQ).begin(); IT != (SEQ).end(); ++IT )

/**
 * A less-verbose way to iterate over a mutable sequence.
 */
#define MUTATE_EACH(TYPE,IT,SEQ) \
  for ( TYPE::iterator IT = (SEQ).begin(); IT != (SEQ).end(); ++IT )

///////////////////////////////////////////////////////////////////////////////

/**
 * A version of std::back_insert_iterator that's suitable as a base-class (the
 * standard one is not).
 *
 * @tparam ContainerType The type of the container to insert into.
 * @tparam DerivedType The type of the class deriving from this.
 */
template<class ContainerType,class DerivedType>
class back_insert_iterator_base :
  public std::iterator<std::output_iterator_tag,void,void,void,void> {
public:
  typedef ContainerType container_type;

  DerivedType& operator*() {
    return *static_cast<DerivedType*>( this );
  }

  DerivedType& operator++() {
    return *static_cast<DerivedType*>( this );
  }

  DerivedType& operator++(int) {
    return *static_cast<DerivedType*>( this );
  }

protected:
  back_insert_iterator_base( ContainerType &c ) : container( &c ) {
  }

  /**
   * A pointer is used (rather than a reference) so this class can be copy
   * constructed.
   */
  ContainerType *container;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * Implementation of SGI's %identity extension.
 * See: http://www.sgi.com/tech/stl/identity.html
 */
template<typename T>
struct identity : std::unary_function<T,T> {
  T const& operator()( T const &a ) const {
    return a;
  }
};

/**
 * Implementation of SGI's %select1st extension.
 * See: http://www.sgi.com/tech/stl/select1st.html
 */
template<typename PairType>
struct select1st : std::unary_function<PairType,typename PairType::first_type> {
  typename PairType::first_type const& operator()( PairType const &a ) const {
    return a.first;
  }
};

///////////////////////////////////////////////////////////////////////////////

/**
 * Determines whether the given type \c T is efficiently passed by value.
 */
template<typename T>
struct is_passable_by_value {
  static bool const value =
        ZORBA_TR1_NS::is_arithmetic<T>::value
    ||  ZORBA_TR1_NS::is_enum<T>::value
    ||  ZORBA_TR1_NS::is_pointer<T>::value
    ||  ZORBA_TR1_NS::is_reference<T>::value
    ||  ZORBA_TR1_NS::is_member_function_pointer<T>::value;
};

/**
 * Useful traits when declaring functions.
 * This class is similar to boost::call_traits.
 *
 * @tparam T A type that is used for a function formal argument or return type.
 */
template<typename T,bool = is_passable_by_value<T>::value>
struct call_traits {
  /**
   * A type that is guaranteed to be the most efficient to use as a formal
   * argument.
   */
  typedef T const arg_type;
};

/**
 * Partial specialization for when \c T is not efficiently passed by value.
 */
template<typename T>
struct call_traits<T,false> {
  typedef T const& arg_type;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * A less-verbose way to determine whether the given set<T> contains a
 * particular element.
 */
template<typename T> inline
bool contains( std::set<T> const &s, typename call_traits<T>::arg_type v ) {
  return s.find( v ) != s.end();
}

/**<commit_msg>Made contains also work for maps.<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * 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.
 */
#pragma once
#ifndef ZORBA_STL_UTIL_H
#define ZORBA_STL_UTIL_H

#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <iterator>
#include <limits>
#include <set>
#include <stack>

#include <zorba/config.h>
#include <zorba/internal/ztd.h>

#include "cxx_util.h"

namespace zorba {
namespace ztd {

using internal::ztd::destroy_delete;
using internal::ztd::has_insertion_operator;
using internal::ztd::less;

///////////////////////////////////////////////////////////////////////////////

/**
 * A less-verbose way to iterate over a constant sequence.
 */
#define FOR_EACH(TYPE,IT,SEQ) \
  for ( TYPE::const_iterator IT = (SEQ).begin(); IT != (SEQ).end(); ++IT )

/**
 * A less-verbose way to iterate over a mutable sequence.
 */
#define MUTATE_EACH(TYPE,IT,SEQ) \
  for ( TYPE::iterator IT = (SEQ).begin(); IT != (SEQ).end(); ++IT )

///////////////////////////////////////////////////////////////////////////////

/**
 * A version of std::back_insert_iterator that's suitable as a base-class (the
 * standard one is not).
 *
 * @tparam ContainerType The type of the container to insert into.
 * @tparam DerivedType The type of the class deriving from this.
 */
template<class ContainerType,class DerivedType>
class back_insert_iterator_base :
  public std::iterator<std::output_iterator_tag,void,void,void,void> {
public:
  typedef ContainerType container_type;

  DerivedType& operator*() {
    return *static_cast<DerivedType*>( this );
  }

  DerivedType& operator++() {
    return *static_cast<DerivedType*>( this );
  }

  DerivedType& operator++(int) {
    return *static_cast<DerivedType*>( this );
  }

protected:
  back_insert_iterator_base( ContainerType &c ) : container( &c ) {
  }

  /**
   * A pointer is used (rather than a reference) so this class can be copy
   * constructed.
   */
  ContainerType *container;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * Implementation of SGI's %identity extension.
 * See: http://www.sgi.com/tech/stl/identity.html
 */
template<typename T>
struct identity : std::unary_function<T,T> {
  T const& operator()( T const &a ) const {
    return a;
  }
};

/**
 * Implementation of SGI's %select1st extension.
 * See: http://www.sgi.com/tech/stl/select1st.html
 */
template<typename PairType>
struct select1st : std::unary_function<PairType,typename PairType::first_type> {
  typename PairType::first_type const& operator()( PairType const &a ) const {
    return a.first;
  }
};

///////////////////////////////////////////////////////////////////////////////

/**
 * Determines whether the given type \c T is efficiently passed by value.
 */
template<typename T>
struct is_passable_by_value {
  static bool const value =
        ZORBA_TR1_NS::is_arithmetic<T>::value
    ||  ZORBA_TR1_NS::is_enum<T>::value
    ||  ZORBA_TR1_NS::is_pointer<T>::value
    ||  ZORBA_TR1_NS::is_reference<T>::value
    ||  ZORBA_TR1_NS::is_member_function_pointer<T>::value;
};

/**
 * Useful traits when declaring functions.
 * This class is similar to boost::call_traits.
 *
 * @tparam T A type that is used for a function formal argument or return type.
 */
template<typename T,bool = is_passable_by_value<T>::value>
struct call_traits {
  /**
   * A type that is guaranteed to be the most efficient to use as a formal
   * argument.
   */
  typedef T const arg_type;
};

/**
 * Partial specialization for when \c T is not efficiently passed by value.
 */
template<typename T>
struct call_traits<T,false> {
  typedef T const& arg_type;
};

///////////////////////////////////////////////////////////////////////////////

/**
 * A less-verbose way to determine whether the given map or set contains a
 * particular element.
 */
template<class ContainerType> inline
bool contains(
    ContainerType const &s,
    typename call_traits<typename ContainerType::key_type>::arg_type v ) {
  return s.find( v ) != s.end();
}

/**
{'fraction_non_alphanumeric': 0.19787093942054435, 'fraction_numerical': 0.004389815627743635, 'mean_word_length': 2.771937086092715, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '766999', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2651, 'n_words': 826}