00001 #ifndef GRAL_BASE_GB_GRID_FUNCTION_HASH_H
00002 #define GRAL_BASE_GB_GRID_FUNCTION_HASH_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "Container/my-hash-map.h"
00020
00021 #include "Utility/pre-post-conditions.h"
00022 #include "Utility/ref-ptr.h"
00023 #include "Gral/Base/common-grid-basics.h"
00024
00025 namespace GrAL {
00026
00058 template<class E, class T>
00059 class grid_function_hash_base {
00060 public:
00061
00062 typedef element_traits<E> et;
00063 typedef E element_type;
00064 typedef typename et::hasher_type hasher_type;
00065 typedef typename et::grid_type grid_type;
00066
00067
00068 typedef STDHASH::hash_map<E,T, hasher_type> table_type;
00069 typedef typename table_type::size_type size_type;
00070 typedef T value_type;
00071
00072
00073 typedef T & reference;
00074 typedef T const& const_reference;
00075 typedef typename table_type::pointer pointer;
00076 typedef typename table_type::difference_type difference_type;
00077
00078 typedef E const& argument_type;
00079 typedef const_reference result_type;
00080
00081 protected:
00082
00083 ref_ptr<grid_type const> g;
00084 table_type table;
00085
00086 public:
00087 grid_function_hash_base() : table() {}
00088 grid_function_hash_base(const grid_type& gg)
00089 : g(gg), table() {}
00090 grid_function_hash_base(ref_ptr<grid_type const> gg)
00091 : g(gg), table() {}
00092
00093
00094 typedef grid_function_hash_base<E,T> gfc2dh;
00095 grid_function_hash_base(const gfc2dh& rs) : g(rs.g), table(rs.table) {}
00096 gfc2dh& operator=(const gfc2dh& rs)
00097 {
00098 if (this != &rs) {
00099 g =rs.g;
00100 table = rs.table;
00101 }
00102 return *this;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112 void set_grid(ref_ptr<grid_type const> gg) {
00113
00114 g = gg;
00115 }
00116 void set_grid(const grid_type& gg) { set_grid(ref_ptr<grid_type const>(gg));}
00117
00118
00119 const grid_type& TheGrid() const {
00120 REQUIRE((g != 0), "No grid!\n",1);
00121 return *g;
00122 }
00123
00124
00125 bool defined(const E& e) const { return (table.find(e) != table.end());}
00126 void undefine(const E& e) {
00127 table.erase(e);
00128 ENSURE( (! defined(e)), "undefine(e), but e is still defined!", 1);
00129 }
00130
00131
00132
00133 unsigned size() const { return table.size();}
00134 bool empty() const { return table.empty();}
00135 void clear() { table.clear(); ENSURE_ALWAYS(empty(), "",1); }
00136
00137 typedef typename table_type::iterator base_it;
00138 class iter : public base_it {
00139 public:
00140 iter() {}
00141 iter(const base_it& i) : base_it(i) {}
00142 T& operator*() const { return (base_it::operator*()).second;}
00143 };
00144
00145 typedef typename table_type::const_iterator base_cit;
00146 class const_iter : public base_cit {
00147 private:
00148 public:
00149 const_iter() {}
00150 const_iter(const iter& i) : base_cit(i) {}
00151 const_iter(const base_cit& i) : base_cit(i) {}
00152 const T& operator*() const { return (base_cit::operator*()).second;}
00153 };
00154
00155 iter begin() { return iter (table.begin());}
00156 iter end() { return iter (table.end());}
00157 const_iter begin() const { return const_iter(table.begin());}
00158 const_iter end() const { return const_iter(table.end());}
00159
00160
00161
00162
00163
00164 typedef iter iterator;
00165 typedef const_iter const_iterator;
00166 };
00167
00168
00169
00170
00171
00183 template<class E, class T>
00184 class grid_function_hash
00185 : public grid_function_hash_base<E,T>
00186 {
00187 typedef grid_function_hash_base<E,T> base_gf;
00188
00189 using base_gf::table;
00190 public:
00191 typedef total_grid_function_category category;
00192 typedef element_traits<E> et1;
00193 typedef typename et1::ElementIterator ElementIterator;
00194 typedef typename base_gf::grid_type grid_type;
00195 typedef typename base_gf::reference reference;
00196 typedef typename base_gf::const_reference const_reference;
00197 using base_gf::TheGrid;
00198
00199 template<class EE, class TT> struct apply { typedef grid_function_hash<EE,TT> type; };
00200
00201
00202
00203 grid_function_hash() {}
00204 grid_function_hash(const grid_type& gg) : base_gf(gg) {}
00205 grid_function_hash(const grid_type& gg, const T& t)
00206 : base_gf(gg)
00207 {
00208 set_value(t);
00209 }
00210 grid_function_hash(ref_ptr<grid_type const> gg) : base_gf(gg) {}
00211 grid_function_hash(ref_ptr<grid_type const> gg, const T& t)
00212 : base_gf(gg)
00213 {
00214 set_value(t);
00215 }
00216 ElementIterator FirstElement() const { return et1::FirstElement(TheGrid());}
00217 ElementIterator EndElement() const { return et1::EndElement (TheGrid());}
00218
00219
00222 void set_value(T const& t) {
00223 for(ElementIterator e = et1::FirstElement(TheGrid()); ! e.IsDone(); ++e)
00224 table[*e] = t;
00225 }
00226
00227 void init(ref_ptr<grid_type const> gg, const T& t) {
00228 set_grid(gg);
00229 set_value(t);
00230 }
00231 void init(const grid_type& gg, const T& t) { init(ref_ptr<grid_type const>(gg), t);}
00232
00233
00234
00237 const_reference operator()(const E& e) const {
00238
00239
00240
00241
00242
00243 REQUIRE( defined(e), "grid_function not defined for item ",1);
00244 return (* (table.find(e))).second;
00245 }
00246
00251 reference operator[](const E& e) {
00252
00253
00254
00255
00256
00257 return (table[e]);
00258 }
00259
00260 };
00261
00262 }
00263
00264 #endif
00265