-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
matrix: Add btree sparse matrix chart
- Loading branch information
Showing
7 changed files
with
165 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/matrix/sparse-matrix/assets/btree-sparse-matrix.drawio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<mxfile host="Electron" modified="2024-08-19T04:55:14.730Z" 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="wi6R7fEnLB70rphQMmgj" version="24.2.5" type="device"> | ||
<diagram name="Page-1" id="G8ZvJWXJsHYqm7BaVf0D"> | ||
<mxGraphModel dx="2270" 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="cXrHrEwbf_wfJcx70135-0" value="BTree Sparse Matrix" 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="80" y="440" width="160" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-0" value="" style="group" vertex="1" connectable="0" parent="pUB8m92aT1Gspuc-Nzx4-1"> | ||
<mxGeometry x="40" y="360" width="120" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-39" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="kMppL0oYQh2jDNrE7ftQ-0" vertex="1"> | ||
<mxGeometry width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-40" value="5" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="kMppL0oYQh2jDNrE7ftQ-0" vertex="1"> | ||
<mxGeometry x="80" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-41" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="kMppL0oYQh2jDNrE7ftQ-0" vertex="1"> | ||
<mxGeometry x="40" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-1" value="" style="group" vertex="1" connectable="0" parent="pUB8m92aT1Gspuc-Nzx4-1"> | ||
<mxGeometry x="-120" y="360" width="120" height="40" as="geometry" /> | ||
</mxCell> | ||
<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="kMppL0oYQh2jDNrE7ftQ-1" vertex="1"> | ||
<mxGeometry 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;" parent="kMppL0oYQh2jDNrE7ftQ-1" vertex="1"> | ||
<mxGeometry x="80" 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;" parent="kMppL0oYQh2jDNrE7ftQ-1" vertex="1"> | ||
<mxGeometry x="40" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-2" value="0" style="group" vertex="1" connectable="0" parent="pUB8m92aT1Gspuc-Nzx4-1"> | ||
<mxGeometry x="-40" y="280" width="120" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-42" value="0" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="kMppL0oYQh2jDNrE7ftQ-2" vertex="1"> | ||
<mxGeometry width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-43" value="4" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#82b366;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#d5e8d4;" parent="kMppL0oYQh2jDNrE7ftQ-2" vertex="1"> | ||
<mxGeometry x="80" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="CJ7Yq6GX4A4KFnFIdWCx-44" value="4" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="kMppL0oYQh2jDNrE7ftQ-2" vertex="1"> | ||
<mxGeometry x="40" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-3" value="" style="group" vertex="1" connectable="0" parent="pUB8m92aT1Gspuc-Nzx4-1"> | ||
<mxGeometry x="80" y="200" width="120" 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;" parent="kMppL0oYQh2jDNrE7ftQ-3" vertex="1"> | ||
<mxGeometry 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;" parent="kMppL0oYQh2jDNrE7ftQ-3" vertex="1"> | ||
<mxGeometry x="80" 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;" parent="kMppL0oYQh2jDNrE7ftQ-3" vertex="1"> | ||
<mxGeometry x="40" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-4" value="" style="group" vertex="1" connectable="0" parent="pUB8m92aT1Gspuc-Nzx4-1"> | ||
<mxGeometry x="200" y="280" width="120" 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;" parent="kMppL0oYQh2jDNrE7ftQ-4" vertex="1"> | ||
<mxGeometry 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;" parent="kMppL0oYQh2jDNrE7ftQ-4" vertex="1"> | ||
<mxGeometry x="80" 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;" parent="kMppL0oYQh2jDNrE7ftQ-4" vertex="1"> | ||
<mxGeometry x="40" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-5" value="" style="group" vertex="1" connectable="0" parent="pUB8m92aT1Gspuc-Nzx4-1"> | ||
<mxGeometry x="280" y="360" width="120" 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;" parent="kMppL0oYQh2jDNrE7ftQ-5" vertex="1"> | ||
<mxGeometry 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;" parent="kMppL0oYQh2jDNrE7ftQ-5" vertex="1"> | ||
<mxGeometry x="80" 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;" parent="kMppL0oYQh2jDNrE7ftQ-5" vertex="1"> | ||
<mxGeometry x="40" width="40" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-6" style="rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;jumpStyle=line;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="CJ7Yq6GX4A4KFnFIdWCx-44" target="CJ7Yq6GX4A4KFnFIdWCx-37"> | ||
<mxGeometry relative="1" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-7" style="rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;jumpStyle=line;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="CJ7Yq6GX4A4KFnFIdWCx-44" target="CJ7Yq6GX4A4KFnFIdWCx-41"> | ||
<mxGeometry relative="1" as="geometry"> | ||
<mxPoint x="30" y="330" as="sourcePoint" /> | ||
<mxPoint x="-90" y="410" as="targetPoint" /> | ||
</mxGeometry> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-9" style="rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;jumpStyle=line;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="CJ7Yq6GX4A4KFnFIdWCx-50" target="CJ7Yq6GX4A4KFnFIdWCx-53"> | ||
<mxGeometry relative="1" as="geometry"> | ||
<mxPoint x="-50" y="330" as="sourcePoint" /> | ||
<mxPoint x="30" y="410" as="targetPoint" /> | ||
</mxGeometry> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-11" style="rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="CJ7Yq6GX4A4KFnFIdWCx-47" target="CJ7Yq6GX4A4KFnFIdWCx-44"> | ||
<mxGeometry relative="1" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-12" style="rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="CJ7Yq6GX4A4KFnFIdWCx-47" target="CJ7Yq6GX4A4KFnFIdWCx-50"> | ||
<mxGeometry relative="1" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-16" style="edgeStyle=orthogonalEdgeStyle;rounded=1;orthogonalLoop=1;jettySize=auto;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="kMppL0oYQh2jDNrE7ftQ-13" target="CJ7Yq6GX4A4KFnFIdWCx-45"> | ||
<mxGeometry relative="1" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-13" 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"> | ||
<mxGeometry x="45" y="120" width="50" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-17" style="edgeStyle=orthogonalEdgeStyle;rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="kMppL0oYQh2jDNrE7ftQ-14" target="CJ7Yq6GX4A4KFnFIdWCx-47"> | ||
<mxGeometry relative="1" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-14" 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"> | ||
<mxGeometry x="110" y="120" width="60" height="40" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-18" style="edgeStyle=orthogonalEdgeStyle;rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=14;fontStyle=0;endArrow=block;endFill=1;fillColor=#dae8fc;" edge="1" parent="pUB8m92aT1Gspuc-Nzx4-1" source="kMppL0oYQh2jDNrE7ftQ-15" target="CJ7Yq6GX4A4KFnFIdWCx-46"> | ||
<mxGeometry relative="1" as="geometry" /> | ||
</mxCell> | ||
<mxCell id="kMppL0oYQh2jDNrE7ftQ-15" 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"> | ||
<mxGeometry x="180" y="120" width="60" height="40" as="geometry" /> | ||
</mxCell> | ||
</root> | ||
</mxGraphModel> | ||
</diagram> | ||
</mxfile> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../matrix/src/btree_sparse_matrix.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# BTree | ||
|
||
以下面的矩阵为例: | ||
|
||
\begin{bmatrix} \\ | ||
0 & 0 & 3 & 0 & 4 \\\\ | ||
0 & 0 & 5 & 7 & 0 \\\\ | ||
0 & 0 & 0 & 0 & 0 \\\\ | ||
0 & 2 & 6 & 0 & 0 \\ | ||
\end{bmatrix} | ||
|
||
这个矩阵用数组存放, 效果如下图: | ||
|
||
![btree sparse matrix](assets/btree-sparse-matrix.svg) | ||
|
||
这种存储方式的特点是: | ||
|
||
- BTree 中节点是按照 key 的顺序进行存储的, 而我们选用 (row, column) 作为 key, 这样 | ||
- 首先以行号递增排序 | ||
- 如果行号相同, 以列号递增排序 | ||
- 查找/插入/删除矩阵中某个节点的值时的性能是 `O(log(m * n))`, 其中 `m` 和 `n` 是矩阵中非 0 元素的最大行列数 | ||
- 比较适合存放随时增减节点的矩阵, 插入或者删除元素的成本比较低, 很灵活 | ||
- 支持范围查找, 比如查找某一行中所有的列 | ||
- 实现简单 | ||
|
||
## 算法的实现 | ||
|
||
使用 BTree 进行存储, 实现起来最简单, 因为我们要求的接口与 BTreeMap 本身的接口非常匹配, 需要额外 | ||
花费的精力很少. | ||
|
||
```rust | ||
{{#include assets/btree_sparse_matrix.rs:5:73}} | ||
``` |
This file was deleted.
Oops, something went wrong.