GEOS  3.4.2
CoordinateList.h
00001 /**********************************************************************
00002  *
00003  * GEOS - Geometry Engine Open Source
00004  * http://geos.osgeo.org
00005  *
00006  * Copyright (C) 2010 Sandro Santilli <strk@keybit.net>
00007  * Copyright (C) 2006 Refractions Research Inc.
00008  *
00009  * This is free software; you can redistribute and/or modify it under
00010  * the terms of the GNU Lesser General Public Licence as published
00011  * by the Free Software Foundation. 
00012  * See the COPYING file for more information.
00013  *
00014  **********************************************************************
00015  *
00016  * Last port: geom/CoordinateList.java ?? (never been in complete sync)
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef GEOS_GEOM_COORDINATELIST_H
00021 #define GEOS_GEOM_COORDINATELIST_H
00022 
00023 #include <geos/export.h>
00024 #include <geos/geom/Coordinate.h> 
00025 
00026 #include <list>
00027 #include <ostream> // for operator<<
00028 #include <memory> // for auto_ptr 
00029 
00030 #ifdef _MSC_VER
00031 #pragma warning(push)
00032 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
00033 #endif
00034 
00035 // Forward declarations
00036 namespace geos {
00037         namespace geom { 
00038                 //class Coordinate;
00039         }
00040 }
00041 
00042 
00043 namespace geos {
00044 namespace geom { // geos::geom
00045 
00055 class GEOS_DLL CoordinateList {
00056 
00057 public:
00058 
00059         typedef std::list<Coordinate>::iterator iterator;
00060         typedef std::list<Coordinate>::const_iterator const_iterator;
00061         typedef std::list<Coordinate>::size_type size_type;
00062 
00063         friend std::ostream& operator<< (std::ostream& os,
00064                 const CoordinateList& cl);
00065 
00075         CoordinateList(const std::vector<Coordinate>& v)
00076                 :
00077                 coords(v.begin(), v.end())
00078         {
00079         }
00080 
00081         CoordinateList()
00082                 :
00083                 coords()
00084         {
00085         }
00086 
00087         size_type size() const
00088         {
00089                 return coords.size();
00090         }
00091 
00092         bool empty() const
00093         {
00094                 return coords.empty();
00095         }
00096 
00097         iterator begin()
00098         {
00099                 return coords.begin();
00100         }
00101 
00102         iterator end()
00103         {
00104                 return coords.end();
00105         }
00106 
00107         const_iterator begin() const
00108         {
00109                 return coords.begin();
00110         }
00111 
00112         const_iterator end() const
00113         {
00114                 return coords.end();
00115         }
00116 
00130         iterator insert(iterator pos, const Coordinate& c, bool allowRepeated)
00131         {
00132                 if ( !allowRepeated && pos != coords.begin() ) {
00133                         iterator prev = pos; --prev;
00134                         if ( c.equals2D(*prev) ) return prev;
00135                 }
00136                 return coords.insert(pos, c);
00137         }
00138 
00139         iterator insert(iterator pos, const Coordinate& c)
00140         {
00141                 return coords.insert(pos, c);
00142         }
00143 
00144         iterator erase(iterator pos)
00145         {
00146                 return coords.erase(pos);
00147         }
00148 
00149         iterator erase(iterator first, iterator last)
00150         {
00151                 return coords.erase(first, last);
00152         }
00153 
00154         std::auto_ptr<Coordinate::Vect> toCoordinateArray() const
00155         {
00156                 std::auto_ptr<Coordinate::Vect> ret(new Coordinate::Vect);
00157                 ret->assign(coords.begin(), coords.end());
00158                 return ret;
00159         }
00160         void closeRing()
00161         {   
00162                 if(!coords.empty() && ! (*(coords.begin())).equals(*(coords.rbegin())))
00163                 {   
00164                         const Coordinate &c = *(coords.begin());
00165                         coords.insert(coords.end(),c);
00166                 }   
00167         }   
00168 
00169 
00170 private:
00171 
00172         std::list<Coordinate> coords;
00173 };
00174 
00175 inline
00176 std::ostream& operator<< (std::ostream& os, const CoordinateList& cl)
00177 {
00178         os << "(";
00179         for (CoordinateList::const_iterator
00180                 it=cl.begin(), end=cl.end();
00181                 it != end;
00182                 ++it)
00183         {
00184                 const Coordinate& c = *it;
00185                 if ( it != cl.begin() ) os << ", ";
00186                 os << c;
00187         }
00188         os << ")";
00189 
00190         return os;
00191 }
00192 
00193 } // namespace geos::geom
00194 } // namespace geos
00195 
00196 #ifdef _MSC_VER
00197 #pragma warning(pop)
00198 #endif
00199 
00200 #endif // ndef GEOS_GEOM_COORDINATELIST_H