[geos-commits] r3155 - in trunk: include/geos/geom tests/unit/geom

svn_geos at osgeo.org svn_geos at osgeo.org
Fri Dec 3 08:57:00 EST 2010


Author: strk
Date: 2010-12-03 05:57:00 -0800 (Fri, 03 Dec 2010)
New Revision: 3155

Modified:
   trunk/include/geos/geom/CoordinateList.h
   trunk/tests/unit/geom/CoordinateListTest.cpp
Log:
Add method to insert coordinats into a CoordinateList w/out allowing duplicates (fixes issue #387)


Modified: trunk/include/geos/geom/CoordinateList.h
===================================================================
--- trunk/include/geos/geom/CoordinateList.h	2010-12-03 10:58:22 UTC (rev 3154)
+++ trunk/include/geos/geom/CoordinateList.h	2010-12-03 13:57:00 UTC (rev 3155)
@@ -11,6 +11,10 @@
  * by the Free Software Foundation. 
  * See the COPYING file for more information.
  *
+ **********************************************************************
+ *
+ * Last port: geom/CoordinateList.java ?? (never been in complete sync)
+ *
  **********************************************************************/
 
 #ifndef GEOS_GEOM_COORDINATELIST_H
@@ -34,8 +38,9 @@
 namespace geos {
 namespace geom { // geos::geom
 
-/**
- * A list of {@link Coordinate}s.
+/** \brief
+ * A list of {@link Coordinate}s, which may
+ * be set to prevent repeated coordinates from occuring in the list.
  *
  * Use this class when fast insertions and removal at arbitrary
  * position is needed.
@@ -53,6 +58,15 @@
 	friend std::ostream& operator<< (std::ostream& os,
 		const CoordinateList& cl);
 
+	/** \brief
+	 * Constructs a new list from an array of Coordinates, allowing
+	 * repeated points.
+	 *
+	 * (I.e. this constructor produces a {@link CoordinateList} with
+	 * exactly the same set of points as the input array.)
+	 *
+	 * @param v the initial coordinates
+	 */
 	CoordinateList(const std::vector<Coordinate>& v)
 		:
 		coords(v.begin(), v.end())
@@ -90,6 +104,28 @@
 		return coords.end();
 	}
 
+	/** \brief
+	 * Inserts the specified coordinate at the specified position in this list.
+	 *
+	 * @param pos the position at which to insert
+	 * @param coord the coordinate to insert
+	 * @param allowRepeated if set to false, repeated coordinates are collapsed
+	 *
+	 * @return an iterator to the newly installed coordinate
+	 *         (or previous, if equal and repeated are not allowed)
+	 * 
+	 * NOTE: when allowRepeated is false _next_ point is not checked
+	 *       this matches JTS behavior
+	 */
+	iterator insert(iterator pos, const Coordinate& c, bool allowRepeated)
+	{
+		if ( !allowRepeated && pos != coords.begin() ) {
+			iterator prev = pos; --prev;
+			if ( c.equals2D(*prev) ) return prev;
+		}
+		return coords.insert(pos, c);
+	}
+
 	iterator insert(iterator pos, const Coordinate& c)
 	{
 		return coords.insert(pos, c);

Modified: trunk/tests/unit/geom/CoordinateListTest.cpp
===================================================================
--- trunk/tests/unit/geom/CoordinateListTest.cpp	2010-12-03 10:58:22 UTC (rev 3154)
+++ trunk/tests/unit/geom/CoordinateListTest.cpp	2010-12-03 13:57:00 UTC (rev 3155)
@@ -103,5 +103,33 @@
 
 	}
 
+  // Test insert with and without duplicates
+  template<>
+  template<>
+  void object::test<2>()
+  {
+    using geos::geom::Coordinate;
 
+    geos::geom::CoordinateList clist;
+    ensure_equals( clist.size(), 0u );
+
+    clist.insert(clist.end(), Coordinate(0, 0));
+    ensure_equals( clist.size(), 1u );
+
+    clist.insert(clist.end(), Coordinate(0, 0), false);
+    ensure_equals( clist.size(), 1u );
+
+    clist.insert(clist.end(), Coordinate(0, 0), true);
+    ensure_equals( clist.size(), 2u );
+
+    clist.insert(clist.end(), Coordinate(1, 1), true);
+    ensure_equals( clist.size(), 3u );
+
+    geos::geom::CoordinateList::iterator it = clist.end(); 
+    --it;
+    clist.insert(it, Coordinate(0, 0), false);
+    ensure_equals( clist.size(), 3u );
+  }
+
+
 } // namespace tut



More information about the geos-commits mailing list