Merge branch 'pr/happynewplay/41'
This commit is contained in:
@@ -21,3 +21,11 @@ bak
|
||||
|
||||
demos
|
||||
benchmarks
|
||||
|
||||
lib/*.lib
|
||||
lib/*.pdb
|
||||
|
||||
findings.md
|
||||
modify_test.md
|
||||
progress.md
|
||||
task_plan.md
|
||||
Generated
-2
@@ -1235,8 +1235,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "esaxx-rs"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d817e038c30374a4bcb22f94d0a8a0e216958d4c3dcde369b1439fec4bdda6e6"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
@@ -45,6 +45,9 @@ sentencepiece = "0.13.1"
|
||||
ahash = "0.8.12"
|
||||
derive_builder = "0.20.2"
|
||||
|
||||
[patch.crates-io]
|
||||
esaxx-rs = { path = "vendor/esaxx-rs" }
|
||||
|
||||
[features]
|
||||
flash-attn = ["candle-flash-attn"]
|
||||
cuda = ["candle-nn/cuda", "candle-core/cuda", "candle-transformers/cuda"]
|
||||
@@ -55,3 +58,5 @@ ffmpeg = ["ffmpeg-next"]
|
||||
needless_range_loop = "allow"
|
||||
single_range_in_vec_init = "allow"
|
||||
manual_div_ceil = "allow"
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -119,6 +119,21 @@ aha cli -m Qwen/Qwen3-ASR-0.6B
|
||||
# Run inference directly (without starting service)
|
||||
aha run -m Qwen/Qwen3-ASR-0.6B -i "audio.wav"
|
||||
|
||||
# Run local all-MiniLM-L6-v2 embedding (native safetensors)
|
||||
aha run -m all-minilm-l6-v2 -i "Rust embedding test" --weight-path D:\model_download\all-MiniLM-L6-v2
|
||||
|
||||
# Run local all-MiniLM-L6-v2 embedding (GGUF)
|
||||
aha run -m all-minilm-l6-v2 -i "Rust embedding test" --artifact-format gguf --gguf-path D:\model_download\All-MiniLM-L6-v2-Embedding-GGUF --tokenizer-dir D:\model_download\all-MiniLM-L6-v2
|
||||
|
||||
# Run local all-MiniLM-L6-v2 embedding (ONNX)
|
||||
aha run -m all-minilm-l6-v2 -i "Rust embedding test" --artifact-format onnx --onnx-path D:\model_download\all-MiniLM-L6-v2\onnx --tokenizer-dir D:\model_download\all-MiniLM-L6-v2
|
||||
|
||||
# Run local GLM-OCR (GGUF)
|
||||
aha run -m glm-ocr -i .\assets\img\ocr_test1.png --artifact-format gguf --gguf-path D:\model_download\GLM-OCR-GGUF
|
||||
|
||||
# Run local GLM-OCR (ONNX)
|
||||
aha run -m glm-ocr -i .\assets\img\ocr_test1.png --artifact-format onnx --onnx-path D:\model_download\GLM-OCR-ONNX --tokenizer-dir D:\model_download\GLM-OCR-ONNX
|
||||
|
||||
# Start service only (model already downloaded)
|
||||
aha serv -m Qwen/Qwen3-ASR-0.6B -p 10100
|
||||
|
||||
|
||||
@@ -119,6 +119,21 @@ aha cli -m Qwen/Qwen3-ASR-0.6B
|
||||
# 直接运行推理(无需启动服务)
|
||||
aha run -m Qwen/Qwen3-ASR-0.6B -i "audio.wav"
|
||||
|
||||
# 本地运行 all-MiniLM-L6-v2 向量模型(原生 safetensors)
|
||||
aha run -m all-minilm-l6-v2 -i "Rust embedding test" --weight-path D:\model_download\all-MiniLM-L6-v2
|
||||
|
||||
# 本地运行 all-MiniLM-L6-v2 向量模型(GGUF)
|
||||
aha run -m all-minilm-l6-v2 -i "Rust embedding test" --artifact-format gguf --gguf-path D:\model_download\All-MiniLM-L6-v2-Embedding-GGUF --tokenizer-dir D:\model_download\all-MiniLM-L6-v2
|
||||
|
||||
# 本地运行 all-MiniLM-L6-v2 向量模型(ONNX)
|
||||
aha run -m all-minilm-l6-v2 -i "Rust embedding test" --artifact-format onnx --onnx-path D:\model_download\all-MiniLM-L6-v2\onnx --tokenizer-dir D:\model_download\all-MiniLM-L6-v2
|
||||
|
||||
# 本地运行 GLM-OCR(GGUF)
|
||||
aha run -m glm-ocr -i .\assets\img\ocr_test1.png --artifact-format gguf --gguf-path D:\model_download\GLM-OCR-GGUF
|
||||
|
||||
# 本地运行 GLM-OCR(ONNX)
|
||||
aha run -m glm-ocr -i .\assets\img\ocr_test1.png --artifact-format onnx --onnx-path D:\model_download\GLM-OCR-ONNX --tokenizer-dir D:\model_download\GLM-OCR-ONNX
|
||||
|
||||
# 仅启动服务(模型已下载)
|
||||
aha serv -m Qwen/Qwen3-ASR-0.6B -p 10100
|
||||
|
||||
|
||||
+39
-5
@@ -66,6 +66,11 @@ aha cli -m Qwen/Qwen3-VL-2B-Instruct --weight-path /path/to/model
|
||||
|
||||
# use gguf-path and mmproj-path
|
||||
aha cli -m qwen3.5-gguf --gguf-path /path/to/xxx.gguf --mmproj-path /path/to/mmproj-xxx.gguf
|
||||
|
||||
# run service with ONNX artifact
|
||||
aha cli -m qwen3-embedding-0.6b --artifact-format onnx \
|
||||
--onnx-path /path/to/Qwen3-Embedding-0.6B-ONNX \
|
||||
--tokenizer-dir /path/to/Qwen3-Embedding-0.6B-ONNX
|
||||
```
|
||||
|
||||
### run - Direct model inference
|
||||
@@ -117,6 +122,9 @@ aha run -m FunAudioLLM/Fun-ASR-Nano-2512 -i "语音转写:" -i "audio.wav" --w
|
||||
# qwen3 text generation (single input)
|
||||
aha run -m Qwen/Qwen3-0.6B -i "你好" --weight-path /path/to/model
|
||||
|
||||
# qwen3 GGUF text generation (single input)
|
||||
aha run -m qwen3-0.6b -i "hello" --artifact-format gguf --gguf-path /path/to/Qwen3-0.6B-Q8_0.gguf
|
||||
|
||||
# qwen2.5vl image understanding (two inputs: prompt text + image file)
|
||||
aha run -m Qwen/Qwen2.5-VL-3B-Instruct -i "请分析图片并提取所有可见文本内容,按从左到右、从上到下的布局,返回纯文本" -i "image.jpg" --weight-path /path/to/model
|
||||
|
||||
@@ -130,6 +138,11 @@ aha run -m qwen3.5-gguf -i 你如何看待AI --gguf-path /path/to/xxx.gguf
|
||||
aha run -m qwen3.5-gguf -i 提取图片中的文本 -i https://ai.bdstatic.com/file/C56CC9B274CF460CA33
|
||||
63E59ECD94423 --gguf-path /path/to/xxx.gguf --mmproj-path /path/to/mmproj-xxx.gguf
|
||||
|
||||
# Qwen3.5 ONNX text-only generation
|
||||
aha run -m qwen3.5-0.8b -i "hello" --artifact-format onnx \
|
||||
--onnx-path /path/to/Qwen3.5-0.8B-ONNX \
|
||||
--tokenizer-dir /path/to/Qwen3.5-0.8B-ONNX
|
||||
|
||||
```
|
||||
|
||||
### serv - Start service
|
||||
@@ -154,6 +167,9 @@ aha serv [OPTIONS] --model <MODEL> [--weight-path <WEIGHT_PATH>] [--gguf-path <G
|
||||
| `--allow-remote-shutdown` | Allow remote shutdown requests (not recommended) | false |
|
||||
| `--gguf-path <GGUF_PATH>` | Local GGUF model weight path(required when using GGUF models) | - |
|
||||
| `--mmproj-path <MMPROJ_PATH>` | Local mmproj GGUF weight path(optional,If not specified, the module will not be loaded) | - |
|
||||
| `--onnx-path <ONNX_PATH>` | Local ONNX model directory/file path(required when using ONNX models) | - |
|
||||
| `--tokenizer-dir <TOKENIZER_DIR>` | Tokenizer/config directory for GGUF/ONNX | - |
|
||||
| `--artifact-format <ARTIFACT_FORMAT>` | Artifact format (`auto|safetensors|gguf|onnx`) | auto |
|
||||
|
||||
**Examples:**
|
||||
|
||||
@@ -395,6 +411,20 @@ After the service starts, the following API endpoints are available:
|
||||
- **Format**: OpenAI Chat Completion format
|
||||
- **Streaming Support**: No
|
||||
|
||||
### Embeddings Endpoint
|
||||
- **Endpoint**: `POST /embeddings` or `POST /v1/embeddings`
|
||||
- **Function**: Text embedding generation
|
||||
- **Supported Models**: Qwen3-Embedding family
|
||||
- **Format**: OpenAI embeddings format
|
||||
- **Streaming Support**: No
|
||||
|
||||
### Rerank Endpoint
|
||||
- **Endpoint**: `POST /rerank` or `POST /v1/rerank`
|
||||
- **Function**: Query-document reranking
|
||||
- **Supported Models**: Qwen3-Reranker family
|
||||
- **Format**: Rerank JSON response (`results[index,relevance_score,document]`)
|
||||
- **Streaming Support**: No
|
||||
|
||||
### Shutdown Endpoint
|
||||
- **Endpoint**: `POST /shutdown`
|
||||
- **Function**: Gracefully shut down the server
|
||||
@@ -404,15 +434,19 @@ After the service starts, the following API endpoints are available:
|
||||
|
||||
## Notes
|
||||
|
||||
1. **serv subcommand requires `--weight-path`**: Since the `serv` subcommand does not download models, you must specify the path to an already downloaded model via `--weight-path`.
|
||||
1. **Local-path rule for GGUF/ONNX**: GGUF and ONNX artifacts are local-path only; use `--gguf-path` or `--onnx-path`. Remote download management is only for safetensors models.
|
||||
|
||||
2. **Download retry mechanism**: By default, retries 3 times, waiting 2 seconds after each failure before retrying. You can adjust the retry count with `--download-retries`.
|
||||
2. **Artifact selection**: `--artifact-format auto` uses model default; you can force `safetensors|gguf|onnx` explicitly.
|
||||
|
||||
3. **Default save directory**: Models are saved to `~/.aha/` directory by default, which can be customized via `--save-dir` or `-s` parameter.
|
||||
3. **Tokenizer directory**: For GGUF/ONNX, if tokenizer files are not colocated with model files, set `--tokenizer-dir`.
|
||||
|
||||
4. **Port occupation**: Ensure the specified port is not occupied before starting the service. The default port is 10100.
|
||||
4. **Download retry mechanism**: By default, retries 3 times, waiting 2 seconds after each failure before retrying. You can adjust the retry count with `--download-retries`.
|
||||
|
||||
5. **Permission issues**: If saving to a system directory (such as `/data/models`), ensure you have the corresponding write permissions.
|
||||
5. **Default save directory**: Models are saved to `~/.aha/` directory by default, which can be customized via `--save-dir` or `-s` parameter.
|
||||
|
||||
6. **Port occupation**: Ensure the specified port is not occupied before starting the service. The default port is 10100.
|
||||
|
||||
7. **Permission issues**: If saving to a system directory (such as `/data/models`), ensure you have the corresponding write permissions.
|
||||
|
||||
## Getting Help
|
||||
|
||||
|
||||
+39
-6
@@ -66,6 +66,11 @@ aha cli -m Qwen/Qwen3-VL-2B-Instruct --weight-path /path/to/model
|
||||
|
||||
# 指定gguf-path和mmproj-path
|
||||
aha cli -m qwen3.5-gguf --gguf-path /path/to/xxx.gguf --mmproj-path /path/to/mmproj-xxx.gguf
|
||||
|
||||
# 使用 ONNX 模型启动服务
|
||||
aha cli -m qwen3-embedding-0.6b --artifact-format onnx \
|
||||
--onnx-path /path/to/Qwen3-Embedding-0.6B-ONNX \
|
||||
--tokenizer-dir /path/to/Qwen3-Embedding-0.6B-ONNX
|
||||
```
|
||||
|
||||
### run - 直接模型推理
|
||||
@@ -117,6 +122,9 @@ aha run -m FunAudioLLM/Fun-ASR-Nano-2512 -i "语音转写:" -i "audio.wav" --w
|
||||
# qwen3 文本生成(单个输入)
|
||||
aha run -m Qwen/Qwen3-0.6B -i "你好" --weight-path /path/to/model
|
||||
|
||||
# qwen3 GGUF 文本生成(单个输入)
|
||||
aha run -m qwen3-0.6b -i "你好" --artifact-format gguf --gguf-path /path/to/Qwen3-0.6B-Q8_0.gguf
|
||||
|
||||
# qwen2.5vl 图像理解(两个输入:提示文本 + 图片文件)
|
||||
aha run -m Qwen/Qwen2.5-VL-3B-Instruct -i "请分析图片并提取所有可见文本内容,按从左到右、从上到下的布局,返回纯文本" -i "image.jpg" --weight-path /path/to/model
|
||||
|
||||
@@ -129,6 +137,11 @@ aha run -m qwen3.5-gguf -i 你如何看待AI --gguf-path /path/to/xxx.gguf
|
||||
# Qwen3.5-GGUF 有mmproj (两个输入:提示文本 + 文件)
|
||||
aha run -m qwen3.5-gguf -i 提取图片中的文本 -i https://ai.bdstatic.com/file/C56CC9B274CF460CA33
|
||||
63E59ECD94423 --gguf-path /path/to/xxx.gguf --mmproj-path /path/to/mmproj-xxx.gguf
|
||||
|
||||
# Qwen3.5 ONNX 文本生成(text-only)
|
||||
aha run -m qwen3.5-0.8b -i "你好" --artifact-format onnx \
|
||||
--onnx-path /path/to/Qwen3.5-0.8B-ONNX \
|
||||
--tokenizer-dir /path/to/Qwen3.5-0.8B-ONNX
|
||||
```
|
||||
|
||||
### serv - 启动服务
|
||||
@@ -139,7 +152,9 @@ GGUF/ONNX模型必须指定本地文件路径
|
||||
|
||||
**语法:**
|
||||
```bash
|
||||
aha serv [OPTIONS] --model <MODEL> [--weight-path <WEIGHT_PATH>] [--gguf-path <GGUF_PATH>] [--mmproj-path <MMPROJ_PATH>]
|
||||
aha serv [OPTIONS] --model <MODEL> [--weight-path <WEIGHT_PATH>] [--gguf-path <GGUF_PATH>] \
|
||||
[--mmproj-path <MMPROJ_PATH>] [--onnx-path <ONNX_PATH>] [--tokenizer-dir <TOKENIZER_DIR>] \
|
||||
[--artifact-format <ARTIFACT_FORMAT>]
|
||||
```
|
||||
|
||||
**选项:**
|
||||
@@ -396,6 +411,20 @@ aha cli -m Qwen/Qwen3-VL-2B-Instruct -a 0.0.0.0 -p 8080
|
||||
- **格式**: OpenAI Chat Completion 格式
|
||||
- **流式支持**: 不支持
|
||||
|
||||
### Embeddings 接口
|
||||
- **端点**: `POST /embeddings` 或 `POST /v1/embeddings`
|
||||
- **功能**: 文本向量生成
|
||||
- **支持模型**: Qwen3-Embedding 系列
|
||||
- **格式**: OpenAI embeddings 格式
|
||||
- **流式支持**: 不支持
|
||||
|
||||
### Rerank 接口
|
||||
- **端点**: `POST /rerank` 或 `POST /v1/rerank`
|
||||
- **功能**: query/document 重排打分
|
||||
- **支持模型**: Qwen3-Reranker 系列
|
||||
- **格式**: Rerank JSON(`results[index,relevance_score,document]`)
|
||||
- **流式支持**: 不支持
|
||||
|
||||
### 关机接口
|
||||
- **端点**: `POST /shutdown`
|
||||
- **功能**: 优雅地关闭服务器
|
||||
@@ -405,15 +434,19 @@ aha cli -m Qwen/Qwen3-VL-2B-Instruct -a 0.0.0.0 -p 8080
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **serv 子命令必须指定 `--weight-path`**:由于 `serv` 子命令不下载模型,必须通过 `--weight-path` 指定已下载的模型路径。
|
||||
1. **GGUF/ONNX 仅支持本地路径**:请使用 `--gguf-path` 或 `--onnx-path`。自动下载管理仅适用于 safetensors 模型。
|
||||
|
||||
2. **下载重试机制**:默认重试 3 次,每次失败后等待 2 秒再重试。可通过 `--download-retries` 调整重试次数。
|
||||
2. **制品格式选择**:`--artifact-format auto` 使用模型默认格式,也可显式指定 `safetensors|gguf|onnx`。
|
||||
|
||||
3. **默认保存目录**:模型默认保存到 `~/.aha/` 目录下,可通过 `--save-dir` 或 `-s` 参数自定义。
|
||||
3. **tokenizer 目录**:GGUF/ONNX 若未与 tokenizer/config 同目录,请额外指定 `--tokenizer-dir`。
|
||||
|
||||
4. **端口占用**:启动服务前确保指定的端口未被占用,默认端口为 10100。
|
||||
4. **下载重试机制**:默认重试 3 次,每次失败后等待 2 秒再重试。可通过 `--download-retries` 调整重试次数。
|
||||
|
||||
5. **权限问题**:如果保存到系统目录(如 `/data/models`),确保有相应的写入权限。
|
||||
5. **默认保存目录**:模型默认保存到 `~/.aha/` 目录下,可通过 `--save-dir` 或 `-s` 参数自定义。
|
||||
|
||||
6. **端口占用**:启动服务前确保指定的端口未被占用,默认端口为 10100。
|
||||
|
||||
7. **权限问题**:如果保存到系统目录(如 `/data/models`),确保有相应的写入权限。
|
||||
|
||||
## 获取帮助
|
||||
|
||||
|
||||
@@ -51,6 +51,23 @@ ZhipuAI/GLM-OCR ZhipuAI ocr ✔
|
||||
| **LFM2.5-1.2B-Instruct** | 1.2B | LiquidAI/LFM2.5-1.2B-Instruct | [lfm1.0](https://huggingface.co/LiquidAI/LFM2.5-1.2B-Instruct/blob/main/LICENSE) |
|
||||
|
||||
|
||||
## Embedding
|
||||
|
||||
| Model | Parameters | Description | License |
|
||||
|-------|-----------|-------------|---------|
|
||||
| **Qwen3-Embedding-0.6B** | 0.6B | Text embedding (safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Embedding-4B** | 4B | Text embedding (safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Embedding-8B** | 8B | Text embedding (safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **all-MiniLM-L6-v2** | 22M | Sentence-transformers embedding (safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/blob/main/LICENSE) |
|
||||
|
||||
## Reranker
|
||||
|
||||
| Model | Parameters | Description | License |
|
||||
|-------|-----------|-------------|---------|
|
||||
| **Qwen3-Reranker-0.6B** | 0.6B | Text reranking (embedding-similarity baseline, safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Reranker-4B** | 4B | Text reranking (embedding-similarity baseline, safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Reranker-8B** | 8B | Text reranking (embedding-similarity baseline, safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
|
||||
## Vision & Multimodal
|
||||
|
||||
| Model | Parameters | Model Id | License |
|
||||
@@ -71,6 +88,8 @@ ZhipuAI/GLM-OCR ZhipuAI ocr ✔
|
||||
| **GLM-OCR** | 8 | ZhipuAI/GLM-OCR | [MIT](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/mit.md) |
|
||||
|
||||
|
||||
GLM-OCR local artifacts: `safetensors`, `gguf`, `onnx`
|
||||
|
||||
## Speech Recognition (ASR)
|
||||
|
||||
| Model | Parameters | Language | Model Id | License |
|
||||
@@ -98,6 +117,57 @@ Models are sourced from:
|
||||
- [Hugging Face](https://huggingface.co) - Primary model hub
|
||||
- [ModelScope](https://modelscope.cn) - Chinese model hub
|
||||
|
||||
## Registered Repositories (Not Runtime-Integrated Yet)
|
||||
|
||||
The following repositories are now cataloged for future integration, but are **not** directly runnable in current `aha` runtime yet:
|
||||
|
||||
### MLX / Format-Specific Variants
|
||||
- Jackrong/MLX-Qwen3.5-27B-Claude-4.6-Opus-Reasoning-Distilled-v2-4bit
|
||||
- Jackrong/MLX-Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-v2-4bit
|
||||
- Jackrong/MLX-Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-v2-6bit
|
||||
- Jackrong/MLX-Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-v2-8bit
|
||||
- Jackrong/MLX-Qwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-v2-4bit
|
||||
- Jackrong/MLX-Qwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-v2-6bit
|
||||
- Jackrong/MLX-Qwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-v2-8bit
|
||||
|
||||
### Embedding Models
|
||||
- google/embeddinggemma-300m
|
||||
- ggml-org/embeddinggemma-300M-GGUF
|
||||
- onnx-community/embeddinggemma-300m-ONNX
|
||||
- unsloth/embeddinggemma-300m-GGUF
|
||||
- onnx-community/Qwen3-Embedding-0.6B-ONNX
|
||||
- Qwen/Qwen3-Embedding-0.6B-GGUF
|
||||
- onnx-community/Qwen3-Embedding-4B-ONNX
|
||||
- Qwen/Qwen3-Embedding-4B-GGUF
|
||||
- Qwen/Qwen3-Embedding-8B-GGUF
|
||||
- onnx-community/Qwen3-Embedding-8B-ONNX
|
||||
- perplexity-ai/pplx-embed-v1-0.6b
|
||||
- nomic-ai/nomic-embed-text-v2-moe
|
||||
- nomic-ai/nomic-embed-text-v2-moe-GGUF
|
||||
- jinaai/jina-embeddings-v5-text-small
|
||||
- jinaai/jina-embeddings-v5-text-nano
|
||||
- jinaai/jina-embeddings-v5-text-small-text-matching
|
||||
- jinaai/jina-embeddings-v5-text-small-text-matching-GGUF
|
||||
- sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
|
||||
|
||||
### Reranker Models
|
||||
- BAAI/bge-reranker-v2-m3
|
||||
- ggml-org/Qwen3-Reranker-0.6B-Q8_0-GGUF
|
||||
|
||||
### ONNX Repositories
|
||||
- onnx-community/GLM-OCR-ONNX
|
||||
- onnx-community/Qwen3-Reranker-0.6B-ONNX
|
||||
- onnx-community/Qwen3.5-2B-ONNX
|
||||
- onnx-community/Qwen3.5-4B-ONNX
|
||||
- onnx-community/Qwen3.5-0.8B-ONNX
|
||||
- onnx-community/Qwen3-VL-2B-Instruct-ONNX
|
||||
- onnx-community/ONNX_Qwen3-Embedding-0.6B
|
||||
- onnx-community/Nanbeige4.1-3B-ONNX
|
||||
- onnx-community/Qwen3-Embedding-8B-ONNX
|
||||
- onnx-community/Qwen3-Embedding-4B-ONNX
|
||||
- onnx-community/bge-reranker-v2-m3-ONNX
|
||||
- onnx-community/all-MiniLM-L6-v2-ONNX
|
||||
|
||||
## Adding New Models
|
||||
|
||||
See [Development Guide](./development.md) for instructions on adding new model integrations.
|
||||
|
||||
@@ -50,6 +50,23 @@ ZhipuAI/GLM-OCR ZhipuAI ocr ✔
|
||||
| **LFM2-1.2B** | 1.2B | LiquidAI/LFM2-1.2B | [lfm1.0](https://huggingface.co/LiquidAI/LFM2-1.2B/blob/main/LICENSE) |
|
||||
| **LFM2.5-1.2B-Instruct** | 1.2B | LiquidAI/LFM2.5-1.2B-Instruct | [lfm1.0](https://huggingface.co/LiquidAI/LFM2.5-1.2B-Instruct/blob/main/LICENSE) |
|
||||
|
||||
## Embedding
|
||||
|
||||
| 模型 | 参数量 | 描述 | 开源协议 |
|
||||
|------|--------|------|---------|
|
||||
| **Qwen3-Embedding-0.6B** | 0.6B | 文本向量(safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Embedding-4B** | 4B | 文本向量(safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Embedding-8B** | 8B | 文本向量(safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **all-MiniLM-L6-v2** | 22M | sentence-transformers 文本向量(safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/blob/main/LICENSE) |
|
||||
|
||||
## Reranker
|
||||
|
||||
| 模型 | 参数量 | 描述 | 开源协议 |
|
||||
|------|--------|------|---------|
|
||||
| **Qwen3-Reranker-0.6B** | 0.6B | 文本重排(embedding-similarity 基线,safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Reranker-4B** | 4B | 文本重排(embedding-similarity 基线,safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
| **Qwen3-Reranker-8B** | 8B | 文本重排(embedding-similarity 基线,safetensors / gguf / onnx) | [Apache 2.0](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md) |
|
||||
|
||||
## 视觉与多模态
|
||||
|
||||
| 模型 | 参数量 | 模型id | 开源协议 |
|
||||
@@ -69,6 +86,8 @@ ZhipuAI/GLM-OCR ZhipuAI ocr ✔
|
||||
| **DeepSeek-OCR** | 多语言 | deepseek-ai/DeepSeek-OCR <br> deepseek-ai/DeepSeek-OCR-2 | [MIT](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/mit.md) |
|
||||
| **GLM-OCR** | 8 | ZhipuAI/GLM-OCR | [MIT](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/mit.md) |
|
||||
|
||||
GLM-OCR 本地制品格式:`safetensors`、`gguf`、`onnx`
|
||||
|
||||
## 语音识别 (ASR)
|
||||
|
||||
| 模型 | 参数量 | 语言 | 模型id | 开源协议 |
|
||||
@@ -97,6 +116,57 @@ ZhipuAI/GLM-OCR ZhipuAI ocr ✔
|
||||
- [Hugging Face](https://huggingface.co) - 主模型中心
|
||||
- [ModelScope](https://modelscope.cn) - 中文模型中心
|
||||
|
||||
## 已收录仓库(当前运行时暂未直接接入)
|
||||
|
||||
以下仓库已纳入项目模型目录,但当前 `aha` 运行时尚不能直接推理:
|
||||
|
||||
### MLX / 特定格式变体
|
||||
- Jackrong/MLX-Qwen3.5-27B-Claude-4.6-Opus-Reasoning-Distilled-v2-4bit
|
||||
- Jackrong/MLX-Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-v2-4bit
|
||||
- Jackrong/MLX-Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-v2-6bit
|
||||
- Jackrong/MLX-Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-v2-8bit
|
||||
- Jackrong/MLX-Qwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-v2-4bit
|
||||
- Jackrong/MLX-Qwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-v2-6bit
|
||||
- Jackrong/MLX-Qwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-v2-8bit
|
||||
|
||||
### Embedding 模型
|
||||
- google/embeddinggemma-300m
|
||||
- ggml-org/embeddinggemma-300M-GGUF
|
||||
- onnx-community/embeddinggemma-300m-ONNX
|
||||
- unsloth/embeddinggemma-300m-GGUF
|
||||
- onnx-community/Qwen3-Embedding-0.6B-ONNX
|
||||
- Qwen/Qwen3-Embedding-0.6B-GGUF
|
||||
- onnx-community/Qwen3-Embedding-4B-ONNX
|
||||
- Qwen/Qwen3-Embedding-4B-GGUF
|
||||
- Qwen/Qwen3-Embedding-8B-GGUF
|
||||
- onnx-community/Qwen3-Embedding-8B-ONNX
|
||||
- perplexity-ai/pplx-embed-v1-0.6b
|
||||
- nomic-ai/nomic-embed-text-v2-moe
|
||||
- nomic-ai/nomic-embed-text-v2-moe-GGUF
|
||||
- jinaai/jina-embeddings-v5-text-small
|
||||
- jinaai/jina-embeddings-v5-text-nano
|
||||
- jinaai/jina-embeddings-v5-text-small-text-matching
|
||||
- jinaai/jina-embeddings-v5-text-small-text-matching-GGUF
|
||||
- sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
|
||||
|
||||
### Reranker 模型
|
||||
- BAAI/bge-reranker-v2-m3
|
||||
- ggml-org/Qwen3-Reranker-0.6B-Q8_0-GGUF
|
||||
|
||||
### ONNX 仓库
|
||||
- onnx-community/GLM-OCR-ONNX
|
||||
- onnx-community/Qwen3-Reranker-0.6B-ONNX
|
||||
- onnx-community/Qwen3.5-2B-ONNX
|
||||
- onnx-community/Qwen3.5-4B-ONNX
|
||||
- onnx-community/Qwen3.5-0.8B-ONNX
|
||||
- onnx-community/Qwen3-VL-2B-Instruct-ONNX
|
||||
- onnx-community/ONNX_Qwen3-Embedding-0.6B
|
||||
- onnx-community/Nanbeige4.1-3B-ONNX
|
||||
- onnx-community/Qwen3-Embedding-8B-ONNX
|
||||
- onnx-community/Qwen3-Embedding-4B-ONNX
|
||||
- onnx-community/bge-reranker-v2-m3-ONNX
|
||||
- onnx-community/all-MiniLM-L6-v2-ONNX
|
||||
|
||||
## 添加新模型
|
||||
|
||||
参见 [开发指南](./development.zh-CN.md) 了解添加新模型集成的说明。
|
||||
|
||||
@@ -27,7 +27,9 @@ impl ExecModel for FunASRNanoExec {
|
||||
println!("Time elapsed in load model is: {:?}", i_duration);
|
||||
|
||||
// Create ChatCompletionParameters for ASR
|
||||
let url = &input[1];
|
||||
let url = input.get(1).ok_or_else(|| {
|
||||
anyhow::anyhow!("fun-asr-nano requires a second input: audio path or URL")
|
||||
})?;
|
||||
let input_url = if url.starts_with("http://")
|
||||
|| url.starts_with("https://")
|
||||
|| url.starts_with("file://")
|
||||
|
||||
@@ -28,7 +28,9 @@ impl ExecModel for GlmASRNanoExec {
|
||||
|
||||
// Create ChatCompletionParameters for ASR
|
||||
// Input should be an audio file path
|
||||
let url = &input[1];
|
||||
let url = input.get(1).ok_or_else(|| {
|
||||
anyhow::anyhow!("glm-asr-nano requires a second input: audio path or URL")
|
||||
})?;
|
||||
let input_url = if url.starts_with("http://")
|
||||
|| url.starts_with("https://")
|
||||
|| url.starts_with("file://")
|
||||
|
||||
@@ -19,7 +19,9 @@ impl ExecModel for Qwen2_5VLExec {
|
||||
} else {
|
||||
input_text.clone()
|
||||
};
|
||||
let url = &input[1];
|
||||
let url = input.get(1).ok_or_else(|| {
|
||||
anyhow::anyhow!("qwen2.5vl requires a second input: image path or URL")
|
||||
})?;
|
||||
let input_url = if url.starts_with("http://")
|
||||
|| url.starts_with("https://")
|
||||
|| url.starts_with("file://")
|
||||
|
||||
+10
-7
@@ -98,14 +98,16 @@ impl<'a> GenerateModel for ModelInstance<'a> {
|
||||
ModelInstance::Lfm2VL(model) => model.generate(mes),
|
||||
ModelInstance::Qwen2_5VL(model) => model.generate(mes),
|
||||
ModelInstance::Qwen3(model) => model.generate(mes),
|
||||
ModelInstance::Qwen3_5(model) => model.generate(mes),
|
||||
|
||||
ModelInstance::Qwen3Embedding(_) => {
|
||||
Err(anyhow!("embedding model does not support chat completions"))
|
||||
}
|
||||
ModelInstance::Qwen3ASR(model) => model.generate(mes),
|
||||
ModelInstance::Qwen3Reranker(_) => {
|
||||
Err(anyhow!("reranker model does not support chat completions"))
|
||||
}
|
||||
ModelInstance::Qwen3_5(model) => model.generate(mes),
|
||||
|
||||
ModelInstance::Qwen3ASR(model) => model.generate(mes),
|
||||
ModelInstance::Qwen3VL(model) => model.generate(mes),
|
||||
ModelInstance::DeepSeekOCR(model) => model.generate(mes),
|
||||
ModelInstance::HunyuanOCR(model) => model.generate(mes),
|
||||
@@ -138,14 +140,15 @@ impl<'a> GenerateModel for ModelInstance<'a> {
|
||||
ModelInstance::Lfm2VL(model) => model.generate_stream(mes),
|
||||
ModelInstance::Qwen2_5VL(model) => model.generate_stream(mes),
|
||||
ModelInstance::Qwen3(model) => model.generate_stream(mes),
|
||||
ModelInstance::Qwen3_5(model) => model.generate_stream(mes),
|
||||
ModelInstance::Qwen3ASR(model) => model.generate_stream(mes),
|
||||
|
||||
ModelInstance::Qwen3Embedding(_) => Err(anyhow!(
|
||||
"embedding model does not support streaming chat completions"
|
||||
)),
|
||||
ModelInstance::Qwen3Reranker(_) => {
|
||||
Err(anyhow!("reranker model does not support chat completions"))
|
||||
}
|
||||
ModelInstance::Qwen3Reranker(_) => Err(anyhow!(
|
||||
"reranker model does not support streaming chat completions"
|
||||
)),
|
||||
ModelInstance::Qwen3_5(model) => model.generate_stream(mes),
|
||||
ModelInstance::Qwen3ASR(model) => model.generate_stream(mes),
|
||||
ModelInstance::Qwen3VL(model) => model.generate_stream(mes),
|
||||
ModelInstance::DeepSeekOCR(model) => model.generate_stream(mes),
|
||||
ModelInstance::HunyuanOCR(model) => model.generate_stream(mes),
|
||||
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"v":1}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "dacdea35f18b551cb23bd3bf6ab58363784a94f4"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
name: build
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ${{matrix.os}}
|
||||
strategy:
|
||||
fail-fast: true
|
||||
matrix:
|
||||
os: [macos-latest, ubuntu-latest, windows-latest]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Check formatting
|
||||
run: |
|
||||
rustup component add rustfmt
|
||||
rustup component add clippy
|
||||
cargo fmt -- --check
|
||||
cargo clippy --all-features --verbose -- -Dwarnings
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --all-features
|
||||
+678
@@ -0,0 +1,678 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anes"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"ciborium-ll",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-io"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-ll"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"half",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
|
||||
|
||||
[[package]]
|
||||
name = "criterion"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
|
||||
dependencies = [
|
||||
"anes",
|
||||
"cast",
|
||||
"ciborium",
|
||||
"clap",
|
||||
"criterion-plot",
|
||||
"is-terminal",
|
||||
"itertools",
|
||||
"num-traits",
|
||||
"once_cell",
|
||||
"oorandom",
|
||||
"plotters",
|
||||
"rayon",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
"tinytemplate",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "criterion-plot"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
|
||||
dependencies = [
|
||||
"cast",
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
"memoffset",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480"
|
||||
dependencies = [
|
||||
"errno-dragonfly",
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno-dragonfly"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "esaxx-rs"
|
||||
version = "0.1.10"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"criterion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "1.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"rustix",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.148"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
||||
|
||||
[[package]]
|
||||
name = "oorandom"
|
||||
version = "11.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
|
||||
|
||||
[[package]]
|
||||
name = "plotters"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"plotters-backend",
|
||||
"plotters-svg",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plotters-backend"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609"
|
||||
|
||||
[[package]]
|
||||
name = "plotters-svg"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab"
|
||||
dependencies = [
|
||||
"plotters-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.67"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
|
||||
dependencies = [
|
||||
"either",
|
||||
"rayon-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon-core"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
|
||||
dependencies = [
|
||||
"crossbeam-deque",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.9.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f25469e9ae0f3d0047ca8b93fc56843f38e6774f0914a107ff8b41be8be8e0b7"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinytemplate"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.48.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2018"
|
||||
name = "esaxx-rs"
|
||||
version = "0.1.10"
|
||||
authors = ["Nicolas Patry <patry.nicolas@protonmail.com>"]
|
||||
description = "Wrapping around sentencepiece's esaxxx library."
|
||||
homepage = "https://github.com/Narsil/esaxx-rs"
|
||||
documentation = "https://docs.rs/esaxx-rs"
|
||||
readme = "README.md"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/Narsil/esaxx-rs"
|
||||
|
||||
[[example]]
|
||||
name = "esaxx"
|
||||
required-features = ["cpp"]
|
||||
|
||||
[[bench]]
|
||||
name = "bench_suffix"
|
||||
harness = false
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies.criterion]
|
||||
version = "0.5"
|
||||
|
||||
[build-dependencies.cc]
|
||||
version = "1.0"
|
||||
optional = true
|
||||
|
||||
[features]
|
||||
cpp = ["cc"]
|
||||
default = ["cpp"]
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
[package]
|
||||
name = "esaxx-rs"
|
||||
version = "0.1.10"
|
||||
authors = ["Nicolas Patry <patry.nicolas@protonmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Wrapping around sentencepiece's esaxxx library."
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://github.com/Narsil/esaxx-rs"
|
||||
documentation = "https://docs.rs/esaxx-rs"
|
||||
repository = "https://github.com/Narsil/esaxx-rs"
|
||||
readme = "README.md"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
[build-dependencies]
|
||||
cc = { version = "1.0", optional = true}
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.5"
|
||||
|
||||
[features]
|
||||
default = ["cpp"]
|
||||
cpp = ["cc"]
|
||||
|
||||
[[bench]]
|
||||
name = "bench_suffix"
|
||||
harness = false
|
||||
|
||||
[[example]]
|
||||
name = "esaxx"
|
||||
required-features = ["cpp"]
|
||||
Vendored
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||

|
||||
|
||||
# esaxx-rs
|
||||
|
||||
This code implements a fast suffix tree / suffix array.
|
||||
|
||||
This code is taken from 
|
||||
and to be used by .
|
||||
|
||||
|
||||
Small wrapper around sentencepiece's esaxx suffix array C++ library.
|
||||
Usage
|
||||
|
||||
```rust
|
||||
let string = "abracadabra";
|
||||
let suffix = esaxx_rs::suffix(string).unwrap();
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let mut iter = suffix.iter();
|
||||
assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
|
||||
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
assert_eq!(iter.next(), None);
|
||||
```
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||

|
||||
|
||||
# {{crate}}
|
||||
|
||||
This code implements a fast suffix tree / suffix array.
|
||||
|
||||
This code is taken from 
|
||||
and to be used by .
|
||||
|
||||
|
||||
{{readme}}
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
use esaxx_rs::{suffix, suffix_rs};
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.".to_string();
|
||||
|
||||
c.bench_function("suffix_cpp_short", |b| {
|
||||
b.iter(|| suffix(black_box(&string)).unwrap())
|
||||
});
|
||||
c.bench_function("suffix_rust_short", |b| {
|
||||
b.iter(|| suffix_rs(black_box(&string)).unwrap())
|
||||
});
|
||||
|
||||
let string = std::fs::read_to_string("data/eighty.txt").unwrap();
|
||||
c.bench_function("suffix_cpp_long", |b| {
|
||||
b.iter(|| suffix(black_box(&string)).unwrap())
|
||||
});
|
||||
c.bench_function("suffix_rust_long", |b| {
|
||||
b.iter(|| suffix_rs(black_box(&string)).unwrap())
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, criterion_benchmark);
|
||||
criterion_main!(benches);
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
#[cfg(feature = "cpp")]
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
fn main() {
|
||||
let mut build = cc::Build::new();
|
||||
build.cpp(true).flag("-std=c++11");
|
||||
|
||||
if std::env::var("CARGO_CFG_TARGET_FEATURE")
|
||||
.unwrap_or_default()
|
||||
.contains("crt-static")
|
||||
{
|
||||
build.static_crt(true);
|
||||
}
|
||||
|
||||
build.file("src/esaxx.cpp").include("src").compile("esaxx");
|
||||
}
|
||||
|
||||
#[cfg(feature = "cpp")]
|
||||
#[cfg(target_os = "macos")]
|
||||
fn main() {
|
||||
let mut build = cc::Build::new();
|
||||
build.cpp(true).flag("-std=c++11").flag("-stdlib=libc++");
|
||||
|
||||
if std::env::var("CARGO_CFG_TARGET_FEATURE")
|
||||
.unwrap_or_default()
|
||||
.contains("crt-static")
|
||||
{
|
||||
build.static_crt(true);
|
||||
}
|
||||
|
||||
build.file("src/esaxx.cpp").include("src").compile("esaxx");
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cpp"))]
|
||||
fn main() {}
|
||||
Vendored
+8406
File diff suppressed because it is too large
Load Diff
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
use esaxx_rs::{suffix, suffix_rs};
|
||||
use std::env::args;
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
// Prints each argument on a separate line
|
||||
let args: Vec<_> = args().skip(1).collect();
|
||||
let version = &args[0];
|
||||
let filename = &args[1];
|
||||
|
||||
let string = fs::read_to_string(filename).unwrap();
|
||||
let (count, version) = if version == "rust" {
|
||||
(suffix_rs(&string).unwrap().iter().count(), "Rust")
|
||||
} else {
|
||||
(suffix(&string).unwrap().iter().count(), "Cpp")
|
||||
};
|
||||
println!("Used {} version ! Found {} nodes", version, count);
|
||||
}
|
||||
Vendored
+126
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* esa.hxx
|
||||
* Copyright (c) 2010 Daisuke Okanohara All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _ESA_HXX
|
||||
#define _ESA_HXX
|
||||
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include "sais.hxx"
|
||||
|
||||
namespace esaxx_private {
|
||||
template<typename string_type, typename sarray_type, typename index_type>
|
||||
index_type suffixtree(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D, index_type n){
|
||||
if (n == 0){
|
||||
return 0;
|
||||
}
|
||||
sarray_type Psi = L;
|
||||
Psi[SA[0]] = SA[n-1];
|
||||
for (index_type i = 1; i < n; ++i){
|
||||
Psi[SA[i]] = SA[i-1];
|
||||
}
|
||||
|
||||
// Compare at most 2n log n charcters. Practically fastest
|
||||
// "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
sarray_type PLCP = R;
|
||||
index_type h = 0;
|
||||
for (index_type i = 0; i < n; ++i){
|
||||
index_type j = Psi[i];
|
||||
while (i+h < n && j+h < n &&
|
||||
T[i+h] == T[j+h]){
|
||||
++h;
|
||||
}
|
||||
PLCP[i] = h;
|
||||
if (h > 0) --h;
|
||||
}
|
||||
|
||||
sarray_type H = L;
|
||||
for (index_type i = 0; i < n; ++i){
|
||||
H[i] = PLCP[SA[i]];
|
||||
}
|
||||
H[0] = -1;
|
||||
|
||||
std::vector<std::pair<index_type, index_type> > S;
|
||||
S.push_back(std::make_pair((index_type)-1, (index_type)-1));
|
||||
size_t nodeNum = 0;
|
||||
for (index_type i = 0; ; ++i){
|
||||
std::pair<index_type, index_type> cur (i, (i == n) ? -1 : H[i]);
|
||||
std::pair<index_type, index_type> cand(S.back());
|
||||
while (cand.second > cur.second){
|
||||
if (i - cand.first > 1){
|
||||
L[nodeNum] = cand.first;
|
||||
R[nodeNum] = i;
|
||||
D[nodeNum] = cand.second;
|
||||
++nodeNum;
|
||||
}
|
||||
cur.first = cand.first;
|
||||
S.pop_back();
|
||||
cand = S.back();
|
||||
}
|
||||
if (cand.second < cur.second){
|
||||
S.push_back(cur);
|
||||
}
|
||||
if (i == n) break;
|
||||
S.push_back(std::make_pair(i, n - SA[i] + 1));
|
||||
}
|
||||
return nodeNum;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Build an enhanced suffix array of a given string in linear time
|
||||
* For an input text T, esaxx() builds an enhancd suffix array in linear time.
|
||||
* i-th internal node is represented as a triple (L[i], R[i], D[i]);
|
||||
* L[i] and R[i] is the left/right boundary of the suffix array as SA[L[i]....R[i]-1]
|
||||
* D[i] is the depth of the internal node
|
||||
* The number of internal node is at most N-1 and return the actual number by
|
||||
* @param T[0...n-1] The input string. (random access iterator)
|
||||
* @param SA[0...n-1] The output suffix array (random access iterator)
|
||||
* @param L[0...n-1] The output left boundary of internal node (random access iterator)
|
||||
* @param R[0...n-1] The output right boundary of internal node (random access iterator)
|
||||
* @param D[0...n-1] The output depth of internal node (random access iterator)
|
||||
* @param n The length of the input string
|
||||
* @param k The alphabet size
|
||||
* @pram nodeNum The output the number of internal node
|
||||
* @return 0 if succeded, -1 or -2 otherwise
|
||||
*/
|
||||
|
||||
template<typename string_type, typename sarray_type, typename index_type>
|
||||
int esaxx(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D,
|
||||
index_type n, index_type k, index_type& nodeNum) {
|
||||
if ((n < 0) || (k <= 0)) return -1;
|
||||
int err = saisxx(T, SA, n, k);
|
||||
if (err != 0){
|
||||
return err;
|
||||
}
|
||||
nodeNum = esaxx_private::suffixtree(T, SA, L, R, D, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif // _ESA_HXX
|
||||
|
||||
Vendored
+89
@@ -0,0 +1,89 @@
|
||||
use crate::sais::saisxx;
|
||||
use crate::types::{SArray, StringT, SuffixError};
|
||||
use std::convert::TryInto;
|
||||
|
||||
fn suffixtree(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
left: &mut SArray,
|
||||
right: &mut SArray,
|
||||
depth: &mut SArray,
|
||||
n: usize,
|
||||
) -> usize {
|
||||
if n == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Psi = l
|
||||
left[suffix_array[0]] = suffix_array[n - 1];
|
||||
for i in 1..n {
|
||||
left[suffix_array[i]] = suffix_array[i - 1];
|
||||
}
|
||||
// Compare at most 2n log n charcters. Practically fastest
|
||||
// "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
// PLCP = r
|
||||
let mut h = 0;
|
||||
for i in 0..n {
|
||||
let j = left[i];
|
||||
while i + h < n && j + h < n && string[i + h] == string[j + h] {
|
||||
h += 1;
|
||||
}
|
||||
right[i] = h;
|
||||
h = h.saturating_sub(1);
|
||||
}
|
||||
|
||||
// H = l
|
||||
for i in 0..n {
|
||||
left[i] = right[suffix_array[i]];
|
||||
}
|
||||
// TODO XXX: i32 necessary
|
||||
// l[0] = -1;
|
||||
|
||||
let mut s: Vec<(i32, i32)> = vec![(-1, -1)];
|
||||
let mut node_num = 0;
|
||||
let mut i: usize = 0;
|
||||
loop {
|
||||
let mut cur: (i32, i32) = (i as i32, if i == n { -1 } else { left[i] as i32 });
|
||||
let mut cand = s[s.len() - 1];
|
||||
while cand.1 > cur.1 {
|
||||
if (i as i32) - cand.0 > 1 {
|
||||
left[node_num] = cand.0.try_into().unwrap();
|
||||
right[node_num] = i;
|
||||
depth[node_num] = cand.1.try_into().unwrap();
|
||||
node_num += 1;
|
||||
if node_num >= n {
|
||||
break;
|
||||
}
|
||||
}
|
||||
cur.0 = cand.0;
|
||||
s.pop();
|
||||
cand = s[s.len() - 1];
|
||||
}
|
||||
if cand.1 < cur.1 {
|
||||
s.push(cur);
|
||||
}
|
||||
if i == n {
|
||||
break;
|
||||
}
|
||||
s.push((
|
||||
i.try_into().unwrap(),
|
||||
(n - suffix_array[i] + 1).try_into().unwrap(),
|
||||
));
|
||||
i += 1;
|
||||
}
|
||||
node_num
|
||||
}
|
||||
|
||||
pub(crate) fn esaxx_rs(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
left: &mut SArray,
|
||||
right: &mut SArray,
|
||||
depth: &mut SArray,
|
||||
k: usize,
|
||||
) -> Result<usize, SuffixError> {
|
||||
let n = string.len();
|
||||
saisxx(string, suffix_array, n, k)?;
|
||||
let node_num = suffixtree(string, suffix_array, left, right, depth, n);
|
||||
Ok(node_num)
|
||||
}
|
||||
Vendored
+633
@@ -0,0 +1,633 @@
|
||||
//
|
||||
// /*
|
||||
// * sais.hxx for sais-lite
|
||||
// * Copyright (c) 2008-2009 Yuta Mori All Rights Reserved.
|
||||
// *
|
||||
// * Permission is hereby granted, free of charge, to any person
|
||||
// * obtaining a copy of this software and associated documentation
|
||||
// * files (the "Software"), to deal in the Software without
|
||||
// * restriction, including without limitation the rights to use,
|
||||
// * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// * copies of the Software, and to permit persons to whom the
|
||||
// * Software is furnished to do so, subject to the following
|
||||
// * conditions:
|
||||
// *
|
||||
// * The above copyright notice and this permission notice shall be
|
||||
// * included in all copies or substantial portions of the Software.
|
||||
// *
|
||||
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// * OTHER DEALINGS IN THE SOFTWARE.
|
||||
// */
|
||||
//
|
||||
// #ifndef _SAIS_HXX
|
||||
// #define _SAIS_HXX 1
|
||||
// #ifdef __cplusplus
|
||||
//
|
||||
// #ifdef __INTEL_COMPILER
|
||||
// #pragma warning(disable : 383 981 1418)
|
||||
// // for icc 64-bit
|
||||
// //#define __builtin_vsnprintf(a, b, c, d) __builtin_vsnprintf(a, b, c, (char *)d)
|
||||
// #endif
|
||||
//
|
||||
// #include <iterator>
|
||||
// #ifdef _OPENMP
|
||||
// # include <omp.h>
|
||||
// #endif
|
||||
//
|
||||
// namespace saisxx_private {
|
||||
//
|
||||
// /* find the start or end of each bucket */
|
||||
// template<typename string_type, typename bucket_type, typename index_type>
|
||||
// void
|
||||
// getCounts(const string_type T, bucket_type C, index_type n, index_type k) {
|
||||
// #ifdef _OPENMP
|
||||
// bucket_type D;
|
||||
// index_type i, j, p, sum, first, last;
|
||||
// int thnum, maxthreads = omp_get_max_threads();
|
||||
// #pragma omp parallel default(shared) private(D, i, thnum, first, last)
|
||||
// {
|
||||
// thnum = omp_get_thread_num();
|
||||
// D = C + thnum * k;
|
||||
// first = n / maxthreads * thnum;
|
||||
// last = (thnum < (maxthreads - 1)) ? n / maxthreads * (thnum + 1) : n;
|
||||
// for(i = 0; i < k; ++i) { D[i] = 0; }
|
||||
// for(i = first; i < last; ++i) { ++D[T[i]]; }
|
||||
// }
|
||||
// if(1 < maxthreads) {
|
||||
// #pragma omp parallel for default(shared) private(i, j, p, sum)
|
||||
// for(i = 0; i < k; ++i) {
|
||||
// for(j = 1, p = i + k, sum = C[i]; j < maxthreads; ++j, p += k) {
|
||||
// sum += C[p];
|
||||
// }
|
||||
// C[i] = sum;
|
||||
// }
|
||||
// }
|
||||
// #else
|
||||
// index_type i;
|
||||
// for(i = 0; i < k; ++i) { C[i] = 0; }
|
||||
// for(i = 0; i < n; ++i) { ++C[T[i]]; }
|
||||
// #endif
|
||||
// }
|
||||
// template<typename bucket_type, typename index_type>
|
||||
// void
|
||||
// getBuckets(const bucket_type C, bucket_type B, index_type k, bool end) {
|
||||
// index_type i, sum = 0;
|
||||
// if(end) { for(i = 0; i < k; ++i) { sum += C[i]; B[i] = sum; } }
|
||||
// else { for(i = 0; i < k; ++i) { sum += C[i]; B[i] = sum - C[i]; } }
|
||||
// }
|
||||
//
|
||||
// /* compute SA and BWT */
|
||||
// template<typename string_type, typename sarray_type,
|
||||
// typename bucket_type, typename index_type>
|
||||
// void
|
||||
// induceSA(string_type T, sarray_type SA, bucket_type C, bucket_type B,
|
||||
// index_type n, index_type k) {
|
||||
// typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
// sarray_type b;
|
||||
// index_type i, j;
|
||||
// char_type c0, c1;
|
||||
// /* compute SAl */
|
||||
// if(C == B) { getCounts(T, C, n, k); }
|
||||
// getBuckets(C, B, k, false); /* find starts of buckets */
|
||||
// b = SA + B[c1 = T[j = n - 1]];
|
||||
// *b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
// for(i = 0; i < n; ++i) {
|
||||
// j = SA[i], SA[i] = ~j;
|
||||
// if(0 < j) {
|
||||
// if((c0 = T[--j]) != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
// *b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
// }
|
||||
// }
|
||||
// /* compute SAs */
|
||||
// if(C == B) { getCounts(T, C, n, k); }
|
||||
// getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
// for(i = n - 1, b = SA + B[c1 = 0]; 0 <= i; --i) {
|
||||
// if(0 < (j = SA[i])) {
|
||||
// if((c0 = T[--j]) != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
// *--b = ((j == 0) || (T[j - 1] > c1)) ? ~j : j;
|
||||
// } else {
|
||||
// SA[i] = ~j;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// template<typename string_type, typename sarray_type,
|
||||
// typename bucket_type, typename index_type>
|
||||
// int
|
||||
// computeBWT(string_type T, sarray_type SA, bucket_type C, bucket_type B,
|
||||
// index_type n, index_type k) {
|
||||
// typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
// sarray_type b;
|
||||
// index_type i, j, pidx = -1;
|
||||
// char_type c0, c1;
|
||||
// /* compute SAl */
|
||||
// if(C == B) { getCounts(T, C, n, k); }
|
||||
// getBuckets(C, B, k, false); /* find starts of buckets */
|
||||
// b = SA + B[c1 = T[j = n - 1]];
|
||||
// *b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
// for(i = 0; i < n; ++i) {
|
||||
// if(0 < (j = SA[i])) {
|
||||
// SA[i] = ~(c0 = T[--j]);
|
||||
// if(c0 != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
// *b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
// } else if(j != 0) {
|
||||
// SA[i] = ~j;
|
||||
// }
|
||||
// }
|
||||
// /* compute SAs */
|
||||
// if(C == B) { getCounts(T, C, n, k); }
|
||||
// getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
// for(i = n - 1, b = SA + B[c1 = 0]; 0 <= i; --i) {
|
||||
// if(0 < (j = SA[i])) {
|
||||
// SA[i] = (c0 = T[--j]);
|
||||
// if(c0 != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
// *--b = ((0 < j) && (T[j - 1] > c1)) ? ~((index_type)T[j - 1]) : j;
|
||||
// } else if(j != 0) {
|
||||
// SA[i] = ~j;
|
||||
// } else {
|
||||
// pidx = i;
|
||||
// }
|
||||
// }
|
||||
// return pidx;
|
||||
// }
|
||||
//
|
||||
// /* find the suffix array SA of T[0..n-1] in {0..k}^n
|
||||
// use a working space (excluding s and SA) of at most 2n+O(1) for a constant alphabet */
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// int
|
||||
// suffixsort(string_type T, sarray_type SA,
|
||||
// index_type fs, index_type n, index_type k,
|
||||
// bool isbwt) {
|
||||
// typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
// sarray_type RA;
|
||||
// index_type i, j, m, p, q, plen, qlen, name, pidx = 0;
|
||||
// bool diff;
|
||||
// int c;
|
||||
// #ifdef _OPENMP
|
||||
// int maxthreads = omp_get_max_threads();
|
||||
// #else
|
||||
// # define maxthreads 1
|
||||
// #endif
|
||||
// char_type c0, c1;
|
||||
//
|
||||
// /* stage 1: reduce the problem by at least 1/2
|
||||
// sort all the S-substrings */
|
||||
// if(fs < (maxthreads * k)) {
|
||||
// index_type *C, *B;
|
||||
// if((C = new index_type[maxthreads * k]) == 0) { return -2; }
|
||||
// B = (1 < maxthreads) ? C + k : C;
|
||||
// getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
// #ifdef _OPENMP
|
||||
// #pragma omp parallel for default(shared) private(i)
|
||||
// #endif
|
||||
// for(i = 0; i < n; ++i) { SA[i] = 0; }
|
||||
// for(i = n - 2, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
// if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
// else if(c != 0) { SA[--B[c1]] = i + 1, c = 0; }
|
||||
// }
|
||||
// induceSA(T, SA, C, B, n, k);
|
||||
// delete [] C;
|
||||
// } else {
|
||||
// sarray_type C, B;
|
||||
// C = SA + n;
|
||||
// B = ((1 < maxthreads) || (k <= (fs - k))) ? C + k : C;
|
||||
// getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
// #ifdef _OPENMP
|
||||
// #pragma omp parallel for default(shared) private(i)
|
||||
// #endif
|
||||
// for(i = 0; i < n; ++i) { SA[i] = 0; }
|
||||
// for(i = n - 2, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
// if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
// else if(c != 0) { SA[--B[c1]] = i + 1, c = 0; }
|
||||
// }
|
||||
// induceSA(T, SA, C, B, n, k);
|
||||
// }
|
||||
//
|
||||
// /* compact all the sorted substrings into the first m items of SA
|
||||
// 2*m must be not larger than n (proveable) */
|
||||
// #ifdef _OPENMP
|
||||
// #pragma omp parallel for default(shared) private(i, j, p, c0, c1)
|
||||
// for(i = 0; i < n; ++i) {
|
||||
// p = SA[i];
|
||||
// if((0 < p) && (T[p - 1] > (c0 = T[p]))) {
|
||||
// for(j = p + 1; (j < n) && (c0 == (c1 = T[j])); ++j) { }
|
||||
// if((j < n) && (c0 < c1)) { SA[i] = ~p; }
|
||||
// }
|
||||
// }
|
||||
// for(i = 0, m = 0; i < n; ++i) { if((p = SA[i]) < 0) { SA[m++] = ~p; } }
|
||||
// #else
|
||||
// for(i = 0, m = 0; i < n; ++i) {
|
||||
// p = SA[i];
|
||||
// if((0 < p) && (T[p - 1] > (c0 = T[p]))) {
|
||||
// for(j = p + 1; (j < n) && (c0 == (c1 = T[j])); ++j) { }
|
||||
// if((j < n) && (c0 < c1)) { SA[m++] = p; }
|
||||
// }
|
||||
// }
|
||||
// #endif
|
||||
// j = m + (n >> 1);
|
||||
// #ifdef _OPENMP
|
||||
// #pragma omp parallel for default(shared) private(i)
|
||||
// #endif
|
||||
// for(i = m; i < j; ++i) { SA[i] = 0; } /* init the name array buffer */
|
||||
// /* store the length of all substrings */
|
||||
// for(i = n - 2, j = n, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
// if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
// else if(c != 0) { SA[m + ((i + 1) >> 1)] = j - i - 1; j = i + 1; c = 0; }
|
||||
// }
|
||||
// /* find the lexicographic names of all substrings */
|
||||
// for(i = 0, name = 0, q = n, qlen = 0; i < m; ++i) {
|
||||
// p = SA[i], plen = SA[m + (p >> 1)], diff = true;
|
||||
// if(plen == qlen) {
|
||||
// for(j = 0; (j < plen) && (T[p + j] == T[q + j]); ++j) { }
|
||||
// if(j == plen) { diff = false; }
|
||||
// }
|
||||
// if(diff != false) { ++name, q = p, qlen = plen; }
|
||||
// SA[m + (p >> 1)] = name;
|
||||
// }
|
||||
//
|
||||
// /* stage 2: solve the reduced problem
|
||||
// recurse if names are not yet unique */
|
||||
// if(name < m) {
|
||||
// RA = SA + n + fs - m;
|
||||
// for(i = m + (n >> 1) - 1, j = m - 1; m <= i; --i) {
|
||||
// if(SA[i] != 0) { RA[j--] = SA[i] - 1; }
|
||||
// }
|
||||
// if(suffixsort(RA, SA, fs + n - m * 2, m, name, false) != 0) { return -2; }
|
||||
// for(i = n - 2, j = m - 1, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
// if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
// else if(c != 0) { RA[j--] = i + 1, c = 0; } /* get p1 */
|
||||
// }
|
||||
// #ifdef _OPENMP
|
||||
// #pragma omp parallel for default(shared) private(i)
|
||||
// #endif
|
||||
// for(i = 0; i < m; ++i) { SA[i] = RA[SA[i]]; } /* get index in s */
|
||||
// }
|
||||
//
|
||||
// /* stage 3: induce the result for the original problem */
|
||||
// if(fs < (maxthreads * k)) {
|
||||
// index_type *B, *C;
|
||||
// if((C = new index_type[maxthreads * k]) == 0) { return -2; }
|
||||
// B = (1 < maxthreads) ? C + k : C;
|
||||
// /* put all left-most S characters into their buckets */
|
||||
// getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
// #ifdef _OPENMP
|
||||
// #pragma omp parallel for default(shared) private(i)
|
||||
// #endif
|
||||
// for(i = m; i < n; ++i) { SA[i] = 0; } /* init SA[m..n-1] */
|
||||
// for(i = m - 1; 0 <= i; --i) {
|
||||
// j = SA[i], SA[i] = 0;
|
||||
// SA[--B[T[j]]] = j;
|
||||
// }
|
||||
// if(isbwt == false) { induceSA(T, SA, C, B, n, k); }
|
||||
// else { pidx = computeBWT(T, SA, C, B, n, k); }
|
||||
// delete [] C;
|
||||
// } else {
|
||||
// sarray_type C, B;
|
||||
// C = SA + n;
|
||||
// B = ((1 < maxthreads) || (k <= (fs - k))) ? C + k : C;
|
||||
// /* put all left-most S characters into their buckets */
|
||||
// getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
// #ifdef _OPENMP
|
||||
// #pragma omp parallel for default(shared) private(i)
|
||||
// #endif
|
||||
// for(i = m; i < n; ++i) { SA[i] = 0; } /* init SA[m..n-1] */
|
||||
// for(i = m - 1; 0 <= i; --i) {
|
||||
// j = SA[i], SA[i] = 0;
|
||||
// SA[--B[T[j]]] = j;
|
||||
// }
|
||||
// if(isbwt == false) { induceSA(T, SA, C, B, n, k); }
|
||||
// else { pidx = computeBWT(T, SA, C, B, n, k); }
|
||||
// }
|
||||
//
|
||||
// return pidx;
|
||||
// #ifndef _OPENMP
|
||||
// # undef maxthreads
|
||||
// #endif
|
||||
// }
|
||||
//
|
||||
// } /* namespace saisxx_private */
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * @brief Constructs the suffix array of a given string in linear time.
|
||||
// * @param T[0..n-1] The input string. (random access iterator)
|
||||
// * @param SA[0..n-1] The output array of suffixes. (random access iterator)
|
||||
// * @param n The length of the given string.
|
||||
// * @param k The alphabet size.
|
||||
// * @return 0 if no error occurred, -1 or -2 otherwise.
|
||||
// */
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// int
|
||||
// saisxx(string_type T, sarray_type SA, index_type n, index_type k = 256) {
|
||||
// int err;
|
||||
// if((n < 0) || (k <= 0)) { return -1; }
|
||||
// if(n <= 1) { if(n == 1) { SA[0] = 0; } return 0; }
|
||||
// try { err = saisxx_private::suffixsort(T, SA, 0U, n, k, false); }
|
||||
// catch(...) { err = -2; }
|
||||
// return err;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @brief Constructs the burrows-wheeler transformed string of a given string in linear time.
|
||||
// * @param T[0..n-1] The input string. (random access iterator)
|
||||
// * @param U[0..n-1] The output string. (random access iterator)
|
||||
// * @param A[0..n-1] The temporary array. (random access iterator)
|
||||
// * @param n The length of the given string.
|
||||
// * @param k The alphabet size.
|
||||
// * @return The primary index if no error occurred, -1 or -2 otherwise.
|
||||
// */
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// index_type
|
||||
// saisxx_bwt(string_type T, string_type U, sarray_type A, index_type n, index_type k = 256) {
|
||||
// typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
// index_type i, pidx;
|
||||
// if((n < 0) || (k <= 0)) { return -1; }
|
||||
// if(n <= 1) { if(n == 1) { U[0] = T[0]; } return n; }
|
||||
// try {
|
||||
// pidx = saisxx_private::suffixsort(T, A, 0, n, k, true);
|
||||
// if(0 <= pidx) {
|
||||
// U[0] = T[n - 1];
|
||||
// for(i = 0; i < pidx; ++i) { U[i + 1] = (char_type)A[i]; }
|
||||
// for(i += 1; i < n; ++i) { U[i] = (char_type)A[i]; }
|
||||
// pidx += 1;
|
||||
// }
|
||||
// } catch(...) { pidx = -2; }
|
||||
// return pidx;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// #endif /* __cplusplus */
|
||||
// #endif /* _SAIS_HXX */
|
||||
// /*
|
||||
// * esa.hxx
|
||||
// * Copyright (c) 2010 Daisuke Okanohara All Rights Reserved.
|
||||
// *
|
||||
// * Permission is hereby granted, free of charge, to any person
|
||||
// * obtaining a copy of this software and associated documentation
|
||||
// * files (the "Software"), to deal in the Software without
|
||||
// * restriction, including without limitation the rights to use,
|
||||
// * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// * copies of the Software, and to permit persons to whom the
|
||||
// * Software is furnished to do so, subject to the following
|
||||
// * conditions:
|
||||
// *
|
||||
// * The above copyright notice and this permission notice shall be
|
||||
// * included in all copies or substantial portions of the Software.
|
||||
// *
|
||||
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// * OTHER DEALINGS IN THE SOFTWARE.
|
||||
// */
|
||||
//
|
||||
// #ifndef _ESA_HXX
|
||||
// #define _ESA_HXX
|
||||
//
|
||||
// #include <vector>
|
||||
// #include <utility>
|
||||
// #include <cassert>
|
||||
// #include "sais.hxx"
|
||||
//
|
||||
// namespace esaxx_private {
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// index_type suffixtree(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D, index_type n){
|
||||
// if (n == 0){
|
||||
// return 0;
|
||||
// }
|
||||
// sarray_type Psi = L;
|
||||
// Psi[SA[0]] = SA[n-1];
|
||||
// for (index_type i = 1; i < n; ++i){
|
||||
// Psi[SA[i]] = SA[i-1];
|
||||
// }
|
||||
//
|
||||
// // Compare at most 2n log n charcters. Practically fastest
|
||||
// // "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
// sarray_type PLCP = R;
|
||||
// index_type h = 0;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// index_type j = Psi[i];
|
||||
// while (i+h < n && j+h < n &&
|
||||
// T[i+h] == T[j+h]){
|
||||
// ++h;
|
||||
// }
|
||||
// PLCP[i] = h;
|
||||
// if (h > 0) --h;
|
||||
// }
|
||||
//
|
||||
// sarray_type H = L;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// H[i] = PLCP[SA[i]];
|
||||
// }
|
||||
// H[0] = -1;
|
||||
//
|
||||
// std::vector<std::pair<index_type, index_type> > S;
|
||||
// S.push_back(std::make_pair((index_type)-1, (index_type)-1));
|
||||
// size_t nodeNum = 0;
|
||||
// for (index_type i = 0; ; ++i){
|
||||
// std::pair<index_type, index_type> cur (i, (i == n) ? -1 : H[i]);
|
||||
// std::pair<index_type, index_type> cand(S.back());
|
||||
// while (cand.second > cur.second){
|
||||
// if (i - cand.first > 1){
|
||||
// L[nodeNum] = cand.first;
|
||||
// R[nodeNum] = i;
|
||||
// D[nodeNum] = cand.second;
|
||||
// ++nodeNum;
|
||||
// }
|
||||
// cur.first = cand.first;
|
||||
// S.pop_back();
|
||||
// cand = S.back();
|
||||
// }
|
||||
// if (cand.second < cur.second){
|
||||
// S.push_back(cur);
|
||||
// }
|
||||
// if (i == n) break;
|
||||
// S.push_back(std::make_pair(i, n - SA[i] + 1));
|
||||
// }
|
||||
// return nodeNum;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @brief Build an enhanced suffix array of a given string in linear time
|
||||
// * For an input text T, esaxx() builds an enhancd suffix array in linear time.
|
||||
// * i-th internal node is represented as a triple (L[i], R[i], D[i]);
|
||||
// * L[i] and R[i] is the left/right boundary of the suffix array as SA[L[i]....R[i]-1]
|
||||
// * D[i] is the depth of the internal node
|
||||
// * The number of internal node is at most N-1 and return the actual number by
|
||||
// * @param T[0...n-1] The input string. (random access iterator)
|
||||
// * @param SA[0...n-1] The output suffix array (random access iterator)
|
||||
// * @param L[0...n-1] The output left boundary of internal node (random access iterator)
|
||||
// * @param R[0...n-1] The output right boundary of internal node (random access iterator)
|
||||
// * @param D[0...n-1] The output depth of internal node (random access iterator)
|
||||
// * @param n The length of the input string
|
||||
// * @param k The alphabet size
|
||||
// * @pram nodeNum The output the number of internal node
|
||||
// * @return 0 if succeded, -1 or -2 otherwise
|
||||
// */
|
||||
//
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// int esaxx(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D,
|
||||
// index_type n, index_type k, index_type& nodeNum) {
|
||||
// if ((n < 0) || (k <= 0)) return -1;
|
||||
// int err = saisxx(T, SA, n, k);
|
||||
// if (err != 0){
|
||||
// return err;
|
||||
// }
|
||||
// nodeNum = esaxx_private::suffixtree(T, SA, L, R, D, n);
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// #endif // _ESA_HXX
|
||||
// /*
|
||||
// * esa.hxx
|
||||
// * Copyright (c) 2010 Daisuke Okanohara All Rights Reserved.
|
||||
// *
|
||||
// * Permission is hereby granted, free of charge, to any person
|
||||
// * obtaining a copy of this software and associated documentation
|
||||
// * files (the "Software"), to deal in the Software without
|
||||
// * restriction, including without limitation the rights to use,
|
||||
// * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// * copies of the Software, and to permit persons to whom the
|
||||
// * Software is furnished to do so, subject to the following
|
||||
// * conditions:
|
||||
// *
|
||||
// * The above copyright notice and this permission notice shall be
|
||||
// * included in all copies or substantial portions of the Software.
|
||||
// *
|
||||
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// * OTHER DEALINGS IN THE SOFTWARE.
|
||||
// */
|
||||
//
|
||||
// #ifndef _ESA_HXX
|
||||
// #define _ESA_HXX
|
||||
//
|
||||
// #include <vector>
|
||||
// #include <utility>
|
||||
// #include <cassert>
|
||||
// #include "sais.hxx"
|
||||
//
|
||||
// namespace esaxx_private {
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// index_type suffixtree(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D, index_type n){
|
||||
// if (n == 0){
|
||||
// return 0;
|
||||
// }
|
||||
// sarray_type Psi = L;
|
||||
// Psi[SA[0]] = SA[n-1];
|
||||
// for (index_type i = 1; i < n; ++i){
|
||||
// Psi[SA[i]] = SA[i-1];
|
||||
// }
|
||||
//
|
||||
// // Compare at most 2n log n charcters. Practically fastest
|
||||
// // "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
// sarray_type PLCP = R;
|
||||
// index_type h = 0;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// index_type j = Psi[i];
|
||||
// while (i+h < n && j+h < n &&
|
||||
// T[i+h] == T[j+h]){
|
||||
// ++h;
|
||||
// }
|
||||
// PLCP[i] = h;
|
||||
// if (h > 0) --h;
|
||||
// }
|
||||
//
|
||||
// sarray_type H = L;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// H[i] = PLCP[SA[i]];
|
||||
// }
|
||||
// H[0] = -1;
|
||||
//
|
||||
// std::vector<std::pair<index_type, index_type> > S;
|
||||
// S.push_back(std::make_pair((index_type)-1, (index_type)-1));
|
||||
// size_t nodeNum = 0;
|
||||
// for (index_type i = 0; ; ++i){
|
||||
// std::pair<index_type, index_type> cur (i, (i == n) ? -1 : H[i]);
|
||||
// std::pair<index_type, index_type> cand(S.back());
|
||||
// while (cand.second > cur.second){
|
||||
// if (i - cand.first > 1){
|
||||
// L[nodeNum] = cand.first;
|
||||
// R[nodeNum] = i;
|
||||
// D[nodeNum] = cand.second;
|
||||
// ++nodeNum;
|
||||
// }
|
||||
// cur.first = cand.first;
|
||||
// S.pop_back();
|
||||
// cand = S.back();
|
||||
// }
|
||||
// if (cand.second < cur.second){
|
||||
// S.push_back(cur);
|
||||
// }
|
||||
// if (i == n) break;
|
||||
// S.push_back(std::make_pair(i, n - SA[i] + 1));
|
||||
// }
|
||||
// return nodeNum;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @brief Build an enhanced suffix array of a given string in linear time
|
||||
// * For an input text T, esaxx() builds an enhancd suffix array in linear time.
|
||||
// * i-th internal node is represented as a triple (L[i], R[i], D[i]);
|
||||
// * L[i] and R[i] is the left/right boundary of the suffix array as SA[L[i]....R[i]-1]
|
||||
// * D[i] is the depth of the internal node
|
||||
// * The number of internal node is at most N-1 and return the actual number by
|
||||
// * @param T[0...n-1] The input string. (random access iterator)
|
||||
// * @param SA[0...n-1] The output suffix array (random access iterator)
|
||||
// * @param L[0...n-1] The output left boundary of internal node (random access iterator)
|
||||
// * @param R[0...n-1] The output right boundary of internal node (random access iterator)
|
||||
// * @param D[0...n-1] The output depth of internal node (random access iterator)
|
||||
// * @param n The length of the input string
|
||||
// * @param k The alphabet size
|
||||
// * @pram nodeNum The output the number of internal node
|
||||
// * @return 0 if succeded, -1 or -2 otherwise
|
||||
// */
|
||||
//
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// int esaxx(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D,
|
||||
// index_type n, index_type k, index_type& nodeNum) {
|
||||
// if ((n < 0) || (k <= 0)) return -1;
|
||||
// std::count<<"Here"<<std::endl;
|
||||
// int err = saisxx(T, SA, n, k);
|
||||
// if (err != 0){
|
||||
// return err;
|
||||
// }
|
||||
// std::cout<<"suffixtree"<<std::endl;
|
||||
// nodeNum = esaxx_private::suffixtree(T, SA, L, R, D, n);
|
||||
// std::count<<"ok"<<std::endl;
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// #endif // _ESA_HXX
|
||||
|
||||
#include <cstdint>
|
||||
#include "esa.hxx"
|
||||
|
||||
extern "C"{
|
||||
|
||||
int esaxx_int32(char32_t* T, int32_t* SA, int32_t* L, int32_t* R, int32_t* D,
|
||||
int32_t n, int32_t k, int32_t &nodeNum) {
|
||||
|
||||
return esaxx(T, SA, L, R, D, n, k, nodeNum);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Vendored
+383
@@ -0,0 +1,383 @@
|
||||
//! Small wrapper around sentencepiece's esaxx suffix array C++ library.
|
||||
//! Usage
|
||||
//!
|
||||
//! ```rust
|
||||
//! #[cfg(feature="cpp")]
|
||||
//! {
|
||||
//! let string = "abracadabra";
|
||||
//! let suffix = esaxx_rs::suffix(string).unwrap();
|
||||
//! let chars: Vec<_> = string.chars().collect();
|
||||
//! let mut iter = suffix.iter();
|
||||
//! assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
//! assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
//! assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
//! assert_eq!(iter.next(), None);
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! The previous version uses unsafe optimized c++ code.
|
||||
//! There exists another implementation a bit slower (~2x slower) that uses
|
||||
//! safe rust. It's a bit slower because it uses usize (mostly 64bit) instead of i32 (32bit).
|
||||
//! But it does seems to fix a few OOB issues in the cpp version
|
||||
//! (which never seemed to cause real problems in tests but still.)
|
||||
//!
|
||||
//! ```rust
|
||||
//! let string = "abracadabra";
|
||||
//! let suffix = esaxx_rs::suffix_rs(string).unwrap();
|
||||
//! let chars: Vec<_> = string.chars().collect();
|
||||
//! let mut iter = suffix.iter();
|
||||
//! assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
//! assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
//! assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
//! assert_eq!(iter.next(), None);
|
||||
//! ```
|
||||
|
||||
use std::convert::TryInto;
|
||||
mod esa;
|
||||
mod sais;
|
||||
mod types;
|
||||
|
||||
use esa::esaxx_rs;
|
||||
use types::SuffixError;
|
||||
|
||||
#[cfg(feature = "cc")]
|
||||
extern "C" {
|
||||
fn esaxx_int32(
|
||||
// This is char32
|
||||
T: *const u32,
|
||||
SA: *mut i32,
|
||||
L: *mut i32,
|
||||
R: *mut i32,
|
||||
D: *mut i32,
|
||||
n: u32,
|
||||
k: u32,
|
||||
nodeNum: &mut u32,
|
||||
) -> i32;
|
||||
}
|
||||
|
||||
#[cfg(feature = "cc")]
|
||||
fn esaxx(
|
||||
chars: &[char],
|
||||
sa: &mut [i32],
|
||||
l: &mut [i32],
|
||||
r: &mut [i32],
|
||||
d: &mut [i32],
|
||||
alphabet_size: u32,
|
||||
node_num: &mut u32,
|
||||
) -> Result<(), SuffixError> {
|
||||
let n = chars.len();
|
||||
if sa.len() != n || l.len() != n || r.len() != n || d.len() != n {
|
||||
return Err(SuffixError::InvalidLength);
|
||||
}
|
||||
unsafe {
|
||||
let err = esaxx_int32(
|
||||
chars.as_ptr() as *const u32,
|
||||
sa.as_mut_ptr(),
|
||||
l.as_mut_ptr(),
|
||||
r.as_mut_ptr(),
|
||||
d.as_mut_ptr(),
|
||||
n.try_into().unwrap(),
|
||||
alphabet_size,
|
||||
node_num,
|
||||
);
|
||||
if err != 0 {
|
||||
return Err(SuffixError::Internal);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub struct SuffixIterator<'a, T> {
|
||||
i: usize,
|
||||
suffix: &'a Suffix<T>,
|
||||
}
|
||||
|
||||
pub struct Suffix<T> {
|
||||
chars: Vec<char>,
|
||||
sa: Vec<T>,
|
||||
l: Vec<T>,
|
||||
r: Vec<T>,
|
||||
d: Vec<T>,
|
||||
node_num: usize,
|
||||
}
|
||||
|
||||
/// Creates the suffix array and provides an iterator over its items (Rust version)
|
||||
/// See [suffix](fn.suffix.html)
|
||||
pub fn suffix_rs(string: &str) -> Result<Suffix<usize>, SuffixError> {
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
let node_num = esaxx_rs(
|
||||
&chars.iter().map(|c| *c as u32).collect::<Vec<_>>(),
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
)?;
|
||||
Ok(Suffix {
|
||||
chars,
|
||||
sa,
|
||||
l,
|
||||
r,
|
||||
d,
|
||||
node_num,
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates the suffix array and provides an iterator over its items (c++ unsafe version)
|
||||
///
|
||||
/// Gives you an iterator over the suffixes of the input array and their count within
|
||||
/// the input srtring.
|
||||
/// ```rust
|
||||
/// let string = "abracadabra";
|
||||
/// let suffix = esaxx_rs::suffix(string).unwrap();
|
||||
/// let chars: Vec<_> = string.chars().collect();
|
||||
/// let mut iter = suffix.iter();
|
||||
/// assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
|
||||
/// assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
/// assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
/// assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
/// assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
#[cfg(feature = "cpp")]
|
||||
pub fn suffix(string: &str) -> Result<Suffix<i32>, SuffixError> {
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let mut node_num = 0;
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
esaxx(
|
||||
&chars,
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
&mut node_num,
|
||||
)?;
|
||||
Ok(Suffix {
|
||||
chars,
|
||||
sa,
|
||||
l,
|
||||
r,
|
||||
d,
|
||||
node_num: node_num.try_into()?,
|
||||
})
|
||||
}
|
||||
|
||||
impl<T> Suffix<T> {
|
||||
pub fn iter(&self) -> SuffixIterator<'_, T> {
|
||||
SuffixIterator { i: 0, suffix: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for SuffixIterator<'a, i32> {
|
||||
type Item = (&'a [char], u32);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.i;
|
||||
if index == self.suffix.node_num {
|
||||
None
|
||||
} else {
|
||||
let left: usize = self.suffix.l[index].try_into().ok()?;
|
||||
let offset: usize = self.suffix.sa[left].try_into().ok()?;
|
||||
let len: usize = self.suffix.d[index].try_into().ok()?;
|
||||
let freq: u32 = (self.suffix.r[index] - self.suffix.l[index])
|
||||
.try_into()
|
||||
.ok()?;
|
||||
self.i += 1;
|
||||
Some((&self.suffix.chars[offset..offset + len], freq))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for SuffixIterator<'a, usize> {
|
||||
type Item = (&'a [char], u32);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.i;
|
||||
if index == self.suffix.node_num {
|
||||
None
|
||||
} else {
|
||||
let left: usize = self.suffix.l[index];
|
||||
let offset: usize = self.suffix.sa[left];
|
||||
let len: usize = self.suffix.d[index];
|
||||
let freq: u32 = (self.suffix.r[index] - self.suffix.l[index])
|
||||
.try_into()
|
||||
.unwrap();
|
||||
self.i += 1;
|
||||
Some((&self.suffix.chars[offset..offset + len], freq))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "cpp")]
|
||||
mod cpp_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_esaxx() {
|
||||
let string = "abracadabra".to_string();
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let mut node_num = 0;
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
esaxx(
|
||||
&chars,
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
&mut node_num,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(node_num, 5);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_esaxx_long() {
|
||||
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.".to_string();
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let mut node_num = 0;
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
esaxx(
|
||||
&chars,
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
&mut node_num,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(chars.len(), 574);
|
||||
assert_eq!(node_num, 260);
|
||||
// assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
// assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
// assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
// assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_suffix() {
|
||||
let suffix = suffix("abracadabra").unwrap();
|
||||
assert_eq!(suffix.node_num, 5);
|
||||
assert_eq!(suffix.sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(suffix.l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(suffix.r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(suffix.d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
|
||||
let mut iter = suffix.iter();
|
||||
let chars: Vec<_> = "abracadabra".chars().collect();
|
||||
assert_eq!(iter.next(), Some((&chars[..4], 2))); // abra
|
||||
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod rs_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_esaxx_rs() {
|
||||
let string = "abracadabra".to_string();
|
||||
let chars: Vec<_> = string.chars().map(|c| c as u32).collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
let node_num = esaxx_rs(&chars, &mut sa, &mut l, &mut r, &mut d, alphabet_size).unwrap();
|
||||
println!("Node num {}", node_num);
|
||||
println!("sa {:?}", sa);
|
||||
println!("l {:?}", l);
|
||||
println!("r {:?}", r);
|
||||
println!("d {:?}", d);
|
||||
assert_eq!(node_num, 5);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_esaxx_rs_long() {
|
||||
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.".to_string();
|
||||
let chars: Vec<_> = string.chars().map(|c| c as u32).collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
let node_num = esaxx_rs(&chars, &mut sa, &mut l, &mut r, &mut d, alphabet_size).unwrap();
|
||||
assert_eq!(chars.len(), 574);
|
||||
assert_eq!(node_num, 260);
|
||||
// assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
// assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
// assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
// assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_suffix_rs() {
|
||||
let suffix = suffix_rs("abracadabra").unwrap();
|
||||
assert_eq!(suffix.node_num, 5);
|
||||
assert_eq!(suffix.sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(suffix.l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(suffix.r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(suffix.d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
|
||||
let mut iter = suffix.iter();
|
||||
let chars: Vec<_> = "abracadabra".chars().collect();
|
||||
assert_eq!(iter.next(), Some((&chars[..4], 2))); // abra
|
||||
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_out_of_bounds_bug() {
|
||||
let string = "banana$band$$";
|
||||
suffix_rs(string).unwrap();
|
||||
}
|
||||
}
|
||||
Vendored
+377
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
* sais.hxx for sais-lite
|
||||
* Copyright (c) 2008-2009 Yuta Mori All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _SAIS_HXX
|
||||
#define _SAIS_HXX 1
|
||||
#ifdef __cplusplus
|
||||
|
||||
#ifdef __INTEL_COMPILER
|
||||
#pragma warning(disable : 383 981 1418)
|
||||
// for icc 64-bit
|
||||
//#define __builtin_vsnprintf(a, b, c, d) __builtin_vsnprintf(a, b, c, (char *)d)
|
||||
#endif
|
||||
|
||||
#include <iterator>
|
||||
#ifdef _OPENMP
|
||||
# include <omp.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
|
||||
int32_t f(const int32_t s){
|
||||
int a = ((s < 0)?-~s:s);
|
||||
return a;
|
||||
}
|
||||
|
||||
namespace saisxx_private {
|
||||
|
||||
/* find the start or end of each bucket */
|
||||
template<typename string_type, typename bucket_type, typename index_type>
|
||||
void
|
||||
getCounts(const string_type T, bucket_type C, index_type n, index_type k) {
|
||||
#ifdef _OPENMP
|
||||
bucket_type D;
|
||||
index_type i, j, p, sum, first, last;
|
||||
int thnum, maxthreads = omp_get_max_threads();
|
||||
#pragma omp parallel default(shared) private(D, i, thnum, first, last)
|
||||
{
|
||||
thnum = omp_get_thread_num();
|
||||
D = C + thnum * k;
|
||||
first = n / maxthreads * thnum;
|
||||
last = (thnum < (maxthreads - 1)) ? n / maxthreads * (thnum + 1) : n;
|
||||
for(i = 0; i < k; ++i) { D[i] = 0; }
|
||||
for(i = first; i < last; ++i) { ++D[T[i]]; }
|
||||
}
|
||||
if(1 < maxthreads) {
|
||||
#pragma omp parallel for default(shared) private(i, j, p, sum)
|
||||
for(i = 0; i < k; ++i) {
|
||||
for(j = 1, p = i + k, sum = C[i]; j < maxthreads; ++j, p += k) {
|
||||
sum += C[p];
|
||||
}
|
||||
C[i] = sum;
|
||||
}
|
||||
}
|
||||
#else
|
||||
index_type i;
|
||||
for(i = 0; i < k; ++i) { C[i] = 0; }
|
||||
for(i = 0; i < n; ++i) { ++C[T[i]]; }
|
||||
#endif
|
||||
}
|
||||
template<typename bucket_type, typename index_type>
|
||||
void
|
||||
getBuckets(const bucket_type C, bucket_type B, index_type k, bool end) {
|
||||
index_type i, sum = 0;
|
||||
if(end) { for(i = 0; i < k; ++i) { sum += C[i]; B[i] = sum; } }
|
||||
else { for(i = 0; i < k; ++i) { sum += C[i]; B[i] = sum - C[i]; } }
|
||||
}
|
||||
|
||||
/* compute SA and BWT */
|
||||
template<typename string_type, typename sarray_type,
|
||||
typename bucket_type, typename index_type>
|
||||
void
|
||||
induceSA(string_type T, sarray_type SA, bucket_type C, bucket_type B,
|
||||
index_type n, index_type k) {
|
||||
typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
sarray_type b;
|
||||
index_type i, j;
|
||||
char_type c0, c1;
|
||||
/* compute SAl */
|
||||
if(C == B) {
|
||||
getCounts(T, C, n, k); }
|
||||
getBuckets(C, B, k, false); /* find starts of buckets */
|
||||
b = SA + B[c1 = T[j = n - 1]];
|
||||
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
|
||||
for(i = 0; i < n; ++i) {
|
||||
j = SA[i], SA[i] = ~j;
|
||||
|
||||
if(0 < j) {
|
||||
if((c0 = T[--j]) != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
}
|
||||
}
|
||||
/* compute SAs */
|
||||
if(C == B) { getCounts(T, C, n, k); }
|
||||
getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
|
||||
for(i = n - 1, b = SA + B[c1 = 0]; 0 <= i; --i) {
|
||||
if(0 < (j = SA[i])) {
|
||||
if((c0 = T[--j]) != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
*--b = ((j == 0) || (T[j - 1] > c1)) ? ~j : j;
|
||||
} else {
|
||||
SA[i] = ~j;
|
||||
}
|
||||
}
|
||||
}
|
||||
template<typename string_type, typename sarray_type,
|
||||
typename bucket_type, typename index_type>
|
||||
int
|
||||
computeBWT(string_type T, sarray_type SA, bucket_type C, bucket_type B,
|
||||
index_type n, index_type k) {
|
||||
typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
sarray_type b;
|
||||
index_type i, j, pidx = -1;
|
||||
char_type c0, c1;
|
||||
/* compute SAl */
|
||||
if(C == B) { getCounts(T, C, n, k); }
|
||||
getBuckets(C, B, k, false); /* find starts of buckets */
|
||||
b = SA + B[c1 = T[j = n - 1]];
|
||||
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
for(i = 0; i < n; ++i) {
|
||||
if(0 < (j = SA[i])) {
|
||||
SA[i] = ~(c0 = T[--j]);
|
||||
if(c0 != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
} else if(j != 0) {
|
||||
SA[i] = ~j;
|
||||
}
|
||||
}
|
||||
/* compute SAs */
|
||||
if(C == B) { getCounts(T, C, n, k); }
|
||||
getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
for(i = n - 1, b = SA + B[c1 = 0]; 0 <= i; --i) {
|
||||
if(0 < (j = SA[i])) {
|
||||
SA[i] = (c0 = T[--j]);
|
||||
if(c0 != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
|
||||
*--b = ((0 < j) && (T[j - 1] > c1)) ? ~((index_type)T[j - 1]) : j;
|
||||
} else if(j != 0) {
|
||||
SA[i] = ~j;
|
||||
} else {
|
||||
pidx = i;
|
||||
}
|
||||
}
|
||||
return pidx;
|
||||
}
|
||||
|
||||
/* find the suffix array SA of T[0..n-1] in {0..k}^n
|
||||
use a working space (excluding s and SA) of at most 2n+O(1) for a constant alphabet */
|
||||
template<typename string_type, typename sarray_type, typename index_type>
|
||||
int
|
||||
suffixsort(string_type T, sarray_type SA,
|
||||
index_type fs, index_type n, index_type k,
|
||||
bool isbwt) {
|
||||
typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
sarray_type RA;
|
||||
index_type i, j, m, p, q, plen, qlen, name;
|
||||
int pidx = 0;
|
||||
bool diff;
|
||||
int c;
|
||||
#ifdef _OPENMP
|
||||
int maxthreads = omp_get_max_threads();
|
||||
#else
|
||||
# define maxthreads 1
|
||||
#endif
|
||||
char_type c0, c1;
|
||||
|
||||
/* stage 1: reduce the problem by at least 1/2
|
||||
sort all the S-substrings */
|
||||
if(fs < (maxthreads * k)) {
|
||||
index_type *C, *B;
|
||||
if((C = new index_type[maxthreads * k]) == 0) { return -2; }
|
||||
B = (1 < maxthreads) ? C + k : C;
|
||||
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for default(shared) private(i)
|
||||
#endif
|
||||
for(i = 0; i < n; ++i) { SA[i] = 0; }
|
||||
for(i = n - 2, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
else if(c != 0) { SA[--B[c1]] = i + 1, c = 0; }
|
||||
}
|
||||
induceSA(T, SA, C, B, n, k);
|
||||
delete [] C;
|
||||
} else {
|
||||
sarray_type C, B;
|
||||
C = SA + n;
|
||||
B = ((1 < maxthreads) || (k <= (fs - k))) ? C + k : C;
|
||||
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for default(shared) private(i)
|
||||
#endif
|
||||
for(i = 0; i < n; ++i) { SA[i] = 0; }
|
||||
for(i = n - 2, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
else if(c != 0) { SA[--B[c1]] = i + 1, c = 0; }
|
||||
}
|
||||
induceSA(T, SA, C, B, n, k);
|
||||
}
|
||||
|
||||
/* compact all the sorted substrings into the first m items of SA
|
||||
2*m must be not larger than n (proveable) */
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for default(shared) private(i, j, p, c0, c1)
|
||||
for(i = 0; i < n; ++i) {
|
||||
p = SA[i];
|
||||
if((0 < p) && (T[p - 1] > (c0 = T[p]))) {
|
||||
for(j = p + 1; (j < n) && (c0 == (c1 = T[j])); ++j) { }
|
||||
if((j < n) && (c0 < c1)) { SA[i] = ~p; }
|
||||
}
|
||||
}
|
||||
for(i = 0, m = 0; i < n; ++i) { if((p = SA[i]) < 0) { SA[m++] = ~p; } }
|
||||
#else
|
||||
for(i = 0, m = 0; i < n; ++i) {
|
||||
p = SA[i];
|
||||
if((0 < p) && (T[p - 1] > (c0 = T[p]))) {
|
||||
for(j = p + 1; (j < n) && (c0 == (c1 = T[j])); ++j) { }
|
||||
if((j < n) && (c0 < c1)) {
|
||||
SA[m++] = p; }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
j = m + (n >> 1);
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for default(shared) private(i)
|
||||
#endif
|
||||
for(i = m; i < j; ++i) { SA[i] = 0; } /* init the name array buffer */
|
||||
/* store the length of all substrings */
|
||||
for(i = n - 2, j = n, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
else if(c != 0) { SA[m + ((i + 1) >> 1)] = j - i - 1; j = i + 1; c = 0; }
|
||||
}
|
||||
/* find the lexicographic names of all substrings */
|
||||
for(i = 0, name = 0, q = n, qlen = 0; i < m; ++i) {
|
||||
p = SA[i], plen = SA[m + (p >> 1)], diff = true;
|
||||
if(plen == qlen) {
|
||||
for(j = 0; (j < plen) && (T[p + j] == T[q + j]); ++j) { }
|
||||
if(j == plen) { diff = false; }
|
||||
}
|
||||
if(diff != false) { ++name, q = p, qlen = plen; }
|
||||
SA[m + (p >> 1)] = name;
|
||||
}
|
||||
|
||||
/* stage 2: solve the reduced problem
|
||||
recurse if names are not yet unique */
|
||||
if(name < m) {
|
||||
RA = SA + n + fs - m;
|
||||
for(i = m + (n >> 1) - 1, j = m - 1; m <= i; --i) {
|
||||
if(SA[i] != 0) { RA[j--] = SA[i] - 1; }
|
||||
}
|
||||
if(suffixsort(RA, SA, fs + n - m * 2, m, name, false) != 0) { return -2; }
|
||||
for(i = n - 2, j = m - 1, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
|
||||
if((c0 = T[i]) < (c1 + c)) { c = 1; }
|
||||
else if(c != 0) { RA[j--] = i + 1, c = 0; } /* get p1 */
|
||||
}
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for default(shared) private(i)
|
||||
#endif
|
||||
for(i = 0; i < m; ++i) { SA[i] = RA[SA[i]]; } /* get index in s */
|
||||
}
|
||||
|
||||
/* stage 3: induce the result for the original problem */
|
||||
if(fs < (maxthreads * k)) {
|
||||
index_type *B, *C;
|
||||
if((C = new index_type[maxthreads * k]) == 0) { return -2; }
|
||||
B = (1 < maxthreads) ? C + k : C;
|
||||
/* put all left-most S characters into their buckets */
|
||||
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for default(shared) private(i)
|
||||
#endif
|
||||
for(i = m; i < n; ++i) { SA[i] = 0; } /* init SA[m..n-1] */
|
||||
for(i = m - 1; 0 <= i; --i) {
|
||||
j = SA[i], SA[i] = 0;
|
||||
SA[--B[T[j]]] = j;
|
||||
}
|
||||
if(isbwt == false) { induceSA(T, SA, C, B, n, k); }
|
||||
else { pidx = computeBWT(T, SA, C, B, n, k); }
|
||||
delete [] C;
|
||||
} else {
|
||||
sarray_type C, B;
|
||||
C = SA + n;
|
||||
B = ((1 < maxthreads) || (k <= (fs - k))) ? C + k : C;
|
||||
/* put all left-most S characters into their buckets */
|
||||
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for default(shared) private(i)
|
||||
#endif
|
||||
for(i = m; i < n; ++i) { SA[i] = 0; } /* init SA[m..n-1] */
|
||||
for(i = m - 1; 0 <= i; --i) {
|
||||
j = SA[i], SA[i] = 0;
|
||||
SA[--B[T[j]]] = j;
|
||||
}
|
||||
if(isbwt == false) { induceSA(T, SA, C, B, n, k); }
|
||||
else { pidx = computeBWT(T, SA, C, B, n, k); }
|
||||
}
|
||||
|
||||
return pidx;
|
||||
#ifndef _OPENMP
|
||||
# undef maxthreads
|
||||
#endif
|
||||
}
|
||||
|
||||
} /* namespace saisxx_private */
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constructs the suffix array of a given string in linear time.
|
||||
* @param T[0..n-1] The input string. (random access iterator)
|
||||
* @param SA[0..n-1] The output array of suffixes. (random access iterator)
|
||||
* @param n The length of the given string.
|
||||
* @param k The alphabet size.
|
||||
* @return 0 if no error occurred, -1 or -2 otherwise.
|
||||
*/
|
||||
template<typename string_type, typename sarray_type, typename index_type>
|
||||
int
|
||||
saisxx(string_type T, sarray_type SA, index_type n, index_type k = 256) {
|
||||
int err;
|
||||
if((n < 0) || (k <= 0)) { return -1; }
|
||||
if(n <= 1) { if(n == 1) { SA[0] = 0; } return 0; }
|
||||
try { err = saisxx_private::suffixsort(T, SA, index_type(0), n, k, false); }
|
||||
catch(...) { err = -2; }
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Constructs the burrows-wheeler transformed string of a given string in linear time.
|
||||
* @param T[0..n-1] The input string. (random access iterator)
|
||||
* @param U[0..n-1] The output string. (random access iterator)
|
||||
* @param A[0..n-1] The temporary array. (random access iterator)
|
||||
* @param n The length of the given string.
|
||||
* @param k The alphabet size.
|
||||
* @return The primary index if no error occurred, -1 or -2 otherwise.
|
||||
*/
|
||||
template<typename string_type, typename sarray_type, typename index_type>
|
||||
index_type
|
||||
saisxx_bwt(string_type T, string_type U, sarray_type A, index_type n, index_type k = 256) {
|
||||
typedef typename std::iterator_traits<string_type>::value_type char_type;
|
||||
index_type i, pidx;
|
||||
if((n < 0) || (k <= 0)) { return -1; }
|
||||
if(n <= 1) { if(n == 1) { U[0] = T[0]; } return n; }
|
||||
try {
|
||||
pidx = saisxx_private::suffixsort(T, A, 0, n, k, true);
|
||||
if(0 <= pidx) {
|
||||
U[0] = T[n - 1];
|
||||
for(i = 0; i < pidx; ++i) { U[i + 1] = (char_type)A[i]; }
|
||||
for(i += 1; i < n; ++i) { U[i] = (char_type)A[i]; }
|
||||
pidx += 1;
|
||||
}
|
||||
} catch(...) { pidx = -2; }
|
||||
return pidx;
|
||||
}
|
||||
|
||||
|
||||
#endif /* __cplusplus */
|
||||
#endif /* _SAIS_HXX */
|
||||
|
||||
Vendored
+485
@@ -0,0 +1,485 @@
|
||||
use crate::types::{Bucket, SArray, StringT, SuffixError};
|
||||
|
||||
fn has_high_bit(j: usize) -> bool {
|
||||
j > usize::MAX / 2
|
||||
}
|
||||
|
||||
fn get_counts(t: &StringT, c: &mut Bucket) {
|
||||
c.iter_mut().for_each(|c| *c = 0);
|
||||
t.iter().for_each(|character| c[*character as usize] += 1);
|
||||
}
|
||||
|
||||
fn get_buckets(c: &Bucket, b: &mut Bucket, _k: usize, end: bool) {
|
||||
let mut sum = 0;
|
||||
if end {
|
||||
b.iter_mut().enumerate().for_each(|(i, b_el)| {
|
||||
sum += c[i];
|
||||
*b_el = sum;
|
||||
});
|
||||
} else {
|
||||
b.iter_mut().enumerate().for_each(|(i, b_el)| {
|
||||
*b_el = sum;
|
||||
sum += c[i];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn induce_sa(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
counts: &mut Bucket,
|
||||
buckets: &mut Bucket,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) {
|
||||
assert!(n <= suffix_array.len());
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, false);
|
||||
|
||||
let mut c0;
|
||||
let mut j = n - 1;
|
||||
let mut c1 = string[j] as usize;
|
||||
let mut index = buckets[c1];
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
for i in 0..n {
|
||||
j = suffix_array[i];
|
||||
suffix_array[i] = !j;
|
||||
if !has_high_bit(j) && j > 0 {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
suffix_array[index] = if j > 0 && !has_high_bit(j) && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute SA
|
||||
// XXX: true here.
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, true);
|
||||
c1 = 0;
|
||||
index = buckets[c1];
|
||||
for i in (0..n).rev() {
|
||||
j = suffix_array[i];
|
||||
if j > 0 && !has_high_bit(j) {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
index -= 1;
|
||||
suffix_array[index] = if j == 0 || (string[j - 1] as usize) > c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
} else {
|
||||
suffix_array[i] = !j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_bwt(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
counts: &mut Bucket,
|
||||
buckets: &mut Bucket,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) -> usize {
|
||||
// TODO
|
||||
let mut pidx = 0;
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, false);
|
||||
let mut j = n - 1;
|
||||
let mut c1 = string[j] as usize;
|
||||
let mut c0;
|
||||
let mut index = buckets[c1];
|
||||
// bb = SA + B[c1 = T[j = n - 1]];
|
||||
// *bb++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
for i in 0..n {
|
||||
j = suffix_array[i];
|
||||
if j > 0 {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
suffix_array[i] = !c0;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
} else if j != 0 {
|
||||
suffix_array[i] = !j;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute SA
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, true);
|
||||
c1 = 0;
|
||||
index = buckets[c1];
|
||||
for i in (0..n).rev() {
|
||||
j = suffix_array[i];
|
||||
if j > 0 {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
suffix_array[i] = c0;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
index -= 1;
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) > c1 {
|
||||
!(string[j - 1] as usize)
|
||||
} else {
|
||||
j
|
||||
};
|
||||
} else if j != 0 {
|
||||
suffix_array[i] = !j;
|
||||
} else {
|
||||
pidx = i
|
||||
}
|
||||
}
|
||||
pidx
|
||||
}
|
||||
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
fn suffixsort(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
fs: usize,
|
||||
n: usize,
|
||||
k: usize,
|
||||
is_bwt: bool,
|
||||
) -> Result<usize, SuffixError> {
|
||||
let mut pidx = 0;
|
||||
let mut c0;
|
||||
|
||||
let mut counts = vec![0; k];
|
||||
let mut buckets = vec![0; k];
|
||||
get_counts(string, &mut counts);
|
||||
get_buckets(&counts, &mut buckets, k, true);
|
||||
// stage 1:
|
||||
// reduce the problem by at least 1/2
|
||||
// sort all the S-substrings
|
||||
for item in suffix_array.iter_mut() {
|
||||
*item = 0;
|
||||
}
|
||||
let mut c_index = 0;
|
||||
let mut c1 = string[n - 1] as usize;
|
||||
for i in (0..n - 1).rev() {
|
||||
c0 = string[i] as usize;
|
||||
if c0 < c1 + c_index {
|
||||
c_index = 1;
|
||||
} else if c_index != 0 {
|
||||
buckets[c1] -= 1;
|
||||
suffix_array[buckets[c1]] = i + 1;
|
||||
c_index = 0;
|
||||
}
|
||||
c1 = c0;
|
||||
}
|
||||
induce_sa(string, suffix_array, &mut counts, &mut buckets, n, k);
|
||||
|
||||
// compact all the sorted substrings into the first m items of SA
|
||||
// 2*m must be not larger than n (proveable)
|
||||
|
||||
// TODO: This was in the parallel loop.
|
||||
let mut p;
|
||||
let mut j;
|
||||
let mut m = 0;
|
||||
for i in 0..n {
|
||||
p = suffix_array[i];
|
||||
c0 = string[p] as usize;
|
||||
if p > 0 && (string[p - 1] as usize) > c0 {
|
||||
// TODO overly complex. But fricking hard to get right.
|
||||
j = p + 1;
|
||||
if j < n {
|
||||
c1 = string[j] as usize;
|
||||
}
|
||||
while j < n && c0 == c1 {
|
||||
c1 = string[j] as usize;
|
||||
j += 1;
|
||||
}
|
||||
if j < n && c0 < c1 {
|
||||
suffix_array[m] = p;
|
||||
m += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
j = m + (n >> 1);
|
||||
for item in suffix_array.iter_mut().take(j).skip(m) {
|
||||
*item = 0;
|
||||
}
|
||||
|
||||
/* store the length of all substrings */
|
||||
j = n;
|
||||
let mut c_index = 0;
|
||||
c1 = string[n - 1] as usize;
|
||||
for i in (0..n - 1).rev() {
|
||||
c0 = string[i] as usize;
|
||||
if c0 < c1 + c_index {
|
||||
c_index = 1;
|
||||
} else if c_index != 0 {
|
||||
suffix_array[m + ((i + 1) >> 1)] = j - i - 1;
|
||||
j = i + 1;
|
||||
c_index = 0;
|
||||
}
|
||||
c1 = c0;
|
||||
}
|
||||
|
||||
/* find the lexicographic names of all substrings */
|
||||
let mut name = 0;
|
||||
let mut q = n;
|
||||
let mut qlen = 0;
|
||||
let mut plen;
|
||||
let mut diff;
|
||||
for i in 0..m {
|
||||
p = suffix_array[i];
|
||||
plen = suffix_array[m + (p >> 1)];
|
||||
diff = true;
|
||||
if plen == qlen {
|
||||
j = 0;
|
||||
while j < plen && string[p + j] == string[q + j] {
|
||||
j += 1;
|
||||
}
|
||||
if j == plen {
|
||||
diff = false;
|
||||
}
|
||||
}
|
||||
if diff {
|
||||
name += 1;
|
||||
q = p;
|
||||
qlen = plen;
|
||||
}
|
||||
suffix_array[m + (p >> 1)] = name;
|
||||
}
|
||||
/* stage 2: solve the reduced problem
|
||||
recurse if names are not yet unique */
|
||||
if name < m {
|
||||
let ra_index = n + fs - m;
|
||||
j = m - 1;
|
||||
let a = m + (n >> 1);
|
||||
for i in (m..a).rev() {
|
||||
if suffix_array[i] != 0 {
|
||||
suffix_array[ra_index + j] = suffix_array[i] - 1;
|
||||
// XXX: Bug underflow caught by Rust yeah (well cpp used i32)
|
||||
j = j.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
// XXX: Could call transmute on SA to avoid allocation.
|
||||
// but it requires unsafe.
|
||||
let ra: Vec<u32> = suffix_array
|
||||
.iter()
|
||||
.skip(ra_index)
|
||||
.take(m)
|
||||
.map(|n| *n as u32)
|
||||
.collect();
|
||||
suffixsort(&ra, suffix_array, fs + n - m * 2, m, name, false)?;
|
||||
// let ra: &[char] =
|
||||
// unsafe { std::mem::transmute::<&[usize], &[char]>(&sa[ra_index..ra_index + m]) };
|
||||
// suffixsort(ra, sa, fs + n - m * 2, m, name, false)?;
|
||||
j = m - 1;
|
||||
c_index = 0;
|
||||
c1 = string[n - 1] as usize;
|
||||
for i in (0..n - 1).rev() {
|
||||
c0 = string[i] as usize;
|
||||
if c0 < c1 + c_index {
|
||||
c_index = 1;
|
||||
} else if c_index != 0 {
|
||||
suffix_array[ra_index + j] = i + 1;
|
||||
c_index = 0;
|
||||
j = j.saturating_sub(1);
|
||||
}
|
||||
c1 = c0;
|
||||
}
|
||||
// get index in s
|
||||
for i in 0..m {
|
||||
suffix_array[i] = suffix_array[ra_index + suffix_array[i]];
|
||||
}
|
||||
}
|
||||
|
||||
/* stage 3: induce the result for the original problem */
|
||||
/* put all left-most S characters into their buckets */
|
||||
get_counts(string, &mut counts);
|
||||
get_buckets(&counts, &mut buckets, k, true);
|
||||
for item in suffix_array.iter_mut().take(n).skip(m) {
|
||||
*item = 0;
|
||||
}
|
||||
for i in (0..m).rev() {
|
||||
j = suffix_array[i];
|
||||
suffix_array[i] = 0;
|
||||
if buckets[string[j] as usize] > 0 {
|
||||
buckets[string[j] as usize] -= 1;
|
||||
suffix_array[buckets[string[j] as usize]] = j;
|
||||
}
|
||||
}
|
||||
if is_bwt {
|
||||
pidx = compute_bwt(string, suffix_array, &mut counts, &mut buckets, n, k);
|
||||
} else {
|
||||
induce_sa(string, suffix_array, &mut counts, &mut buckets, n, k);
|
||||
}
|
||||
|
||||
Ok(pidx)
|
||||
}
|
||||
|
||||
pub fn saisxx(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) -> Result<(), SuffixError> {
|
||||
if n == 1 {
|
||||
suffix_array[0] = 0;
|
||||
return Ok(());
|
||||
}
|
||||
let fs = 0;
|
||||
suffixsort(string, suffix_array, fs, n, k, false)?;
|
||||
Ok(())
|
||||
}
|
||||
fn _saisxx_bwt(
|
||||
t: &StringT,
|
||||
u: &mut StringT,
|
||||
sa: &mut SArray,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) -> Result<usize, SuffixError> {
|
||||
if n <= 1 {
|
||||
if n == 1 {
|
||||
u[0] = t[0];
|
||||
}
|
||||
return Ok(n);
|
||||
}
|
||||
let mut pidx = suffixsort(t, sa, 0, n, k, true)?;
|
||||
u[0] = t[n - 1];
|
||||
for i in 0..pidx {
|
||||
u[i + 1] = sa[i] as u32;
|
||||
}
|
||||
for i in pidx + 1..n {
|
||||
u[i] = sa[i] as u32
|
||||
}
|
||||
pidx += 1;
|
||||
Ok(pidx)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_induce_sa() {
|
||||
let chars: Vec<_> = "abracadabra".chars().map(|c| c as u32).collect();
|
||||
let mut c = vec![0; 256];
|
||||
let mut b = vec![0; 256];
|
||||
|
||||
let mut sa = vec![0, 0, 3, 5, 7, 0, 0, 0, 0, 0, 0];
|
||||
induce_sa(&chars, &mut sa, &mut b, &mut c, chars.len(), 256);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
|
||||
let mut sa = vec![0, 0, 7, 3, 5, 0, 0, 0, 0, 0, 0];
|
||||
induce_sa(&chars, &mut sa, &mut b, &mut c, chars.len(), 256);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_induce_sa_long() {
|
||||
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.".to_string();
|
||||
let chars: Vec<_> = string.chars().map(|c| c as u32).collect();
|
||||
let mut c = vec![0; 256];
|
||||
let mut b = vec![0; 256];
|
||||
let mut sa = vec![
|
||||
5, 11, 14, 21, 27, 32, 35, 39, 48, 52, 64, 74, 80, 86, 90, 95, 99, 110, 119, 125, 130,
|
||||
135, 141, 145, 152, 157, 160, 168, 176, 181, 183, 190, 193, 198, 202, 212, 215, 218,
|
||||
223, 225, 230, 239, 245, 248, 252, 261, 265, 270, 275, 286, 290, 295, 299, 304, 309,
|
||||
320, 333, 343, 355, 366, 369, 373, 385, 388, 392, 398, 403, 407, 415, 418, 427, 434,
|
||||
445, 451, 457, 467, 471, 476, 485, 490, 498, 509, 518, 523, 529, 539, 549, 558, 561,
|
||||
567, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 396, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 534, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 113, 116, 185, 206, 220, 250, 302,
|
||||
337, 351, 360, 371, 379, 412, 423, 439, 459, 462, 515, 0, 0, 0, 208, 501, 0, 0, 0, 139,
|
||||
204, 234, 313, 358, 479, 542, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 102, 526, 545, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 3, 29, 56, 58, 78, 127, 133, 155, 174, 188, 237, 283, 324, 326, 335,
|
||||
347, 409, 425, 430, 449, 464, 537, 551, 565, 0, 0, 0, 0, 0, 512, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 505, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 16, 42, 45, 61, 137, 171, 257, 329, 340, 381, 400, 442, 487, 503,
|
||||
520, 554, 0, 0, 0, 0, 0, 163, 494, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 268,
|
||||
483, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 122, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
437, 556, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 375,
|
||||
496, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 106, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 104, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
assert_eq!(sa.len(), chars.len());
|
||||
induce_sa(&chars, &mut sa, &mut b, &mut c, chars.len(), 256);
|
||||
assert_eq!(
|
||||
sa,
|
||||
vec![
|
||||
145, 392, 523, 5, 80, 451, 567, 245, 366, 418, 74, 445, 561, 529, 181, 223, 290,
|
||||
157, 48, 198, 467, 90, 239, 286, 275, 434, 490, 21, 119, 309, 343, 130, 270, 183,
|
||||
86, 248, 385, 539, 64, 99, 304, 11, 212, 299, 518, 218, 471, 261, 32, 190, 415,
|
||||
558, 265, 457, 373, 39, 168, 498, 476, 333, 407, 202, 427, 14, 135, 509, 230, 110,
|
||||
252, 27, 125, 35, 95, 141, 295, 388, 403, 215, 176, 193, 225, 52, 320, 355, 160,
|
||||
549, 369, 152, 398, 485, 108, 151, 285, 332, 466, 573, 73, 244, 365, 148, 396, 149,
|
||||
146, 393, 147, 395, 394, 524, 6, 81, 452, 568, 246, 367, 419, 0, 75, 446, 562, 534,
|
||||
530, 182, 224, 531, 462, 337, 439, 220, 535, 185, 351, 291, 206, 158, 49, 199, 468,
|
||||
113, 360, 302, 116, 515, 379, 88, 250, 371, 412, 423, 459, 91, 208, 501, 240, 287,
|
||||
319, 139, 479, 276, 358, 234, 542, 435, 204, 313, 51, 118, 201, 211, 260, 384, 470,
|
||||
364, 115, 491, 545, 22, 120, 526, 67, 102, 38, 98, 140, 144, 197, 222, 229, 274,
|
||||
298, 391, 406, 414, 475, 517, 522, 533, 301, 411, 233, 312, 478, 210, 259, 383,
|
||||
363, 92, 430, 409, 310, 3, 78, 449, 565, 335, 93, 155, 237, 347, 480, 277, 133,
|
||||
174, 537, 551, 283, 464, 56, 324, 492, 344, 425, 420, 431, 58, 326, 131, 29, 127,
|
||||
188, 34, 192, 417, 560, 271, 512, 47, 63, 342, 444, 508, 548, 331, 184, 532, 362,
|
||||
463, 402, 489, 87, 249, 359, 37, 97, 143, 297, 390, 405, 154, 429, 505, 350, 318,
|
||||
282, 520, 235, 16, 386, 137, 540, 65, 100, 45, 61, 340, 442, 506, 546, 329, 338,
|
||||
440, 171, 42, 305, 554, 12, 381, 503, 213, 400, 487, 272, 257, 180, 243, 221, 521,
|
||||
536, 163, 494, 378, 525, 300, 410, 311, 209, 187, 502, 519, 186, 352, 292, 543, 19,
|
||||
268, 353, 483, 4, 10, 79, 85, 450, 456, 566, 572, 219, 336, 207, 236, 24, 122, 472,
|
||||
17, 25, 123, 94, 156, 159, 167, 238, 387, 138, 357, 541, 50, 200, 469, 114, 66,
|
||||
101, 46, 62, 341, 443, 507, 547, 330, 361, 317, 339, 441, 162, 267, 262, 164, 556,
|
||||
437, 172, 348, 43, 481, 306, 278, 217, 294, 308, 33, 191, 416, 559, 511, 179, 242,
|
||||
316, 266, 436, 555, 178, 241, 496, 375, 473, 1, 76, 447, 563, 263, 165, 303, 497,
|
||||
458, 196, 228, 232, 55, 323, 18, 374, 40, 169, 7, 82, 453, 569, 499, 376, 134, 175,
|
||||
538, 205, 422, 117, 474, 516, 477, 2, 77, 334, 408, 448, 564, 281, 41, 170, 380,
|
||||
315, 552, 255, 106, 71, 13, 89, 109, 251, 372, 397, 433, 528, 557, 150, 284, 465,
|
||||
461, 203, 413, 382, 57, 325, 346, 424, 428, 504, 15, 136, 553, 493, 293, 510, 231,
|
||||
460, 345, 111, 69, 104, 8, 83, 454, 570, 253, 31, 129, 214, 247, 264, 289, 368,
|
||||
426, 112, 438, 28, 126, 173, 401, 488, 36, 96, 142, 296, 389, 404, 349, 44, 60,
|
||||
328, 482, 216, 307, 177, 495, 421, 314, 70, 105, 432, 59, 327, 279, 513, 194, 226,
|
||||
53, 321, 500, 544, 377, 9, 84, 455, 571, 23, 121, 356, 161, 280, 254, 527, 68, 103,
|
||||
288, 273, 258, 132, 550, 256, 370, 514, 153, 399, 486, 166, 30, 128, 20, 26, 124,
|
||||
189, 269, 354, 484, 107, 72, 195, 227, 54, 322
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
pub type Bucket = [usize];
|
||||
/// We need to use u32 instead of char, because when we recurse
|
||||
/// we use suffix array elements as ways to replace our original
|
||||
/// string. Using chars can fail. Look for ra variable.
|
||||
pub type StringT = [u32];
|
||||
pub type SArray = [usize];
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SuffixError {
|
||||
InvalidLength,
|
||||
Internal,
|
||||
IntConversion(std::num::TryFromIntError),
|
||||
}
|
||||
|
||||
impl From<std::num::TryFromIntError> for SuffixError {
|
||||
fn from(err: std::num::TryFromIntError) -> Self {
|
||||
Self::IntConversion(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user