00001 #ifndef GRAL_BASE_GB_GRID_FUNCTION_VECTOR_H
00002 #define GRAL_BASE_GB_GRID_FUNCTION_VECTOR_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <vector>
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
00027
00053 template<class E, class T>
00054 class grid_function_vector {
00055 public:
00056 typedef total_grid_function_category category;
00057
00058 typedef std::vector<T> table_type;
00059 typedef typename table_type::size_type size_type;
00060 typedef typename table_type::value_type value_type;
00061 typedef typename table_type::reference reference;
00062 typedef typename table_type::const_reference const_reference;
00063 typedef typename table_type::pointer pointer;
00064 typedef typename table_type::difference_type difference_type;
00065
00066
00067 typedef element_traits<E> et;
00068 typedef E element_type;
00069 typedef typename et::ElementIterator ElementIterator;
00070 typedef typename et::handle_type element_handle;
00071 typedef typename et::grid_type grid_type;
00072
00073 template<class EE, class TT> struct apply { typedef grid_function_vector<EE,TT> type; };
00074
00075
00076 typedef E const& argument_type;
00077
00078 typedef const_reference result_type;
00079 protected:
00080
00081 ref_ptr<grid_type const> g;
00082 table_type table;
00083
00084 public:
00085
00086 grid_function_vector() : g() , table() {}
00087 grid_function_vector(const grid_type& gg)
00088 : g(gg), table(et::size(gg)) {}
00089 grid_function_vector(const grid_type& gg, const T& t)
00090 : g(gg), table(et::size(gg),t) {}
00091 grid_function_vector(ref_ptr<grid_type const> gg)
00092 : g(gg), table(et::size(gg)) {}
00093 grid_function_vector(ref_ptr<grid_type const> gg, const T& t)
00094 : g(gg), table(et::size(gg),t) {}
00095
00096
00097 typedef grid_function_vector<E,T> gfc2dv;
00098 grid_function_vector(const gfc2dv& rs) : g(rs.g), table(rs.table) {}
00099 gfc2dv& operator=(const gfc2dv& rs)
00100 { if (this != &rs) { g =rs.g; table = rs.table;} return *this;}
00101
00106 void set_value(value_type const& t) { ::std::fill(table.begin(),table.end(),t); }
00107
00112 template<class ForwardIterator>
00113 void set_values(ForwardIterator t_begin, ForwardIterator t_end)
00114 {
00115 std::copy(t_begin, t_end, table.begin());
00116 }
00117
00118 void init(ref_ptr<grid_type const> gg, const T& t) {
00119 REQUIRE((g == 0), "grid_function<>::init: grid must be 0!\n",1);
00120 g = gg;
00121 table.resize(et::size(*g),t);
00122 }
00123 void init(const grid_type& gg, const T& t) { init(ref_ptr<grid_type const>(gg), t);}
00124
00125 void set_grid(ref_ptr<grid_type const> gg) {
00126 REQUIRE((g == 0), "set grid: grid must be 0!\n",1);
00127 g = gg;
00128 table = table_type(et::size(*g));
00129 }
00130 void set_grid(const grid_type& gg) { set_grid(ref_ptr<grid_type const>(gg));}
00131
00132 void rebind(ref_ptr<grid_type const> gg) {
00133 g = gg;
00134 table.resize(et::size(*g));
00135 ENSURE( (table.size() == (size_type)et::size(*g)), "", 1);
00136 }
00137 void rebind(const grid_type& gg) { rebind(ref_ptr<grid_type const>(gg));}
00138
00139
00140
00141 void resize(size_type n) { table.resize(n); }
00142
00143
00144
00145
00146 const grid_type& TheGrid() const {
00147 REQUIRE((g != 0), "No grid!\n",1);
00148 return *g;
00149 }
00150
00151 ElementIterator FirstElement() const { return et::FirstElement(*g);}
00152 ElementIterator EndElement() const { return et::EndElement (*g);}
00153
00154
00155
00156
00157 const_reference operator()(const E& e) const { cv(e); return table[et::handle(e)]; }
00158 reference operator[](const E& e) { cv(e); return table[et::handle(e)]; }
00159 const_reference operator[](const E& e) const { cv(e); return table[et::handle(e)]; }
00160
00161 bool valid(element_handle e) const { return bound() && (element_handle(0) <= e) && (e < element_handle(table.size()));}
00162 bool bound() const { return g != 0;}
00163 void cv(const E& e) const { cv(e.handle());}
00164 void cv(element_handle e) const { REQUIRE(valid(e), "grid_function: invalid access: pos = " << e,1); }
00165 void cb() const { REQUIRE(bound(), "", 1);}
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 const_reference operator()(const element_handle& e) const { cv(e); return table[e]; }
00176 reference operator[](const element_handle& e) { cv(e); return table[e]; }
00177
00178
00179
00180
00181 unsigned size() const { return table.size();}
00182
00183 typedef typename table_type::iterator iter;
00184 typedef typename table_type::const_iterator const_iter;
00185 iter begin() { return iter(table.begin());}
00186 iter end() { return iter(table.end());}
00187 const_iter begin() const { return const_iter(table.begin());}
00188 const_iter end() const { return const_iter(table.end());}
00189
00190
00191
00192
00193
00194 typedef iter iterator;
00195 typedef const_iter const_iterator;
00196 };
00197
00198 }
00199
00200 #endif
00201