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

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

image-20220614170304909

这是数据库领域的一门课程, 由卡内基梅隆大学副教授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 <memory>
#include <stdexcept>
#include <vector>
#include "common/exception.h"
#include "common/logger.h"namespace bustub {/*** The Matrix type defines a common* interface for matrix operations.*/
template <typename T>
class Matrix {protected:/*** TODO(P0): Add implementation** Construct a new Matrix instance.* @param rows The number of rows* @param cols The number of columns**/Matrix(int rows, int cols) : rows_(rows), cols_(cols) {linear_ = new T[rows_ * cols_];memset(linear_, 0, rows_ * cols_);}/** The number of rows in the matrix */int rows_;/** The number of columns in the matrix */int cols_;/*** TODO(P0): Allocate the array in the constructor.* TODO(P0): Deallocate the array in the destructor.* A flattened array containing the elements of the matrix.*/T *linear_;public:/** @return The number of rows in the matrix */virtual int GetRowCount() const = 0;/** @return The number of columns in the matrix */virtual int GetColumnCount() const = 0;/*** Get the (i,j)th matrix element.** Throw OUT_OF_RANGE if either index is out of range.** @param i The row index* @param j The column index* @return The (i,j)th matrix element* @throws OUT_OF_RANGE if either index is out of range*/virtual T GetElement(int i, int j) const = 0;/*** Set the (i,j)th matrix element.** Throw OUT_OF_RANGE if either index is out of range.** @param i The row index* @param j The column index* @param val The value to insert* @throws OUT_OF_RANGE if either index is out of range*/virtual void SetElement(int i, int j, T val) = 0;/*** Fill the elements of the matrix from `source`.** Throw OUT_OF_RANGE in the event that `source`* does not contain the required number of elements.** @param source The source container* @throws OUT_OF_RANGE if `source` is incorrect size*/virtual void FillFrom(const std::vector<T> &source) = 0;/*** Destroy a matrix instance.* TODO(P0): Add implementation*/virtual ~Matrix() {if (linear_ == nullptr) {return;}delete[] linear_;}
};/*** The RowMatrix type is a concrete matrix implementation.* It implements the interface defined by the Matrix type.*/
template <typename T>
class RowMatrix : public Matrix<T> {public:/*** TODO(P0): Add implementation** Construct a new RowMatrix instance.* @param rows The number of rows* @param cols The number of columns*/RowMatrix(int rows, int cols) : Matrix<T>(rows, cols) {data_ = new T *[rows];for (int i = 0; i < rows; ++i) {data_[i] = Matrix<T>::linear_ + i * cols;}}/*** TODO(P0): Add implementation* @return The number of rows in the matrix*/int GetRowCount() const override { return this->rows_; }/*** TODO(P0): Add implementation* @return The number of columns in the matrix*/int GetColumnCount() const override { return this->cols_; }/*** TODO(P0): Add implementation** Get the (i,j)th matrix element.** Throw OUT_OF_RANGE if either index is out of range.** @param i The row index* @param j The column index* @return The (i,j)th matrix element* @throws OUT_OF_RANGE if either index is out of range*/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]);}/*** Set the (i,j)th matrix element.** Throw OUT_OF_RANGE if either index is out of range.** @param i The row index* @param j The column index* @param val The value to insert* @throws OUT_OF_RANGE if either index is out of range*/void SetElement(int i, int j, T val) override {if (i < 0 || i >= Matrix<T>::rows_ || j < 0 || j >= Matrix<T>::cols_) {throw Exception(ExceptionType::OUT_OF_RANGE, "index is out of range");} elsedata_[i][j] = val;}/*** TODO(P0): Add implementation** Fill the elements of the matrix from `source`.** Throw OUT_OF_RANGE in the event that `source`* does not contain the required number of elements.** @param source The source container* @throws OUT_OF_RANGE if `source` is incorrect size*/void FillFrom(const std::vector<T> &source) override {int rowCount = this->GetRowCount();int colCount = this->GetColumnCount();if (source.size() != static_cast<std::size_t>(rowCount * colCount)) {throw Exception(ExceptionType::OUT_OF_RANGE, "index is out of range");}for (int i = 0; i < rowCount * colCount; ++i) {Matrix<T>::linear_[i] = source[i];}}/*** TODO(P0): Add implementation** Destroy a RowMatrix instance.*/~RowMatrix() override {//    for (int i = 0; i < this->GetRowCount(); ++i) {//      delete[] data_[i];//    }delete[] data_;}private:/*** A 2D array containing the elements of the matrix in row-major format.** TODO(P0):* - Allocate the array of row pointers in the constructor.* - Use these pointers to point to corresponding elements of the `linear` array.* - Don't forget to deallocate the array in the destructor.*/T **data_;
};/*** The RowMatrixOperations class defines operations* that may be performed on instances of `RowMatrix`.*/
template <typename T>
class RowMatrixOperations {public:/*** Compute (`matrixA` + `matrixB`) and return the result.* Return `nullptr` if dimensions mismatch for input matrices.* @param matrixA Input matrix* @param matrixB Input matrix* @return The result of matrix addition*/static std::unique_ptr<RowMatrix<T>> Add(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) {if (matrixA->GetRowCount() != matrixB->GetRowCount() || matrixA->GetColumnCount() != matrixB->GetColumnCount())return std::unique_ptr<RowMatrix<T>>(nullptr);int rowCount = matrixA->GetRowCount();int colCount = matrixB->GetColumnCount();std::unique_ptr<RowMatrix<T>> mat = std::make_unique<RowMatrix<T>>(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;}/*** Compute the matrix multiplication (`matrixA` * `matrixB` and return the result.* Return `nullptr` if dimensions mismatch for input matrices.* @param matrixA Input matrix* @param matrixB Input matrix* @return The result of matrix multiplication*/static std::unique_ptr<RowMatrix<T>> Multiply(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB) {// TODO(P0): Add implementationif (matrixA->GetColumnCount() != matrixB->GetRowCount()) {return std::unique_ptr<RowMatrix<T>>(nullptr);}int row = matrixA->GetRowCount();int col = matrixB->GetColumnCount();T sum;std::unique_ptr<RowMatrix<T>> mat = std::make_unique<RowMatrix<T>>(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;}/*** Simplified General Matrix Multiply operation. Compute (`matrixA` * `matrixB` + `matrixC`).* Return `nullptr` if dimensions mismatch for input matrices.* @param matrixA Input matrix* @param matrixB Input matrix* @param matrixC Input matrix* @return The result of general matrix multiply*/static std::unique_ptr<RowMatrix<T>> GEMM(const RowMatrix<T> *matrixA, const RowMatrix<T> *matrixB,const RowMatrix<T> *matrixC) {std::unique_ptr tempMat = std::move(Multiply(matrixA, matrixB));if (tempMat == nullptr) {return std::unique_ptr<RowMatrix<T>>(nullptr);}return Add(tempMat, matrixC);}
};
}  // namespace bustubx<T> *matrixB,const RowMatrix<T> *matrixC) {std::unique_ptr tempMat = std::move(Multiply(matrixA, matrixB));if (tempMat == nullptr) {return std::unique_ptr<RowMatrix<T>>(nullptr);}return Add(tempMat, matrixC);}
};
}  // namespace bustub

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407945.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

简单JS实现对表的行的增删

这段代码非常的简单&#xff0c;仅仅作为自己的一个小小的记录&#xff01; ok&#xff0c;先上一个简单的图例&#xff0c;效果如下&#xff08;注意&#xff1a;这只是一个简单的例子&#xff0c;不过可以根据这个简单的例子&#xff0c;变化出更为复杂的效果&#xff09;&am…

用 JavaScript 操作字符串

虽然 JavaScript 有很多用处&#xff0c;但是处理字符串是其中最流行的一个。下面让我们深入地分析一下使用 JavaScript 操作字符串。在 JavaScript 中&#xff0c; String 是对象。 String 对象并不是以字符数组的方式存储的&#xff0c;所以我们必须使用内建函数来操纵它们的…

The Ranges Library --- C++20

The Ranges Library — C20 ranges可以让我们更加舒服的写代码了, 不用再敲那么多的代码 之前我们需要这样标准库的算法对容器的操作 #include <iostream> #include <ranges> #include <vector> #include <algorithm>int main() {std::vector vec{ …

什么是反射(.NET)[转]

From: http://www.cnblogs.com/zxsoft/archive/2007/09/17/895236.html 反射&#xff08;Reflection&#xff09;是.NET中的重要机制&#xff0c;通过放射&#xff0c;可以在运行时获得.NET中每一个类型&#xff08;包括类、结构、委托、接口和枚举等&#xff09;的成员&#x…

Yii权限管理工具Srbac使用小结

一、关于Srbac Srbac是基于Yii 框架的 RBAC&#xff08;基于角色的访问控制&#xff09; 插件模块&#xff0c;用于帮助Yii开发人员更方便地进行权限控制&#xff0c;在实际应用过程中也比较方便。 二、安装配置Srbac 在Yii的官方网站的Extensions中已经收录了Srbac插件&#x…

概念concept和requires ---C++ 20

概念concept和requires —C 20 concept concept简化了模板编程的难度 我们可以使用**concept定义模板形参的约束条件T** 模板实力替换T后必须满足std::is_integral_v<C>;为true 例子: requires关键字可以直接约束模板形参T 如下: template <class C> concept …

辞职中

不知道为什么就不想再做下去了&#xff0c;说不上来工作有什么好&#xff0c;当然也说不上来什么不好。SP这个行业&#xff0c;可能不太适合我。准备走了&#xff0c;考研还是继续找工作&#xff1f;我不知道该怎么走&#xff01;看着前面都是路却找不到一条适合自己的。转载于…

向DataGridView中添加新的一行数据,可以添加到最后一行或作为第一行

我的开发环境&#xff1a;Microsoft Visual Studio .net 2005 这个程序是Windows Forms Application 新建一个Windows Forms Application项目&#xff0c;打开Form1&#xff0c;在窗体上放一个DataGridView控件和Button,在DataGridView的Columns中添加两列&#xff0c;Name分别…

Linq学习

IEnumerable<DataRow> cbCurrent from dr in cbRows.AsEnumerable() where dr.Field<string>("ObjectNO").ToString() row["运输单号"].…

非类型模板参数(参考《C++ Templates 英文版第二版》)

非类型模板参数(参考《C Templates 英文版第二版》) Chapter 3 3.1 非类型类模板参数 与前几章的简单例子不同,你也可以通过std::array实例化一个固定大小的栈,这样做的优点在于内存管理, #include <array> #include <cassert>template<typename T, std::si…

I AM NOTHING vs I AM SOMETHING

女友推荐链接&#xff1a;BBS上一个女生的创业感言 自创业的一路艰辛 http://bbs.trendsmag.com/showthread.php?t163960

DataGridView添加一行数据、全选、取消全选、清空数据、删除选中行

.net 2005下的Windows Form Application,一个DataGridView控件和4个Button&#xff0c;界面设置如下&#xff1a; 代码如下&#xff0c;有注解&#xff0c;相信大家都看得明白&#xff1a; using System;using System.Collections.Generic;using System.ComponentModel;using S…

类型萃取类型检查 Type-Traits LibraryType Checks --- C++20

类型萃取:类型检查 Type-Traits Library:Type Checks — C20 Type-Traits library 在C11的时候就已经发布,但依然随着C版本在不断更新 类型检查 Type Checks 每种类型就是十四种主要类型之一 主要类型 template <class T> struct is_void; template <class T>…

转:如何在 LoadRunner 脚本中做关联 (Correlation)

如何在 LoadRunner 脚本中做关联 (Correlation) 当录制脚本时&#xff0c;VuGen会拦截client端&#xff08;浏览器&#xff09;与server端&#xff08;网站服务器&#xff09;之间的对话&#xff0c;并且通通记录下来&#xff0c;产生脚本。在VuGen的Recording Log中&#xff0…

开博碎语

结束了5月26号的软考&#xff0c;就萌生了建一个技术博客的想法-----技术或许太空泛&#xff0c;其实就是把工作中&#xff0c;学习上技术方面的一些资料&#xff0c;一些体会汇聚一起&#xff0c;呈现出来&#xff0c;博客当然是个不错的选择。baidu一下&#xff0c;技术博客为…