栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

PROJECT #0 - C++ PRIMER [CMU 15-445645]笔记

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PROJECT #0 - C++ PRIMER [CMU 15-445645]笔记

PROJECT #0 - C++ PRIMER [CMU 15-445/645]笔记

这是数据库领域的一门课程, 由卡内基梅隆大学副教授Andy Pavlo授课, 目前在网上有授课视频资料、实验以及配套的在线测评环境 (限时开放至2021年12月31日)

环境: wsl2 + Clion

Project #0 - C++ Primer

还是很简单的,主要目的是让学生熟悉 C++17 的基本语法

代码如下:

//===----------------------------------------------------------------------===//
//
//                         BusTub
//
// p0_starter.h
//
// Identification: src/include/primer/p0_starter.h
//
// Copyright (c) 2015-2020, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include 
#include 
#include 
#include "common/exception.h"
#include "common/logger.h"

namespace bustub {


template 
class Matrix {
 protected:
  
  Matrix(int rows, int cols) : rows_(rows), cols_(cols) {
    linear_ = new T[rows_ * cols_];
    memset(linear_, 0, rows_ * cols_);
  }

  
  int rows_;
  
  int cols_;

  
  T *linear_;

 public:
  
  virtual int GetRowCount() const = 0;

  
  virtual int GetColumnCount() const = 0;

  
  virtual T GetElement(int i, int j) const = 0;

  
  virtual void SetElement(int i, int j, T val) = 0;

  
  virtual void FillFrom(const std::vector &source) = 0;

  
  virtual ~Matrix() {
    if (linear_ == nullptr) {
      return;
    }
    delete[] linear_;
  }
};


template 
class RowMatrix : public Matrix {
 public:
  

  RowMatrix(int rows, int cols) : Matrix(rows, cols) {
    data_ = new T *[rows];
    for (int i = 0; i < rows; ++i) {
      data_[i] = Matrix::linear_ + i * cols;
    }
  }

  
  int GetRowCount() const override { return this->rows_; }

  
  int GetColumnCount() const override { return this->cols_; }

  
  T GetElement(int i, int j) const override {
    if (i < 0 || i >= this->GetRowCount()) throw Exception(ExceptionType::OUT_OF_RANGE, "index is out of range");

    if (j < 0 || j >= this->GetColumnCount()) throw Exception(ExceptionType::OUT_OF_RANGE, "index is out of range");

    return std::move(data_[i][j]);
  }

  
  void SetElement(int i, int j, T val) override {
    if (i < 0 || i >= Matrix::rows_ || j < 0 || j >= Matrix::cols_) {
      throw Exception(ExceptionType::OUT_OF_RANGE, "index is out of range");
    } else
      data_[i][j] = val;
  }

  
  void FillFrom(const std::vector &source) override {
    int rowCount = this->GetRowCount();
    int colCount = this->GetColumnCount();
    if (source.size() != static_cast(rowCount * colCount)) {
      throw Exception(ExceptionType::OUT_OF_RANGE, "index is out of range");
    }
    for (int i = 0; i < rowCount * colCount; ++i) {
      Matrix::linear_[i] = source[i];
    }
  }

  
  ~RowMatrix() override {
    //    for (int i = 0; i < this->GetRowCount(); ++i) {
    //      delete[] data_[i];
    //    }
    delete[] data_;
  }

 private:
  
  T **data_;
};


template 
class RowMatrixOperations {
 public:
  
  static std::unique_ptr> Add(const RowMatrix *matrixA, const RowMatrix *matrixB) {
    if (matrixA->GetRowCount() != matrixB->GetRowCount() || matrixA->GetColumnCount() != matrixB->GetColumnCount())
      return std::unique_ptr>(nullptr);

    int rowCount = matrixA->GetRowCount();
    int colCount = matrixB->GetColumnCount();

    std::unique_ptr> mat = std::make_unique>(rowCount, colCount);

    for (int i = 0; i < rowCount; ++i) {
      for (int j = 0; j < colCount; ++j) {
        T temp = matrixA->GetElement(i, j) + matrixB->GetElement(i, j);
        mat->SetElement(i, j, std::move(temp));
      }
    }
    return mat;
  }

  
  static std::unique_ptr> Multiply(const RowMatrix *matrixA, const RowMatrix *matrixB) {
    // TODO(P0): Add implementation
    if (matrixA->GetColumnCount() != matrixB->GetRowCount()) {
      return std::unique_ptr>(nullptr);
    }

    int row = matrixA->GetRowCount();
    int col = matrixB->GetColumnCount();

    T sum;

    std::unique_ptr> mat = std::make_unique>(row, col);
    for (int i = 0; i < row; ++i) {
      for (int j = 0; j < col; ++j) {
        sum = 0;
        for (int k = 0; k < matrixA->GetColumnCount(); ++k) {
          sum += matrixA->GetElement(i, k) * matrixB->GetElement(k, j);
        }
        mat->SetElement(i, j, sum);
      }
    }
    return mat;
  }

  
  static std::unique_ptr> GEMM(const RowMatrix *matrixA, const RowMatrix *matrixB,
                                            const RowMatrix *matrixC) {
    std::unique_ptr tempMat = std::move(Multiply(matrixA, matrixB));
    if (tempMat == nullptr) {
      return std::unique_ptr>(nullptr);
    }
    return Add(tempMat, matrixC);
  }
};
}  // namespace bustub



x *matrixB,
                                            const RowMatrix *matrixC) {
    std::unique_ptr tempMat = std::move(Multiply(matrixA, matrixB));
    if (tempMat == nullptr) {
      return std::unique_ptr>(nullptr);
    }
    return Add(tempMat, matrixC);
  }
};
}  // namespace bustub
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979791.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号