Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:23

0001 //  Boost string_algo library classification.hpp header file  ---------------------------//
0002 
0003 //  Copyright Pavol Droba 2002-2003.
0004 // 
0005 // Distributed under the Boost Software License, Version 1.0.
0006 //    (See accompanying file LICENSE_1_0.txt or copy at
0007 //          http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 //  See http://www.boost.org/ for updates, documentation, and revision history.
0010 
0011 #ifndef BOOST_STRING_CLASSIFICATION_DETAIL_HPP
0012 #define BOOST_STRING_CLASSIFICATION_DETAIL_HPP
0013 
0014 #include <boost/algorithm/string/config.hpp>
0015 #include <algorithm>
0016 #include <cstring>
0017 #include <functional>
0018 #include <locale>
0019 
0020 #include <boost/range/begin.hpp>
0021 #include <boost/range/distance.hpp>
0022 #include <boost/range/end.hpp>
0023 
0024 #include <boost/algorithm/string/predicate_facade.hpp>
0025 #include <boost/type_traits/remove_const.hpp>
0026 
0027 namespace boost {
0028     namespace algorithm {
0029         namespace detail {
0030 
0031 //  classification functors -----------------------------------------------//
0032 
0033    // is_classified functor
0034             struct is_classifiedF :
0035                 public predicate_facade<is_classifiedF>
0036             {
0037                 // Boost.ResultOf support
0038                 typedef bool result_type;
0039 
0040                 // Constructor from a locale
0041                 is_classifiedF(std::ctype_base::mask Type, std::locale const & Loc = std::locale()) :
0042                     m_Type(Type), m_Locale(Loc) {}
0043                 // Operation
0044                 template<typename CharT>
0045                 bool operator()( CharT Ch ) const
0046                 {
0047                     return std::use_facet< std::ctype<CharT> >(m_Locale).is( m_Type, Ch );
0048                 }
0049 
0050                 #if defined(BOOST_BORLANDC) && (BOOST_BORLANDC >= 0x560) && (BOOST_BORLANDC <= 0x582) && !defined(_USE_OLD_RW_STL)
0051                     template<>
0052                     bool operator()( char const Ch ) const
0053                     {
0054                         return std::use_facet< std::ctype<char> >(m_Locale).is( m_Type, Ch );
0055                     }
0056                 #endif
0057 
0058             private:
0059                 std::ctype_base::mask m_Type;
0060                 std::locale m_Locale;
0061             };
0062 
0063 
0064             // is_any_of functor
0065             /*
0066                 returns true if the value is from the specified set
0067             */
0068             template<typename CharT>
0069             struct is_any_ofF :
0070                 public predicate_facade<is_any_ofF<CharT> >
0071             {
0072             private:
0073                 // set cannot operate on const value-type
0074                 typedef typename ::boost::remove_const<CharT>::type set_value_type;
0075 
0076             public:     
0077                 // Boost.ResultOf support
0078                 typedef bool result_type;
0079 
0080                 // Constructor
0081                 template<typename RangeT>
0082                 is_any_ofF( const RangeT& Range ) : m_Size(0)
0083                 {
0084                     // Prepare storage
0085                     m_Storage.m_dynSet=0;
0086 
0087                     std::size_t Size=::boost::distance(Range);
0088                     m_Size=Size;
0089                     set_value_type* Storage=0;
0090 
0091                     if(use_fixed_storage(m_Size))
0092                     {
0093                         // Use fixed storage
0094                         Storage=&m_Storage.m_fixSet[0];
0095                     }
0096                     else
0097                     {
0098                         // Use dynamic storage
0099                         m_Storage.m_dynSet=new set_value_type[m_Size];
0100                         Storage=m_Storage.m_dynSet;
0101                     }
0102 
0103                     // Use fixed storage
0104                     ::std::copy(::boost::begin(Range), ::boost::end(Range), Storage);
0105                     ::std::sort(Storage, Storage+m_Size);
0106                 }
0107 
0108                 // Copy constructor
0109                 is_any_ofF(const is_any_ofF& Other) : m_Size(Other.m_Size)
0110                 {
0111                     // Prepare storage
0112                     m_Storage.m_dynSet=0;               
0113                     const set_value_type* SrcStorage=0;
0114                     set_value_type* DestStorage=0;
0115 
0116                     if(use_fixed_storage(m_Size))
0117                     {
0118                         // Use fixed storage
0119                         DestStorage=&m_Storage.m_fixSet[0];
0120                         SrcStorage=&Other.m_Storage.m_fixSet[0];
0121                     }
0122                     else
0123                     {
0124                         // Use dynamic storage
0125                         m_Storage.m_dynSet=new set_value_type[m_Size];
0126                         DestStorage=m_Storage.m_dynSet;
0127                         SrcStorage=Other.m_Storage.m_dynSet;
0128                     }
0129 
0130                     // Use fixed storage
0131                     ::std::memcpy(DestStorage, SrcStorage, sizeof(set_value_type)*m_Size);
0132                 }
0133 
0134                 // Destructor
0135                 ~is_any_ofF()
0136                 {
0137                     if(!use_fixed_storage(m_Size) && m_Storage.m_dynSet!=0)
0138                     {
0139                         delete [] m_Storage.m_dynSet;
0140                     }
0141                 }
0142 
0143                 // Assignment
0144                 is_any_ofF& operator=(const is_any_ofF& Other)
0145                 {
0146                     // Handle self assignment
0147                     if(this==&Other) return *this;
0148 
0149                     // Prepare storage             
0150                     const set_value_type* SrcStorage;
0151                     set_value_type* DestStorage;
0152 
0153                     if(use_fixed_storage(Other.m_Size))
0154                     {
0155                         // Use fixed storage
0156                         DestStorage=&m_Storage.m_fixSet[0];
0157                         SrcStorage=&Other.m_Storage.m_fixSet[0];
0158 
0159                         // Delete old storage if was present
0160                         if(!use_fixed_storage(m_Size) && m_Storage.m_dynSet!=0)
0161                         {
0162                             delete [] m_Storage.m_dynSet;
0163                         }
0164 
0165                         // Set new size
0166                         m_Size=Other.m_Size;
0167                     }
0168                     else
0169                     {
0170                         // Other uses dynamic storage
0171                         SrcStorage=Other.m_Storage.m_dynSet;
0172 
0173                         // Check what kind of storage are we using right now
0174                         if(use_fixed_storage(m_Size))
0175                         {
0176                             // Using fixed storage, allocate new
0177                             set_value_type* pTemp=new set_value_type[Other.m_Size];
0178                             DestStorage=pTemp;
0179                             m_Storage.m_dynSet=pTemp;
0180                             m_Size=Other.m_Size;
0181                         }
0182                         else
0183                         {
0184                             // Using dynamic storage, check if can reuse
0185                             if(m_Storage.m_dynSet!=0 && m_Size>=Other.m_Size && m_Size<Other.m_Size*2)
0186                             {
0187                                 // Reuse the current storage
0188                                 DestStorage=m_Storage.m_dynSet;
0189                                 m_Size=Other.m_Size;
0190                             }
0191                             else
0192                             {
0193                                 // Allocate the new one
0194                                 set_value_type* pTemp=new set_value_type[Other.m_Size];
0195                                 DestStorage=pTemp;
0196                         
0197                                 // Delete old storage if necessary
0198                                 if(m_Storage.m_dynSet!=0)
0199                                 {
0200                                     delete [] m_Storage.m_dynSet;
0201                                 }
0202                                 // Store the new storage
0203                                 m_Storage.m_dynSet=pTemp;
0204                                 // Set new size
0205                                 m_Size=Other.m_Size;
0206                             }
0207                         }
0208                     }
0209 
0210                     // Copy the data
0211                     ::std::memcpy(DestStorage, SrcStorage, sizeof(set_value_type)*m_Size);
0212 
0213                     return *this;
0214                 }
0215 
0216                 // Operation
0217                 template<typename Char2T>
0218                 bool operator()( Char2T Ch ) const
0219                 {
0220                     const set_value_type* Storage=
0221                         (use_fixed_storage(m_Size))
0222                         ? &m_Storage.m_fixSet[0]
0223                         : m_Storage.m_dynSet;
0224 
0225                     return ::std::binary_search(Storage, Storage+m_Size, Ch);
0226                 }
0227             private:
0228                 // check if the size is eligible for fixed storage
0229                 static bool use_fixed_storage(std::size_t size)
0230                 {
0231                     return size<=sizeof(set_value_type*)*2;
0232                 }
0233 
0234 
0235             private:
0236                 // storage
0237                 // The actual used storage is selected on the type
0238                 union
0239                 {
0240                     set_value_type* m_dynSet;
0241                     set_value_type m_fixSet[sizeof(set_value_type*)*2];
0242                 } 
0243                 m_Storage;
0244         
0245                 // storage size
0246                 ::std::size_t m_Size;
0247             };
0248 
0249             // is_from_range functor
0250             /*
0251                 returns true if the value is from the specified range.
0252                 (i.e. x>=From && x>=To)
0253             */
0254             template<typename CharT>
0255             struct is_from_rangeF :
0256                 public predicate_facade< is_from_rangeF<CharT> >
0257             {
0258                 // Boost.ResultOf support
0259                 typedef bool result_type;
0260 
0261                 // Constructor
0262                 is_from_rangeF( CharT From, CharT To ) : m_From(From), m_To(To) {}
0263 
0264                 // Operation
0265                 template<typename Char2T>
0266                 bool operator()( Char2T Ch ) const
0267                 {
0268                     return ( m_From <= Ch ) && ( Ch <= m_To );
0269                 }
0270 
0271             private:
0272                 CharT m_From;
0273                 CharT m_To;
0274             };
0275 
0276             // class_and composition predicate
0277             template<typename Pred1T, typename Pred2T>
0278             struct pred_andF :
0279                 public predicate_facade< pred_andF<Pred1T,Pred2T> >
0280             {
0281             public:
0282 
0283                 // Boost.ResultOf support
0284                 typedef bool result_type;
0285 
0286                 // Constructor
0287                 pred_andF( Pred1T Pred1, Pred2T Pred2 ) :
0288                     m_Pred1(Pred1), m_Pred2(Pred2) {}
0289 
0290                 // Operation
0291                 template<typename CharT>
0292                 bool operator()( CharT Ch ) const
0293                 {
0294                     return m_Pred1(Ch) && m_Pred2(Ch);
0295                 }
0296 
0297             private:
0298                 Pred1T m_Pred1;
0299                 Pred2T m_Pred2;
0300             };
0301 
0302             // class_or composition predicate
0303             template<typename Pred1T, typename Pred2T>
0304             struct pred_orF :
0305                 public predicate_facade< pred_orF<Pred1T,Pred2T> >
0306             {
0307             public:
0308                 // Boost.ResultOf support
0309                 typedef bool result_type;
0310 
0311                 // Constructor
0312                 pred_orF( Pred1T Pred1, Pred2T Pred2 ) :
0313                     m_Pred1(Pred1), m_Pred2(Pred2) {}
0314 
0315                 // Operation
0316                 template<typename CharT>
0317                 bool operator()( CharT Ch ) const
0318                 {
0319                     return m_Pred1(Ch) || m_Pred2(Ch);
0320                 }
0321 
0322             private:
0323                 Pred1T m_Pred1;
0324                 Pred2T m_Pred2;
0325             };
0326 
0327             // class_not composition predicate
0328             template< typename PredT >
0329             struct pred_notF :
0330                 public predicate_facade< pred_notF<PredT> >
0331             {
0332             public:
0333                 // Boost.ResultOf support
0334                 typedef bool result_type;
0335 
0336                 // Constructor
0337                 pred_notF( PredT Pred ) : m_Pred(Pred) {}
0338 
0339                 // Operation
0340                 template<typename CharT>
0341                 bool operator()( CharT Ch ) const
0342                 {
0343                     return !m_Pred(Ch);
0344                 }
0345 
0346             private:
0347                 PredT m_Pred;
0348             };
0349 
0350         } // namespace detail
0351     } // namespace algorithm
0352 } // namespace boost
0353 
0354 
0355 #endif  // BOOST_STRING_CLASSIFICATION_DETAIL_HPP