00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "InsertandFindExamples.h"
00030 #include "SharedCode.h"
00031 #include <e32test.h>
00032
00033 extern CConsoleBase* console;
00034
00035
00036
00037
00038
00039
00040
00041 LOCAL_C void InsertionUsingKeyOffsetsL()
00042 {
00043 TExampleStruct* values = GenerateValuesLC();
00044
00045
00046
00047 RArray<TExampleStruct> array(KDefaultGranularity, _FOFF(TExampleStruct, iB));
00048 CleanupClosePushL(array);
00049
00050
00051
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
00060 for (TInt i=0; i < KMaxElements; i++)
00061 {
00062 User::LeaveIfError(array.FindInSignedKeyOrder(values[i]));
00063 }
00064
00065
00066
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
00078
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
00091
00092
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
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
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
00120
00121
00122
00123 LOCAL_C void FindUsingLinearSearchL()
00124 {
00125 TExampleStruct* values = GenerateValuesLC();
00126
00127
00128
00129
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
00141 for (TInt i=0; i< KMaxElements; i++)
00142 User::LeaveIfError(array.Append(values[i]));
00143
00144
00145 const TIdentityRelation<TExampleStruct> relation(TExampleStruct::IsEqual);
00146 User::LeaveIfError(array.Find(s, relation));
00147
00148 CleanupStack::PopAndDestroy(2, values);
00149 }
00150
00151
00152
00153
00154
00155
00156 LOCAL_C void FindUsingLinearOffsetL()
00157 {
00158 TExampleStruct* values = GenerateValuesLC();
00159
00160
00161
00162
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
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
00186
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
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
00218
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