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>
27 #include <ostream> // for operator<<
28 #include <memory> // for auto_ptr
32 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
35 // Forward declarations
44 namespace geom
{ // geos::geom
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
52 * The class keeps ownership of the Coordinates.
55 class GEOS_DLL CoordinateList
{
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
);
67 * Constructs a new list from an array of Coordinates, allowing
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())
87 size_type
size() const
94 return coords
.empty();
99 return coords
.begin();
107 const_iterator
begin() const
109 return coords
.begin();
112 const_iterator
end() const
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());
162 if(!coords
.empty() && ! (*(coords
.begin())).equals(*(coords
.rbegin())))
164 const Coordinate
&c
= *(coords
.begin());
165 coords
.insert(coords
.end(),c
);
172 std::list
<Coordinate
> coords
;
176 std::ostream
& operator<< (std::ostream
& os
, const CoordinateList
& cl
)
179 for (CoordinateList::const_iterator
180 it
=cl
.begin(), end
=cl
.end();
184 const Coordinate
& c
= *it
;
185 if ( it
!= cl
.begin() ) os
<< ", ";
193 } // namespace geos::geom
200 #endif // ndef GEOS_GEOM_COORDINATELIST_H