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_LABELS_HH 00027 # define MLN_ACCU_COUNT_LABELS_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 // Forward declaration. 00041 namespace value { template <typename E> struct Symbolic; } 00042 00043 namespace accu 00044 { 00045 00050 // 00051 template <typename L> 00052 struct count_labels 00053 : public mln::accu::internal::base< unsigned , count_labels<L> >, 00054 mlc_is_a(L, mln::value::Symbolic)::check_t 00055 { 00056 typedef L argument; 00057 00058 count_labels(); 00059 00062 void init(); 00063 void take(const argument&); 00064 void take(const count_labels<L>& other); 00065 00067 void set_value(unsigned c); 00069 00071 unsigned to_result() const; 00072 00075 bool is_valid() const; 00076 00077 protected: 00079 unsigned count_labels_; 00080 std::vector<bool> deja_vu_; 00081 }; 00082 00083 00084 namespace meta 00085 { 00086 00088 struct count_labels : public Meta_Accumulator< count_labels > 00089 { 00090 template <typename L> 00091 struct with 00092 { 00093 typedef accu::count_labels<L> ret; 00094 }; 00095 }; 00096 00097 } // end of namespace mln::accu::meta 00098 00099 00100 00101 # ifndef MLN_INCLUDE_ONLY 00102 00103 template <typename L> 00104 inline 00105 count_labels<L>::count_labels() 00106 { 00107 init(); 00108 } 00109 00110 template <typename L> 00111 inline 00112 void 00113 count_labels<L>::init() 00114 { 00115 count_labels_ = 0; 00116 deja_vu_.resize(mln_max(L), false); 00117 } 00118 00119 template <typename L> 00120 inline 00121 void 00122 count_labels<L>::take(const argument& l) 00123 { 00124 if (!deja_vu_[l]) 00125 { 00126 ++count_labels_; 00127 deja_vu_[l] = true; 00128 } 00129 //else 00130 // No-op 00131 } 00132 00133 template <typename L> 00134 inline 00135 void 00136 count_labels<L>::take(const count_labels<L>& other) 00137 { 00138 count_labels_ += other.count_labels_; 00139 for (unsigned i = 0; i < deja_vu_.size(); ++i) 00140 deja_vu_[i] = deja_vu_[i] || other.deja_vu_[i]; 00141 } 00142 00143 template <typename L> 00144 inline 00145 unsigned 00146 count_labels<L>::to_result() const 00147 { 00148 // The background label MUST not be counted. 00149 return count_labels_ - 1; 00150 } 00151 00152 template <typename L> 00153 inline 00154 void 00155 count_labels<L>::set_value(unsigned c) 00156 { 00157 count_labels_ = c; 00158 } 00159 00160 template <typename L> 00161 inline 00162 bool 00163 count_labels<L>::is_valid() const 00164 { 00165 return true; 00166 } 00167 00168 # endif // ! MLN_INCLUDE_ONLY 00169 00170 } // end of namespace mln::accu 00171 00172 } // end of namespace mln 00173 00174 00175 #endif // ! MLN_ACCU_COUNT_LABELS_HH