添加Rust版本bvid算法

This commit is contained in:
stackinspector 2023-05-16 02:56:18 +08:00
parent 16d455ff09
commit f8a8f8a245

View File

@ -74,9 +74,9 @@
## 编程实现
使用 Python、C、TypeScript、Java、Kotlin 以及 Golang 等语言作为示例,欢迎社区提交更多例程
使用 [Python](#Python) [C](#C) [TypeScript](#TypeScript) [Java](#Java) [Kotlin](#Kotlin) [Golang](#Golang) [Rust](#Rust) 等语言作为示例,欢迎社区提交更多例程
注: 新算法只提供了Python版本
注: 新算法只提供了 [Python](#Python) 和 [Rust](#Rust) 版本
### Python
```python
@ -385,3 +385,40 @@ func main() {
BV17x411w7KC
170001
```
### Rust
crate: https://github.com/stackinspector/bvid
```rust
// Copyright (c) 2023 stackinspector. MIT license.
const XORN: u64 = 177451812;
const ADDN: u64 = 100618342136696320;
const TABLE: [u8; 58] = *b"fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
const MAP: [usize; 10] = [9, 8, 1, 6, 2, 4, 0, 7, 3, 5];
const REV_TABLE: [u8; 74] = [
13, 12, 46, 31, 43, 18, 40, 28, 5, 0, 0, 0, 0, 0, 0, 0, 54, 20, 15, 8,
39, 57, 45, 36, 0, 38, 51, 42, 49, 52, 0, 53, 7, 4, 9, 50, 10, 44, 34, 6,
25, 1, 0, 0, 0, 0, 0, 0, 26, 29, 56, 3, 24, 0, 47, 27, 22, 41, 16, 0,
11, 37, 2, 35, 21, 17, 33, 30, 48, 23, 55, 32, 14, 19,
];
const POW58: [u64; 10] = [
1, 58, 3364, 195112, 11316496, 656356768, 38068692544,
2207984167552, 128063081718016, 7427658739644928,
];
pub fn av2bv(avid: u64) -> [u8; 10] {
let a = (avid ^ XORN) + ADDN;
let mut bvid = [0; 10];
for i in 0..10 {
bvid[MAP[i]] = TABLE[(a / POW58[i]) as usize % 58];
}
bvid
}
pub fn bv2av(bvid: [u8; 10]) -> u64 {
let mut a = 0;
for i in 0..10 {
a += REV_TABLE[bvid[MAP[i]] as usize - 49] as u64 * POW58[i];
}
(a - ADDN) ^ XORN
}
```