Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: cargo
directory: /
schedule:
interval: weekly
open-pull-requests-limit: 5
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI

on:
push:
pull_request:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt
- run: cargo fmt --all -- --check
- run: cargo clippy --all-targets --all-features -- -D warnings
- run: cargo test --all-features
- run: cargo bench --no-run
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[package]
name = "common_substrings"
description = "Finding all common strings"
description = "Find common substrings across a collection of strings"
version = "1.0.0"
authors = ["hanwencheng <heawen.cheng@gmail.com>"]
edition = "2018"
edition = "2024"
rust-version = "1.86"
license = "Apache-2.0"
readme = "README.md"
homepage = "https://github.com/hanwencheng/common_substrings_rust"
Expand All @@ -12,7 +13,7 @@ repository = "https://github.com/hanwencheng/common_substrings_rust"
[dependencies]

[dev-dependencies]
criterion = "0.3"
criterion = "0.8.2"

[[bench]]
name = "benchmark"
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Find all common substrings

![versom](https://img.shields.io/crates/v/common_substrings)
![crates.io version](https://img.shields.io/crates/v/common_substrings)

A method for finding all common strings. Check it on [Crates.io](https://crates.io/crates/common_substrings).

The algorithms uses a two dimension trie to get all the fragment. The vertical one is the standard suffix trie, but all the node of the last word in each suffix is linked, which I call them virtually horizontally linked.
The algorithm uses a two-dimensional trie to find the fragments. The vertical dimension is a standard suffix trie; nodes at the end of each suffix are also linked horizontally.

## Usage

Use the function `get_substrings` to get all the common strings in the strings list,
Use `get_substrings` to get the common substrings in a list of strings.

### Example
```rust
Expand All @@ -17,7 +17,7 @@ let input_strings = vec!["java", "javascript", "typescript", "coffeescript", "co
let result_substrings = get_substrings(input_strings, 2, 3);
```

which gives the result list of
This produces results such as:
```shell
Substring(sources: {2, 3}, name: escript, weight: 14)
Substring(sources: {1, 0}, name: java, weight: 8)
Expand All @@ -26,9 +26,13 @@ Substring(sources: {4, 3}, name: coffee, weight: 12)

### Arguments

* `input` - The target input string vector.
* `min_occurrences` The minimal occurrence of the captured common substrings.
* `min_length` The minimal length of the captured common substrings.
* `input` — The input strings.
* `min_occurrences` — The minimum number of input strings containing a result.
* `min_length` — The minimum result length, measured in Unicode scalar values.

Both thresholds must be positive. When `min_occurrences` exceeds the input size, the result is empty.

The minimum supported Rust version is 1.86.

## Algorithm

Expand Down
35 changes: 22 additions & 13 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
use criterion::{criterion_group, criterion_main, Criterion};
use common_substrings::get_substrings;
use criterion::{Criterion, criterion_group, criterion_main};
use std::hint::black_box;

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("get substring", |b| b.iter(|| get_substrings(vec![
"java",
"offe",
"coffescript",
"typescript",
"typed",
"javacoffie",
"fessss",
"fe",
], 2, 3)));
fn criterion_benchmark(criterion: &mut Criterion) {
criterion.bench_function("get substring", |bencher| {
bencher.iter(|| {
get_substrings(
black_box(vec![
"java",
"offe",
"coffescript",
"typescript",
"typed",
"javacoffie",
"fessss",
"fe",
]),
2,
3,
)
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
criterion_main!(benches);
Loading