Fix CoordinateList.closeRing() use of past-the-end operator
[geos.git] / include / geos / geom / CoordinateList.h
blob1b3ea3bee0d42193b2528d7f9d61e603ddaed85c
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2010 Sandro Santilli <strk@keybit.net>
7 * Copyright (C) 2006 Refractions Research Inc.
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
14 **********************************************************************
16 * Last port: geom/CoordinateList.java ?? (never been in complete sync)
18 **********************************************************************/
20 #ifndef GEOS_GEOM_COORDINATELIST_H
21 #define GEOS_GEOM_COORDINATELIST_H
23 #include <geos/export.h>
24 #include <geos/geom/Coordinate.h>
26 #include <list>
27 #include <ostream> // for operator<<
28 #include <memory> // for auto_ptr
30 #ifdef _MSC_VER
31 #pragma warning(push)
32 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
33 #endif
35 // Forward declarations
36 namespace geos {
37 namespace geom {
38 //class Coordinate;
43 namespace geos {
44 namespace geom { // geos::geom
46 /** \brief
47 * A list of {@link Coordinate}s, which may
48 * be set to prevent repeated coordinates from occuring in the list.
50 * Use this class when fast insertions and removal at arbitrary
51 * position is needed.
52 * The class keeps ownership of the Coordinates.
55 class GEOS_DLL CoordinateList {
57 public:
59 typedef std::list<Coordinate>::iterator iterator;
60 typedef std::list<Coordinate>::const_iterator const_iterator;
61 typedef std::list<Coordinate>::size_type size_type;
63 friend std::ostream& operator<< (std::ostream& os,
64 const CoordinateList& cl);
66 /** \brief
67 * Constructs a new list from an array of Coordinates, allowing
68 * repeated points.
70 * (I.e. this constructor produces a {@link CoordinateList} with
71 * exactly the same set of points as the input array.)
73 * @param v the initial coordinates
75 CoordinateList(const std::vector<Coordinate>& v)
77 coords(v.begin(), v.end())
81 CoordinateList()
83 coords()
87 size_type size() const
89 return coords.size();
92 bool empty() const
94 return coords.empty();
97 iterator begin()
99 return coords.begin();
102 iterator end()
104 return coords.end();
107 const_iterator begin() const
109 return coords.begin();
112 const_iterator end() const
114 return coords.end();
117 /** \brief
118 * Inserts the specified coordinate at the specified position in this list.
120 * @param pos the position at which to insert
121 * @param coord the coordinate to insert
122 * @param allowRepeated if set to false, repeated coordinates are collapsed
124 * @return an iterator to the newly installed coordinate
125 * (or previous, if equal and repeated are not allowed)
127 * NOTE: when allowRepeated is false _next_ point is not checked
128 * this matches JTS behavior
130 iterator insert(iterator pos, const Coordinate& c, bool allowRepeated)
132 if ( !allowRepeated && pos != coords.begin() ) {
133 iterator prev = pos; --prev;
134 if ( c.equals2D(*prev) ) return prev;
136 return coords.insert(pos, c);
139 iterator insert(iterator pos, const Coordinate& c)
141 return coords.insert(pos, c);
144 iterator erase(iterator pos)
146 return coords.erase(pos);
149 iterator erase(iterator first, iterator last)
151 return coords.erase(first, last);
154 std::auto_ptr<Coordinate::Vect> toCoordinateArray() const
156 std::auto_ptr<Coordinate::Vect> ret(new Coordinate::Vect);
157 ret->assign(coords.begin(), coords.end());
158 return ret;
160 void closeRing()
162 if(!coords.empty() && ! (*(coords.begin())).equals(*(coords.rbegin())))
164 const Coordinate &c = *(coords.begin());
165 coords.insert(coords.end(),c);
170 private:
172 std::list<Coordinate> coords;
175 inline
176 std::ostream& operator<< (std::ostream& os, const CoordinateList& cl)
178 os << "(";
179 for (CoordinateList::const_iterator
180 it=cl.begin(), end=cl.end();
181 it != end;
182 ++it)
184 const Coordinate& c = *it;
185 if ( it != cl.begin() ) os << ", ";
186 os << c;
188 os << ")";
190 return os;
193 } // namespace geos::geom
194 } // namespace geos
196 #ifdef _MSC_VER
197 #pragma warning(pop)
198 #endif
200 #endif // ndef GEOS_GEOM_COORDINATELIST_H