00001 // Copyright (C) 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_ACCU_COUNT_VALUE_HH 00027 # define MLN_ACCU_COUNT_VALUE_HH 00028 00032 00033 # include <mln/accu/internal/base.hh> 00034 # include <mln/core/concept/meta_accumulator.hh> 00035 # include <mln/metal/is_a.hh> 00036 00037 namespace mln 00038 { 00039 00040 namespace accu 00041 { 00042 00046 // 00047 template <typename V> 00048 struct count_value 00049 : public mln::accu::internal::base< unsigned , count_value<V> > 00050 { 00051 typedef V argument; 00052 00053 count_value(); 00054 explicit count_value(const V& ref); 00055 00058 void init(); 00059 void take(const argument&); 00060 void take(const count_value<V>& other); 00061 void untake(const argument&); 00062 void untake(const count_value<V>& other); 00063 00065 void set_value(unsigned c); 00067 00069 unsigned to_result() const; 00070 00073 bool is_valid() const; 00074 00075 protected: 00077 unsigned count_; 00079 V ref_; 00080 00082 bool valid_; 00083 }; 00084 00085 00086 namespace meta 00087 { 00088 00092 struct count_value : public Meta_Accumulator< count_value > 00093 { 00094 00095 template <typename V> 00096 struct with 00097 { 00098 typedef accu::count_value<V> ret; 00099 }; 00100 00101 }; 00102 00103 } // end of namespace mln::accu::meta 00104 00105 00106 00107 # ifndef MLN_INCLUDE_ONLY 00108 00109 template <typename V> 00110 inline 00111 count_value<V>::count_value() 00112 { 00113 init(); 00114 valid_ = false; 00115 } 00116 00117 template <typename V> 00118 inline 00119 count_value<V>::count_value(const V& ref) 00120 { 00121 ref_ = ref; 00122 valid_ = true; 00123 init(); 00124 } 00125 00126 template <typename V> 00127 inline 00128 void 00129 count_value<V>::init() 00130 { 00131 count_ = 0; 00132 } 00133 00134 template <typename V> 00135 inline 00136 void 00137 count_value<V>::take(const argument& l) 00138 { 00139 if (l == ref_) 00140 ++count_; 00141 } 00142 00143 template <typename V> 00144 inline 00145 void 00146 count_value<V>::take(const count_value<V>& other) 00147 { 00148 mln_precondition(other.is_valid()); 00149 mln_precondition(other.ref_ == ref_); 00150 count_ += other.count_; 00151 } 00152 00153 template <typename V> 00154 inline 00155 void 00156 count_value<V>::untake(const argument& l) 00157 { 00158 if (l == ref_) 00159 --count_; 00160 } 00161 00162 template <typename V> 00163 inline 00164 void 00165 count_value<V>::untake(const count_value<V>& other) 00166 { 00167 mln_precondition(other.is_valid()); 00168 mln_precondition(other.ref_ == ref_); 00169 count_ -= other.count_; 00170 } 00171 00172 template <typename V> 00173 inline 00174 unsigned 00175 count_value<V>::to_result() const 00176 { 00177 // The background label MUST not be counted. 00178 return count_; 00179 } 00180 00181 template <typename V> 00182 inline 00183 void 00184 count_value<V>::set_value(unsigned c) 00185 { 00186 count_ = c; 00187 } 00188 00189 template <typename V> 00190 inline 00191 bool 00192 count_value<V>::is_valid() const 00193 { 00194 return valid_; 00195 } 00196 00197 # endif // ! MLN_INCLUDE_ONLY 00198 00199 } // end of namespace mln::accu 00200 00201 } // end of namespace mln 00202 00203 00204 #endif // ! MLN_ACCU_COUNT_VALUE_HH