widgetmodel/alfwidgetmodel/src/alfsort.cpp
branchRCL_3
changeset 26 0e9bb658ef58
equal deleted inserted replaced
25:4ea6f81c838a 26:0e9bb658ef58
       
     1 /*
       
     2 * Copyright (c) 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:  sorting routine. uses Symbian services: User::QuickSort
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //INCLUDES
       
    20 #include <e32std.h>
       
    21 #include <alf/ialfmap.h>
       
    22 #include <alf/alfvarianttype.h>
       
    23 #include <alf/ialfsortfunction.h>
       
    24 //#include "alf/alfperf.h"
       
    25 #include "alfsort.h"
       
    26 
       
    27 using namespace Alf;
       
    28 
       
    29 /**
       
    30 *  Defines the characteristics of a key used
       
    31 *  to access the elements of an array. Used for sort.
       
    32 *  @since S60 ?S60_version
       
    33 */
       
    34 NONSHARABLE_CLASS(AlfSortKey) : public TKey
       
    35     {
       
    36 public:
       
    37 
       
    38     /**
       
    39      * constructor
       
    40      *
       
    41      * @since S60 ?S60_version
       
    42      * @param aSortFunction callback interface for sort.
       
    43      * @param aArr array to sort.
       
    44      * @param aCount. count of items in the array.
       
    45      */
       
    46     AlfSortKey( const IAlfSortFunction& aSortFunction,
       
    47     IAlfVariantType** aArr, uint aCount );
       
    48 
       
    49     /**
       
    50      * returns data from array
       
    51      *
       
    52      * @since S60 ?S60_version
       
    53      * @param anIndex index to element to return
       
    54      * @return element at index anIndex.
       
    55      */
       
    56     TAny *At(TInt anIndex) const;
       
    57 
       
    58     /**
       
    59      * compares two items.
       
    60      *
       
    61      * @since S60 ?S60_version
       
    62      * @param aLeft index to the array for left parameter.
       
    63      * @return the value returned from sort function:
       
    64      *  < 0, if aFirst is less than aSecond,
       
    65      *  > 0 if aFirst is more than aSecond,
       
    66      *  0, if aFirst equals aSecond.
       
    67      */
       
    68     TInt Compare(TInt aLeft, TInt aRight) const;
       
    69 private:
       
    70 
       
    71     //sort function not owned.
       
    72     const IAlfSortFunction& iSortFunction;
       
    73 
       
    74     //array to sort. not owned
       
    75     IAlfVariantType** iArr;
       
    76 
       
    77     //count of items in array.
       
    78     uint iCount;
       
    79     };
       
    80 
       
    81 /**
       
    82 *  Defines the basic behaviour for swapping two elements of an array.
       
    83 *  Used for sort
       
    84 *  @since S60 ?S60_version
       
    85 */
       
    86 NONSHARABLE_CLASS(AlfSortSwap) : public TSwap
       
    87     {
       
    88 public:
       
    89 
       
    90     /**
       
    91      * constructor
       
    92      *
       
    93      * @since S60 ?S60_version
       
    94      * @param aArr array, which elements are to be swapped.
       
    95      * @param aCount. count of items in the array.
       
    96      */
       
    97     AlfSortSwap( IAlfVariantType** aArr, uint aCount );
       
    98 
       
    99     /**
       
   100      * swaps two items in the array.
       
   101      *
       
   102      * @since S60 ?S60_version
       
   103      * @param aLeft  left index
       
   104      * @param aRight right index.
       
   105      */
       
   106     void Swap(TInt aLeft, TInt aRight) const;
       
   107 private:
       
   108 
       
   109     //array, which elements are to be swapped. Not owned.
       
   110     IAlfVariantType** iArr;
       
   111 
       
   112     //count of items in the array.
       
   113     uint iCount;
       
   114     };
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Description : constructor
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 AlfSortKey::AlfSortKey( const IAlfSortFunction& aSortFunction,
       
   121                         IAlfVariantType** aArr, uint aCount ) :
       
   122         iSortFunction(aSortFunction), iArr(aArr), iCount(aCount)
       
   123     {
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // Description : return element from index anIndex.
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 TAny *AlfSortKey::At(TInt anIndex) const
       
   131     {
       
   132     return iArr[anIndex];
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // Description : compares two items.
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 TInt AlfSortKey::Compare(TInt aLeft, TInt aRight) const
       
   140     {
       
   141     const IAlfMap* leftMap = iArr[aLeft]->map();
       
   142     const IAlfMap* rightMap = iArr[aRight]->map();
       
   143     return iSortFunction.compareLeafs( leftMap, rightMap );
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // Description : constructor
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 AlfSortSwap::AlfSortSwap( IAlfVariantType** aArr, uint aCount ) :
       
   151         iArr(aArr), iCount(aCount)
       
   152     {
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // Description : swaps two items
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 void AlfSortSwap::Swap(TInt aLeft, TInt aRight) const
       
   160     {
       
   161     IAlfVariantType* tmp = iArr[aLeft];
       
   162     iArr[aLeft] = iArr[aRight];
       
   163     iArr[aRight] = tmp;
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // Description : The sorting routine.
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 void AlfSort::sort(IAlfVariantType** aArr, uint aCount,
       
   171                    const IAlfSortFunction& aSortFunction )
       
   172     {
       
   173     //ALF_PERF_START( perfdata, "AlfSort-Sort-Sorting")
       
   174     AlfSortKey key(aSortFunction, aArr, aCount);
       
   175     AlfSortSwap swap(aArr, aCount);
       
   176     User::QuickSort( aCount, key, swap );
       
   177     //ALF_PERF_STOP( perfdata, "AlfSort-Sort-Sorting")
       
   178     }
       
   179