hotspotfw/hsserver/inc/algorithm.h
changeset 19 10810c91db26
parent 3 ff3b37722600
child 22 498f36116140
equal deleted inserted replaced
3:ff3b37722600 19:10810c91db26
     1 /*
       
     2 * Copyright (c) 2002-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Algorithms for HotSpot FW   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #ifndef ALGORITHM_H
       
    24 #define ALGORITHM_H
       
    25 
       
    26 /**
       
    27 * global operator!= for type T
       
    28 * @param aLhs left hand side
       
    29 * @param aRhs right hand side
       
    30 * @return ETrue equal, EFalse not equal
       
    31 */
       
    32 template< class T >
       
    33 inline TBool operator!= (
       
    34     const T& aLhs,
       
    35     const T& aRhs)
       
    36     {
       
    37     return !( aLhs == aRhs );
       
    38     }
       
    39 
       
    40 /**
       
    41 * global operator > for type T
       
    42 * @param aLhs left hand side
       
    43 * @param aRhs right hand side
       
    44 * @return ETrue greater, EFalse else
       
    45 */
       
    46 template< class T >
       
    47 inline TBool operator > (
       
    48     const T& aLhs,
       
    49     const T& aRhs)
       
    50     {
       
    51     return ( aRhs < aLhs );
       
    52     }
       
    53 
       
    54 /**
       
    55 * global operator <= for type T
       
    56 * @param aLhs left hand side
       
    57 * @param aRhs right hand side
       
    58 * @return ETrue smaller or equal, EFalse else
       
    59 */
       
    60 template< class T >
       
    61 inline TBool operator <= (
       
    62     const T& aLhs,
       
    63     const T& aRhs)
       
    64     {
       
    65     return !( aRhs < aLhs );
       
    66     }
       
    67 
       
    68 /**
       
    69 * global operator >= for type T
       
    70 * @param aLhs left hand side
       
    71 * @param aRhs right hand side
       
    72 * @return ETrue greater or equal, EFalse else
       
    73 */
       
    74 template< class T >
       
    75 inline TBool operator >= (
       
    76     const T& aLhs,
       
    77     const T& aRhs)
       
    78     {
       
    79     return !( aLhs < aRhs );
       
    80     }
       
    81 
       
    82 /**
       
    83 * Just like equal in C++ STL for testing equality for T type
       
    84 * Checks weather elements in the range [aBeg, aEnd) are equal 
       
    85 * to the elements in the range starting with aCmpBeg
       
    86 * Complexity: linear
       
    87 * @param aBeg begin of the search range
       
    88 * @param aEnd end ( one past last element ) of the search range
       
    89 * @param aCmpBeg begin of the range to be compared with
       
    90 * @return ETrue equal, EFalse not equal
       
    91 */
       
    92 template< class T >
       
    93 inline TBool equal( 
       
    94     const T* aBeg, 
       
    95     const T*const aEnd, 
       
    96     const T* aCmpBeg)
       
    97     {
       
    98     while ( aBeg != aEnd )
       
    99         {
       
   100         if ( *aBeg != *aCmpBeg )
       
   101             {
       
   102             return EFalse;
       
   103             }
       
   104         ++aBeg;
       
   105         ++aCmpBeg;
       
   106         }
       
   107 
       
   108     return ETrue;
       
   109     }
       
   110 
       
   111 /**
       
   112 * Just like find in C++ STL
       
   113 * Returns the position of the first element in the range [aBeg, aEnd)
       
   114 * that has a value equal to aValue
       
   115 * Complexity: linear
       
   116 * @param aBeg begin of range
       
   117 * @param aEnd  end ( one past last element ) of the range
       
   118 * @param aValue value to be searched
       
   119 * @return the position of the first element in the range (aBeg, aEnd]
       
   120 * that has a value equal to aValue. aEnd is returned if no matching 
       
   121 * element is found
       
   122 */
       
   123 template< class T, class J >
       
   124 inline T* find(
       
   125     const T* aBeg,
       
   126     const T* const aEnd,
       
   127     const J& aValue)
       
   128     {
       
   129     while ( aBeg != aEnd )
       
   130         {
       
   131         if ( *aBeg == aValue )
       
   132             {
       
   133             break;
       
   134             }
       
   135 
       
   136         ++aBeg;
       
   137         }
       
   138 
       
   139     return const_cast<T*>(aBeg);
       
   140     }
       
   141 
       
   142 /**
       
   143 * Just like find_if in C++ STL
       
   144 * Returns the position of the first element in the range [aBeg, aEnd)
       
   145 * for which the unary predicate op(elem) yields true
       
   146 * Complexity: linear
       
   147 * @param aBeg begin of range
       
   148 * @param aEnd  end ( one past last element ) of the range
       
   149 * @param aUnaryPredicate a unary predicate
       
   150 * @return the position of the first element in the range (aBeg, aEnd]
       
   151 * for which the unary predicate op(elem) yields true. 
       
   152 * aEnd is returned if no matching element is found
       
   153 * NOTE: aUnaryPredicate should not change its state during a function call
       
   154 */
       
   155 template< class T, class J >
       
   156 inline T* find_if(
       
   157     const T* aBeg,
       
   158     const T* const aEnd,
       
   159     const J& aUnaryPredicate)
       
   160     {
       
   161     while ( aBeg != aEnd )
       
   162         {
       
   163         if ( aUnaryPredicate( *aBeg ) )
       
   164             {
       
   165             break;
       
   166             }
       
   167 
       
   168         ++aBeg;
       
   169         }
       
   170 
       
   171     return const_cast<T*>(aBeg);
       
   172     }
       
   173 
       
   174 /**
       
   175 * Just like fill in C++ STL for T type
       
   176 * Assigns aValue to each element in the range [aBeg, aEnd)
       
   177 * The caller must ensure that the destination range is big enough 
       
   178 * Complexity: linear
       
   179 * @param aBeg begin of range
       
   180 * @param aEnd  end ( one past last element ) of the range
       
   181 * @param aValue value to be assigned
       
   182 */
       
   183 template< class T, class J>
       
   184 inline void fill(
       
   185     T* aBeg,
       
   186     const T* const aEnd,
       
   187     const J& aValue)
       
   188     {
       
   189     while ( aBeg != aEnd )
       
   190         {
       
   191         *aBeg = aValue;
       
   192         ++aBeg;
       
   193         }
       
   194     }
       
   195 
       
   196 /**
       
   197 * Just like fill_n in C++ STL for T type
       
   198 * Assigns aValue to the first aNum elements in the range starting with aBeg
       
   199 * The caller must ensure that the destination range is big enough 
       
   200 * Complexity: linear
       
   201 * @param aBeg begin of range
       
   202 * @param aNum number of elements to be processed
       
   203 * @param aValue value to be assigned
       
   204 */
       
   205 template< class T, class J >
       
   206 inline void fill_n(
       
   207     T* aBeg,
       
   208     TUint32 aNum,
       
   209     const J& aValue)
       
   210     {
       
   211     while ( aNum )
       
   212         {
       
   213         *aBeg = aValue;
       
   214         ++aBeg;
       
   215         --aNum;
       
   216         }
       
   217     }
       
   218 
       
   219 /**
       
   220 * Just like copy in C++ STL 
       
   221 * @param aSrc begin of copy
       
   222 * @param aSrcEnd end of copy
       
   223 * @param aDest target of copy
       
   224 * @return aDest
       
   225 */
       
   226 template<class T>
       
   227 inline T* copy(
       
   228     const T* aSrc,
       
   229     const T* aSrcEnd,
       
   230     T* aDest)
       
   231     {
       
   232     while ( aSrc != aSrcEnd )
       
   233         {
       
   234         *aDest = *aSrc;
       
   235         ++aSrc;
       
   236         ++aDest;
       
   237         }
       
   238 
       
   239     return aDest;
       
   240     }
       
   241 
       
   242 template<class T>
       
   243 inline T* reverse_copy(
       
   244     const T* aSrcBeg,
       
   245     const T* aSrcEnd,
       
   246     T* aDest)
       
   247     {
       
   248     --aSrcEnd;
       
   249     while ( aSrcEnd >= aSrcBeg )
       
   250         {        
       
   251         *aDest = *aSrcEnd;
       
   252         --aSrcEnd;
       
   253         ++aDest;
       
   254         }
       
   255 
       
   256     return aDest;
       
   257     }
       
   258 
       
   259 /**
       
   260 * Does a looped copy for T type
       
   261 * @param aSource source of the copy
       
   262 * @param aDest destination where to copy
       
   263 * @param aCount number of iterations
       
   264 * @return aDest
       
   265 */
       
   266 template<class T>
       
   267 inline TAny* assign(
       
   268     const T* aSource,
       
   269     T* aDest,
       
   270     const TInt aCount)
       
   271     {
       
   272     TAny* origdest = static_cast<TAny*>(aDest);
       
   273     for ( TInt idx = 0 ; idx != aCount ; ++idx, ++aSource, ++aDest )
       
   274         {
       
   275         *aDest = *aSource;
       
   276         }
       
   277 
       
   278     return origdest;
       
   279     }
       
   280 
       
   281 #endif      // ALGORITHM_H
       
   282             
       
   283 // End of File