Skip to content

Commit

Permalink
matrix: Add linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Aug 15, 2024
1 parent 0fd42dd commit 9ff5d67
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 33 deletions.
5 changes: 2 additions & 3 deletions list/src/doubly_linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,8 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
}
}

impl<T> ExactSizeIterator for IntoIter<T> {}

impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

Expand Down Expand Up @@ -662,8 +664,6 @@ impl<'a, T> Iterator for Iter<'a, T> {
}
}

impl<T> ExactSizeIterator for IntoIter<T> {}

impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.len == 0 {
Expand All @@ -681,7 +681,6 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {

impl<'a, T> ExactSizeIterator for Iter<'a, T> {}


impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;

Expand Down
5 changes: 3 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
- [矩阵 Matrix](matrix/index.md)
- [矩阵的常用操作](matrix/adt.md)
- [稀疏矩阵](matrix/sparse-matrix/index.md)
- [数组表示法](matrix/sparse-matrix/array.md)
- [List of Lists 表示法](matrix/sparse-matrix/list-of-lists.md)
- [数组](matrix/sparse-matrix/array.md)
- [链表](matrix/sparse-matrix/linked-list.md)
- [List of Lists](matrix/sparse-matrix/list-of-lists.md)
- [字典 Dictionary](matrix/sparse-matrix/dictionary.md)
- [动态数组 Vector](vector/index.md)
- [动态数组的常用操作](vector/adt.md)
Expand Down
8 changes: 4 additions & 4 deletions src/matrix/sparse-matrix/array.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# 数组表示法
# 数组

数组表示法来存储稀疏矩阵, 就是只在数组中存储里面非零的元素.

数组中每个项包含三部分:
数组中每个元素项都包含三部分:

- 该元素在矩阵中的行号
- 该元素在矩阵中的列号
Expand All @@ -28,13 +28,13 @@
- 从头到尾以行编号递增
- 相同行编号时, 以列编号递增
- 即整体上行编号有序递增, 整体上列编号无序, 但局部上列编号递增
- 查找矩阵中某个节点的值时的性能是 `O(log(m) * log(n))`, 其中 `m``n` 是矩阵的行列数,
- 查找矩阵中某个节点的值时的性能是 `O(log(m) * log(n))`, 其中 `m``n` 是矩阵中非 0 元素的最大行列数,
因为是有序排列的, 可以用二分查找法
- 比较适合存放固定不变的矩阵, 插入或者删除元素的成本比较高

## 算法的实现

这里只实现了构造稀疏矩阵和查找元素等少数方法, 因为插入和删除元素的效率低, 我们不把这种方法作为存储矩阵的主要方式.
这里只实现了构造稀疏矩阵和查找元素等少数方法.

```rust
{{#include assets/array_sparse_matrix.rs:5:86}}
Expand Down
47 changes: 25 additions & 22 deletions src/matrix/sparse-matrix/assets/array-sparse-matrix.drawio
Original file line number Diff line number Diff line change
@@ -1,72 +1,75 @@
<mxfile host="Electron" modified="2024-08-15T06:42:37.955Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/24.2.5 Chrome/120.0.6099.109 Electron/28.1.0 Safari/537.36" etag="U45PDUj-IbwJNPa3cGmG" version="24.2.5" type="device">
<mxfile host="Electron" modified="2024-08-15T08:34:46.286Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/24.2.5 Chrome/120.0.6099.109 Electron/28.1.0 Safari/537.36" etag="xjJVF3YixrH0gmK6zzll" version="24.2.5" type="device">
<diagram name="Page-1" id="G8ZvJWXJsHYqm7BaVf0D">
<mxGraphModel dx="1420" dy="821" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="pUB8m92aT1Gspuc-Nzx4-0" />
<mxCell id="pUB8m92aT1Gspuc-Nzx4-1" parent="pUB8m92aT1Gspuc-Nzx4-0" />
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-33" value="0" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-33" value="0" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="120" y="160" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-34" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-34" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="120" y="240" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-37" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-37" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="120" y="200" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-39" value="0" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-39" value="0" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="160" y="160" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-40" value="4" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-40" value="4" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="160" y="240" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-41" value="4" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-41" value="4" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="160" y="200" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-42" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-42" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="200" y="160" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-43" value="5" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-43" value="5" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="200" y="240" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-44" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-44" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="200" y="200" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-45" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-45" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="240" y="160" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-46" value="7" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-46" value="7" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="240" y="240" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-47" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-47" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="240" y="200" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-48" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-48" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="280" y="160" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-49" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-49" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="280" y="240" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-50" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-50" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="280" y="200" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-51" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-51" value="3" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="320" y="160" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-52" value="6" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-52" value="6" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="320" y="240" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-53" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-53" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="320" y="200" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-54" value="Row" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Ubuntu;fontSize=16;fontStyle=0;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-54" value="Row" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Ubuntu;fontSize=16;fontStyle=0;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="40" y="160" width="80" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-55" value="Column" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Ubuntu;fontSize=16;fontStyle=0;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-55" value="Column" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Ubuntu;fontSize=16;fontStyle=0;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="40" y="200" width="80" height="40" as="geometry" />
</mxCell>
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-56" value="Value" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Ubuntu;fontSize=16;fontStyle=0;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-56" value="Value" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Ubuntu;fontSize=16;fontStyle=0;" parent="pUB8m92aT1Gspuc-Nzx4-1" vertex="1">
<mxGeometry x="40" y="240" width="80" height="40" as="geometry" />
</mxCell>
<mxCell id="cXrHrEwbf_wfJcx70135-0" value="Array Sparse Matrix" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Ubuntu;fontSize=16;fontStyle=0;" vertex="1" parent="pUB8m92aT1Gspuc-Nzx4-1">
<mxGeometry x="140" y="320" width="160" height="40" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
Expand Down
2 changes: 1 addition & 1 deletion src/matrix/sparse-matrix/assets/array-sparse-matrix.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9ff5d67

Please sign in to comment.