AbstractSTRtree.h

00001 /**********************************************************************
00002  * $Id: AbstractSTRtree.h 1971 2007-02-07 00:34:26Z strk $
00003  *
00004  * GEOS - Geometry Engine Open Source
00005  * http://geos.refractions.net
00006  *
00007  * Copyright (C) 2006 Refractions Research Inc.
00008  *
00009  * This is free software; you can redistribute and/or modify it under
00010  * the terms of the GNU Lesser General Public Licence as published
00011  * by the Free Software Foundation. 
00012  * See the COPYING file for more information.
00013  *
00014  **********************************************************************/
00015 
00016 #ifndef GEOS_INDEX_STRTREE_ABSTRACTSTRTREE_H
00017 #define GEOS_INDEX_STRTREE_ABSTRACTSTRTREE_H
00018 
00019 #include <geos/index/strtree/AbstractNode.h> // for inlines
00020 
00021 #include <vector>
00022 #include <list>
00023 #include <memory> // for auto_ptr
00024 #include <cassert> // for inlines
00025 
00026 // Forward declarations
00027 namespace geos {
00028         namespace index { 
00029                 class ItemVisitor;
00030                 namespace strtree { 
00031                         class Boundable;
00032                         class AbstractNode;
00033                 }
00034         }
00035 }
00036 
00037 namespace geos {
00038 namespace index { // geos::index
00039 namespace strtree { // geos::index::strtree
00040 
00042 typedef std::vector<Boundable*> BoundableList;
00043 //typedef std::list<Boundable*> BoundableList;
00044 
00057 class AbstractSTRtree {
00058 
00059 private:
00060         bool built;
00061         BoundableList* itemBoundables;
00062 
00073         virtual AbstractNode* createHigherLevels(
00074                         BoundableList* boundablesOfALevel,
00075                         int level);
00076 
00077         virtual std::auto_ptr<BoundableList> sortBoundables(const BoundableList* input)=0;
00078 
00079         bool remove(const void* searchBounds, AbstractNode& node, void* item);
00080         bool removeItem(AbstractNode& node, void* item);
00081 
00082 protected:
00083 
00089         class IntersectsOp {
00090                 public:
00099                         virtual bool intersects(const void* aBounds,
00100                                         const void* bBounds)=0;
00101 
00102                         virtual ~IntersectsOp() {}
00103         };
00104 
00105         AbstractNode *root;
00106 
00107         std::vector <AbstractNode *> *nodes;
00108 
00109         // Ownership to caller (TODO: return by auto_ptr)
00110         virtual AbstractNode* createNode(int level)=0;
00111 
00116         virtual std::auto_ptr<BoundableList> createParentBoundables(
00117                         BoundableList* childBoundables, int newLevel);
00118 
00119         virtual AbstractNode* lastNode(BoundableList* nodes)
00120         {
00121                 assert(!nodes->empty());
00122                 // Cast from Boundable to AbstractNode
00123                 return static_cast<AbstractNode*>( nodes->back() );
00124         }
00125 
00126         virtual AbstractNode* getRoot() {
00127                 return root;
00128         }
00129 
00131         virtual void insert(const void* bounds,void* item);
00132 
00134         void query(const void* searchBounds, std::vector<void*>& foundItems);
00135 
00136 #if 0
00138         std::vector<void*>* query(const void* searchBounds) {
00139                 vector<void*>* matches = new vector<void*>();
00140                 query(searchBounds, *matches);
00141                 return matches;
00142         }
00143 #endif
00144 
00145 
00147         void query(const void* searchBounds, ItemVisitor& visitor);
00148 
00149         void query(const void* searchBounds, const AbstractNode& node, ItemVisitor& visitor);
00150   
00152         bool remove(const void* itemEnv, void* item);
00153 
00154         std::auto_ptr<BoundableList> boundablesAtLevel(int level);
00155 
00156         // @@ should be size_t, probably
00157         size_t nodeCapacity;
00158 
00165         virtual IntersectsOp *getIntersectsOp()=0;
00166  
00167 
00168 public:
00169 
00174         AbstractSTRtree(size_t newNodeCapacity)
00175                 :
00176                 built(false),
00177                 itemBoundables(new BoundableList()),
00178                 nodes(new std::vector<AbstractNode *>()),
00179                 nodeCapacity(newNodeCapacity)
00180         {
00181                 assert(newNodeCapacity>1);
00182         }
00183 
00184         static bool compareDoubles(double a, double b) {
00185                 return a<b;
00186         }
00187 
00188         virtual ~AbstractSTRtree();
00189 
00196         virtual void build();
00197 
00201         virtual size_t getNodeCapacity() { return nodeCapacity; }
00202 
00203         virtual void query(const void* searchBounds, const AbstractNode* node, std::vector<void*>* matches);
00204 
00208         virtual void boundablesAtLevel(int level, AbstractNode* top,
00209                         BoundableList* boundables);
00210 };
00211 
00212 
00213 } // namespace geos::index::strtree
00214 } // namespace geos::index
00215 } // namespace geos
00216 
00217 #endif // GEOS_INDEX_STRTREE_ABSTRACTSTRTREE_H
00218 
00219 /**********************************************************************
00220  * $Log$
00221  * Revision 1.3  2006/06/12 10:49:43  strk
00222  * unsigned int => size_t
00223  *
00224  * Revision 1.2  2006/06/08 11:20:24  strk
00225  * Added missing virtual destructor to abstract classes.
00226  *
00227  * Revision 1.1  2006/03/21 10:47:34  strk
00228  * indexStrtree.h split
00229  *
00230  **********************************************************************/
00231 

Generated on Fri Mar 27 04:52:33 2009 for GEOS by  doxygen 1.5.4