diff options
Diffstat (limited to 'meowpp/gra/IdentityPoints.h')
-rw-r--r-- | meowpp/gra/IdentityPoints.h | 140 |
1 files changed, 96 insertions, 44 deletions
diff --git a/meowpp/gra/IdentityPoints.h b/meowpp/gra/IdentityPoints.h index c33eb55..1646bf7 100644 --- a/meowpp/gra/IdentityPoints.h +++ b/meowpp/gra/IdentityPoints.h @@ -2,12 +2,11 @@ #define gra_IdentityPoints_H__ #include "../Self.h" - #include "../math/Vector.h" - #include "../oo/ObjBase.h" #include <map> +#include <set> #include <cstdlib> @@ -24,45 +23,42 @@ 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(Myself const& m): points_(m.points_), dimension_(m.dimension_) { } - Myself& copyFrom(Myself const& b) { - points_ = b.points_; - dimension_ = b.dimension_; - return *this; + ~Myself() { } }; - + Self<Myself> const self; public: /*! * @brief constructor */ - IdentityPoints(): self(true) { + IdentityPoints(): self() { } - + /*! * @brief constructor, 並且複製資料 */ - IdentityPoints(IdentityPoints const& b): self(false) { - copyFrom(b); + IdentityPoints(IdentityPoints const& b): + self(b.self, Self<Myself>::COPY_FROM) { } - + /*! * @brief destructor */ ~IdentityPoints() { } - + /*! * @brief 複製資料 */ @@ -70,7 +66,7 @@ public: self().copyFrom(b.self); return *this; } - + /*! * @brief 參照 */ @@ -78,42 +74,42 @@ public: 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, 並且清空資料 */ @@ -122,7 +118,7 @@ public: clear(); return dimension(); } - + /*! * @brief 設定dimension, 並且針對每個identity point指定重設dimension */ @@ -134,14 +130,14 @@ public: } return dimension(); } - + /*! * @brief 取得所有identity points */ IdentityPointsMap const& identityPoints() const { return self->points_; } - + /*! * @brief 設定所有identity points */ @@ -149,7 +145,7 @@ public: clear(); return identityPointsAdd(points); } - + /*! * @brief 加入identity Points */ @@ -159,7 +155,7 @@ public: } return identityPoints(); } - + /*! * @brief 移除identity Points */ @@ -170,14 +166,14 @@ public: } 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 */ @@ -187,7 +183,7 @@ public: } return identityPoint(id); } - + /*! * @brief 新增一個identity point */ @@ -197,42 +193,99 @@ public: } 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 { - return false; + 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) { - return false; + 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一個自己 @@ -242,7 +295,7 @@ public: ObjBase* create() const { return new IdentityPoints(); } - + /*! @brief 複製資料 * * 輸入型別是 \c ObjBase \c const* @@ -255,16 +308,15 @@ public: ObjBase* copyFrom(ObjBase const* b) { return &(copyFrom(*(IdentityPoints*)b)); } - + /*! @brief 回傳class的type * * @return \c char \c const\c * 形式的typename */ - char const* ctype() const{ - static char const* ptr = typeid(*this).name(); - return ptr; + char const* ctype() const { + return typeid(*this).name(); } - + /*! @brief 回傳class的type * * @return \c std::string 形式的typename |