Coordinates.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (c) 2009-2011 C.B. Barber. All rights reserved.
00004 ** $Id: //main/2011/qhull/src/libqhullcpp/Coordinates.cpp#2 $$Change: 1342 $
00005 ** $DateTime: 2011/03/07 21:55:47 $$Author: bbarber $
00006 **
00007 ****************************************************************************/
00008 
00009 #include "functionObjects.h"
00010 #include "QhullError.h"
00011 #include "Coordinates.h"
00012 
00013 #include <iostream>
00014 #include <iterator>
00015 #include <algorithm>
00016 
00017 #ifdef _MSC_VER  // Microsoft Visual C++ -- warning level 4
00018 #endif
00019 
00020 namespace orgQhull {
00021 
00022 #//! Coordinates -- vector of coordT (normally double)
00023 
00024 #//Element access
00025 
00026 // Inefficient without result-value-optimization or implicitly shared object
00027 Coordinates Coordinates::
00028 mid(int idx, int length) const
00029 {
00030     int newLength= length;
00031     if(length<0 || idx+length > count()){
00032         newLength= count()-idx;
00033     }
00034     Coordinates result;
00035     if(newLength>0){
00036         std::copy(begin()+idx, begin()+(idx+newLength), std::back_inserter(result));
00037     }
00038     return result;
00039 }//mid
00040 
00041 coordT Coordinates::
00042 value(int idx, const coordT &defaultValue) const
00043 {
00044     return ((idx < 0 || idx >= count()) ? defaultValue : (*this)[idx]);
00045 }//value
00046 
00047 #//Operator
00048 
00049 Coordinates Coordinates::
00050 operator+(const Coordinates &other) const
00051 {
00052     Coordinates result(*this);
00053     std::copy(other.begin(), other.end(), std::back_inserter(result));
00054     return result;
00055 }//operator+
00056 
00057 Coordinates & Coordinates::
00058 operator+=(const Coordinates &other)
00059 {
00060     if(&other==this){
00061         Coordinates clone(other);
00062         std::copy(clone.begin(), clone.end(), std::back_inserter(*this));
00063     }else{
00064         std::copy(other.begin(), other.end(), std::back_inserter(*this));
00065     }
00066     return *this;
00067 }//operator+=
00068 
00069 #//Read-write
00070 
00071 coordT Coordinates::
00072 takeAt(int idx)
00073 {
00074     coordT c= at(idx);
00075     erase(begin()+idx);
00076     return c;
00077 }//takeAt
00078 
00079 coordT Coordinates::
00080 takeLast()
00081 {
00082     coordT c= last();
00083     removeLast();
00084     return c;
00085 }//takeLast
00086 
00087 void Coordinates::
00088 swap(int idx, int other)
00089 {
00090     coordT c= at(idx);
00091     at(idx)= at(other);
00092     at(other)= c;
00093 }//swap
00094 
00095 #//Search
00096 
00097 bool Coordinates::
00098 contains(const coordT &t) const
00099 {
00100     CoordinatesIterator i(*this);
00101     return i.findNext(t);
00102 }//contains
00103 
00104 int Coordinates::
00105 count(const coordT &t) const
00106 {
00107     CoordinatesIterator i(*this);
00108     int result= 0;
00109     while(i.findNext(t)){
00110         ++result;
00111     }
00112     return result;
00113 }//count
00114 
00115 int Coordinates::
00116 indexOf(const coordT &t, int from) const
00117 {
00118     if(from<0){
00119         from += count();
00120         if(from<0){
00121             from= 0;
00122         }
00123     }
00124     if(from<count()){
00125         const_iterator i= begin()+from;
00126         while(i!=constEnd()){
00127             if(*i==t){
00128                 return (static_cast<int>(i-begin())); // WARN64
00129             }
00130             ++i;
00131         }
00132     }
00133     return -1;
00134 }//indexOf
00135 
00136 int Coordinates::
00137 lastIndexOf(const coordT &t, int from) const
00138 {
00139     if(from<0){
00140         from += count();
00141     }else if(from>=count()){
00142         from= count()-1;
00143     }
00144     if(from>=0){
00145         const_iterator i= begin()+from+1;
00146         while(i-- != constBegin()){
00147             if(*i==t){
00148                 return (static_cast<int>(i-begin())); // WARN64
00149             }
00150         }
00151     }
00152     return -1;
00153 }//lastIndexOf
00154 
00155 void Coordinates::
00156 removeAll(const coordT &t)
00157 {
00158     MutableCoordinatesIterator i(*this);
00159     while(i.findNext(t)){
00160         i.remove();
00161     }
00162 }//removeAll
00163 
00164 }//namespace orgQhull
00165 
00166 #//Global functions
00167 
00168 using std::endl;
00169 using std::istream;
00170 using std::ostream;
00171 using std::string;
00172 using std::ws;
00173 using orgQhull::Coordinates;
00174 
00175 ostream &
00176 operator<<(ostream &os, const Coordinates &cs)
00177 {
00178     Coordinates::const_iterator c= cs.begin();
00179     for(int i=cs.count(); i--; ){
00180         os << *c++ << " ";
00181     }
00182     return os;
00183 }//operator<<
00184 


libqhull-ours
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:10