DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * The contents of this file are subject to the Netscape Public License
 * Version 1.0 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 *
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

/** 
 * This file defines the binary tree class and it
 * nsNode child class. Note that like all nsBTree
 * containers, this one does not automatically balance.
 * (Find for random data, bad for pre-ordered data).
 *
 * This simple version stores nodes, and the
 * nodes store void* ptrs.
 * 
 * @update gess 4/11/98
 */


/**
 * nsNode
 * 
 * @update gess 4/11/98
 * @param  
 * @return 
 */

#ifndef _BTREE_H
#define _BTREE_H

#include "nscore.h"

struct NS_BASE nsNode {

  /**
   * 
   * @update	gess4/20/98
   * @param 
   * @return
   */
  nsNode();

  /**
   * Copy constructor
   * @update gess 4/11/98
   */  
  nsNode(const nsNode& aNode);

  /**
   * destructor
   * @update gess 4/11/98
   */  
  virtual ~nsNode();

  /**
   * Retrieve parent node
   *
   * @update gess 4/11/98
   * @return 
   */  
  nsNode*  GetParentNode(void) const;

  /**
   * 
   * @update gess 4/11/98
   * @param  
   * @return 
   */  
  nsNode*  GetLeftNode() const;

  /**
   * 
   * @update gess 4/11/98
   * @param  
   * @return 
   */  
  nsNode*  GetRightNode() const;

  /**
   * 
   * @update gess 4/11/98
   * @param  
   * @return 
   */  
  virtual nsNode& operator=(const nsNode& aNode);


  /**
   * This method gets called to determine which of 
   * two nodes is less. When you create your own
   * subclass of nsNode, this is the most important
   * method for you to overload.
   * 
   * @update gess 4/11/98
   * @param  
   * @return 
   */ 
  virtual PRBool operator<(const nsNode& aNode) const=0;

  /** 
   * This method gets called to determine which of 
   * two nodes is less. When you create your own
   * subclass of nsNode, this is the most important
   * method for you to overload.
   * 
   * @update gess 4/11/98
   * @param  
   * @return 
   */ 
  virtual PRBool operator==(const nsNode& aNode) const=0;

  enum eRBColor {eRed,eBlack};

  nsNode*   mParent;
  nsNode*   mLeft;
  nsNode*   mRight;
  eRBColor  mColor;
};

/**
 * The Nodefunctor class is used when you want to create
 * callbacks between the nsRBTree and your generic code.
 *
 * @update	gess4/20/98
 */
class NS_BASE nsNodeFunctor {
public:
  virtual nsNodeFunctor& operator()(nsNode& aNode)=0;
};


/****************************************************
 * Here comes the nsBTree class...
 ****************************************************/

class NS_BASE nsBTree {
public:
friend class nsBTreeIterator;
  
  nsBTree();
  virtual ~nsBTree();

  /** 
   * Add given node reference into our tree.
   * 
   * @update gess 4/11/98
   * @param  aNode is a ref to a node to be added
   * @return newly added node
   */ 
  nsNode*  Add(nsNode& aNode);

  /** 
   * Remove given node reference into our tree.
   * 
   * @update gess 4/11/98
   * @param  aNode is a ref to a node to be removed
   * @return Ptr to node if found (and removed) or NULL
   */ 
  nsNode*  Remove(nsNode& aNode);

  /**
   * Clears the tree of any data. 
   * Be careful here if your objects are heap based!
   * This method doesn't free the objects, so if you
   * don't have your own pointers, they will become
   * orphaned.
   * 
   * @update gess 4/11/98
   * @param  
   * @return this
   */
   nsBTree& Empty(nsNode* aNode=0);

  /**
   * This method destroys all the objects in the tree.
   * WARNING: Never call this method on stored objects
   *          that are stack-based!
   * 
   * @update gess 4/11/98
   * @param  
   * @return this
   */
   nsBTree& Erase(nsNode* aNode=0);

  /** 
   * Retrieve ptr to 1st node in tree (starting at root)
   * 
   * @update gess 4/11/98
   * @return ptr to 1st node, possible to be NULL
   */ 
  nsNode*  First(void) const;

  /** 
   * Find first node in tree starting at given node
   * 
   * @update gess 4/11/98
   * @param  aNode node to begin 1st search from
   * @return ptr to 1st node below given node
   */ 
  nsNode*  First(const nsNode& aNode) const;

  /** 
   * Retrieve ptr to last node in tree relative to root.
   * 
   * @update gess 4/11/98
   * @return ptr to last node or NULL
   */ 
  nsNode* Last(void) const;

  /** 
   * Retrieve ptr to last node in tree relative to given node.
   * 
   * @update gess 4/11/98
   * @param  node to find last node from 
   * @return ptr to last node or NULL
   */ 
  nsNode* Last(const nsNode& aNode) const;

  /** 
   * Retrieve a ptr to the node that preceeds given node
   * 
   * @update gess 4/11/98
   * @param  aNode used as reference to find prev. 
   * @return ptr to prev node or NULL
   */ 
  nsNode*  Before(const nsNode& aNode) const;

  /** 
   * Retrieve a ptr to the node after given node
   * 
   * @update gess 4/11/98
   * @param  aNode used as reference to find next. 
   * @return ptr to next node or NULL
   */ 
  nsNode*  After(const nsNode& aNode) const;

  /** 
   * Find given node in tree.
   * (Why would you want to find a node you already have?)
   * 
   * @update gess 4/11/98
   * @param  aNode is the node you're searching for
   * @return ptr to node if found, or NULL
   */ 
  nsNode*  Find(const nsNode& aNode) const;

  /** 
   * Walks the tree, starting with root.
   * 
   * @update gess 4/11/98
   */ 
  virtual const nsBTree& ForEach(nsNodeFunctor& aFunctor,nsNode* aNode=0) const;
            
protected:

  /**
   * Rebalances tree around the given node. This only
   * needs to be called after a node is deleted.
   * 
   * @update gess 4/11/98
   * @param  aNode -- node to balance around
   * @return this
   */
  virtual nsBTree& ReBalance(nsNode& aNode);


  nsNode*  mRoot;
};

#endif