aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/gra/IdentityPoints.h
blob: 1646bf794b8a23d9a7749a7ad8da372158541add (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#ifndef   gra_IdentityPoints_H__
#define   gra_IdentityPoints_H__

#include "../Self.h"
#include "../math/Vector.h"
#include "../oo/ObjBase.h"

#include <map>
#include <set>

#include <cstdlib>

namespace meow {

/*!
 * @brief 把一個 \c std::map<ID,Vector<Scalar> > 包起來
 *
 * @author cat_leopard
 */
template<class ID, class Scalar>
class IdentityPoints: public ObjBase {
public:
  typedef typename std::map<ID, Vector<Scalar> >     IdentityPointsMap;
  typedef typename IdentityPointsMap::      iterator IdentityPointsMapIter;
  typedef typename IdentityPointsMap::const_iterator IdentityPointsMapIterK;

private:
  struct Myself {
    IdentityPointsMap    points_;
    size_t            dimension_;

    Myself() {
      dimension_ = 1;
    }
    Myself(Myself const& m): points_(m.points_), dimension_(m.dimension_) {
    }
    ~Myself() {
    }
  };

  Self<Myself> const self;
public:
  /*!
   * @brief constructor
   */
  IdentityPoints(): self() {
  }

  /*!
   * @brief constructor, 並且複製資料
   */
  IdentityPoints(IdentityPoints const& b):
  self(b.self, Self<Myself>::COPY_FROM) {
  }

  /*!
   * @brief destructor
   */
  ~IdentityPoints() {
  }

  /*!
   * @brief 複製資料
   */
  IdentityPoints& copyFrom(IdentityPoints const& b) {
    self().copyFrom(b.self);
    return *this;
  }

  /*!
   * @brief 參照
   */
  IdentityPoints& referenceFrom(IdentityPoints const& b) {
    self().referenceFrom(b.self);
    return *this;
  }

  /*!
   * @brief 清除一切identity points
   */
  void clear() {
    self()->points_.clear();
  }

  /*!
   * @brief 回傳有幾個identity points
   */
  size_t size() const {
    return self->points_.size();
  }

  /*!
   * @brief 回傳是否沒有identity points
   */
  bool empty() const {
    return (size() == 0u);
  }

  /*!
   * @brief 檢查某id是否有使用
   */
  bool exist(ID const& id) const {
    return (self->points_.find(id) != self->points_.end());
  }

  /*!
   * @brief 回傳dimension
   */
  size_t dimension() const {
    return self->dimension_;
  }

  /*!
   * @brief 設定dimension, 並且清空資料
   */
  size_t dimension(size_t dim) {
    self()->dimension_ = dim;
    clear();
    return dimension();
  }

  /*!
   * @brief 設定dimension, 並且針對每個identity point指定重設dimension
   */
  size_t dimension(size_t dim, Scalar const& init_value) {
    self()->dimension_ = dim;
    for (IdentityPointsMapIter
         it = self()->points_.begin(); it != self()->points_.end(); ++it) {
      it.second.dimension(dim, init_value);
    }
    return dimension();
  }

  /*!
   * @brief 取得所有identity points
   */
  IdentityPointsMap const& identityPoints() const {
    return self->points_;
  }

  /*!
   * @brief 設定所有identity points
   */
  IdentityPointsMap const& identityPoints(IdentityPointsMap const& points) {
    clear();
    return identityPointsAdd(points);
  }

  /*!
   * @brief 加入identity Points
   */
  IdentityPointsMap const& identityPointsAdd(IdentityPointsMap const& points) {
    for (IdentityPointsMapIterK it = points.begin(); it != points.end(); ++it) {
      identityPointAdd(it.first, it.second);
    }
    return identityPoints();
  }

  /*!
   * @brief 移除identity Points
   */
  IdentityPointsMap const& identityPointsDel(std::set<ID> const& ids) {
    for (typename std::set<ID>::const_iterator
         it = ids.begin(); it != ids.end(); ++it) {
      identityPointDel(*it);
    }
    return identityPoints();
  }

  /*!
   * @brief 取得一個identity point
   */
  Vector<Scalar> identityPoint(ID const& id) const {
    return (exist(id) ? self->points_.find(id)->second : Vector<Scalar>());
  }

  /*!
   * @brief 修改一個identity point
   */
  Vector<Scalar> identityPoint(ID const& id, Vector<Scalar> const& b) {
    if (b.dimension() == self->dimension_ && exist(id)) {
      self()->points_[id].copyFrom(b);
    }
    return identityPoint(id);
  }

  /*!
   * @brief 新增一個identity point
   */
  Vector<Scalar> identityPointAdd(ID const& id, Vector<Scalar> const& b) {
    if (b.dimension() == self->dimension_ && !exist(id)) {
      self()->points_[id].copyFrom(b);
    }
    return identityPoint(id);
  }

  /*!
   * @brief 刪除一個identity point
   */
  void identityPointDel(ID const& id) {
    self()->points_.erase(id);
  }

  /*!
   * @brief 取得一個identity point, non-constant reference
   */
  Vector<Scalar>& identityPointGet(ID const& id) {
    return self()->points_[id];
  }

  /*!
   * @brief same as \c copyFrom(b)
   */
  IdentityPoints& operator=(IdentityPoints const& b) {
    return copyFrom(b);
  }

  /*! @brief 將資料寫入檔案
   *
   *  @note 未完成
   */
  bool write(FILE* f, bool bin, unsigned int fg) const {
    if (bin) {
      long dim, ct;
      if (fwrite(&(dim = dimension()), sizeof(dim), 1, f) < 1) return false;
      if (fwrite(&(ct = size()), sizeof(ct), 1, f) < 1) return false;
      for (IdentityPointsMapIterK
           it = identityPoints().begin(), ed = identityPoints().end();
           it != ed; ++it) {
        double tmp;
        if (fwrite(&(tmp = it->first), sizeof(tmp), 1, f) < 1) return false;
        for (long i = 0; i < dim; ++i) {
          if (fwrite(&(tmp = it->second(i)), sizeof(tmp), 1, f) < 1)
            return false;
        }
      }
    }
    else {
      if (fprintf(f, "%ld %lu\n", dimension(), size()) < 1) return false;
      for (IdentityPointsMapIterK
           it = identityPoints().begin(), ed = identityPoints().end();
           it != ed; ++it) {
        if (fprintf(f, "%f   ", (double)it->first) < 1) return false;
        for (long i = 0, I = dimension(); i < I; ++i) {
          if (fprintf(f, "%f ", (double)it->second(i)) < 1) return false;
        }
        fprintf(f, "\n");
      }
    }
    return true;
  }

  /*! @brief 將資料讀入
   *
   *  @note 未完成
   */
  bool read(FILE* f, bool bin, unsigned int fg) {
    long dim, ct;
    if (bin) {
      if (fread(&dim, sizeof(dim), 1, f) < 1) return false;
      dimension(dim);
      if (fread(&ct, sizeof(ct), 1, f) < 1) return false;
      double id, tt;
      Vector<Scalar> tmp(dim, 0);
      for (int i = 0; i < ct; ++i) {
        if (fread(&id, sizeof(id), 1, f) < 1) return false;
        for (size_t j = 0, J = dim; j < J; ++j) {
          if (fread(&tt, sizeof(tt), 1, f) < 1) return false;
          tmp.entry(j, tt);
        }
        identityPointAdd((ID)id, tmp);
      }
    }
    else {
      if (fscanf(f, "%ld %ld", &dim, &ct) < 2) return false;
      dimension(dim);
      double id, tt;
      Vector<Scalar> tmp(dim, 0);
      for (int i = 0; i < ct; ++i) {
        if (fscanf(f, "%lf", &id) < 1) return false;
        for (int j = 0, J = dim; j < J; ++j) {
          if (fscanf(f, "%lf", &tt) < 1) return false;
          tmp.entry(j, tt);
        }
        identityPointAdd((ID)id, tmp);
      }
    }
    return true;
  }

  /*! @brief new一個自己
   *
   *  @return 一個new出來的Bitmap<Pixel>
   */
  ObjBase* create() const {
    return new IdentityPoints();
  }

  /*! @brief 複製資料
   *
   * 輸入型別是 \c ObjBase \c const*
   * 這裡假設實體其實是 \c Bitmap.
   * 事實上這個method就只是幫忙轉型然後呼叫原本的\c copyFrom
   *
   *  @param [in] b 資料來源
   *  @return this
   */
  ObjBase* copyFrom(ObjBase const* b) {
    return &(copyFrom(*(IdentityPoints*)b));
  }

  /*! @brief 回傳class的type
   *
   *  @return \c char \c const\c * 形式的typename
   */
  char const* ctype() const {
    return typeid(*this).name();
  }

  /*! @brief 回傳class的type
   *
   *  @return \c std::string 形式的typename
   */
  std::string type() const {
    return std::string(ctype());
  }
};

}

#endif // gra_IdentityPoints_H__