oaXYTree.h

Go to the documentation of this file.
00001 // *****************************************************************************
00002 // *****************************************************************************
00003 // oaXYTree.h
00004 //
00005 // This file contains the definition for the oaXYTree, oaXYTreeNode, oaXYTreeRec,
00006 // and oaXYTreeProd classes. These classes implement a 2-D region querying tree
00007 // for quickly finding objects that are in a particular region.
00008 // Capabilities they support include:
00009 //
00010 //  1)  The oaXYTree class is the container for the tree. It is an abstract base
00011 //      class. It keeps objects by index, which must be passed in. Applications
00012 //      are responsible for creating the sub-class, which must provide
00013 //      information about the stored objects' bBoxes and provide a means to
00014 //      link objects together.
00015 //  2)  The oaXYTreeNode class implements a node in the tree. A node can be
00016 //      either a leaf node or a branch node. To maximize efficiency, the nodes
00017 //      of a tree are allocated through a pool maintained by the tree itself.
00018 //      Note that this object is only to be allocated in arrays, so non-array
00019 //      allocation (new/delete) is disallowed
00020 //  3)  The oaXYTreeRec class implements a record in a treeNode. A record
00021 //      contains the index of the object and its next object index. To maximize
00022 //      efficiency, the records of a treeNode are allocated through a pool
00023 //      maintained by the tree itself.
00024 //      Note that this object is only to be allocated in arrays, so non-array
00025 //      allocation (new/delete) is disallowed
00026 //  4)  The oaXYTreeProd class implements a producer that walks a specified tree
00027 //      and produces all objects that intersect a specified region. This is an
00028 //      abstract class that must be overridden to provide specific
00029 //      functionalities.
00030 //
00031 // *****************************************************************************
00032 // Except as specified in the OpenAccess terms of use of Cadence or Silicon
00033 // Integration Initiative, this material may not be copied, modified,
00034 // re-published, uploaded, executed, or distributed in any way, in any medium,
00035 // in whole or in part, without prior written permission from Cadence.
00036 //
00037 //                Copyright 2002-2005 Cadence Design Systems, Inc.
00038 //                           All Rights Reserved.
00039 //
00040 //  $Author: icftcm $
00041 //  $Revision: #1 $
00042 //  $Date: 2010/08/09 $
00043 //  $State: Exp $
00044 // *****************************************************************************
00045 // *****************************************************************************
00046 
00047 
00048 
00049 #if !defined(oaXYTree_P)
00050 #define oaXYTree_P
00051 
00052 
00053 
00054 // *****************************************************************************
00055 // Nested includes
00056 // *****************************************************************************
00057 #include "oaBox.h"
00058 
00059 
00060 
00061 // *****************************************************************************
00062 // Declare and define types in the OpenAccess namespace.
00063 // *****************************************************************************
00064 BEGIN_OA_NAMESPACE
00065 
00066 
00067 
00068 // *****************************************************************************
00069 // Forward Class Declarations
00070 // *****************************************************************************
00071 template<class T, class M, class UD>
00072 class oaXYTree;
00073 template<class T, class M, class UD>
00074 class oaXYTreeProd;
00075 class oaBox;
00076 
00077 
00078 // *****************************************************************************
00079 // Flag Bits, Masks, and Shifts
00080 // *****************************************************************************
00081 // Following are used with the 'flags' bits for xy-tree node. Those 'flags'
00082 // bits are shared with the member 'midChild' and occupy the higher bits.
00083 #define oavXYTreeNodeBBoxIsInvalid          0x80000000u
00084 #define oavXYTreeNodeIsHorizontal           0x40000000u
00085 #define oavXYTreeNodeSplitHasFailed         0x40000000u
00086 #define oavXYTreeNodeMidChildMask           0x3fffffffu
00087 
00088 
00089 
00090 // *****************************************************************************
00091 // oaXYTreeRec
00092 // *****************************************************************************
00093 template<class T>
00094 class oaXYTreeRec {
00095 public:
00096     T                       object;
00097     oaUInt4                 next;
00098 
00099     void                    *operator new[](size_t size);
00100     void                    operator delete[](void *ptr);
00101 
00102 private:
00103     void                    *operator new(size_t);
00104     void                    operator delete(void *);
00105 };
00106 
00107 
00108 
00109 // *****************************************************************************
00110 // oaXYTreeNode
00111 // *****************************************************************************
00112 template<class T, class M, class UD>
00113 class oaXYTreeNode {
00114 public:
00115     typedef oaXYTree<T, M, UD>      treeType;
00116     typedef oaXYTreeNode<T, M, UD>  nodeType;
00117     typedef oaXYTreeRec<T>          recType;
00118     typedef oaXYTreeProd<T, M, UD>  prodType;
00119 
00120     void                            add(treeType    *tree,
00121                                         T           obj,
00122                                         const oaBox &objBBox,
00123                                         oaUInt4     depth = 0);
00124     void                            remove(treeType     *tree,
00125                                            T            obj,
00126                                            const oaBox  &objBBox);
00127 
00128     const oaBox                     &getBBox(const treeType *tree) const;
00129 
00130     oaUInt4                         getNumObjects() const;
00131 
00132     oaBoolean                       isLeaf() const;
00133     oaBoolean                       isUsed() const;
00134 
00135     nodeType                        *getLoChild(const treeType *tree) const;
00136     nodeType                        *getHiChild(const treeType *tree) const;
00137     nodeType                        *getMidChild(const treeType *tree) const;
00138 
00139     void                            split(treeType      *tree,
00140                                           const oaBox   &objBBoxLatest);
00141 
00142     void                            flatten(treeType    *tree,
00143                                             nodeType    *parent = NULL);
00144 
00145     void                            produce(const treeType  *tree,
00146                                             const oaBox     &region,
00147                                             prodType        *prod) const;
00148     void                            produce(const treeType  *tree,
00149                                             prodType        *prod) const;
00150 
00151     oaBoolean                       findNearestNeighborToObj(const treeType *tree,
00152                                                              T              to,
00153                                                              const oaBox    &toBBox,
00154                                                              oaDouble       &closestDistanceSoFar,
00155                                                              oaBox          &worstCaseBBox,
00156                                                              T              &result);
00157     oaBoolean                       findNearestNeighborToArea(const treeType    *tree,
00158                                                               const oaBox       &to,
00159                                                               oaDouble          &closestDistanceSoFar,
00160                                                               oaBox             &worstCaseBBox,
00161                                                               T                 &result);
00162 
00163     void                            *operator new[](size_t size);
00164     void                            operator delete[](void *ptr);
00165 
00166 private:
00167     void                            init();
00168 
00169     void                            rawAdd(treeType     *tree,
00170                                            T            obj,
00171                                            const oaBox  &objBox,
00172                                            oaBoolean    doSplit = true);
00173     void                            balanceAdd(treeType     *tree,
00174                                                T            obj,
00175                                                const oaBox  &objBox);
00176 
00177     oaBoolean                       insertAtRoot(treeType       *tree,
00178                                                  T              obj,
00179                                                  const oaBox    &objBox);
00180 
00181     void                            addToList(treeType      *tree,
00182                                               T             obj,
00183                                               const oaBox   &objBBox);
00184     void                            removeFromList(treeType     *tree,
00185                                                    T            obj,
00186                                                    const oaBox  &objBBox);
00187 
00188     void                            *operator new(size_t size);
00189     void                            operator delete(void *ptr);
00190 
00191     oaUInt4                         numObjects;
00192     mutable oaBox                   bBox;
00193     oaInt4                          median;
00194 
00195     union {
00196         oaUInt4             loChild;
00197         oaUInt4             firstObj;
00198     };
00199     union {
00200         oaUInt4             midChild;
00201         oaUInt4             flags;
00202     };
00203     union {
00204         oaUInt4             hiChild;
00205         oaUInt4             nextDeleted;
00206     };
00207 
00208     friend class oaXYTree<T, M, UD>;
00209 };
00210 
00211 
00212 
00213 // *****************************************************************************
00214 // oaXYTree
00215 // *****************************************************************************
00216 template<class T, class M, class UD>
00217 class oaXYTree {
00218 public:
00219     typedef oaXYTreeNode<T, M, UD>  nodeType;
00220 
00221                                     oaXYTree(UD         measurerArgIn,
00222                                              oaUInt4    numObjs = 0);
00223     virtual                         ~oaXYTree();
00224 
00225     oaBoolean                       add(T item);
00226     oaBoolean                       remove(T item);
00227     void                            clear();
00228 
00229     nodeType                        *getRoot() const;
00230 
00231     oaUInt4                         getNumObjects() const;
00232     virtual const oaBox             &getBBox();
00233     oaUInt4                         getNumDots() const;
00234 
00235     oaBoolean                       findNearestNeighborToObj(T  to,
00236                                                              T  &result);
00237     oaBoolean                       findNearestNeighborToArea(const oaBox   &to,
00238                                                               T             &result);
00239 
00240 protected:
00241     typedef oaXYTreeRec<T>          recType;
00242 
00243     oaUInt4                         allocNode();
00244     void                            freeNode(oaUInt4 index);
00245 
00246     oaUInt4                         allocRec();
00247     void                            freeRec(oaUInt4 index);
00248 
00249     oaBoolean                       busy();
00250 
00251     oaUInt4                         getFirstUsedNode() const;
00252     oaUInt4                         getNextUsedNode(oaUInt4 index) const;
00253 
00254     void                            updateMaxDim(const oaUInt4 dimSize);
00255     oaUInt4                         getMaxDim() const;
00256 
00257     oaUInt8                         calcVMSize() const;
00258 
00259     oaUInt4                         root;
00260 
00261     oaUInt4                         nodeSize;
00262     oaUInt4                         numNodeUsed;
00263     oaUInt4                         numNodeDeleted;
00264     oaUInt4                         firstNodeDeleted;
00265     oaUInt4                         numDots;
00266     nodeType                        *nodes;
00267 
00268     oaUInt4                         recSize;
00269     oaUInt4                         numRecUsed;
00270     oaUInt4                         numRecDeleted;
00271     oaUInt4                         firstRecDeleted;
00272     recType                         *recs;
00273 
00274     oaUInt4                         maxDim;
00275     oaBoolean                       bBoxValid;
00276     UD                              measurerArg;
00277 
00278     oaXYTreeProd<T, M, UD>          *activeProds;
00279 
00280     enum {
00281         iniSize         = 1,
00282         threshold       = 16,
00283         thresholdResize = 4096,
00284         thresholdDepth  = 32
00285     };
00286 
00287     friend class oaXYTreeNode<T, M, UD>;
00288     friend class oaXYTreeProd<T, M, UD>;
00289 };
00290 
00291 
00292 
00293 // *****************************************************************************
00294 // oaXYTreeProd
00295 // *****************************************************************************
00296 template<class T, class M, class UD>
00297 class oaXYTreeProd {
00298 public:
00299     typedef oaXYTree<T, M, UD>  treeType;
00300 
00301                                 oaXYTreeProd(treeType   *treeIn,
00302                                              oaUInt4    thresholdIn = 0,
00303                                              oaBoolean  doDots = false);
00304     virtual                     ~oaXYTreeProd();
00305 
00306     void                        produce(const oaBox &region,
00307                                         oaBoolean   *isAbortedIn);
00308     void                        produce(oaBoolean *isAbortedIn);
00309 
00310     oaUInt4                     getThreshold() const;
00311 
00312 protected:
00313     oaBoolean                   *isAborted;
00314     oaBoolean                   processDots;
00315 
00316 private:
00317     virtual void                process(T item) = 0;
00318 
00319     treeType                    *tree;
00320     oaUInt4                     threshold;
00321     oaXYTreeProd                *nextActiveProd;
00322 
00323     friend class oaXYTreeNode<T, M, UD>;
00324     friend class oaXYTree<T, M, UD>;
00325 };
00326 
00327 
00328 
00329 END_OA_NAMESPACE
00330 
00331 #endif

Return to top of page