Templates -- Meow  1.1.2
不能,也不應該先編譯成obj-file的templates
IdentityPoints.h
Go to the documentation of this file.
1 #ifndef gra_IdentityPoints_H__
2 #define gra_IdentityPoints_H__
3 
4 #include "../Self.h"
5 
6 #include "../math/Vector.h"
7 
8 #include "../oo/ObjBase.h"
9 
10 #include <map>
11 
12 #include <cstdlib>
13 
14 namespace meow {
15 
21 template<class ID, class Scalar>
22 class IdentityPoints: public ObjBase {
23 public:
24  typedef typename std::map<ID, Vector<Scalar> > IdentityPointsMap;
25  typedef typename IdentityPointsMap:: iterator IdentityPointsMapIter;
26  typedef typename IdentityPointsMap::const_iterator IdentityPointsMapIterK;
27 
28 private:
29  struct Myself {
30  IdentityPointsMap points_;
31  size_t dimension_;
32 
33  Myself() {
34  dimension_ = 1;
35  }
36  ~Myself() {
37  }
38  Myself& copyFrom(Myself const& b) {
39  points_ = b.points_;
40  dimension_ = b.dimension_;
41  return *this;
42  }
43  };
44 
45  Self<Myself> const self;
46 public:
50  IdentityPoints(): self(true) {
51  }
52 
56  IdentityPoints(IdentityPoints const& b): self(false) {
57  copyFrom(b);
58  }
59 
64  }
65 
70  self().copyFrom(b.self);
71  return *this;
72  }
73 
78  self().referenceFrom(b.self);
79  return *this;
80  }
81 
85  void clear() {
86  self()->points_.clear();
87  }
88 
92  size_t size() const {
93  return self->points_.size();
94  }
95 
99  bool empty() const {
100  return (size() == 0u);
101  }
102 
106  bool exist(ID const& id) const {
107  return (self->points_.find(id) != self->points_.end());
108  }
109 
113  size_t dimension() const {
114  return self->dimension_;
115  }
116 
120  size_t dimension(size_t dim) {
121  self()->dimension_ = dim;
122  clear();
123  return dimension();
124  }
125 
129  size_t dimension(size_t dim, Scalar const& init_value) {
130  self()->dimension_ = dim;
132  it = self()->points_.begin(); it != self()->points_.end(); ++it) {
133  it.second.dimension(dim, init_value);
134  }
135  return dimension();
136  }
137 
142  return self->points_;
143  }
144 
149  clear();
150  return identityPointsAdd(points);
151  }
152 
157  for (IdentityPointsMapIterK it = points.begin(); it != points.end(); ++it) {
158  identityPointAdd(it.first, it.second);
159  }
160  return identityPoints();
161  }
162 
166  IdentityPointsMap const& identityPointsDel(std::set<ID> const& ids) {
167  for (typename std::set<ID>::const_iterator
168  it = ids.begin(); it != ids.end(); ++it) {
169  identityPointDel(*it);
170  }
171  return identityPoints();
172  }
173 
177  Vector<Scalar> identityPoint(ID const& id) const {
178  return (exist(id) ? self->points_.find(id)->second : Vector<Scalar>());
179  }
180 
184  Vector<Scalar> identityPoint(ID const& id, Vector<Scalar> const& b) {
185  if (b.dimension() == self->dimension_ && exist(id)) {
186  self()->points_[id].copyFrom(b);
187  }
188  return identityPoint(id);
189  }
190 
195  if (b.dimension() == self->dimension_ && !exist(id)) {
196  self()->points_[id].copyFrom(b);
197  }
198  return identityPoint(id);
199  }
200 
204  void identityPointDel(ID const& id) {
205  self()->points_.erase(id);
206  }
207 
212  return self()->points_[id];
213  }
214 
219  return copyFrom(b);
220  }
221 
226  bool write(FILE* f, bool bin, unsigned int fg) const {
227  return false;
228  }
229 
234  bool read(FILE* f, bool bin, unsigned int fg) {
235  return false;
236  }
237 
242  ObjBase* create() const {
243  return new IdentityPoints();
244  }
245 
255  ObjBase* copyFrom(ObjBase const* b) {
256  return &(copyFrom(*(IdentityPoints*)b));
257  }
258 
263  char const* ctype() const{
264  static char const* ptr = typeid(*this).name();
265  return ptr;
266  }
267 
272  std::string type() const {
273  return std::string(ctype());
274  }
275 };
276 
277 }
278 
279 #endif // gra_IdentityPoints_H__