[geos-devel] Pass by reference in Java and C++

Martin Davis mbdavis at VividSolutions.com
Tue Nov 12 14:57:52 EST 2002


Dave has good points here.  Factory methods should do as little copying as possible.  They should expect to be handed object that they can "own", and they will return references to the constructed object which become the caller's responsibility to manage.  If copying is required to avoid unwanted aliasing, the caller is responsible for doing that.

Also, you're right about CoordinateList - the whole idea of having it is so that we can wrapper a client's own point format in a form that GEOS can work with. And after the CoordinateList has been constructed, it should never be copied again - the various factory methods should just wrapper it with the appropriate GEOS types (LinearRing, Polygon, etc).  Note that the destructor for a GEOS Geometry type should destroy any CoordinateList objects that it owns.

Dave, note that it's the caller's responsibility to make his own CoordinateList subtypes to wrapper whatever point format he happens to use.

I think now that we have okay performance we'd better focus on getting the API semantics correct, since they are going to become legacy very quickly.

Martin Davis, Senior Technical Specialist
Vivid Solutions Inc.
Suite #1A-2328 Government Street   Victoria, B.C.   V8T 5G5
Phone: (250) 385 6040    Fax: (250) 385 6046
EMail: mbdavis at vividsolutions.com  Web: www.vividsolutions.com


> -----Original Message-----
> From: David Blasby [mailto:dblasby at refractions.net]
> Sent: Tuesday, November 12, 2002 10:26 AM
> To: geos-devel at geos.refractions.net
> Subject: Re: [geos-devel] Pass by reference in Java and C++
> 
> 
> Martin Davis wrote:
> 
> > You got it right, Norman - in Java *everything* is passed 
> by reference, whereas in C++ you get a choice.    I think 
> when Yury did the initial translation of the code he stuck 
> very close to the Java way of doing things, partly because 
> it's hard to know without understanding the code fairly well 
> whether passing-by-reference is needed or not (i.e. sometimes 
> you really do want an alias to a shared object).
> >
> > It would certainly help for anyone who has time to look at 
> the code and see if there's obvious wins to be had by 
> switching to pass-by-value in particular cases.
> 
> I'm just starting playing around with creating GEOS 
> "Geometry" objects.  Here's a little snippet of code:
> 
> Geometry createGEOSPoint(POINT3D *pt)
> {
>  Coordinate *c = new Coordinate(pt->x, pt->y, pt->z);
> 
>  return geomFactory->createPoint(*c);
> }
> 
> 
> This causes the Coordinate to be copied (to the createPoint() 
> constructor), and the resulting Geometry to be copied out of 
> the function.  It would be much more efficient to:
> 
> Geometry *createGEOSPoint(POINT3D *pt)
> {
>  Coordinate *c = new Coordinate(pt->x, pt->y, pt->z);
> 
>  return geomFactory->createPoint(c);
> }
> 
> NOTE:   Geometry *createPoint( *Coordinate)
>        =>  geomFactory->createPoint() returning a Geometry* 
> and taking a Coordinate*.
> 
> Or am I missing something?
> 
> 
> 
> Also, the CoordinateList should have a constructor like:
> 
> cl = new CoordinateList3D(POINT3D* points, int npoints);
> cl = new CoordinateList2D(POINT2D* points, int npoints);
> 
> Where POINT3D is a simple {double x,y,z} struct, and POINT2D 
> is a simple {double x, double y}.
> 
> I dont really want to take my list of 100,000 points, convert 
> them all to Coordindates, then make a CoordinateList, then a 
> LinearRing, copy the linearRing over to the Polygon creator, 
> then make a copy of the polygon to actually return.  Seems 
> like an lot of work.
> 
> Is this the way to actually construct Geometry objects?  Or 
> am I missing something?
> 
> 
> dave
> ps. For the C/C++ non-programmers - Geometry* means 
> Pointer-to-Geometry.
> 
> 
> _______________________________________________
> geos-devel mailing list
> geos-devel at geos.refractions.net
> http://geos.refractions.net/mailman/listinfo/geos-devel
> 




More information about the geos-devel mailing list