Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

81 #92

Closed
wants to merge 2 commits into from
Closed

81 #92

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 19 additions & 38 deletions src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{ConstRelay, DeadRelay, LambdaRelay, Relay, Sodg};
use anyhow::{anyhow, Result};
use log::trace;
use std::collections::VecDeque;
use std::str::FromStr;

use anyhow::{anyhow, Result};
use log::trace;

use crate::{ConstRelay, DeadRelay, LambdaRelay, Relay, Sodg};

impl Relay for ConstRelay {
fn re(&self, _v: u32, _a: &str) -> Result<String> {
Ok(self.s.clone())
Expand Down Expand Up @@ -124,36 +126,9 @@ impl Sodg {
}
}
#[allow(clippy::let_and_return)]
let v = self.find_with_indent(v1, loc, relay, 0);
#[cfg(feature = "sober")]
{
let cp = self as *const Self;
let mp = cp as *mut Self;
unsafe {
(&mut *mp).finds.remove(&badge);
}
}
v
}

/// Find a vertex, printing the log with an indentation prefix.
///
/// This function is used only by [`Sodg::find].
fn find_with_indent<T: Relay>(
&self,
v1: u32,
loc: &str,
relay: &T,
depth: usize,
) -> Result<u32> {
#[cfg(feature = "sober")]
{
if depth > 16 {
return Err(anyhow!("The depth {depth} is too big"));
}
}
let mut v = v1;
let mut locator: VecDeque<String> = VecDeque::new();
let depth = 0;
loc.split('.')
.filter(|k| !k.is_empty())
.for_each(|k| locator.push_back(k.to_string()));
Expand Down Expand Up @@ -195,12 +170,9 @@ impl Sodg {
trace!("#find(ν{v1}, {loc}): {indent}calling relay(ν{v}, {k})...");
let fault = match relay.re(v, &k) {
Ok(re) => {
if let Ok(to) = self.find_with_indent(v, re.as_str(), relay, depth + 1) {
trace!("#find(ν{v1}, {loc}): {indent}ν{v}.{k} relayed to ν{to} (re: {re})");
v = to;
continue;
}
format!("re to '{re}' didn't help")
trace!("#find(ν{v1}, {loc}): {indent}ν{v}.{k} relayed to {re}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@l3r8yJ since we don't have recursion, no need to use indent, I believe

locator.push_front(re);
continue;
}
Err(err) => {
trace!(
Expand All @@ -214,7 +186,16 @@ impl Sodg {
return Err(anyhow!("Can't find .{k} in {}: ({fault})", self.v_print(v)));
}
trace!("#find(ν{v1}, {loc}): {indent}found ν{v} in {jumps} jumps");
Ok(v)
let found = Ok(v);
#[cfg(feature = "sober")]
{
let cp = self as *const Self;
let mp = cp as *mut Self;
unsafe {
(&mut *mp).finds.remove(&badge);
}
}
found
}
}

Expand Down