没有合适的资源?快使用搜索试试~ 我知道了~
project4-good-example-2021fall-by徐临风1
需积分: 0 0 下载量 145 浏览量
2022-08-03
17:58:00
上传
评论
收藏 1.67MB PDF 举报
温馨提示
![preview](https://dl-preview.csdnimg.cn/86298550/0001-6b35fb60b50b3451938a90f483cc00ee_thumbnail.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
试读
27页
① The constructor with no parameters is to test the assignment operator ② The co
资源详情
资源评论
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/86298550/bg1.jpg)
CS205 C/ C++ Programming - Project #4
Name: 徐临风 (Xu Linfeng)
SID: 11812407
Part 1 – Analysis
This project is to design a class for matrices, which should satisfy the following requirements:
✔ This class should contain the member variable to realize that one element of matrix
storage a vector.
✔ This class can support different kinds of data types, such as unsigned char, short, int, float,
double, etc.
✔ Constructors and destructors should be carefully designed to avoid problems of memory
management.
✔ Some general operators should be overloaded to support the normal matrix computation.
✔ In particular, the assignment operator should be overloaded to avoid hard copy when data of
one matrix is assigned to another ones.
✔ Apply the idea of Region of Interest to matrices: try to revise the desired submatrix
without hard copy.
✔ Try to use the optimization method of matrix multiplication in in this class. For
user convenience, the operator can be overloaded with optimization algorithm applied.
✔ Test this class on and platform: compare the differences, in particular for
and for .
In ,we defined a structure for matrices. Some operations, such as matrix
multiplication, matrix copy, memory management, are realized by corresponding functions.
Moreover, the programming language we used is , meaning that there are many convenient
tools we cannot use last time.
In , we design a class for matrices rather than structure. Therefore, the
corresponding functions for matrix operation and memory management are replaced by member
functions (friend functions) and constructors and destructors, respectively. Apart from the above
differences, we can overload some frequently used operators, such as , which is
convenient for the input and output of matrices into files or onto screen.
Some detailed descriptions of idea to realize the above requirements are stated as follows:
![](https://csdnimg.cn/release/download_crawler_static/86298550/bg2.jpg)
◉ In in , it uses union containing 5 pointers of different data types pointing
to matrix data, which share the same memory. This is a good idea, but I cannot make it. Apart
from union, the class template can also support different kinds of data, but it needs to define
more member variables and thus much more space consumption. We define a macro
to change the data type by hand, which is not wise but can save a lot space compared with that of
class template.
◉ We define constructors and copy constructor to create objects:
① The constructor with no parameters is to test the assignment operator , and also to replace
the default constructor.
② The constructor with row number and column number specified is to create matrices whose
data is provided by .txt files, and also to create temporary objects which can be returned in
matrix computation .
③ The constructor with data pointer specified additionally is to create matrices whose data is
already known.
④ The copy constructor is designed to achieve soft copy and avoid problems of memory
management.
The destructor is designed to free the memory in time. As you will see in , the
destructor is key to achieve soft copy.
◉ In this matrix class, we overload the operators:
◉ In particular, we overload the assignment operator as follows:
Mat();
Mat(int row, int col, int channel);
Mat(int row, int col, const myType * data, int channel);
Mat(const Mat & mat); // soft copy
bool operator == (const Mat & mat_cmp); // 比较矩阵元素
Mat & operator = (const Mat & mat_copy); // 矩阵复制
Mat operator + (const Mat & mat); // 矩阵加法
Mat operator - (const Mat & mat); // 矩阵减法
Mat operator * (const Mat & mat); // 矩阵乘法
Mat operator ~ (); // 矩阵转置
Mat & submat (int a, int b, string fileName); // ROI修改特定区域的矩阵数据
friend Mat operator * (myType a, const Mat & rmat); // 矩阵左数乘
friend Mat operator * (const Mat & lmat, myType a); // 矩阵右数乘
friend ostream & operator << (ostream & os, const Mat & mat); // 输出矩阵到屏幕
friend ifstream & operator >> (ifstream & ifs, const Mat & mat); // 从文件读取矩阵
friend ofstream & operator << (ofstream & ofs, const Mat & mat); // 输出矩阵到文件
Mat & Mat::operator = (const Mat & mat_copy) // soft copy
{
cout << "Mat & Mat::operator = (const Mat & mat_copy)" << endl;
if (this == & mat_copy)
![](https://csdnimg.cn/release/download_crawler_static/86298550/bg3.jpg)
① If the object is assigned to itself, then just return the reference of itself.
② Before the assignment operation, we should delete the space that the data point refers to to
avoid memory leak. Here we do not use because the pointer dataptr does not directly
point to the matrix data. To manage the memory safely, we try to use the idea of smart pointers:
shared_ptr: define a member variable to count the number of pointers which point to the space
of the data of this matrix. However, since the parameter of the assignment operator overloading
member function and the copy constructor are limited by the keyword const, we cannot directly
add one member variable to count the number of pointers pointing to the space of the data of
this matrix. Therefore, we design another class:: , which contains the pointer pointing to the
matrix data and the member variable to count the number of pointers pointing to the
space of the data of this matrix. The design of the class:: is shown as follows: (more detailed
analysis will be given in )
③ Copy the corresponding member variables: .
④ Judge whether the pointer of the object you want to assign is . If it points to the space
which contains the pointer pointing to the matrix data, then we just use to assign the address
of data of two matrices. In this way, two pointers point to the same space, sharing the same data,
which is actually called "soft copy".
⑤ Surely, the direct assignment of address is easy. The difficult part is to manage the memory.
After achieving data-sharing, we call the member function of class:: to
reveal that one more pointer is pointing to this space now.
return *this;
delete this->dataptr;
this->row = mat_copy.row;
this->col = mat_copy.col;
this->size = mat_copy.size;
this->channel = mat_copy.channel;
if (mat_copy.dataptr)
{
this->dataptr = mat_copy.dataptr;
this->dataptr->addPtrCount();
}
return *this;
}
class Data
{
private:
myType * dataval;
int ptr_count;
public:
Data(size_t length);
~Data();
myType * getDataValue();
void addPtrCount();
void minusPtrCount();
int getPtrCount();
};
![](https://csdnimg.cn/release/download_crawler_static/86298550/bg4.jpg)
◉ Following the above idea of soft copy, we can realize the ROI function in this class. is
usually used in picture processing to "cut" and "revise" the particular part of the whole picture.
Similarly, in our case of matirx class, we need to find the submatrix with the given indices (start
index, end index). Moreover, to avoid hard copy, we can follow the above idea of soft copy, we
just assign the address of two pointers the same, sharing the same space. Then we can revise the
data of the submatrix by pointers.
The definition of the member function ROI is shown as follows: (more detailed analysis will be
given in the related test cases in )
Mat & Mat::submat (int a, int b, string fileName) // 传入的文件是想要修改到目标区域的
数据
{
if (a > this->size || b > this->size)
{
cout << "invalid ROI" << endl;
return *this;
}
int a_row = a / this->col + 1; // 一维数组的index a对应的行数
int a_col = a % this->col + 1; // 一维数组的index a对应的列数
int b_row = b / this->col + 1; // 一维数组的index b对应的行数
int b_col = b % this->col + 1; // 一维数组的index b对应的行数
int file_row = getRowNum(fileName); // 读入文件的行数
int file_col = getColNum(fileName); // 读入文件的列数
int max_row = max(a_row, b_row); // 判断index a和b谁对应的行数是起点,谁对应的列数
是起点
int min_row = min(a_row, b_row);
int max_col = max(a_col, b_col);
int min_col = min(a_col, b_col);
int size_row = max_row - min_row + 1;
int size_col = max_col - min_col + 1;
if (size_row * size_col != file_row * file_col) // 判断传入文件的矩阵的大小是否和
想要修改的区域大小相同
{
cout << "invalid ROI" << endl;
return *this;
}
ifstream fin(fileName);
Mat roi_data = Mat(file_row,file_col,1); // 将文件中的数据存入Mat类的对象中
fin >> roi_data;
fin.close();
// 通过指针修改对应区域的数据
myType * roi_ptr = this->dataptr->getDataValue(); // 指向存储数据的首地址
int copydata_index = 0;
for (int i = (min_row-1)*this->col+min_col-1; i <= (max_row-1)*this-
>col+max_col-1; i++)
{
int ptr_row = i / this->col + 1;
int ptr_col = i % this->col + 1;
if (ptr_row >= min_row && ptr_row <= max_row && ptr_col >= min_col &&
ptr_col <= max_col)
{
*(roi_ptr+i) = roi_data.dataptr->getDataValue()[copydata_index];
copydata_index++;
}
![](https://csdnimg.cn/release/download_crawler_static/86298550/bg5.jpg)
◉ For user convenience, we overload the operator for several matrix multiplication algorithms
rather than directly set them as member functions. However, this can also cause inconvenience
because the same parameter of overloading operator will cause redifinition. Hence, each time
we run the program, we need to comment out the other overloading for . In this class, we
overload the operator with the following algorithms: (more detailed analysis will be given in
related test cases in )
① general loop (in , we prove that is the most efficient order to
compute matrix multiplication for general algorithms)
② optimization ( : one loop, four calculations)
③ optimization ( : one loop, four calculations)
Remark:
⚝ To meet the requirement, this matrix class should contain a member variable called .
If , this means that each data of the matrix is actually a vector, just like
sheets in . For convenience, we define the normal matrix computation as
corresponding channel operations, .
⚝ Compared with , the programming language here is instead of . Hence,
for operator overloading and member functions (friend functions), if we can return by reference
or pass by reference, we avoid using objects. In this case, the program can be more efficient and
also save more space (avoid copying objects).
⚝ The pointer member variable which points to the matrix data is suggested to be
rather than . This is because pointer usage needs to request memory twice,
which may be discontinuous, while case only needs once, avoiding this risk.
Part 2 – Code
There are 6 source code files: data.hpp, data.cpp, mat.hpp, mat.cpp, mat_arm.cpp, main.cpp.
Since the code is relatively long, we do not show the whole code in this part (The complete code is
attached in blackboard). Instead, we just show some key parts and give necessary explanations to
describe the idea analyzed in .
◉ The design of the class:: is shown in , so here we just show part of the definition
of the member functions of class:: in data.cpp:
}
cout << "After revision in ROI, ROI_final = " << endl;
cout << *this;
return *this;
}
Data::Data(size_t length)
{
this->dataval = new myType[length]{};
剩余26页未读,继续阅读
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
Jaihwoe
- 粉丝: 19
- 资源: 350
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0