1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef ARY_SYMTREE_NODE_HXX 29*cdf0e10cSrcweir #define ARY_SYMTREE_NODE_HXX 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir // USED SERVICES 33*cdf0e10cSrcweir // BASE CLASSES 34*cdf0e10cSrcweir // OTHER 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir namespace ary 39*cdf0e10cSrcweir { 40*cdf0e10cSrcweir namespace symtree 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir /** Represents a node in a tree of symbols like a namespace tree or a 46*cdf0e10cSrcweir directory tree. 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir @tpl NODE_TRAITS 49*cdf0e10cSrcweir Needs to define the types: 50*cdf0e10cSrcweir entity_base_type: The type of the entities in that storage, 51*cdf0e10cSrcweir e.g. ->ary::cpp::CodeEntity. 52*cdf0e10cSrcweir id_type: The type of the ids of those entities, 53*cdf0e10cSrcweir e.g. ->ary::cpp::Ce_id. 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir Needs to define the functions: 56*cdf0e10cSrcweir 1. static entity_base_type & 57*cdf0e10cSrcweir EntityOf_( 58*cdf0e10cSrcweir id_type i_id ); 59*cdf0e10cSrcweir 2. static symtree::Node<LeNode_Traits> * 60*cdf0e10cSrcweir NodeOf_( 61*cdf0e10cSrcweir const entity_base_type & 62*cdf0e10cSrcweir i_entity ); 63*cdf0e10cSrcweir 3. static const String & 64*cdf0e10cSrcweir LocalNameOf_( 65*cdf0e10cSrcweir const entity_base_type & 66*cdf0e10cSrcweir i_entity ); 67*cdf0e10cSrcweir 4. static entity_base_type * 68*cdf0e10cSrcweir ParentOf_( 69*cdf0e10cSrcweir const entity_base_type & 70*cdf0e10cSrcweir i_entity ); 71*cdf0e10cSrcweir 5. template <class KEY> 72*cdf0e10cSrcweir static id_t Search_( 73*cdf0e10cSrcweir const entity_base_type & 74*cdf0e10cSrcweir i_entity, 75*cdf0e10cSrcweir const KEY & i_localKey ); 76*cdf0e10cSrcweir */ 77*cdf0e10cSrcweir template <class NODE_TRAITS> 78*cdf0e10cSrcweir class Node 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir public: 81*cdf0e10cSrcweir typedef Node<NODE_TRAITS> node_self; 82*cdf0e10cSrcweir typedef typename NODE_TRAITS::entity_base_type entity_t; 83*cdf0e10cSrcweir typedef typename NODE_TRAITS::id_type id_t; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // LIFECYCLE 87*cdf0e10cSrcweir /// @attention Always needs to be followed by ->Assign_Entity()! 88*cdf0e10cSrcweir Node(); 89*cdf0e10cSrcweir explicit Node( 90*cdf0e10cSrcweir entity_t & i_entity ); 91*cdf0e10cSrcweir void Assign_Entity( 92*cdf0e10cSrcweir entity_t & i_entity ); 93*cdf0e10cSrcweir ~Node(); 94*cdf0e10cSrcweir // INQUIRY 95*cdf0e10cSrcweir id_t Id(); 96*cdf0e10cSrcweir const String Name() const; 97*cdf0e10cSrcweir int Depth() const; 98*cdf0e10cSrcweir const entity_t & Entity() const; 99*cdf0e10cSrcweir const node_self * Parent() const; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir /** Gets a child with a specific name and of a specific type. 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir There may be more childs with the same name. 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir @return id_t(0), if no matching child is found. 106*cdf0e10cSrcweir */ 107*cdf0e10cSrcweir template <class KEY> 108*cdf0e10cSrcweir typename NODE_TRAITS::id_type 109*cdf0e10cSrcweir Search( 110*cdf0e10cSrcweir const KEY & i_localKey ) const 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir // Inline here to workaround SUNW8 compiler bug, works in SUNW12. 113*cdf0e10cSrcweir return NODE_TRAITS::Search_(Entity(), i_localKey); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir /** Gets a child with a specific qualified name below this node. 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir The child may not exists. 120*cdf0e10cSrcweir */ 121*cdf0e10cSrcweir template <class KEY> 122*cdf0e10cSrcweir void SearchBelow( 123*cdf0e10cSrcweir id_t & o_return, // Workaround SUNW8 compiler bug 124*cdf0e10cSrcweir StringVector::const_iterator 125*cdf0e10cSrcweir i_qualifiedSearchedName_begin, 126*cdf0e10cSrcweir StringVector::const_iterator 127*cdf0e10cSrcweir i_qualifiedSearchedName_end, 128*cdf0e10cSrcweir const KEY & i_localKey ) const; 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir /** Gets a child with a specific qualified name, either below this node 131*cdf0e10cSrcweir or below any of the parent nodes. 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir The child may not exists. 134*cdf0e10cSrcweir */ 135*cdf0e10cSrcweir template <class KEY> 136*cdf0e10cSrcweir void SearchUp( 137*cdf0e10cSrcweir id_t & o_return, // Workaround SUNW8 compiler bug 138*cdf0e10cSrcweir StringVector::const_iterator 139*cdf0e10cSrcweir i_qualifiedSearchedName_begin, 140*cdf0e10cSrcweir StringVector::const_iterator 141*cdf0e10cSrcweir i_qualifiedSearchedName_end, 142*cdf0e10cSrcweir const KEY & i_localKey ) const; 143*cdf0e10cSrcweir // ACCESS 144*cdf0e10cSrcweir entity_t & Entity(); 145*cdf0e10cSrcweir node_self * Parent(); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir private: 148*cdf0e10cSrcweir // Forbid copying: 149*cdf0e10cSrcweir Node(const node_self&); 150*cdf0e10cSrcweir node_self& operator=(const node_self&); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir // Locals 153*cdf0e10cSrcweir void InitDepth(); 154*cdf0e10cSrcweir node_self * Get_Parent() const; 155*cdf0e10cSrcweir node_self * NodeOf( 156*cdf0e10cSrcweir id_t i_id ) const; 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir // DATA 159*cdf0e10cSrcweir entity_t * pEntity; 160*cdf0e10cSrcweir int nDepth; 161*cdf0e10cSrcweir }; 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir // IMPLEMENTATION 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir template <class NODE_TRAITS> 169*cdf0e10cSrcweir inline const typename Node<NODE_TRAITS>::entity_t & 170*cdf0e10cSrcweir Node<NODE_TRAITS>::Entity() const 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir csv_assert(pEntity != 0); 173*cdf0e10cSrcweir return *pEntity; 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir template <class NODE_TRAITS> 177*cdf0e10cSrcweir inline Node<NODE_TRAITS> * 178*cdf0e10cSrcweir Node<NODE_TRAITS>::NodeOf(id_t i_id) const 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir if (i_id.IsValid()) 181*cdf0e10cSrcweir return NODE_TRAITS::NodeOf_(NODE_TRAITS::EntityOf_(i_id)); 182*cdf0e10cSrcweir return 0; 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir template <class NODE_TRAITS> 186*cdf0e10cSrcweir inline Node<NODE_TRAITS> * 187*cdf0e10cSrcweir Node<NODE_TRAITS>::Get_Parent() const 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir entity_t * 190*cdf0e10cSrcweir parent = NODE_TRAITS::ParentOf_(Entity()); 191*cdf0e10cSrcweir if (parent != 0) 192*cdf0e10cSrcweir return NODE_TRAITS::NodeOf_(*parent); 193*cdf0e10cSrcweir return 0; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir template <class NODE_TRAITS> 197*cdf0e10cSrcweir Node<NODE_TRAITS>::Node() 198*cdf0e10cSrcweir : pEntity(0), 199*cdf0e10cSrcweir nDepth(0) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir template <class NODE_TRAITS> 204*cdf0e10cSrcweir Node<NODE_TRAITS>::Node(entity_t & i_entity) 205*cdf0e10cSrcweir : pEntity(&i_entity), 206*cdf0e10cSrcweir nDepth(0) 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir InitDepth(); 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir template <class NODE_TRAITS> 212*cdf0e10cSrcweir void 213*cdf0e10cSrcweir Node<NODE_TRAITS>::Assign_Entity(entity_t & i_entity) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir pEntity = &i_entity; 216*cdf0e10cSrcweir InitDepth(); 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir template <class NODE_TRAITS> 220*cdf0e10cSrcweir Node<NODE_TRAITS>::~Node() 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir template <class NODE_TRAITS> 225*cdf0e10cSrcweir inline typename Node<NODE_TRAITS>::id_t 226*cdf0e10cSrcweir Node<NODE_TRAITS>::Id() 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir return NODE_TRAITS::IdOf(Entity()); 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir template <class NODE_TRAITS> 232*cdf0e10cSrcweir inline const String 233*cdf0e10cSrcweir Node<NODE_TRAITS>::Name() const 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir return NODE_TRAITS::LocalNameOf_(Entity()); 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir template <class NODE_TRAITS> 239*cdf0e10cSrcweir inline int 240*cdf0e10cSrcweir Node<NODE_TRAITS>::Depth() const 241*cdf0e10cSrcweir { 242*cdf0e10cSrcweir return nDepth; 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir template <class NODE_TRAITS> 246*cdf0e10cSrcweir inline const Node<NODE_TRAITS> * 247*cdf0e10cSrcweir Node<NODE_TRAITS>::Parent() const 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir return Get_Parent(); 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir template <class NODE_TRAITS> 253*cdf0e10cSrcweir template <class KEY> 254*cdf0e10cSrcweir void 255*cdf0e10cSrcweir Node<NODE_TRAITS>::SearchBelow( 256*cdf0e10cSrcweir id_t & o_return, // Workaround SUNW8 compiler bug 257*cdf0e10cSrcweir StringVector::const_iterator i_qualifiedSearchedName_begin, 258*cdf0e10cSrcweir StringVector::const_iterator i_qualifiedSearchedName_end, 259*cdf0e10cSrcweir const KEY & i_localKey ) const 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir if (i_qualifiedSearchedName_begin != i_qualifiedSearchedName_end) 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir id_t 264*cdf0e10cSrcweir next = Search(*i_qualifiedSearchedName_begin); 265*cdf0e10cSrcweir if (next.IsValid()) 266*cdf0e10cSrcweir { 267*cdf0e10cSrcweir const node_self * 268*cdf0e10cSrcweir subnode = NodeOf(next); 269*cdf0e10cSrcweir if (subnode != 0) 270*cdf0e10cSrcweir { 271*cdf0e10cSrcweir subnode->SearchBelow( o_return, 272*cdf0e10cSrcweir i_qualifiedSearchedName_begin+1, 273*cdf0e10cSrcweir i_qualifiedSearchedName_end , 274*cdf0e10cSrcweir i_localKey ); 275*cdf0e10cSrcweir return; 276*cdf0e10cSrcweir } 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir o_return = id_t(0); 279*cdf0e10cSrcweir return; 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir o_return = Search(i_localKey); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir template <class NODE_TRAITS> 286*cdf0e10cSrcweir template <class KEY> 287*cdf0e10cSrcweir void 288*cdf0e10cSrcweir Node<NODE_TRAITS>::SearchUp( 289*cdf0e10cSrcweir id_t & o_return, // Workaround SUNW8 compiler bug 290*cdf0e10cSrcweir StringVector::const_iterator i_qualifiedSearchedName_begin, 291*cdf0e10cSrcweir StringVector::const_iterator i_qualifiedSearchedName_end, 292*cdf0e10cSrcweir const KEY & i_localKey ) const 293*cdf0e10cSrcweir { 294*cdf0e10cSrcweir SearchBelow( o_return, 295*cdf0e10cSrcweir i_qualifiedSearchedName_begin, 296*cdf0e10cSrcweir i_qualifiedSearchedName_end, 297*cdf0e10cSrcweir i_localKey ); 298*cdf0e10cSrcweir if (o_return.IsValid()) 299*cdf0e10cSrcweir return; 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir node_self * 302*cdf0e10cSrcweir parent = Get_Parent(); 303*cdf0e10cSrcweir if (parent != 0) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir parent->SearchUp( o_return, 306*cdf0e10cSrcweir i_qualifiedSearchedName_begin, 307*cdf0e10cSrcweir i_qualifiedSearchedName_end, 308*cdf0e10cSrcweir i_localKey ); 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir 312*cdf0e10cSrcweir template <class NODE_TRAITS> 313*cdf0e10cSrcweir typename Node<NODE_TRAITS>::entity_t & 314*cdf0e10cSrcweir Node<NODE_TRAITS>::Entity() 315*cdf0e10cSrcweir { 316*cdf0e10cSrcweir csv_assert(pEntity != 0); 317*cdf0e10cSrcweir return *pEntity; 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir template <class NODE_TRAITS> 321*cdf0e10cSrcweir inline Node<NODE_TRAITS> * 322*cdf0e10cSrcweir Node<NODE_TRAITS>::Parent() 323*cdf0e10cSrcweir { 324*cdf0e10cSrcweir return Get_Parent(); 325*cdf0e10cSrcweir } 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir template <class NODE_TRAITS> 328*cdf0e10cSrcweir void 329*cdf0e10cSrcweir Node<NODE_TRAITS>::InitDepth() 330*cdf0e10cSrcweir { 331*cdf0e10cSrcweir Node<NODE_TRAITS> * 332*cdf0e10cSrcweir pp = Get_Parent(); 333*cdf0e10cSrcweir if (pp != 0) 334*cdf0e10cSrcweir nDepth = pp->Depth() + 1; 335*cdf0e10cSrcweir else 336*cdf0e10cSrcweir nDepth = 0; 337*cdf0e10cSrcweir } 338*cdf0e10cSrcweir 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir } // namespace symtree 343*cdf0e10cSrcweir } // namespace ary 344*cdf0e10cSrcweir #endif 345