Skip to content

Commit fc35b28

Browse files
committed
feat: update general-sam to 0.4.2
1 parent cc15a62 commit fc35b28

3 files changed

Lines changed: 61 additions & 44 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "general-sam-py"
3-
version = "0.3.0"
3+
version = "0.4.2"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "Python bindings for general-sam and some utilities"
@@ -9,13 +9,12 @@ repository = "https://github.com/ModelTC/general-sam-py"
99
readme = "README.md"
1010
authors = ["Chielo Newctle <ChieloNewctle@gmail.com>"]
1111

12-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1312
[lib]
1413
name = "general_sam"
1514
crate-type = ["cdylib"]
1615

1716
[dependencies]
18-
general-sam = "0.3.0"
17+
general-sam = { version = "0.4.2", features = ["all"] }
1918
pyo3 = { version = "0.20.0", features = [
2019
"extension-module",
2120
"abi3-py38",

src/sam.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -136,28 +136,32 @@ impl GeneralSAMState {
136136
);
137137
for_both!(sam_state_and_trie, (sam_state, trie) => {
138138
let tn = trie.get_state(trie_node_id.unwrap_or(trie_rs::TRIE_ROOT_NODE_ID));
139-
sam_state.dfs_along(tn, |event| match event {
140-
TravelEvent::Push((st, tn), key_opt) => Python::with_gil(|py| {
141-
in_stack_callback
142-
.call1(
139+
sam_state.dfs_along(tn, |event| {
140+
match event {
141+
TravelEvent::PushRoot((st, tn)) => Python::with_gil(|py| {
142+
in_stack_callback.call1(
143143
py,
144144
(
145145
GeneralSAMState(self.0.clone(), st.node_id),
146146
tn.node_id,
147-
key_opt,
147+
None::<()>,
148148
),
149149
)
150-
.map(|_| ())
151-
})
152-
.map(|_| ()),
153-
TravelEvent::Pop((st, tn)) => Python::with_gil(|py| {
154-
out_stack_callback
155-
.call1(
150+
}),
151+
TravelEvent::Push((st, tn), _, key) => Python::with_gil(|py| {
152+
in_stack_callback.call1(
153+
py,
154+
(GeneralSAMState(self.0.clone(), st.node_id), tn.node_id, key),
155+
)
156+
}),
157+
TravelEvent::Pop((st, tn), _) => Python::with_gil(|py| {
158+
out_stack_callback.call1(
156159
py,
157160
(GeneralSAMState(self.0.clone(), st.node_id), tn.node_id),
158161
)
159-
.map(|_| ())
160-
}),
162+
}),
163+
}
164+
.map(|_| ())
161165
})
162166
})
163167
}
@@ -177,28 +181,32 @@ impl GeneralSAMState {
177181
);
178182
for_both!(sam_state_and_trie, (sam_state, trie) => {
179183
let tn = trie.get_state(trie_node_id.unwrap_or(trie_rs::TRIE_ROOT_NODE_ID));
180-
sam_state.bfs_along(tn, |event| match event {
181-
TravelEvent::Push((st, tn), key_opt) => Python::with_gil(|py| {
182-
in_stack_callback
183-
.call1(
184+
sam_state.bfs_along(tn, |event| {
185+
match event {
186+
TravelEvent::PushRoot((st, tn)) => Python::with_gil(|py| {
187+
in_stack_callback.call1(
184188
py,
185189
(
186190
GeneralSAMState(self.0.clone(), st.node_id),
187191
tn.node_id,
188-
key_opt,
192+
None::<()>,
189193
),
190194
)
191-
.map(|_| ())
192-
})
193-
.map(|_| ()),
194-
TravelEvent::Pop((st, tn)) => Python::with_gil(|py| {
195-
out_stack_callback
196-
.call1(
195+
}),
196+
TravelEvent::Push((st, tn), _, key) => Python::with_gil(|py| {
197+
in_stack_callback.call1(
198+
py,
199+
(GeneralSAMState(self.0.clone(), st.node_id), tn.node_id, key),
200+
)
201+
}),
202+
TravelEvent::Pop((st, tn), _) => Python::with_gil(|py| {
203+
out_stack_callback.call1(
197204
py,
198205
(GeneralSAMState(self.0.clone(), st.node_id), tn.node_id),
199206
)
200-
.map(|_| ())
201-
}),
207+
}),
208+
}
209+
.map(|_| ())
202210
})
203211
})
204212
}

src/trie.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Trie {
9393
let mut res = Vec::new();
9494
state
9595
.bfs_travel(|event| -> Result<(), Infallible> {
96-
if let TravelEvent::Push(s, _) = event {
96+
if let TravelEvent::Push(s, _, _) = event {
9797
res.push(s.node_id);
9898
}
9999
Ok(())
@@ -129,14 +129,19 @@ impl Trie {
129129
if root_state.is_nil() {
130130
return Ok(());
131131
}
132-
root_state.dfs_travel(|event| match event {
133-
TravelEvent::Push(tn, key_opt) => Python::with_gil(|py| {
134-
in_stack_callback.call1(py, (tn.node_id, key_opt))
135-
})
136-
.map(|_| ()),
137-
TravelEvent::Pop(tn) => {
138-
Python::with_gil(|py| out_stack_callback.call1(py, (tn.node_id,))).map(|_| ())
132+
root_state.dfs_travel(|event| {
133+
match event {
134+
TravelEvent::PushRoot(tn) => {
135+
Python::with_gil(|py| in_stack_callback.call1(py, (tn.node_id, None::<()>)))
136+
}
137+
TravelEvent::Push(tn, _, key) => {
138+
Python::with_gil(|py| in_stack_callback.call1(py, (tn.node_id, key)))
139+
}
140+
TravelEvent::Pop(tn, _) => {
141+
Python::with_gil(|py| out_stack_callback.call1(py, (tn.node_id,)))
142+
}
139143
}
144+
.map(|_| ())
140145
})
141146
})
142147
}
@@ -153,14 +158,19 @@ impl Trie {
153158
if root_state.is_nil() {
154159
return Ok(());
155160
}
156-
root_state.bfs_travel(|event| match event {
157-
TravelEvent::Push(tn, key_opt) => Python::with_gil(|py| {
158-
in_stack_callback.call1(py, (tn.node_id, key_opt))
159-
})
160-
.map(|_| ()),
161-
TravelEvent::Pop(tn) => {
162-
Python::with_gil(|py| out_stack_callback.call1(py, (tn.node_id,))).map(|_| ())
161+
root_state.bfs_travel(|event| {
162+
match event {
163+
TravelEvent::PushRoot(tn) => {
164+
Python::with_gil(|py| in_stack_callback.call1(py, (tn.node_id, None::<()>)))
165+
}
166+
TravelEvent::Push(tn, _, key) => {
167+
Python::with_gil(|py| in_stack_callback.call1(py, (tn.node_id, key)))
168+
}
169+
TravelEvent::Pop(tn, _) => {
170+
Python::with_gil(|py| out_stack_callback.call1(py, (tn.node_id,)))
171+
}
163172
}
173+
.map(|_| ())
164174
})
165175
})
166176
}

0 commit comments

Comments
 (0)