83
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2004 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: Iterates over a tree, depth first
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#ifndef XNDEPTHFIRSTTREEITERATOR_H
|
|
19 |
#define XNDEPTHFIRSTTREEITERATOR_H
|
|
20 |
|
|
21 |
// INCLUDES
|
|
22 |
#include <e32base.h>
|
|
23 |
#include "xnchilditerator.h"
|
|
24 |
|
|
25 |
// Constants
|
|
26 |
/**
|
|
27 |
* Queue granularity value
|
|
28 |
*/
|
|
29 |
const TInt KMemoryAlocS = 64;
|
|
30 |
|
|
31 |
|
|
32 |
template < class T >
|
|
33 |
class CXnDepthFirstTreeIterator: public CBase, public MXnChildIterator
|
|
34 |
{
|
|
35 |
public:
|
|
36 |
/**
|
|
37 |
* Two-phased constructor.
|
|
38 |
* @param aStartNode Object to iterate over
|
|
39 |
*/
|
|
40 |
static CXnDepthFirstTreeIterator< T >* NewL( T& aStartNode );
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Destructor.
|
|
44 |
*/
|
|
45 |
virtual ~CXnDepthFirstTreeIterator();
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Get the next iterator value.
|
|
49 |
* @return Next object or NULL if no more values.
|
|
50 |
*/
|
|
51 |
T* NextL();
|
|
52 |
|
|
53 |
/**
|
|
54 |
* Get the current iterator value.
|
|
55 |
* @return Current object
|
|
56 |
*/
|
|
57 |
T* Value();
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Get the previous iterator value. Obsolete!
|
|
61 |
* @return NULL
|
|
62 |
*/
|
|
63 |
T* PreviousL();
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Get the current iterator index. Obsolete!
|
|
67 |
* @return value 0
|
|
68 |
*/
|
|
69 |
TUint Index() const;
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Get the current iterator level in the tree.
|
|
73 |
* @return current level
|
|
74 |
*/
|
|
75 |
TUint Level() const;
|
|
76 |
|
|
77 |
protected:
|
|
78 |
|
|
79 |
CXnDepthFirstTreeIterator( T* aStartNode );
|
|
80 |
|
|
81 |
void ConstructL();
|
|
82 |
|
|
83 |
private:
|
|
84 |
//Stack
|
|
85 |
RPointerArray< T > iStack;
|
|
86 |
//Object to iterate over
|
|
87 |
T* iStart;
|
|
88 |
// Current object
|
|
89 |
T* iCurrent;
|
|
90 |
|
|
91 |
};
|
|
92 |
|
|
93 |
// -----------------------------------------------------------------------------
|
|
94 |
// CXnDepthFirstTreeIterator< T >::NewL()
|
|
95 |
// -----------------------------------------------------------------------------
|
|
96 |
//
|
|
97 |
template< class T > CXnDepthFirstTreeIterator< T >*
|
|
98 |
CXnDepthFirstTreeIterator<T>::NewL( T& aStartNode )
|
|
99 |
{
|
|
100 |
CXnDepthFirstTreeIterator<T>* self =
|
|
101 |
new ( ELeave )CXnDepthFirstTreeIterator< T >( &aStartNode );
|
|
102 |
CleanupStack::PushL( self );
|
|
103 |
self->ConstructL();
|
|
104 |
CleanupStack::Pop();
|
|
105 |
return self;
|
|
106 |
}
|
|
107 |
|
|
108 |
// -----------------------------------------------------------------------------
|
|
109 |
// CXnDepthFirstTreeIterator< T >::CXnDepthFirstTreeIterator()
|
|
110 |
// C++ default constructor
|
|
111 |
// -----------------------------------------------------------------------------
|
|
112 |
//
|
|
113 |
template< class T > CXnDepthFirstTreeIterator< T >::CXnDepthFirstTreeIterator(
|
|
114 |
T* aStartNode ):
|
|
115 |
iStart( aStartNode ),
|
|
116 |
iCurrent( aStartNode ),
|
|
117 |
iStack( KMemoryAlocS )
|
|
118 |
{
|
|
119 |
}
|
|
120 |
|
|
121 |
// -----------------------------------------------------------------------------
|
|
122 |
// CXnDepthFirstTreeIterator< T >::~CXnDepthFirstTreeIterator()
|
|
123 |
// C++ default destructor.
|
|
124 |
// -----------------------------------------------------------------------------
|
|
125 |
//
|
|
126 |
template< class T >
|
|
127 |
CXnDepthFirstTreeIterator< T >::~CXnDepthFirstTreeIterator()
|
|
128 |
{
|
|
129 |
iStack.Close();
|
|
130 |
}
|
|
131 |
|
|
132 |
// -----------------------------------------------------------------------------
|
|
133 |
// CXnDepthFirstTreeIterator< T >::ConstructL()
|
|
134 |
// Symbian 2nd phase constructor can leave.
|
|
135 |
// -----------------------------------------------------------------------------
|
|
136 |
//
|
|
137 |
template< class T > void CXnDepthFirstTreeIterator< T >::ConstructL()
|
|
138 |
{
|
|
139 |
iStack.AppendL( iStart );
|
|
140 |
}
|
|
141 |
|
|
142 |
// -----------------------------------------------------------------------------
|
|
143 |
// CXnDepthFirstTreeIterator< T >::NextL()
|
|
144 |
// -----------------------------------------------------------------------------
|
|
145 |
//
|
|
146 |
template< class T > T* CXnDepthFirstTreeIterator< T >::NextL()
|
|
147 |
{
|
|
148 |
if( iStack.Count() )
|
|
149 |
{
|
|
150 |
// Pop
|
|
151 |
TInt topIndex( iStack.Count() - 1 );
|
|
152 |
iCurrent = iStack[topIndex];
|
|
153 |
iStack.Remove( topIndex );
|
|
154 |
|
|
155 |
RPointerArray< T >& currentChildNodes( iCurrent->Children() );
|
|
156 |
TInt currentChildCount( currentChildNodes.Count() );
|
|
157 |
|
|
158 |
// Push left most child to top
|
|
159 |
for( TInt i = currentChildCount - 1; i >= 0; --i )
|
|
160 |
{
|
|
161 |
iStack.AppendL( currentChildNodes[i] );
|
|
162 |
}
|
|
163 |
}
|
|
164 |
else
|
|
165 |
{
|
|
166 |
iCurrent = NULL;
|
|
167 |
iStack.Reset();
|
|
168 |
iStack.AppendL( iStart );
|
|
169 |
}
|
|
170 |
return iCurrent;
|
|
171 |
}
|
|
172 |
|
|
173 |
// -----------------------------------------------------------------------------
|
|
174 |
// CXnDepthFirstTreeIterator< T >::Value()
|
|
175 |
// -----------------------------------------------------------------------------
|
|
176 |
//
|
|
177 |
template< class T > T* CXnDepthFirstTreeIterator< T >::Value()
|
|
178 |
{
|
|
179 |
return iCurrent;
|
|
180 |
}
|
|
181 |
|
|
182 |
// -----------------------------------------------------------------------------
|
|
183 |
// CXnDepthFirstTreeIterator< T >::PreviousL()
|
|
184 |
// -----------------------------------------------------------------------------
|
|
185 |
//
|
|
186 |
template< class T > T* CXnDepthFirstTreeIterator< T >::PreviousL()
|
|
187 |
{
|
|
188 |
return NULL;
|
|
189 |
}
|
|
190 |
|
|
191 |
// -----------------------------------------------------------------------------
|
|
192 |
// CXnDepthFirstTreeIterator< T >::Index()
|
|
193 |
// -----------------------------------------------------------------------------
|
|
194 |
//
|
|
195 |
template< class T > TUint CXnDepthFirstTreeIterator< T >::Index() const
|
|
196 |
{
|
|
197 |
return 0;
|
|
198 |
}
|
|
199 |
|
|
200 |
// -----------------------------------------------------------------------------
|
|
201 |
// CXnDepthFirstTreeIterator< T >::Level()
|
|
202 |
// Get the current iterator level in the tree.
|
|
203 |
// -----------------------------------------------------------------------------
|
|
204 |
template< class T > TUint CXnDepthFirstTreeIterator< T >::Level() const
|
|
205 |
{
|
|
206 |
return iStack.Count();
|
|
207 |
}
|
|
208 |
|
|
209 |
|
|
210 |
#endif // CXNDEPTHFIRSTTREEITERATOR_H
|
|
211 |
|
|
212 |
// End of File
|