00001 /* 00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 00003 00004 Redistribution and use in source and binary forms, with or without 00005 modification, are permitted provided that the following conditions are met: 00006 00007 * Redistributions of source code must retain the above copyright notice, this 00008 list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright notice, 00010 this list of conditions and the following disclaimer in the documentation 00011 and/or other materials provided with the distribution. 00012 * Neither the name of Nokia Corporation nor the names of its contributors 00013 may be used to endorse or promote products derived from this software 00014 without specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00027 Description: 00028 */ 00029 #include "SortingExamples.h" 00030 #include "SharedCode.h" 00031 #include <e32test.h> 00032 00033 extern CConsoleBase* console; 00034 00035 // --------------------------------------------------------- 00036 // void SortExistingArrayL() 00037 // Sorts a prepopulated array using the supplied ordering 00038 // function 00039 // --------------------------------------------------------- 00040 // 00041 LOCAL_C void SortExistingArrayL() 00042 { 00043 TExampleStruct* values = GenerateValuesLC(); 00044 00045 RExampleArray array(sizeof(TExampleStruct), values, KMaxElements); // First we sort the array 00046 CleanupStack::Pop(values); 00047 CleanupClosePushL(array); 00048 00049 // now sort the array by the first Key 00050 TLinearOrder<TExampleStruct> ordering(TExampleStruct::RelativeAOrdering); 00051 array.Sort(ordering); 00052 00053 // now verify that we are sorted correctly in order 00054 for (TInt i = 0; i < array.Count()-1; i++) 00055 { 00056 if (TExampleStruct::RelativeAOrdering(array[i], array[i+1]) > 0) 00057 User::Leave(KErrGeneral); 00058 } 00059 00060 CleanupStack::PopAndDestroy(&array); 00061 } 00062 00063 00064 // --------------------------------------------------------- 00065 // void SortExistingArrayUsingKeyOffsetsL() 00066 // This example sorts the array of random values 00067 // using the 2nd field of the structure. 00068 // This uses the offset functions of RArray 00069 // --------------------------------------------------------- 00070 // 00071 LOCAL_C void SortExistingArrayUsingKeyOffsetsL() 00072 { 00073 TExampleStruct* values = GenerateValuesLC(); 00074 00075 RArray<TExampleStruct> array(KDefaultGranularity, _FOFF(TExampleStruct, iB)); 00076 CleanupClosePushL(array); 00077 00078 // Fill the array but not in any order 00079 for (TInt i=0; i < KMaxElements; i++) 00080 User::LeaveIfError(array.Append(values[i])); 00081 00082 // now sort it based on the iB key of TExampleStruct 00083 array.SortSigned(); 00084 for (TInt i=0; i < array.Count()-1; i++) 00085 { 00086 // Should be sorted in ascending order 00087 // with duplicates based on the iB field. 00088 if (array[i].iB > array[i+1].iB) 00089 User::Leave(KErrGeneral); 00090 } 00091 00092 CleanupStack::PopAndDestroy(2, values); 00093 } 00094 00095 // --------------------------------------------------------- 00096 // void SortingExamplesL() 00097 // Holdds the sorting functions 00098 // --------------------------------------------------------- 00099 // 00100 void SortingExamplesL() 00101 { 00102 console->Printf(_L("Sort an existing array\n")); 00103 SortExistingArrayL(); 00104 User::After(0); 00105 00106 console->Printf(_L("Sort an existing array using a field offset\n")); 00107 SortExistingArrayUsingKeyOffsetsL(); 00108 User::After(0); 00109 } 00110