SyFi
0.3
|
00001 // Copyright (C) 2006-2009 Kent-Andre Mardal and Simula Research Laboratory 00002 // 00003 // This file is part of SyFi. 00004 // 00005 // SyFi is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 2 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // SyFi is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with SyFi. If not, see <http://www.gnu.org/licenses/>. 00017 00018 #include "Ptv.h" 00019 #include "OrderedPtvSet.h" 00020 #include <vector> 00021 #include <stdexcept> 00022 #include <algorithm> 00023 00024 namespace SyFi 00025 { 00026 00027 // --- OrderedPtvSet 00028 00029 OrderedPtvSet:: OrderedPtvSet() 00030 { 00031 } 00032 00033 OrderedPtvSet::~OrderedPtvSet() 00034 { 00035 Ptvs.clear(); 00036 } 00037 00038 OrderedPtvSet:: OrderedPtvSet(const Ptv& p0, const Ptv& p1) 00039 { 00040 Ptvs.push_back(p0); 00041 Ptvs.push_back(p1); 00042 std::sort(Ptvs.begin(), Ptvs.end(), Ptv_is_less()); 00043 } 00044 00045 OrderedPtvSet:: OrderedPtvSet(const Ptv& p0, const Ptv& p1, const Ptv& p2) 00046 { 00047 Ptvs.push_back(p0); 00048 Ptvs.push_back(p1); 00049 Ptvs.push_back(p2); 00050 std::sort(Ptvs.begin(), Ptvs.end(), Ptv_is_less()); 00051 } 00052 00053 OrderedPtvSet:: OrderedPtvSet(const Ptv& p0, const Ptv& p1, const Ptv& p2, const Ptv& p3) 00054 { 00055 Ptvs.push_back(p0); 00056 Ptvs.push_back(p1); 00057 Ptvs.push_back(p2); 00058 Ptvs.push_back(p3); 00059 std::sort(Ptvs.begin(), Ptvs.end(), Ptv_is_less()); 00060 } 00061 00062 void OrderedPtvSet:: append(const Ptv& p) 00063 { 00064 Ptvs.push_back(p); 00065 std::sort(Ptvs.begin(), Ptvs.end(), Ptv_is_less()); 00066 } 00067 00068 unsigned int OrderedPtvSet:: size() const 00069 { 00070 return Ptvs.size(); 00071 } 00072 00073 const Ptv& OrderedPtvSet:: operator [] (unsigned int i) const 00074 { 00075 return Ptvs[i]; 00076 } 00077 00078 Ptv& OrderedPtvSet:: operator [] (unsigned int i) 00079 { 00080 return Ptvs[i]; 00081 } 00082 00083 OrderedPtvSet& OrderedPtvSet::operator = (const OrderedPtvSet& p) 00084 { 00085 if ( this != &p) 00086 { 00087 Ptvs.clear(); 00088 for (unsigned int i=0; i<p.size(); i++) 00089 { 00090 Ptvs.push_back(p[i]); 00091 } 00092 } 00093 return *this; 00094 } 00095 00096 bool OrderedPtvSet:: less(const OrderedPtvSet& p) const 00097 { 00098 if ( Ptvs.size() < p.size() ) return true ; 00099 if ( Ptvs.size() > p.size() ) return false; 00100 00101 for (unsigned int i=Ptvs.size()-1; i>= 0; i--) 00102 { 00103 if ( Ptvs[i].less(p[i]) && p[i].less(Ptvs[i])) 00104 { 00105 } 00106 else if ( Ptvs[i].less(p[i]) ) 00107 { 00108 return true; 00109 } 00110 else if ( p[i].less(Ptvs[i]) ) 00111 { 00112 return false; 00113 } 00114 } 00115 return false; 00116 } 00117 00118 std::ostream & operator<< ( std::ostream& os, const OrderedPtvSet& p) 00119 { 00120 if (p.size() >= 1) 00121 { 00122 os <<"["; 00123 for (unsigned int i=0; i< p.size()-1; i++) 00124 { 00125 os <<p[i]<<","; 00126 } 00127 os <<p[p.size()-1]<<"]"; 00128 } 00129 else 00130 { 00131 os <<"OrderedPtvSet not created properly"<<std::endl; 00132 } 00133 return os; 00134 } 00135 00136 // --- OrderedPtvSet_i 00137 00138 OrderedPtvSet_i :: OrderedPtvSet_i () 00139 { 00140 } 00141 00142 OrderedPtvSet_i::~OrderedPtvSet_i() 00143 { 00144 } 00145 00146 OrderedPtvSet_i :: OrderedPtvSet_i (OrderedPtvSet& s, unsigned int i) 00147 { 00148 si.first = s; 00149 si.second.push_back(i); 00150 } 00151 00152 OrderedPtvSet_i :: OrderedPtvSet_i (OrderedPtvSet& s, unsigned int i0, unsigned int i1) 00153 { 00154 si.first = s; 00155 si.second.push_back(i0); 00156 si.second.push_back(i1); 00157 } 00158 00159 const OrderedPtvSet& OrderedPtvSet_i:: get_OrderedPtvSet() const 00160 { 00161 return si.first; 00162 } 00163 00164 unsigned int OrderedPtvSet_i:: get_i(unsigned int n) const 00165 { 00166 if ( n >= 0 && n < si.second.size() ) 00167 { 00168 return si.second[n]; 00169 } 00170 else 00171 { 00172 00173 throw(std::out_of_range("The index is out of range!")); 00174 00175 } 00176 } 00177 00178 unsigned int OrderedPtvSet_i:: size() const 00179 { 00180 return si.second.size(); 00181 } 00182 00183 bool OrderedPtvSet_i::less(const OrderedPtvSet_i& ss) const 00184 { 00185 if ( si.second.size() < ss.size() ) return true; 00186 if ( si.second.size() > ss.size() ) return false; 00187 00188 for (unsigned int d=0; d< si.second.size(); d++) 00189 { 00190 if ( si.second[d] < ss.get_i(d) ) return true; 00191 if ( si.second[d] > ss.get_i(d) ) return false; 00192 } 00193 00194 return si.first.less(ss.get_OrderedPtvSet()); 00195 00196 } 00197 00198 std::ostream & operator<< ( std::ostream& os, const OrderedPtvSet_i& p) 00199 { 00200 cout <<p.get_OrderedPtvSet(); 00201 if (p.size() >= 1) 00202 { 00203 os <<",["; 00204 for (unsigned int i=0; i< p.size()-1; i++) 00205 { 00206 os <<p.get_i(i)<<","; 00207 } 00208 os <<p.get_i(p.size()-1)<<"]"; 00209 } 00210 else 00211 { 00212 os <<"OrderedPtvSet_i not created properly"<<std::endl; 00213 } 00214 return os; 00215 } 00216 00217 } //namespace SyFi