Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
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 #include "../math/Vector.h"
6 #include "../oo/ObjBase.h"
7 
8 #include <map>
9 #include <set>
10 
11 #include <cstdlib>
12 
13 namespace meow {
14 
20 template<class ID, class Scalar>
21 class IdentityPoints: public ObjBase {
22 public:
23  typedef typename std::map<ID, Vector<Scalar> > IdentityPointsMap;
24  typedef typename IdentityPointsMap:: iterator IdentityPointsMapIter;
25  typedef typename IdentityPointsMap::const_iterator IdentityPointsMapIterK;
26 
27 private:
28  struct Myself {
29  IdentityPointsMap points_;
30  size_t dimension_;
31 
32  Myself() {
33  dimension_ = 1;
34  }
35  Myself(Myself const& m): points_(m.points_), dimension_(m.dimension_) {
36  }
37  ~Myself() {
38  }
39  };
40 
41  Self<Myself> const self;
42 public:
46  IdentityPoints(): self() {
47  }
48 
53  self(b.self, Self<Myself>::COPY_FROM) {
54  }
55 
60  }
61 
66  self().copyFrom(b.self);
67  return *this;
68  }
69 
74  self().referenceFrom(b.self);
75  return *this;
76  }
77 
81  void clear() {
82  self()->points_.clear();
83  }
84 
88  size_t size() const {
89  return self->points_.size();
90  }
91 
95  bool empty() const {
96  return (size() == 0u);
97  }
98 
102  bool exist(ID const& id) const {
103  return (self->points_.find(id) != self->points_.end());
104  }
105 
109  size_t dimension() const {
110  return self->dimension_;
111  }
112 
116  size_t dimension(size_t dim) {
117  self()->dimension_ = dim;
118  clear();
119  return dimension();
120  }
121 
125  size_t dimension(size_t dim, Scalar const& init_value) {
126  self()->dimension_ = dim;
128  it = self()->points_.begin(); it != self()->points_.end(); ++it) {
129  it.second.dimension(dim, init_value);
130  }
131  return dimension();
132  }
133 
138  return self->points_;
139  }
140 
145  clear();
146  return identityPointsAdd(points);
147  }
148 
153  for (IdentityPointsMapIterK it = points.begin(); it != points.end(); ++it) {
154  identityPointAdd(it.first, it.second);
155  }
156  return identityPoints();
157  }
158 
162  IdentityPointsMap const& identityPointsDel(std::set<ID> const& ids) {
163  for (typename std::set<ID>::const_iterator
164  it = ids.begin(); it != ids.end(); ++it) {
165  identityPointDel(*it);
166  }
167  return identityPoints();
168  }
169 
173  Vector<Scalar> identityPoint(ID const& id) const {
174  return (exist(id) ? self->points_.find(id)->second : Vector<Scalar>());
175  }
176 
180  Vector<Scalar> identityPoint(ID const& id, Vector<Scalar> const& b) {
181  if (b.dimension() == self->dimension_ && exist(id)) {
182  self()->points_[id].copyFrom(b);
183  }
184  return identityPoint(id);
185  }
186 
191  if (b.dimension() == self->dimension_ && !exist(id)) {
192  self()->points_[id].copyFrom(b);
193  }
194  return identityPoint(id);
195  }
196 
200  void identityPointDel(ID const& id) {
201  self()->points_.erase(id);
202  }
203 
208  return self()->points_[id];
209  }
210 
215  return copyFrom(b);
216  }
217 
222  bool write(FILE* f, bool bin, unsigned int fg) const {
223  if (bin) {
224  long dim, ct;
225  if (fwrite(&(dim = dimension()), sizeof(dim), 1, f) < 1) return false;
226  if (fwrite(&(ct = size()), sizeof(ct), 1, f) < 1) return false;
228  it = identityPoints().begin(), ed = identityPoints().end();
229  it != ed; ++it) {
230  double tmp;
231  if (fwrite(&(tmp = it->first), sizeof(tmp), 1, f) < 1) return false;
232  for (long i = 0; i < dim; ++i) {
233  if (fwrite(&(tmp = it->second(i)), sizeof(tmp), 1, f) < 1)
234  return false;
235  }
236  }
237  }
238  else {
239  if (fprintf(f, "%ld %lu\n", dimension(), size()) < 1) return false;
241  it = identityPoints().begin(), ed = identityPoints().end();
242  it != ed; ++it) {
243  if (fprintf(f, "%f ", (double)it->first) < 1) return false;
244  for (long i = 0, I = dimension(); i < I; ++i) {
245  if (fprintf(f, "%f ", (double)it->second(i)) < 1) return false;
246  }
247  fprintf(f, "\n");
248  }
249  }
250  return true;
251  }
252 
257  bool read(FILE* f, bool bin, unsigned int fg) {
258  long dim, ct;
259  if (bin) {
260  if (fread(&dim, sizeof(dim), 1, f) < 1) return false;
261  dimension(dim);
262  if (fread(&ct, sizeof(ct), 1, f) < 1) return false;
263  double id, tt;
264  Vector<Scalar> tmp(dim, 0);
265  for (int i = 0; i < ct; ++i) {
266  if (fread(&id, sizeof(id), 1, f) < 1) return false;
267  for (size_t j = 0, J = dim; j < J; ++j) {
268  if (fread(&tt, sizeof(tt), 1, f) < 1) return false;
269  tmp.entry(j, tt);
270  }
271  identityPointAdd((ID)id, tmp);
272  }
273  }
274  else {
275  if (fscanf(f, "%ld %ld", &dim, &ct) < 2) return false;
276  dimension(dim);
277  double id, tt;
278  Vector<Scalar> tmp(dim, 0);
279  for (int i = 0; i < ct; ++i) {
280  if (fscanf(f, "%lf", &id) < 1) return false;
281  for (int j = 0, J = dim; j < J; ++j) {
282  if (fscanf(f, "%lf", &tt) < 1) return false;
283  tmp.entry(j, tt);
284  }
285  identityPointAdd((ID)id, tmp);
286  }
287  }
288  return true;
289  }
290 
295  ObjBase* create() const {
296  return new IdentityPoints();
297  }
298 
308  ObjBase* copyFrom(ObjBase const* b) {
309  return &(copyFrom(*(IdentityPoints*)b));
310  }
311 
316  char const* ctype() const {
317  return typeid(*this).name();
318  }
319 
324  std::string type() const {
325  return std::string(ctype());
326  }
327 };
328 
329 }
330 
331 #endif // gra_IdentityPoints_H__
std::string type() const
回傳class的type
size_t dimension() const
return dimension
Definition: Vector.h:100
size_t dimension(size_t dim)
設定dimension, 並且清空資料
ObjBase * create() const
new一個自己
Vector< Scalar > & identityPointGet(ID const &id)
取得一個identity point, non-constant reference
bool read(FILE *f, bool bin, unsigned int fg)
將資料讀入
bool empty() const
回傳是否沒有identity points
IdentityPointsMap const & identityPoints(IdentityPointsMap const &points)
設定所有identity points
ObjBase * copyFrom(ObjBase const *b)
複製資料
Scalar entry(size_t i) const
return i -th entry
Definition: Vector.h:125
把一個 std::map<ID,Vector<Scalar> > 包起來
char const * ctype() const
回傳class的type
IdentityPoints & copyFrom(IdentityPoints const &b)
複製資料
size_t size() const
回傳有幾個identity points
IdentityPoints & referenceFrom(IdentityPoints const &b)
參照
bool exist(ID const &id) const
檢查某id是否有使用
void identityPointDel(ID const &id)
刪除一個identity point
void clear()
清除一切identity points
Vector< Scalar > identityPointAdd(ID const &id, Vector< Scalar > const &b)
新增一個identity point
IdentityPoints(IdentityPoints const &b)
constructor, 並且複製資料
std::map< ID, Vector< Scalar > > IdentityPointsMap
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
IdentityPoints()
constructor
vector
Definition: Vector.h:19
IdentityPointsMap const & identityPoints() const
取得所有identity points
size_t dimension() const
回傳dimension
IdentityPoints & operator=(IdentityPoints const &b)
same as copyFrom(b)
size_t dimension(size_t dim, Scalar const &init_value)
設定dimension, 並且針對每個identity point指定重設dimension
Vector< Scalar > identityPoint(ID const &id) const
取得一個identity point
~IdentityPoints()
destructor
bool write(FILE *f, bool bin, unsigned int fg) const
將資料寫入檔案
IdentityPointsMap::iterator IdentityPointsMapIter
IdentityPointsMap::const_iterator IdentityPointsMapIterK
Vector< Scalar > identityPoint(ID const &id, Vector< Scalar > const &b)
修改一個identity point
IdentityPointsMap const & identityPointsAdd(IdentityPointsMap const &points)
加入identity Points
IdentityPointsMap const & identityPointsDel(std::set< ID > const &ids)
移除identity Points