finder.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 647 行 · 第 1/2 页
HPP
647 行
// Boost string_algo library finder.hpp header file ---------------------------//// Copyright Pavol Droba 2002-2006.//// Distributed under the Boost Software License, Version 1.0.// (See accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)// See http://www.boost.org/ for updates, documentation, and revision history.#ifndef BOOST_STRING_FINDER_DETAIL_HPP#define BOOST_STRING_FINDER_DETAIL_HPP#include <boost/algorithm/string/config.hpp>#include <boost/algorithm/string/constants.hpp>#include <boost/detail/iterator.hpp>#include <boost/range/iterator_range.hpp>#include <boost/range/begin.hpp>#include <boost/range/end.hpp>#include <boost/range/empty.hpp>#include <boost/range/as_literal.hpp>namespace boost { namespace algorithm { namespace detail {// find first functor -----------------------------------------------// // find a subsequence in the sequence ( functor ) /* Returns a pair <begin,end> marking the subsequence in the sequence. If the find fails, functor returns <End,End> */ template<typename SearchIteratorT,typename PredicateT> struct first_finderF { typedef SearchIteratorT search_iterator_type; // Construction template< typename SearchT > first_finderF( const SearchT& Search, PredicateT Comp ) : m_Search(::boost::begin(Search), ::boost::end(Search)), m_Comp(Comp) {} first_finderF( search_iterator_type SearchBegin, search_iterator_type SearchEnd, PredicateT Comp ) : m_Search(SearchBegin, SearchEnd), m_Comp(Comp) {} // Operation template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> operator()( ForwardIteratorT Begin, ForwardIteratorT End ) const { typedef iterator_range<ForwardIteratorT> result_type; typedef ForwardIteratorT input_iterator_type; // Outer loop for(input_iterator_type OuterIt=Begin; OuterIt!=End; ++OuterIt) { // Sanity check if( boost::empty(m_Search) ) return result_type( End, End ); input_iterator_type InnerIt=OuterIt; search_iterator_type SubstrIt=m_Search.begin(); for(; InnerIt!=End && SubstrIt!=m_Search.end(); ++InnerIt,++SubstrIt) { if( !( m_Comp(*InnerIt,*SubstrIt) ) ) break; } // Substring matching succeeded if ( SubstrIt==m_Search.end() ) return result_type( OuterIt, InnerIt ); } return result_type( End, End ); } private: iterator_range<search_iterator_type> m_Search; PredicateT m_Comp; };// find last functor -----------------------------------------------// // find the last match a subseqeunce in the sequence ( functor ) /* Returns a pair <begin,end> marking the subsequence in the sequence. If the find fails, returns <End,End> */ template<typename SearchIteratorT, typename PredicateT> struct last_finderF { typedef SearchIteratorT search_iterator_type; typedef first_finderF< search_iterator_type, PredicateT> first_finder_type; // Construction template< typename SearchT > last_finderF( const SearchT& Search, PredicateT Comp ) : m_Search(::boost::begin(Search), ::boost::end(Search)), m_Comp(Comp) {} last_finderF( search_iterator_type SearchBegin, search_iterator_type SearchEnd, PredicateT Comp ) : m_Search(SearchBegin, SearchEnd), m_Comp(Comp) {} // Operation template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> operator()( ForwardIteratorT Begin, ForwardIteratorT End ) const { typedef iterator_range<ForwardIteratorT> result_type; if( boost::empty(m_Search) ) return result_type( End, End ); typedef BOOST_STRING_TYPENAME boost::detail:: iterator_traits<ForwardIteratorT>::iterator_category category; return findit( Begin, End, category() ); } private: // forward iterator template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> findit( ForwardIteratorT Begin, ForwardIteratorT End, std::forward_iterator_tag ) const { typedef ForwardIteratorT input_iterator_type; typedef iterator_range<ForwardIteratorT> result_type; first_finder_type first_finder( m_Search.begin(), m_Search.end(), m_Comp ); result_type M=first_finder( Begin, End ); result_type Last=M; while( M ) { Last=M; M=first_finder( ::boost::end(M), End ); } return Last; } // bidirectional iterator template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> findit( ForwardIteratorT Begin, ForwardIteratorT End, std::bidirectional_iterator_tag ) const { typedef iterator_range<ForwardIteratorT> result_type; typedef ForwardIteratorT input_iterator_type; // Outer loop for(input_iterator_type OuterIt=End; OuterIt!=Begin; ) { input_iterator_type OuterIt2=--OuterIt; input_iterator_type InnerIt=OuterIt2; search_iterator_type SubstrIt=m_Search.begin(); for(; InnerIt!=End && SubstrIt!=m_Search.end(); ++InnerIt,++SubstrIt) { if( !( m_Comp(*InnerIt,*SubstrIt) ) ) break; } // Substring matching succeeded if( SubstrIt==m_Search.end() ) return result_type( OuterIt2, InnerIt ); } return result_type( End, End ); } private: iterator_range<search_iterator_type> m_Search; PredicateT m_Comp; };// find n-th functor -----------------------------------------------// // find the n-th match of a subsequence in the sequence ( functor ) /* Returns a pair <begin,end> marking the subsequence in the sequence. If the find fails, returns <End,End> */ template<typename SearchIteratorT, typename PredicateT> struct nth_finderF { typedef SearchIteratorT search_iterator_type; typedef first_finderF< search_iterator_type, PredicateT> first_finder_type; typedef last_finderF< search_iterator_type, PredicateT> last_finder_type; // Construction template< typename SearchT > nth_finderF( const SearchT& Search, int Nth, PredicateT Comp) : m_Search(::boost::begin(Search), ::boost::end(Search)), m_Nth(Nth), m_Comp(Comp) {} nth_finderF( search_iterator_type SearchBegin, search_iterator_type SearchEnd, int Nth, PredicateT Comp) : m_Search(SearchBegin, SearchEnd), m_Nth(Nth), m_Comp(Comp) {} // Operation template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> operator()( ForwardIteratorT Begin, ForwardIteratorT End ) const { if(m_Nth>=0) { return find_forward(Begin, End, m_Nth); } else { return find_backward(Begin, End, -m_Nth); } } private: // Implementation helpers template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> find_forward( ForwardIteratorT Begin, ForwardIteratorT End, unsigned int N) const { typedef ForwardIteratorT input_iterator_type; typedef iterator_range<ForwardIteratorT> result_type; // Sanity check if( boost::empty(m_Search) ) return result_type( End, End ); // Instantiate find functor first_finder_type first_finder( m_Search.begin(), m_Search.end(), m_Comp ); result_type M( Begin, Begin ); for( unsigned int n=0; n<=N; ++n ) { // find next match M=first_finder( ::boost::end(M), End ); if ( !M ) { // Subsequence not found, return return M; } } return M; } template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> find_backward( ForwardIteratorT Begin, ForwardIteratorT End, unsigned int N) const { typedef ForwardIteratorT input_iterator_type; typedef iterator_range<ForwardIteratorT> result_type; // Sanity check if( boost::empty(m_Search) ) return result_type( End, End ); // Instantiate find functor last_finder_type last_finder( m_Search.begin(), m_Search.end(), m_Comp ); result_type M( End, End ); for( unsigned int n=1; n<=N; ++n ) { // find next match M=last_finder( Begin, ::boost::begin(M) ); if ( !M ) { // Subsequence not found, return return M; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?