Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

AbstractSTRtree.h

00001 /**********************************************************************
00002  * $Id: AbstractSTRtree.h 2556 2009-06-06 22:22:28Z 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/export.h>
00020 
00021 #include <geos/index/strtree/AbstractNode.h> // for inlines
00022 
00023 #include <vector>
00024 #include <list>
00025 #include <memory> // for auto_ptr
00026 #include <cassert> // for inlines
00027 #include <algorithm>
00028 
00029 // Forward declarations
00030 namespace geos {
00031         namespace index { 
00032                 class ItemVisitor;
00033                 namespace strtree { 
00034                         class Boundable;
00035                         class AbstractNode;
00036                 }
00037         }
00038 }
00039 
00040 namespace geos {
00041 namespace index { // geos::index
00042 namespace strtree { // geos::index::strtree
00043 
00045 typedef std::vector<Boundable*> BoundableList;
00046 //typedef std::list<Boundable*> BoundableList;
00047 
00050 class ItemsList;
00051 
00052 class ItemsListItem
00053 {
00054 public:
00055     enum type {
00056         item_is_geometry,
00057         item_is_list
00058     };
00059 
00060     ItemsListItem(void *item_)
00061       : t(item_is_geometry)
00062     {
00063         item.g = item_;
00064     }
00065     ItemsListItem(ItemsList* item_)
00066       : t(item_is_list)
00067     {
00068         item.l = item_;
00069     }
00070 
00071     type get_type() const { return t; }
00072 
00073     void* get_geometry() const
00074     {
00075         assert(t == item_is_geometry);
00076         return item.g;
00077     }
00078     ItemsList* get_itemslist() const
00079     {
00080         assert(t == item_is_list);
00081         return item.l;
00082     }
00083 
00084     type t;
00085     union {
00086         void* g;
00087         ItemsList* l;
00088     } item;
00089 };
00090 
00091 class ItemsList : public std::vector<ItemsListItem>
00092 {
00093 private:
00094     typedef std::vector<ItemsListItem> base_type;
00095 
00096     static void delete_item(ItemsListItem& item)
00097     {
00098         if (ItemsListItem::item_is_list == item.t)
00099             delete reinterpret_cast<ItemsList*>(item.item.l);
00100     }
00101 
00102 public:
00103     ~ItemsList()
00104     {
00105         std::for_each(begin(), end(), &ItemsList::delete_item);
00106     }
00107 
00108     // lists of items need to be deleted in the end
00109     void push_back(void* item)
00110     {
00111         this->base_type::push_back(ItemsListItem(item));
00112     }
00113 
00114     // lists of items need to be deleted in the end
00115     void push_back_owned(ItemsList* itemList)
00116     {
00117         this->base_type::push_back(ItemsListItem(itemList));
00118     }
00119 };
00120 
00133 class GEOS_DLL AbstractSTRtree {
00134 
00135 private:
00136         bool built;
00137         BoundableList* itemBoundables;
00138 
00149         virtual AbstractNode* createHigherLevels(
00150                         BoundableList* boundablesOfALevel,
00151                         int level);
00152 
00153         virtual std::auto_ptr<BoundableList> sortBoundables(const BoundableList* input)=0;
00154 
00155         bool remove(const void* searchBounds, AbstractNode& node, void* item);
00156         bool removeItem(AbstractNode& node, void* item);
00157 
00158     ItemsList* itemsTree(AbstractNode* node);
00159 
00160 protected:
00161 
00167         class IntersectsOp {
00168                 public:
00177                         virtual bool intersects(const void* aBounds,
00178                                         const void* bBounds)=0;
00179 
00180                         virtual ~IntersectsOp() {}
00181         };
00182 
00183         AbstractNode *root;
00184 
00185         std::vector <AbstractNode *> *nodes;
00186 
00187         // Ownership to caller (TODO: return by auto_ptr)
00188         virtual AbstractNode* createNode(int level)=0;
00189 
00194         virtual std::auto_ptr<BoundableList> createParentBoundables(
00195                         BoundableList* childBoundables, int newLevel);
00196 
00197         virtual AbstractNode* lastNode(BoundableList* nodes)
00198         {
00199                 assert(!nodes->empty());
00200                 // Cast from Boundable to AbstractNode
00201                 return static_cast<AbstractNode*>( nodes->back() );
00202         }
00203 
00204         virtual AbstractNode* getRoot() {
00205                 return root;
00206         }
00207 
00209         virtual void insert(const void* bounds,void* item);
00210 
00212         void query(const void* searchBounds, std::vector<void*>& foundItems);
00213 
00214 #if 0
00215 
00216         std::vector<void*>* query(const void* searchBounds) {
00217                 vector<void*>* matches = new vector<void*>();
00218                 query(searchBounds, *matches);
00219                 return matches;
00220         }
00221 #endif
00222 
00223 
00225         void query(const void* searchBounds, ItemVisitor& visitor);
00226 
00227         void query(const void* searchBounds, const AbstractNode& node, ItemVisitor& visitor);
00228   
00230         bool remove(const void* itemEnv, void* item);
00231 
00232         std::auto_ptr<BoundableList> boundablesAtLevel(int level);
00233 
00234         // @@ should be size_t, probably
00235         size_t nodeCapacity;
00236 
00243         virtual IntersectsOp *getIntersectsOp()=0;
00244  
00245 
00246 public:
00247 
00252         AbstractSTRtree(size_t newNodeCapacity)
00253                 :
00254                 built(false),
00255                 itemBoundables(new BoundableList()),
00256                 nodes(new std::vector<AbstractNode *>()),
00257                 nodeCapacity(newNodeCapacity)
00258         {
00259                 assert(newNodeCapacity>1);
00260         }
00261 
00262         static bool compareDoubles(double a, double b) {
00263                 return a<b;
00264         }
00265 
00266         virtual ~AbstractSTRtree();
00267 
00274         virtual void build();
00275 
00279         virtual size_t getNodeCapacity() { return nodeCapacity; }
00280 
00281         virtual void query(const void* searchBounds, const AbstractNode* node, std::vector<void*>* matches);
00282 
00286         virtual void boundablesAtLevel(int level, AbstractNode* top,
00287                         BoundableList* boundables);
00288 
00303     ItemsList* itemsTree();
00304 };
00305 
00306 
00307 } // namespace geos::index::strtree
00308 } // namespace geos::index
00309 } // namespace geos
00310 
00311 #endif // GEOS_INDEX_STRTREE_ABSTRACTSTRTREE_H
00312 
00313 /**********************************************************************
00314  * $Log$
00315  * Revision 1.3  2006/06/12 10:49:43  strk
00316  * unsigned int => size_t
00317  *
00318  * Revision 1.2  2006/06/08 11:20:24  strk
00319  * Added missing virtual destructor to abstract classes.
00320  *
00321  * Revision 1.1  2006/03/21 10:47:34  strk
00322  * indexStrtree.h split
00323  *
00324  **********************************************************************/
00325 

Generated on Thu Jun 11 06:17:00 2009 for GEOS by  doxygen 1.4.4