plda源码(七)

plda源码(七)

FastMatrix
vals和mapped_vec



class FastMatrix {

 public:
  struct FElem {
    int col;
    double val;
  };

  class ElemIter {//行迭代器
   public:
    ElemIter(FElem * ptr, int size)
        : ptr_(ptr),
          size_(size),
          index_(0) {
    }
    virtual ~ElemIter() {
    }
    bool HasNext() {
    }
    void GetNext(double &val, int &index) {
    }
    FElem GetNext() {
    }
  };

 public:
  FastMatrix(int row_size, int col_size, int col_capacity)
      : size1_(row_size),
        size2_(col_capacity),
        mapped_vec(row_size) {
    vals = new FElem[row_size * col_capacity];
    linecnts = new int[row_size];
    rowheads = new FElem*[row_size];
    for (int i = 0; i < row_size; i++) {
      linecnts[i] = 0;
      rowheads[i] = vals + i * col_capacity;
    }
  }

  double get(int row, int col) const {
    const map<int, double> &lmap = mapped_vec[row];
    return lmap.at(col);
  }

  void set(int row, int col, double val) {
    //设置vals
    mapped_vec[row][col] = val;
  }

  ElemIter GetRowWithIterUnsorted(int row) const {
    FElem * ptr = rowheads[row];
    return ElemIter(ptr, linecnts[row]);
  }

 private:
  int size1_;
  int size2_;
  FElem * vals;
  int * linecnts;
  FElem ** rowheads;
  vector<map<int, double>> mapped_vec;
};


WordSparseMatrix
spare matrix



#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>

class WordSparseMatrix {

  using matrix_type = boost::numeric::ublas::compressed_matrix<double>;
  using matrix_row_type = boost::numeric::ublas::matrix_row<matrix_type>;

 public:
  class ElemIter {

   public:
    ElemIter(matrix_row_type matrix_row_content)
        : row_content(matrix_row_content) {
      elem_iter = row_content.begin();
    }

    bool HasNext() {
    }

    double GetNext() {
    }
    void GetNext(double &val, int &index) {
    }
    
   private:
    matrix_row_type::iterator elem_iter;
    matrix_row_type row_content;
  };

 public:
  WordSparseMatrix(int row_size, int col_size, int col_capacity)
      : mx(matrix_type(row_size, col_size)) {
  }

 private:
  matrix_type mx;
};

double WordSparseMatrix::get(int row, int col) {
	return mx(row, col);
}

void WordSparseMatrix::set(int row, int col, double val) {
	mx(row, col) = val;
}

WordSparseMatrix::ElemIter WordSparseMatrix::GetRowWithIterUnsorted(int row_num) {
	matrix_row_type mat_row = row(mx, row_num);
	return ElemIter(mat_row);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值