mulwidgets/muldatamodel/src/multree.cpp
changeset 0 e83bab7cf002
equal deleted inserted replaced
-1:000000000000 0:e83bab7cf002
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  MulTree Implementaiton
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "multree.h"
       
    20 
       
    21 #include <stdexcept>
       
    22 
       
    23 #include "mulassert.h"
       
    24 #include "mulmodeldef.h"
       
    25 #include "muldatapath.h"
       
    26 
       
    27 namespace Alf
       
    28 	{
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // AddNode
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 void MulTree::AddNode(const MulDataPath& aPath,int aIndex)
       
    37 	{
       
    38 	//Find parent node
       
    39 	MulTreeNode* node = FindNode( aPath );
       
    40 	
       
    41 	//update index of other child
       
    42 	for( int i = 0 ; i < node->ChildCount() ; i++ )
       
    43 		{
       
    44 		MulTreeNode* childNode = node->Child(i);
       
    45 		int index = childNode->Index();
       
    46 		if( childNode->Index() >= aIndex )
       
    47 			{
       
    48 			childNode->SetIndex( childNode->Index()+1 );
       
    49 			}
       
    50 		}	
       
    51 	
       
    52 	//insert node in to parent node
       
    53 	std::auto_ptr<MulTreeNode> childNode( new (EMM) MulTreeNode(aIndex));
       
    54 	node->InsertChild(childNode,aIndex);
       
    55 	}
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // RemoveNode
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void MulTree::RemoveNode(const MulDataPath& aPath, int aIndex )
       
    62 	{
       
    63 	MulTreeNode* node = FindNode( aPath );
       
    64 	node->RemoveChild(aIndex);
       
    65 	
       
    66 	//update index of other child
       
    67 	for( int i = 0 ; i < node->ChildCount() ; i++ )
       
    68 		{
       
    69 		MulTreeNode* childNode = node->Child(i);
       
    70 		int index = childNode->Index();
       
    71 		if( childNode->Index() > aIndex )
       
    72 			{
       
    73 			childNode->SetIndex( childNode->Index()-1 );
       
    74 			}
       
    75 		}	
       
    76 	}
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // FindNode
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 MulTreeNode* MulTree::FindNode( const MulDataPath& aPath ) const
       
    83 	{	
       
    84 	MulTreeNode* currentNode = mRootNode.get();
       
    85 	for( int i = 0; i < aPath.Depth() ;i++ )
       
    86 		{
       
    87 		int index = aPath[i];
       
    88 		if( currentNode->HasChild() )
       
    89 			{
       
    90 			currentNode = FindChildNode( currentNode,index);
       
    91 			}
       
    92 		else
       
    93 			{
       
    94 			__MUL_ASSERT_DEBUG( false , KLInvalidArgument );
       
    95 			}
       
    96 		}
       
    97 	return currentNode;
       
    98 	}
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // FindNode
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 MulTreeNode* MulTree::FindChildNode(MulTreeNode* aParentNode,int aIndex) const
       
   105 	{
       
   106 	MulTreeNode* currentNode = aParentNode;
       
   107 	MulTreeNode* foundNode = NULL;
       
   108 	int index = 0; 
       
   109 	bool found(false);
       
   110 	int childCount = currentNode->ChildCount();
       
   111 	while( !found && index < childCount )
       
   112 		{	
       
   113 		MulTreeNode* childNode = currentNode->Child(index);
       
   114 		if( aIndex == childNode->Index() )
       
   115 			{
       
   116 			found = true;
       
   117 			foundNode = childNode;
       
   118 			}
       
   119 		index++;
       
   120 		}
       
   121 		
       
   122 	if( found == false )
       
   123 		{
       
   124 		__MUL_ASSERT_DEBUG( false , KLInvalidArgument );
       
   125 		}
       
   126 		
       
   127 	return foundNode;
       
   128 	}
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // FindNode
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 MulDataPath MulTree::FindNode( int aIndex ) const
       
   135 	{	
       
   136 	int absoluteIndex = -1;
       
   137 	return FindNode( mRootNode.get(), absoluteIndex, aIndex );	
       
   138 	}
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // FindNode
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 MulDataPath MulTree::FindNode( MulTreeNode* aNode, int& aAbsoluteIndex, int aIndex ) const
       
   145 	{	
       
   146 	MulTreeNode* currentNode = aNode;
       
   147 	if( currentNode->HasChild() )
       
   148 		{
       
   149 		for(int i = 0 ; i < currentNode->ChildCount() ; i++ )
       
   150 			{
       
   151 			aAbsoluteIndex++;
       
   152 			MulTreeNode* childNode = currentNode->Child(i);
       
   153 			
       
   154 #ifdef _DEBUG
       
   155 			bool hasChild = childNode->HasChild();
       
   156 			int count = childNode->ChildCount();
       
   157 			bool isExpanded = childNode->IsExpanded();
       
   158 #endif //_DEBUG
       
   159 			
       
   160 			if( aAbsoluteIndex == aIndex )
       
   161 				{
       
   162 				MulDataPath path= Path(*childNode);
       
   163 				path.SetIndex(i);
       
   164 				return path;
       
   165 				}
       
   166 			 else if( childNode->HasChild() && childNode->IsExpanded() )
       
   167 				{
       
   168 				MulDataPath path = FindNode( childNode, aAbsoluteIndex , aIndex );
       
   169 				if(path.Index() != KNotInitialized )
       
   170 					{
       
   171 					return path;
       
   172 					}
       
   173 				}			
       
   174 			}
       
   175 		}
       
   176 	//__MUL_ASSERT_DEBUG( false , KLInvalidArgument );
       
   177 	MulDataPath path;
       
   178 	path.SetIndex(KNotInitialized);
       
   179 	return path; 
       
   180 	}
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // Path
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 MulDataPath MulTree::Path(MulTreeNode& aNode) const
       
   187 	{
       
   188 	MulTreeNode* parentNode = aNode.Parent();
       
   189 	std::vector<int> pathArray;
       
   190 	while( parentNode && parentNode != mRootNode.get() )
       
   191 		{
       
   192 		int index = parentNode->Index();
       
   193 		pathArray.push_back( parentNode->Index() );
       
   194 		parentNode = parentNode->Parent();
       
   195 		}
       
   196 	
       
   197 	int count = pathArray.size();
       
   198 	
       
   199 	MulDataPath path;
       
   200 	for( int i= pathArray.size()- 1 ; i >=0 ; i-- )
       
   201 		{
       
   202 		path.Append( pathArray[i] );
       
   203 		}
       
   204 	pathArray.clear();
       
   205 	return path;
       
   206 	}
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // NodeCount
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 int MulTree::NodeCount() const
       
   213 	{
       
   214 	int nodeCount =NodeCount( mRootNode.get() );
       
   215 	return nodeCount;
       
   216 	}
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // NodeCount
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 int MulTree::NodeCount(MulTreeNode* aCurrentNode ) const
       
   223 	{
       
   224 	int nodeCount = 0;
       
   225 	if(aCurrentNode->HasChild())
       
   226 		{
       
   227 		nodeCount = aCurrentNode->ChildCount();
       
   228 		for(int i=0;i< aCurrentNode->ChildCount();i++)
       
   229 			{
       
   230 			nodeCount+= NodeCount(aCurrentNode->Child(i));
       
   231 			}
       
   232 		}
       
   233 	return nodeCount;
       
   234 	}
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // ExpandedNodeCount
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 int MulTree::ExpandedNodeCount() const
       
   241 	{
       
   242 	int nodeCount = ExpandedNodeCount( mRootNode.get() );
       
   243 	return nodeCount;
       
   244 	}
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // ExpandedNodeCount
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 int MulTree::ExpandedNodeCount(MulTreeNode* aCurrentNode ) const
       
   251 	{
       
   252 	int nodeCount = 0;
       
   253 	if( aCurrentNode->HasChild() && aCurrentNode->IsExpanded() )
       
   254 		{
       
   255 		nodeCount = aCurrentNode->ChildCount();
       
   256 		for( int i=0; i< aCurrentNode->ChildCount() ; i++ )
       
   257 			{
       
   258 			nodeCount+= ExpandedNodeCount( aCurrentNode->Child(i) );
       
   259 			}
       
   260 		}
       
   261 	return nodeCount;
       
   262 	}
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // NodeIndex
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 int MulTree::NodeIndex( const MulDataPath& aPath, int aIndex ) const
       
   269 	{
       
   270 	MulDataPath path( aPath );
       
   271 	path.SetIndex( aIndex );
       
   272 	
       
   273 	int absoluteIndex = -1;
       
   274 	int index = NodeIndex( mRootNode.get(), path, absoluteIndex );
       
   275 	
       
   276 	return index;
       
   277 	}
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // NodeIndex
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 int MulTree::NodeIndex( MulTreeNode* aCurrentNode, const MulDataPath& aPath, int& aAbsoluteIndex ) const
       
   284 	{	
       
   285 	MulTreeNode* currentNode = aCurrentNode;
       
   286 	if( currentNode->HasChild() )
       
   287 		{
       
   288 		for(int i = 0 ; i < currentNode->ChildCount() ; i++ )
       
   289 			{
       
   290 			aAbsoluteIndex++;
       
   291 			MulTreeNode* childNode = currentNode->Child(i);
       
   292 			
       
   293 #ifdef _DEBUG
       
   294 			bool hasChild = childNode->HasChild();
       
   295 			int count = childNode->ChildCount();
       
   296 			bool isExpanded = childNode->IsExpanded();
       
   297 #endif //_DEBUG
       
   298 			
       
   299 			MulDataPath path = Path(*childNode);
       
   300 			path.SetIndex(i);
       
   301 			
       
   302 			if( path.IsEqual(aPath) )
       
   303 				{
       
   304 				return aAbsoluteIndex;
       
   305 				}
       
   306 			 else if( childNode->HasChild() && childNode->IsExpanded() )
       
   307 				{
       
   308 				int index = NodeIndex( childNode, aPath, aAbsoluteIndex );
       
   309 				if( index != KNotInitialized )
       
   310 					{
       
   311 					return index;
       
   312 					}
       
   313 				}			
       
   314 			}
       
   315 		}
       
   316 	return KNotInitialized; 
       
   317 	}
       
   318 		
       
   319 	} //namespace Alf
       
   320 	
       
   321 	//End of file