diff --git a/src/ec.rs b/src/ec.rs index ef842d6..3c2cbc0 100644 --- a/src/ec.rs +++ b/src/ec.rs @@ -242,9 +242,9 @@ fn ec_bin() -> String { } } -/// if STORE_INPUTS is true, this is where the inputs are saved. +/// if `STORE_INPUTS` is true, this is where the inputs are saved. fn store_input_path(i: u64) -> String { - let store_dir = env::var("EC_STORAGE").unwrap_or(String::from("ec_storage")); + let store_dir = env::var("EC_STORAGE").unwrap_or_else(|_| String::from("ec_storage")); format!("{}/{}_{}.json", store_dir, STORE_FILENAME_PREFIX, i) } @@ -260,7 +260,7 @@ fn primitives() -> HashSet { PRIMS_ARR.iter().map(|&s| String::from(s)).collect() } -/// run_ec is the lower-level function that produces the ec results for a +/// `run_ec` is the lower-level function that produces the ec results for a /// given context and course iteration. fn run_ec(ctx: &Context, i: u64) -> Results { let mut c = Course::load(i); @@ -290,20 +290,17 @@ fn run_ec(ctx: &Context, i: u64) -> Results { } let raw_results = String::from_utf8(output.stdout).expect("read ec output"); if LOG_LEVEL & 4 != 0 { - let err = match output.stderr.is_empty() { - true => String::from(""), - false => { - let raw_err = String::from_utf8(output.stderr).expect("read ec err"); - format!("EC ERROR:\n{}\n", raw_err) - } + let err = if output.stderr.is_empty() { String::from("") } else { + let raw_err = String::from_utf8(output.stderr).expect("read ec err"); + format!("EC ERROR:\n{}\n", raw_err) }; println!("{}EC OUTPUT:\n{}", err, raw_results) } Results::from_string(raw_results) } -/// exprs_in_context takes a set of items in the context as given by -/// Context::get() or Context::explore() and returns the combinators +/// `exprs_in_context` takes a set of items in the context as given by +/// `Context::get()` or `Context::explore()` and returns the combinators /// contained in those that are readable by ec. fn exprs_in_context(ctx: Vec<(usize, &'static str, Rc)>) -> HashMap { ctx.into_iter() @@ -317,12 +314,12 @@ fn exprs_in_context(ctx: Vec<(usize, &'static str, Rc)>) -> HashMap)>, - exprs: &Vec<&String>) + exprs: &[&str]) -> Vec> { let exprs_in_ctx = exprs_in_context(ctx); exprs @@ -334,12 +331,12 @@ fn find_exprs_in_context(ctx: Vec<(usize, &'static str, Rc)>, .collect() } -/// find_expr_in_context is like find_exprs_in_context but for a single -/// combinator. +/// `find_expr_in_context` is like `find_exprs_in_context` but for a +/// single combinator. fn find_expr_in_context(ctx: Vec<(usize, &'static str, Rc)>, - expr: String) + expr: &str) -> Option { - find_exprs_in_context(ctx, &vec![&expr])[0] + find_exprs_in_context(ctx, &[expr])[0] } /// mech is the ec mechanism as it should be registered/used by an Skn @@ -379,7 +376,7 @@ pub fn mech(ctx: Context, i: u64) { .iter() .filter(|t| t.result.is_some()) .map(|t| { - let ref r = t.result; + let r = &t.result; let r = r.clone().unwrap(); (r.expr, r.log_probability) }) @@ -393,25 +390,26 @@ pub fn mech(ctx: Context, i: u64) { // orient to most probable comb let mut ctx = ctx; - let most_probable = learned - .iter() - .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap()) - .unwrap() - .0 - .clone(); - let result = find_expr_in_context(ctx.explore(), most_probable); - if let Some(id) = result { - if LOG_LEVEL & 2 != 0 { - println!(" ctx.orient({})", id); + { + let most_probable = &learned + .iter() + .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap()) + .unwrap() + .0; + let result = find_expr_in_context(ctx.explore(), most_probable); + if let Some(id) = result { + if LOG_LEVEL & 2 != 0 { + println!(" ctx.orient({})", id); + } + ctx.orient(id); + ctx = ctx.update(); } - ctx.orient(id); - ctx = ctx.update(); } // make accesses ~ usage learned.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); // reversed sort - let exprs = &learned.iter().map(|&(ref s, _)| s).collect(); - let findings = find_exprs_in_context(ctx.get(), exprs); + let exprs: Vec<&str> = learned.iter().map(|l| l.0.as_str()).collect(); + let findings = find_exprs_in_context(ctx.get(), &exprs); let mut access_info: Vec<(&String, f64, usize)> = learned .iter() .zip(findings.into_iter()) @@ -443,7 +441,7 @@ pub fn mech(ctx: Context, i: u64) { .map(|&(ref s, _)| s) .filter(|&s| !prims.contains(s) && !exprs_in_ctx.contains_key(s)) .take(EC_MAX_IN_ARTIFACT) - .map(|s| s.clone()) + .cloned() .collect(); if !new_combs.is_empty() { ctx.grow(json!(new_combs).to_string()); diff --git a/src/knowledge.rs b/src/knowledge.rs index f8881bd..106a53b 100644 --- a/src/knowledge.rs +++ b/src/knowledge.rs @@ -166,7 +166,7 @@ impl Network { let mut edges = edges.clone(); edges.remove(&id); let item = Item::new(mech, edges, data, id); - id = id + 1; + id += 1; item }) .collect(); @@ -186,7 +186,7 @@ impl Network { fn item_count(&self, epoch: usize, id: usize, count: u64) { let mut net = self.net.borrow_mut(); { - let ref mut item = net.graph[id]; + let item = &mut net.graph[id]; item.add_count(epoch, count); } net.epochs[epoch].2.insert(id); @@ -200,7 +200,7 @@ impl Network { items .into_iter() .map(move |id| { - let ref item = net.graph[id]; + let item = &net.graph[id]; (id, item.mech, item.data.clone()) }) .collect() @@ -245,19 +245,18 @@ impl Network { // truncate less-used items let take = size - ctx.len(); ext.sort_by_key(|&id| { - let ref item = net.graph[id]; + let item = &net.graph[id]; -(item.adj.len() as i64) }); ctx.extend(ext.iter().take(take)); break; } - selected = ext.iter() + selected = *ext.iter() .max_by_key(|&id| { - let ref item = net.graph[*id]; + let item = &net.graph[*id]; item.recent_count(epoch) }) - .unwrap() - .clone(); + .unwrap(); ctx.extend(ext); } } @@ -282,7 +281,7 @@ impl Network { let mut sum = 0; let counts: HashSet<(usize, u64)> = ids.iter() .map(|&id| { - let ref item = net.graph[id]; + let item = &net.graph[id]; let count = 1 + item.recent_count(epoch); sum += count; (id, count) @@ -323,7 +322,7 @@ impl Network { // update other end of new edges (undirected network) for oid in &edges { - let ref mut item = net.graph[*oid]; + let item = &mut net.graph[*oid]; item.adj.insert(id); } @@ -341,7 +340,7 @@ impl Network { let frontier: HashSet = items .iter() .flat_map(|&id| { - let ref item = net.graph[id]; + let item = &net.graph[id]; item.adj.clone() }) .collect(); @@ -370,7 +369,7 @@ impl Network { let net = self.net.borrow(); let mut body = String::new(); for id in 0..net.graph.len() { - let label = format!("id={} {}", id, &net.graph[id].data.as_str().clone()); + let label = format!("id={} {}", id, (*net.graph[id].data).clone()); body.push_str(format!(" N{} [shape=box,label={:?}];\n", id, label).as_str()); } body.pop(); @@ -396,7 +395,7 @@ impl Network { } } -/// MechanismRegistry maintains a set of mechanisms used by the knowledge +/// `MechanismRegistry` maintains a set of mechanisms used by the knowledge /// network. A mechanism is a function which takes a Context and an /// iteration number. struct MechanismRegistry<'a> {