Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
lorepozo committed May 26, 2017
1 parent 092af9b commit a2bbe17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
68 changes: 33 additions & 35 deletions src/ec.rs
Expand Up @@ -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)
}

Expand All @@ -260,7 +260,7 @@ fn primitives() -> HashSet<String> {
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);
Expand Down Expand Up @@ -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<String>)>) -> HashMap<String, usize> {
ctx.into_iter()
Expand All @@ -317,12 +314,12 @@ fn exprs_in_context(ctx: Vec<(usize, &'static str, Rc<String>)>) -> HashMap<Stri
.collect()
}

/// find_exprs_in_context takes a set of items in the context as given by
/// Context::get() or Context::explore() and a vector of combinators.
/// It returns a vector of the same size as exprs, with Some(id) if a match
/// `find_exprs_in_context` takes a set of items in the context as given by
/// `Context::get()` or `Context::explore()` and a vector of combinators.
/// It returns a vector of the same size as exprs, with `Some(id)` if a match
/// was found or None otherwise.
fn find_exprs_in_context(ctx: Vec<(usize, &'static str, Rc<String>)>,
exprs: &Vec<&String>)
exprs: &[&str])
-> Vec<Option<usize>> {
let exprs_in_ctx = exprs_in_context(ctx);
exprs
Expand All @@ -334,12 +331,12 @@ fn find_exprs_in_context(ctx: Vec<(usize, &'static str, Rc<String>)>,
.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<String>)>,
expr: String)
expr: &str)
-> Option<usize> {
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
Expand Down Expand Up @@ -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)
})
Expand All @@ -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())
Expand Down Expand Up @@ -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());
Expand Down
25 changes: 12 additions & 13 deletions src/knowledge.rs
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}

Expand All @@ -341,7 +340,7 @@ impl Network {
let frontier: HashSet<usize> = items
.iter()
.flat_map(|&id| {
let ref item = net.graph[id];
let item = &net.graph[id];
item.adj.clone()
})
.collect();
Expand Down Expand Up @@ -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();
Expand All @@ -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> {
Expand Down

0 comments on commit a2bbe17

Please sign in to comment.