Milena (Olena)
User documentation 2.0a Id
|
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_VALUE_VALUE_ARRAY_HH 00027 # define MLN_VALUE_VALUE_ARRAY_HH 00028 00032 00033 # include <mln/value/set.hh> 00034 00035 00036 namespace mln 00037 { 00038 00039 namespace value 00040 { 00041 00042 00044 template <typename T, typename V> 00045 struct value_array 00046 { 00047 enum { 00048 nvalues = mln_card(T) 00049 }; 00050 00053 value_array(); 00054 value_array(const V& v); 00055 value_array(const value_array<T, V>& other); 00056 value_array& operator=(const value_array<T, V>& other); 00058 00061 const V& operator()(const T& v) const; 00062 V& operator()(const T& v); 00064 00067 const V& operator[](unsigned i) const; 00068 V& operator[](unsigned i); 00070 00072 const mln::value::set<T>& vset() const; 00073 00074 protected: 00075 00076 const mln::value::set<T>& s_; 00077 V v_[nvalues]; 00078 }; 00079 00080 00081 template <typename T, typename V> 00082 std::ostream& operator<<(std::ostream& ostr, const value_array<T, V>& a); 00083 00084 00085 # ifndef MLN_INCLUDE_ONLY 00086 00087 template <typename T, typename V> 00088 inline 00089 value_array<T,V>::value_array() 00090 : s_ (mln::value::set<T>::the()) 00091 { 00092 typedef value::internal::iterable_set< T, set<T> > U; 00093 mlc_is(set<T>, U)::check(); 00094 } 00095 00096 template <typename T, typename V> 00097 inline 00098 value_array<T,V>::value_array(const V& v) 00099 : s_(mln::value::set<T>::the()) 00100 { 00101 typedef value::internal::iterable_set< T, set<T> > U; 00102 mlc_is(set<T>, U)::check(); 00103 00104 memset(v_, v, nvalues * sizeof(V)); 00105 } 00106 00107 template <typename T, typename V> 00108 inline 00109 value_array<T,V>::value_array(const value_array<T, V>& other) 00110 : s_(other.s_) 00111 { 00112 memcpy(v_, other.v_, nvalues * sizeof(V)); 00113 } 00114 00115 template <typename T, typename V> 00116 inline 00117 value_array<T,V>& 00118 value_array<T,V>::operator=(const value_array<T, V>& other) 00119 { 00120 if (&other != this) 00121 memcpy(v_, other.v_, nvalues * sizeof(V)); 00122 return *this; 00123 } 00124 00125 template <typename T, typename V> 00126 inline 00127 const V& 00128 value_array<T,V>::operator()(const T& v) const 00129 { 00130 return v_[s_.index_of(v)]; 00131 } 00132 00133 template <typename T, typename V> 00134 inline 00135 V& 00136 value_array<T,V>::operator()(const T& v) 00137 { 00138 return v_[s_.index_of(v)]; 00139 } 00140 00141 template <typename T, typename V> 00142 inline 00143 const mln::value::set<T>& 00144 value_array<T,V>::vset() const 00145 { 00146 return s_; 00147 } 00148 00149 template <typename T, typename V> 00150 inline 00151 const V& 00152 value_array<T,V>::operator[](unsigned i) const 00153 { 00154 mln_precondition(i < nvalues); 00155 return v_[i]; 00156 } 00157 00158 template <typename T, typename V> 00159 inline 00160 V& 00161 value_array<T,V>::operator[](unsigned i) 00162 { 00163 mln_precondition(i < nvalues); 00164 return v_[i]; 00165 } 00166 00167 template <typename T, typename V> 00168 inline 00169 std::ostream& operator<<(std::ostream& ostr, const value_array<T,V>& a) 00170 { 00171 mln_viter(mln::value::set<T>) v(a.vset()); 00172 for_all(v) 00173 ostr << v << ':' << h(v) << ' '; 00174 return ostr; 00175 } 00176 00177 # endif // ! MLN_INCLUDE_ONLY 00178 00179 } // end of namespace mln::value 00180 00181 } // end of namespace mln 00182 00183 00184 #endif // ! MLN_VALUE_VALUE_ARRAY_HH