00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 00025 00026 #ifndef MLN_VALUE_VITER_HH 00027 # define MLN_VALUE_VITER_HH 00028 00036 # include <mln/core/concept/value_iterator.hh> 00037 # include <mln/core/concept/value_set.hh> 00038 00039 00040 namespace mln 00041 { 00042 00043 namespace value 00044 { 00045 00046 00051 template <typename S> 00052 struct fwd_viter_ : public Value_Iterator< fwd_viter_<S> > 00053 { 00055 typedef mln_value(S) value; 00056 00058 fwd_viter_(); 00059 00061 fwd_viter_(const Value_Set<S>& s); 00062 00063 void change_target(const S& s); 00064 00066 bool is_valid() const; 00067 00069 void invalidate(); 00070 00072 void start(); 00073 00075 void next_(); 00076 00078 operator mln_value(S) () const; 00079 00081 unsigned index_() const; 00082 00083 private: 00084 00085 const S* s_; 00086 unsigned i_; 00087 }; 00088 00089 00090 00091 00096 template <typename S> 00097 struct bkd_viter_ : public Value_Iterator< bkd_viter_<S> > 00098 { 00100 typedef mln_value(S) value; 00101 00103 bkd_viter_(); 00104 00106 bkd_viter_(const Value_Set<S>& s); 00107 00108 void change_target(const S& s); 00109 00111 bool is_valid() const; 00112 00114 void invalidate(); 00115 00117 void start(); 00118 00120 void next_(); 00121 00123 operator mln_value(S) () const; 00124 00126 unsigned index_() const; 00127 00128 private: 00129 00130 const S* s_; 00131 unsigned i_; 00132 }; 00133 00134 00135 00136 # ifndef MLN_INCLUDE_ONLY 00137 00138 00139 // fwd_viter_<S> 00140 00141 template <typename S> 00142 inline 00143 fwd_viter_<S>::fwd_viter_() 00144 : s_(0) 00145 { 00146 } 00147 00148 template <typename S> 00149 inline 00150 fwd_viter_<S>::fwd_viter_(const Value_Set<S>& s) 00151 { 00152 change_target(exact(s)); 00153 } 00154 00155 template <typename S> 00156 inline 00157 void 00158 fwd_viter_<S>::change_target(const S& s) 00159 { 00160 s_ = &s; 00161 invalidate(); 00162 } 00163 00164 template <typename S> 00165 inline 00166 bool 00167 fwd_viter_<S>::is_valid() const 00168 { 00169 return s_ != 0 && i_ < s_->nvalues(); 00170 } 00171 00172 template <typename S> 00173 inline 00174 void 00175 fwd_viter_<S>::invalidate() 00176 { 00177 i_ = s_->nvalues(); 00178 } 00179 00180 template <typename S> 00181 inline 00182 void 00183 fwd_viter_<S>::start() 00184 { 00185 i_ = 0; 00186 } 00187 00188 template <typename S> 00189 inline 00190 void 00191 fwd_viter_<S>::next_() 00192 { 00193 ++i_; 00194 } 00195 00196 template <typename S> 00197 inline 00198 fwd_viter_<S>::operator mln_value(S) () const 00199 { 00200 mln_precondition(is_valid()); 00201 return (*s_)[i_]; 00202 } 00203 00204 template <typename S> 00205 inline 00206 unsigned 00207 fwd_viter_<S>::index_() const 00208 { 00209 return i_; 00210 } 00211 00212 00213 // bkd_viter_<S> 00214 00215 template <typename S> 00216 inline 00217 bkd_viter_<S>::bkd_viter_() 00218 : s_(0) 00219 { 00220 } 00221 00222 template <typename S> 00223 inline 00224 bkd_viter_<S>::bkd_viter_(const Value_Set<S>& s) 00225 { 00226 change_target(exact(s)); 00227 } 00228 00229 template <typename S> 00230 inline 00231 void 00232 bkd_viter_<S>::change_target(const S& s) 00233 { 00234 s_ = &s; 00235 invalidate(); 00236 } 00237 00238 template <typename S> 00239 inline 00240 bool 00241 bkd_viter_<S>::is_valid() const 00242 { 00243 return s_ != 0 && i_ != s_->nvalues(); 00244 } 00245 00246 template <typename S> 00247 inline 00248 void 00249 bkd_viter_<S>::invalidate() 00250 { 00251 i_ = s_->nvalues(); 00252 } 00253 00254 template <typename S> 00255 inline 00256 void 00257 bkd_viter_<S>::start() 00258 { 00259 i_ = s_->nvalues() - 1; 00260 } 00261 00262 template <typename S> 00263 inline 00264 void 00265 bkd_viter_<S>::next_() 00266 { 00267 if (i_ == 0) 00268 { 00269 invalidate(); 00270 return; 00271 } 00272 --i_; 00273 } 00274 00275 template <typename S> 00276 inline 00277 bkd_viter_<S>::operator mln_value(S) () const 00278 { 00279 mln_precondition(is_valid()); 00280 return (*s_)[i_]; 00281 } 00282 00283 template <typename S> 00284 inline 00285 unsigned 00286 bkd_viter_<S>::index_() const 00287 { 00288 return i_; 00289 } 00290 00291 # endif // ! MLN_INCLUDE_ONLY 00292 00293 } // end of namespace mln::value 00294 00295 } // end of namespace mln 00296 00297 00298 #endif // ! MLN_VALUE_VITER_HH