[geos-commits] r2407 - in trunk/source: headers/geos/index headers/geos/index/quadtree index/quadtree

svn_geos at osgeo.org svn_geos at osgeo.org
Fri Apr 24 20:23:06 EDT 2009


Author: strk
Date: 2009-04-24 20:23:06 -0400 (Fri, 24 Apr 2009)
New Revision: 2407

Modified:
   trunk/source/headers/geos/index/SpatialIndex.h
   trunk/source/headers/geos/index/quadtree/Key.h
   trunk/source/headers/geos/index/quadtree/Node.h
   trunk/source/index/quadtree/Key.cpp
   trunk/source/index/quadtree/Node.cpp
   trunk/source/index/quadtree/Root.cpp
Log:
Const-correctness, reduced heap allocations and port info for quadtree::Key class, a few more cleanups in user classes, to be continued.


Modified: trunk/source/headers/geos/index/SpatialIndex.h
===================================================================
--- trunk/source/headers/geos/index/SpatialIndex.h	2009-04-24 23:44:43 UTC (rev 2406)
+++ trunk/source/headers/geos/index/SpatialIndex.h	2009-04-25 00:23:06 UTC (rev 2407)
@@ -51,6 +51,8 @@
 	/** \brief
 	 * Adds a spatial item with an extent specified by the given Envelope
 	 * to the index
+	 *
+	 * TODO: document ownership of 'itemEnv' and 'item'
 	 */
 	virtual void insert(const geom::Envelope *itemEnv, void *item) = 0;
 

Modified: trunk/source/headers/geos/index/quadtree/Key.h
===================================================================
--- trunk/source/headers/geos/index/quadtree/Key.h	2009-04-24 23:44:43 UTC (rev 2406)
+++ trunk/source/headers/geos/index/quadtree/Key.h	2009-04-25 00:23:06 UTC (rev 2407)
@@ -4,25 +4,29 @@
  * GEOS - Geometry Engine Open Source
  * http://geos.refractions.net
  *
+ * Copyright (C) 2009  Sandro Santilli <strk at keybit.net>
  * Copyright (C) 2006 Refractions Research Inc.
+ * Copyright (C) 2001-2002 Vivid Solutions Inc.
  *
  * This is free software; you can redistribute and/or modify it under
  * the terms of the GNU Lesser General Public Licence as published
  * by the Free Software Foundation. 
  * See the COPYING file for more information.
  *
+ **********************************************************************
+ *
+ * Last port: index/quadtree/Key.java rev 1.8 (JTS-1.10)
+ *
  **********************************************************************/
 
 #ifndef GEOS_IDX_QUADTREE_KEY_H
 #define GEOS_IDX_QUADTREE_KEY_H
 
+#include <geos/geom/Coordinate.h> // for composition
+#include <geos/geom/Envelope.h> // for composition
+
 // Forward declarations
-namespace geos {
-	namespace geom {
-		class Envelope;
-		class Coordinate;
-	}
-}
+// ...
 
 namespace geos {
 namespace index { // geos::index
@@ -39,36 +43,43 @@
 public:
 
 	// Doesn't touch the Envelope, might as well be const
-	static int computeQuadLevel(geom::Envelope *env);
+	static int computeQuadLevel(const geom::Envelope& env);
 
-	Key(geom::Envelope *itemEnv);
-	virtual ~Key();
+	// Reference to argument won't be used after construction
+	Key(const geom::Envelope& itemEnv);
 
+	// used to be virtual, but I don't see subclasses...
+	~Key();
+
 	/// Returned object ownership retained by this class
-	geom::Coordinate* getPoint();
+	const geom::Coordinate& getPoint() const;
 
-	int getLevel();
+	int getLevel() const;
 
 	/// Returned object ownership retained by this class
-	geom::Envelope* getEnvelope();
+	const geom::Envelope& getEnvelope() const;
 
 	/// Returns newly allocated object (ownership transferred)
-	geom::Coordinate* getCentre();
+	geom::Coordinate* getCentre() const;
 
-	void computeKey(geom::Envelope *itemEnv);
+	/**
+	 * return a square envelope containing the argument envelope,
+	 * whose extent is a power of two and which is based at a power of 2
+	 */
+	void computeKey(const geom::Envelope& itemEnv);
+
 private:	
 	// the fields which make up the key
 
 	// Owned by this class
-	geom::Coordinate *pt;
+	geom::Coordinate pt;
 
 	int level;
 
 	// auxiliary data which is derived from the key for use in computation
-	// Owned by this class
-	geom::Envelope *env;
+	geom::Envelope env;
 
-	void computeKey(int level,geom::Envelope *itemEnv);
+	void computeKey(int level, const geom::Envelope& itemEnv);
 };
 
 } // namespace geos::index::quadtree

Modified: trunk/source/headers/geos/index/quadtree/Node.h
===================================================================
--- trunk/source/headers/geos/index/quadtree/Node.h	2009-04-24 23:44:43 UTC (rev 2406)
+++ trunk/source/headers/geos/index/quadtree/Node.h	2009-04-25 00:23:06 UTC (rev 2407)
@@ -21,6 +21,7 @@
 #include <geos/geom/Envelope.h> // for inline
 
 #include <string>
+#include <memory>
 
 // Forward declarations
 namespace geos {
@@ -65,9 +66,9 @@
 
 public:
 
-	static Node* createNode(geom::Envelope *env);
+	static std::auto_ptr<Node> createNode(const geom::Envelope& env);
 
-	static Node* createExpanded(Node *node,
+	static std::auto_ptr<Node> createExpanded(Node *node,
 			const geom::Envelope *addEnv);
 
 	// Takes ownership of envelope

Modified: trunk/source/index/quadtree/Key.cpp
===================================================================
--- trunk/source/index/quadtree/Key.cpp	2009-04-24 23:44:43 UTC (rev 2406)
+++ trunk/source/index/quadtree/Key.cpp	2009-04-25 00:23:06 UTC (rev 2407)
@@ -4,6 +4,7 @@
  * GEOS - Geometry Engine Open Source
  * http://geos.refractions.net
  *
+ * Copyright (C) 2009  Sandro Santilli <strk at keybit.net>
  * Copyright (C) 2006 Refractions Research Inc.
  * Copyright (C) 2001-2002 Vivid Solutions Inc.
  *
@@ -12,6 +13,10 @@
  * by the Free Software Foundation. 
  * See the COPYING file for more information.
  *
+ **********************************************************************
+ *
+ * Last port: index/quadtree/Key.java rev 1.8 (JTS-1.10)
+ *
  **********************************************************************/
 
 #include <geos/index/quadtree/Key.h>
@@ -35,12 +40,13 @@
 namespace index { // geos.index
 namespace quadtree { // geos.index.quadtree
 
+/* static public */
 int
-Key::computeQuadLevel(Envelope *env)
+Key::computeQuadLevel(const Envelope& env)
 {
-	double dx=env->getWidth();
-	double dy=env->getHeight();
-	double dMax=dx>dy?dx:dy;
+	double dx = env.getWidth();
+	double dy = env.getHeight();
+	double dMax = dx > dy ? dx : dy;
 	int level=DoubleBits::exponent(dMax)+1;
 #if GEOS_DEBUG
 	std::cerr<<"Maxdelta:"<<dMax<<" exponent:"<<(level-1)<<std::endl;
@@ -48,67 +54,75 @@
 	return level;
 }
 
-Key::Key(Envelope *itemEnv){
-	pt=new Coordinate();
-	level=0;
-	env=NULL;
+Key::Key(const Envelope& itemEnv)
+	:
+	pt(),
+	level(0),
+	env()
+{
 	computeKey(itemEnv);
 }
 
-Key::~Key(){
-	delete pt;
-	delete env;
+Key::~Key()
+{
 }
 
-Coordinate* Key::getPoint() {
+const Coordinate&
+Key::getPoint() const
+{
 	return pt;
 }
 
-int Key::getLevel() {
+int
+Key::getLevel() const
+{
 	return level;
 }
 
-Envelope* Key::getEnvelope() {
+const Envelope&
+Key::getEnvelope() const
+{
 	return env;
 }
 
-Coordinate* Key::getCentre() {
+Coordinate*
+Key::getCentre() const
+{
 	return new Coordinate(
-					(env->getMinX()+env->getMaxX())/2,
-					(env->getMinY()+env->getMaxY())/2);
+			( env.getMinX() + env.getMaxX() ) / 2,
+			( env.getMinY() + env.getMaxY() ) / 2
+		);
 }
 
-/**
- * return a square envelope containing the argument envelope,
- * whose extent is a power of two and which is based at a power of 2
- */
-void Key::computeKey(Envelope *itemEnv) {
+/*public*/
+void
+Key::computeKey(const Envelope& itemEnv)
+{
 	level=computeQuadLevel(itemEnv);
-	delete env;
-	env=new Envelope(); 
-	computeKey(level,itemEnv);
+	env.init(); // reset to null 
+	computeKey(level, itemEnv);
 	// MD - would be nice to have a non-iterative form of this algorithm
-	while (!env->contains(itemEnv)) {
+	while (!env.contains(itemEnv)) {
 		level+=1;
-		computeKey(level,itemEnv);
+		computeKey(level, itemEnv);
 	}
 #if GEOS_DEBUG
 	std::cerr<<"Key::computeKey:"<<std::endl;
-	std::cerr<<" itemEnv: "<<itemEnv->toString()<<std::endl;
-	std::cerr<<"  keyEnv: "<<env->toString()<<std::endl;
+	std::cerr<<" itemEnv: "<<itemEnv.toString()<<std::endl;
+	std::cerr<<"  keyEnv: "<<env.toString()<<std::endl;
 	std::cerr<<"  keyLvl: "<<level<<std::endl;
 
 #endif
 }
 
 void
-Key::computeKey(int level,Envelope *itemEnv)
+Key::computeKey(int level, const Envelope& itemEnv)
 {
 	double quadSize=DoubleBits::powerOf2(level);
 	//double quadSize=pow2.power(level);
-	pt->x=std::floor(itemEnv->getMinX()/quadSize)*quadSize;
-	pt->y=std::floor(itemEnv->getMinY()/quadSize)*quadSize;
-	env->init(pt->x,pt->x+quadSize,pt->y,pt->y+quadSize);
+	pt.x = std::floor(itemEnv.getMinX()/quadSize)*quadSize;
+	pt.y = std::floor(itemEnv.getMinY()/quadSize)*quadSize;
+	env.init(pt.x, pt.x + quadSize, pt.y, pt.y + quadSize);
 }
 
 } // namespace geos.index.quadtree

Modified: trunk/source/index/quadtree/Node.cpp
===================================================================
--- trunk/source/index/quadtree/Node.cpp	2009-04-24 23:44:43 UTC (rev 2406)
+++ trunk/source/index/quadtree/Node.cpp	2009-04-25 00:23:06 UTC (rev 2407)
@@ -37,26 +37,29 @@
 namespace index { // geos.index
 namespace quadtree { // geos.index.quadtree
 
-Node*
-Node::createNode(Envelope *env)
+/* public static */
+std::auto_ptr<Node>
+Node::createNode(const Envelope& env)
 {
-	Key* key=new Key(env);
-	Node *node=new Node(new Envelope(*(key->getEnvelope())),key->getLevel());
-	delete key;
+	Key key(env);
+	std::auto_ptr<Node> node (
+		new Node(new Envelope(key.getEnvelope()),
+	                 key.getLevel())
+	);
 	return node;
 }
 
-Node*
+/* static public */
+std::auto_ptr<Node>
 Node::createExpanded(Node *node, const Envelope *addEnv)
 {
-	Envelope *expandEnv=new Envelope(*addEnv);
-	if (node!=NULL) expandEnv->expandToInclude(node->env);
+	Envelope expandEnv(*addEnv);
+	if (node!=NULL) expandEnv.expandToInclude(node->env);
 #if GEOS_DEBUG
-	cerr<<"Node::createExpanded computed "<<expandEnv->toString()<<endl;
+	cerr<<"Node::createExpanded computed "<<expandEnv.toString()<<endl;
 #endif
-	Node *largerNode=createNode(expandEnv);
+	std::auto_ptr<Node> largerNode = createNode(expandEnv);
 	if (node!=NULL) largerNode->insertNode(node);
-	delete expandEnv;
 	return largerNode;
 }
 

Modified: trunk/source/index/quadtree/Root.cpp
===================================================================
--- trunk/source/index/quadtree/Root.cpp	2009-04-24 23:44:43 UTC (rev 2406)
+++ trunk/source/index/quadtree/Root.cpp	2009-04-25 00:23:06 UTC (rev 2407)
@@ -73,9 +73,11 @@
 	 *  have to expand the tree upward to contain the item.
 	 */
 	if (node==NULL || !node->getEnvelope()->contains(itemEnv)) {
-		Node *largerNode=Node::createExpanded(node,itemEnv);
+		std::auto_ptr<Node> largerNode = Node::createExpanded(node,
+							              itemEnv);
 		//delete subnode[index];
-		subnode[index]=largerNode;
+		// TODO: should subnode[] be an array of auto_ptrs ?
+		subnode[index]=largerNode.release();
 	}
 
 	/*



More information about the geos-commits mailing list