examples/SFExamples/Example_RArray/InsertAndFindExamples.cpp

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 "InsertandFindExamples.h"
00030 #include "SharedCode.h"
00031 #include <e32test.h>
00032 
00033 extern CConsoleBase* console;
00034 
00035 // ---------------------------------------------------------
00036 // void InsertionUsingKeyOffsetsL()
00037 // Sorts the array by inserting each element in its correct 
00038 // position
00039 // ---------------------------------------------------------
00040 //
00041 LOCAL_C void InsertionUsingKeyOffsetsL()
00042         {
00043         TExampleStruct* values = GenerateValuesLC();
00044         
00045         // Construct an RArray and let it take ownership of the
00046         // block of random data
00047         RArray<TExampleStruct> array(KDefaultGranularity, _FOFF(TExampleStruct, iB));
00048         CleanupClosePushL(array);
00049         
00050         // Now insert the values
00051         // into the array
00052         for (TInt i=0; i < KMaxElements; i++)
00053                 {
00054                 const TInt err = array.InsertInSignedKeyOrder(values[i]);
00055                 if (err != KErrNone && err != KErrAlreadyExists)
00056                         User::Leave(err);
00057                 }
00058                 
00059         // Verify the elements can be found in the newly sorted array
00060         for (TInt i=0; i < KMaxElements; i++)
00061                 {
00062                 User::LeaveIfError(array.FindInSignedKeyOrder(values[i]));
00063                 }
00064         
00065         // Verify the items are correctly sorted
00066         // Note there are never any duplicates
00067         for (TInt i=0; i < array.Count()-1; i++)
00068                 {
00069                 if (array[i].iB >= array[i+1].iB)
00070                         User::Leave(KErrGeneral);
00071                 }
00072         
00073         CleanupStack::PopAndDestroy(2, values);
00074         }
00075 
00076 // ---------------------------------------------------------
00077 // void InsertionUsingFunctionL()
00078 // Sorts the array using the TLinearOrder functionality 
00079 // ---------------------------------------------------------
00080 //
00081 LOCAL_C void InsertionUsingFunctionL()
00082         {
00083         TExampleStruct* values = GenerateValuesLC();
00084 
00085         RExampleArray array(KDefaultGranularity);
00086         CleanupClosePushL(array);
00087 
00088         const TLinearOrder<TExampleStruct> order(TExampleStruct::RelativeAOrdering);
00089         
00090         // we construct an array by inserting the values into the array
00091         // in the case of duplicates we just ignore them which
00092         // means array.Count() may not be KMaxElements
00093         for (TInt i=0; i < KMaxElements; i++)
00094                 {
00095                 const TInt err = array.InsertInOrder(values[i], order);
00096                 if (err != KErrNone && err != KErrAlreadyExists)
00097                         User::Leave(err);
00098                 }
00099         
00100         // now verify each element of values can be found in the array
00101         for (TInt i = 0; i < KMaxElements; i++)
00102                 {
00103                 TInt err = array.FindInOrder(values[i], order);
00104                 if (err == KErrNotFound)
00105                         User::Leave(KErrNotFound);
00106                 }
00107         
00108         // now verify the array is sorted
00109         for (TInt i = 0; i < array.Count()-1; i++)
00110                 {
00111                 if (TExampleStruct::RelativeAOrdering(array[i], array[i+1]) > 0)
00112                         User::Leave(KErrGeneral);
00113                 }
00114 
00115         CleanupStack::PopAndDestroy(2, values);
00116         }
00117 
00118 // ---------------------------------------------------------
00119 // void FindUsingLinearSearchL()
00120 // Finds the items using a linear search (horribly inefficient) 
00121 // ---------------------------------------------------------
00122 //
00123 LOCAL_C void FindUsingLinearSearchL()
00124         {
00125         TExampleStruct* values = GenerateValuesLC();
00126 
00127         // We need to make sure our randomly generated
00128         // data does not contain duplicate copies of the
00129         // key
00130         const TExampleStruct& s = values[KSearchIndex];
00131         for (TInt i= KSearchIndex-1; i>=0; i--)
00132                 {
00133                 if (values[i].iA == s.iA)
00134                         values[i].iA = 0;
00135                 }       
00136 
00137         RExampleArray array;
00138         CleanupClosePushL(array);
00139         
00140         // Add the items to the array
00141         for (TInt i=0; i< KMaxElements; i++)
00142                 User::LeaveIfError(array.Append(values[i]));
00143         
00144         // now find the items in linear order
00145         const TIdentityRelation<TExampleStruct> relation(TExampleStruct::IsEqual);
00146         User::LeaveIfError(array.Find(s, relation));
00147         
00148         CleanupStack::PopAndDestroy(2, values);
00149         }
00150 
00151 // ---------------------------------------------------------
00152 // FindUsingLinearOffsetL
00153 // Finds the items using a linear search offset(horribly inefficient) 
00154 // ---------------------------------------------------------
00155 //
00156 LOCAL_C void FindUsingLinearOffsetL()
00157         {
00158         TExampleStruct* values = GenerateValuesLC();
00159 
00160         // We need to make sure our randomly generated
00161         // data does not contain duplicate copies of the
00162         // key
00163         const TExampleStruct& s = values[KSearchIndex];
00164         for (TInt i= KSearchIndex-1; i>=0; i--)
00165                 {
00166                 if (values[i].iB == s.iB)
00167                         values[i].iB = 0;
00168                 }       
00169         
00170         RExampleArray array(KDefaultGranularity, _FOFF(TExampleStruct, iB));
00171         CleanupClosePushL(array);
00172         
00173         for (TInt i=0; i < KMaxElements; i++)
00174                 User::LeaveIfError(array.Append(values[i]));
00175         
00176         // now find the items in offset
00177         const TInt index = array.Find(s);
00178         if (index != KSearchIndex)
00179                 User::Leave(KErrNotFound);
00180         
00181         CleanupStack::PopAndDestroy(2, values);
00182         }
00183 
00184 // ---------------------------------------------------------
00185 // void FindUsingAKeyL()
00186 // Finds an element using the key style functions 
00187 // ---------------------------------------------------------
00188 //
00189 LOCAL_C void FindUsingAKeyL()
00190         {
00191         TExampleStruct* values = GenerateValuesLC();
00192 
00193         RExampleArray array(KDefaultGranularity, _FOFF(TExampleStruct, iA));
00194         CleanupClosePushL(array);
00195         
00196         const TLinearOrder<TExampleStruct> order(TExampleStruct::RelativeAOrdering);
00197         for (TInt i=0; i < KMaxElements; i++)
00198                 {
00199                 const TInt err = array.InsertInOrder(values[i], order);
00200                 if (err != KErrNone && err != KErrAlreadyExists)
00201                         User::Leave(err);
00202                 }
00203         
00204         const TExampleStruct& s = array[KSearchIndex];
00205         
00206         // now find the item using the key function
00207         const TInt index = array.FindInOrder<TInt>(s.iA, TExampleStruct::KeyCompare);
00208         if (index != KSearchIndex)
00209                 {
00210                 User::Leave(KErrNotFound);
00211                 }
00212         
00213         CleanupStack::PopAndDestroy(2, values);
00214         }
00215 
00216 // ---------------------------------------------------------
00217 // void InsertAndFindExamplesL()
00218 // Inserting and finding examples
00219 // ---------------------------------------------------------
00220 //
00221 void InsertAndFindExamplesL()
00222         {
00223         console->Printf(_L("Insert using offset value\n"));
00224         InsertionUsingKeyOffsetsL();
00225         User::After(0);
00226 
00227         console->Printf(_L("Insert using Linear order function\n"));
00228         InsertionUsingFunctionL();
00229         User::After(0);
00230 
00231         console->Printf(_L("Find using offset value\n"));
00232         FindUsingLinearOffsetL();
00233         User::After(0);
00234         
00235         
00236         console->Printf(_L("Find using Linear order function\n"));
00237         FindUsingLinearSearchL();
00238         User::After(0);
00239 
00240         console->Printf(_L("Find using the key search function\n"));
00241         FindUsingAKeyL();
00242         User::After(0);
00243         }
00244 

Generated by  doxygen 1.6.2