|
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Attribute Look-Up-Table |
|
15 // The Attribute Lookup Table maintains a global set of unique IDs to be defined for document |
|
16 // node attributes and language tags. It does this by generating a unique ID for any descriptor |
|
17 // passed to it. This descriptor is in fact a pointer to a copy of the string. Once a string |
|
18 // has been seen once, any further references to it will map back to the same ID (pointer) |
|
19 // value. |
|
20 // |
|
21 // |
|
22 |
|
23 // system includes |
|
24 // - |
|
25 #include <e32base.h> |
|
26 #include <e32test.h> |
|
27 |
|
28 // our include |
|
29 // |
|
30 #include <attrlut.h> |
|
31 |
|
32 // Hash table size |
|
33 // |
|
34 #define KHashModulo 32 |
|
35 |
|
36 |
|
37 //============================================================== |
|
38 // CAttributeLookupTableNode |
|
39 //============================================================== |
|
40 |
|
41 // Internal node class |
|
42 // |
|
43 //##ModelId=3B666737014E |
|
44 class CAttributeLookupTableNode : public CBase |
|
45 { |
|
46 public: |
|
47 //##ModelId=3B6667370176 |
|
48 CAttributeLookupTableNode() : CBase() |
|
49 { |
|
50 //iDes = 0; |
|
51 } |
|
52 //##ModelId=3B666737016C |
|
53 ~CAttributeLookupTableNode() |
|
54 { |
|
55 delete iDes; |
|
56 } |
|
57 //##ModelId=3B6667370165 |
|
58 HBufC* iDes; |
|
59 }; |
|
60 |
|
61 |
|
62 //============================================================== |
|
63 // CAttributeLookupTable |
|
64 //============================================================== |
|
65 |
|
66 CAttributeLookupTable::CAttributeLookupTable() : CBase() |
|
67 { |
|
68 } |
|
69 |
|
70 /** Destructor. |
|
71 */ |
|
72 EXPORT_C CAttributeLookupTable::~CAttributeLookupTable() |
|
73 { |
|
74 Reset(); |
|
75 delete iList; |
|
76 } |
|
77 |
|
78 void CAttributeLookupTable::ConstructL() |
|
79 { |
|
80 iList = new (ELeave) CArrayPtrFlat<CArrayPtrSeg<CAttributeLookupTableNode> >( KHashModulo ); |
|
81 iList->ResizeL( KHashModulo ); |
|
82 for ( TInt i = 0; i < KHashModulo; i++ ) |
|
83 { |
|
84 (*iList)[i] = NULL; |
|
85 } |
|
86 } |
|
87 |
|
88 /** Allocates and constructs a new attribute lookup table. |
|
89 |
|
90 @return New attribute lookup table |
|
91 */ |
|
92 EXPORT_C CAttributeLookupTable* CAttributeLookupTable::NewL() |
|
93 { |
|
94 CAttributeLookupTable* iLUT = new (ELeave) CAttributeLookupTable(); |
|
95 CleanupStack::PushL( iLUT ); |
|
96 iLUT->ConstructL(); |
|
97 CleanupStack::Pop(); |
|
98 return iLUT; |
|
99 } |
|
100 |
|
101 |
|
102 // Lookup with allocating |
|
103 // |
|
104 /** Gets a unique ID for a specified descriptor. |
|
105 |
|
106 If this is the first time the descriptor has been used, a new ID will be allocated. |
|
107 |
|
108 @return ID |
|
109 @param aAttributeName Descriptor to get ID for |
|
110 */ |
|
111 EXPORT_C const HBufC* CAttributeLookupTable::Des2IDL( const TDesC& aAttributeName ) |
|
112 { |
|
113 // lookup the attribute |
|
114 CAttributeLookupTableNode* node = FindDes( aAttributeName ); |
|
115 if ( node ) |
|
116 return node->iDes; |
|
117 |
|
118 // create a new node at the end of the appropriate array |
|
119 CAttributeLookupTableNode* newnode = new (ELeave) CAttributeLookupTableNode(); |
|
120 CleanupStack::PushL( newnode ); |
|
121 newnode->iDes = aAttributeName.AllocL(); |
|
122 |
|
123 TInt hash = Hash( aAttributeName ); |
|
124 CArrayPtrSeg<CAttributeLookupTableNode>* list = iList->At( hash ); |
|
125 if ( !list ) |
|
126 list = iList->At( hash ) = new (ELeave) CArrayPtrSeg<CAttributeLookupTableNode>( 2 ); |
|
127 |
|
128 list->AppendL(newnode); |
|
129 |
|
130 CleanupStack::Pop(); // newnode |
|
131 |
|
132 return newnode->iDes; |
|
133 } |
|
134 |
|
135 |
|
136 // Lookup without allocating |
|
137 // |
|
138 /** Tests if an ID has been allocated for a specified descriptor. |
|
139 |
|
140 @return ID, or NULL if none has been allocated |
|
141 @param aKey Descriptor to test |
|
142 */ |
|
143 EXPORT_C const HBufC* CAttributeLookupTable::KeyExists( const TDesC& aKey ) |
|
144 { |
|
145 CAttributeLookupTableNode* node = FindDes( aKey ); |
|
146 if ( node ) |
|
147 return node->iDes; |
|
148 return NULL; // 0 is an invalid ID |
|
149 } |
|
150 |
|
151 |
|
152 // Find the given descriptor in the hash table |
|
153 // |
|
154 CAttributeLookupTableNode* CAttributeLookupTable::FindDes( const TDesC& aAttributeName ) |
|
155 { |
|
156 CArrayPtrSeg<CAttributeLookupTableNode>* list = iList->At( Hash( aAttributeName ) ); |
|
157 if ( list ) |
|
158 { |
|
159 TInt length=aAttributeName.Length(); |
|
160 TInt count = list->Count(); |
|
161 for ( TInt i = 0; i < count; i++ ) |
|
162 { |
|
163 CAttributeLookupTableNode* node = list->At(i); |
|
164 if ( node->iDes->Length()==length && node->iDes->Compare( aAttributeName ) == 0 ) // ** CASE SENSITIVE ** |
|
165 return node; |
|
166 } |
|
167 } |
|
168 return NULL; |
|
169 } |
|
170 |
|
171 |
|
172 // Generate a hash value |
|
173 // |
|
174 TUint CAttributeLookupTable::Hash( const TDesC& aDes ) |
|
175 { |
|
176 // *** CASE SENSITIVE ** |
|
177 TInt len=aDes.Length(); |
|
178 TUint hash = 0; |
|
179 const TText* ptr=aDes.Ptr(); |
|
180 for ( TInt i = 0; i < len; i++ ) |
|
181 hash = 131*hash + *ptr++; |
|
182 return hash % KHashModulo; |
|
183 } |
|
184 |
|
185 |
|
186 // Empty the table |
|
187 // |
|
188 /** Clears all allocated IDs. |
|
189 */ |
|
190 EXPORT_C void CAttributeLookupTable::Reset() |
|
191 { |
|
192 if ( iList ) |
|
193 { |
|
194 for ( TInt i = 0; i < iList->Count(); i++ ) |
|
195 { |
|
196 CArrayPtrSeg<CAttributeLookupTableNode>* list = iList->At( i ); |
|
197 if ( list ) |
|
198 { |
|
199 list->ResetAndDestroy(); |
|
200 delete list; |
|
201 iList->At( i ) = 0; |
|
202 } |
|
203 } |
|
204 } |
|
205 } |
|
206 |
|
207 |
|
208 // Debug hook |
|
209 // |
|
210 #if defined (_DEBUG) |
|
211 EXPORT_C void CAttributeLookupTable::Debug(TDebugSelectorType aSelector, TAny* aParam1, TAny* /*aParam2*/) |
|
212 { |
|
213 switch(aSelector) |
|
214 { |
|
215 case EPrint: |
|
216 { |
|
217 RTest* console = REINTERPRET_CAST(RTest*,aParam1); |
|
218 for ( TInt i = 0; i < iList->Count(); i++ ) |
|
219 { |
|
220 if ( i == 0 || (i%15) == 0 ) |
|
221 console->Printf( _L("\n%d: "), i ); |
|
222 TInt count = 0; |
|
223 CArrayPtrSeg<CAttributeLookupTableNode>* list = iList->At(i); |
|
224 if ( list ) |
|
225 count = list->Count(); |
|
226 console->Printf( _L("%d\t"), count ); |
|
227 } |
|
228 console->Printf( _L("\n") ); |
|
229 break; |
|
230 } |
|
231 default: |
|
232 break; |
|
233 } |
|
234 } |
|
235 #else |
|
236 EXPORT_C void CAttributeLookupTable::Debug(TDebugSelectorType /*aSelector*/, TAny* /*aParam1*/, TAny* /*aParam2*/) |
|
237 { |
|
238 } |
|
239 #endif |