Coder Social home page Coder Social logo

hubcap's People

Watchers

 avatar

hubcap's Issues

dfs

use std::collections::HashMap;
use std::collections::LinkedList;

fn main(){
// create graph: adjcency list
/*
0: 1,2
1:
2: 3
3:
*/
let mut graph :HashMap<String, Vec> = HashMap::new();
let mut path :LinkedList = LinkedList::new();

graph.insert("0".to_string(), vec!["1".to_string(),"2".to_string()]);
graph.insert("1".to_string(), vec![]);
graph.insert("2".to_string(), vec!["3".to_string()]);
graph.insert("3".to_string(), vec![]);

// search graph to find if node a is reachable by node b
//get start value
let current = "0".to_string();
let destination = "3".to_string();
println!("starting point: {}", current);

// visit the first node,
// graph: is borrowed, dont want to return it to access it again
// current: is borrowed, dont want to return it to access it again
// path: is borrowed and mutable, should change, we are returning it
// destination: is borrowed, dont want to return it to access it again
let solution = visit(&graph, &current, &mut path, &destination);

print!("{:?}", solution)
}

// graph: is borrowed, dont want to return it to access it again
// current: is borrowed, dont want to return it to access it again
// path: is borrowed and mutable, should change, we are returning it
// destination: is borrowed, dont want to return it to access it again
// 'a lifetime of the return type is the same as the graph itself
fn visit<'a>(graph: &'a HashMap<String, Vec>,
node: &String,
mut path: &'a mut LinkedList,
destination: &String) -> &'a mut LinkedList {

println!("visiting {}, with path {:?}", node, path);

// borrowed pointer to the list of neighbors
let neighbors: &Vec = graph.get("0").unwrap();

// clone the current node and add it to the path, path now owns it own copy
path.push_back(node.clone());

//
for neighbor in neighbors {
path = visit(&graph, &neighbor, path, &destination);

if path.len() > 0 {
  if path.back().unwrap() == destination {
    return path;
  }
}

}
path.pop_back();
return path;
}

multithreaded is prime

use std::time::SystemTime;
use std::thread;
use std::thread::JoinHandle;

fn is_prime(n:&u32) -> bool{
if *n <= 2 {
return false;
}
for i in 2..n/2 + 1 {
if n % i == 0 {
return false;
}
}
return true;
}

fn main(){

let max_val:u32 = 10000;

for num_of_groups in 1..6 {
    let now = SystemTime::now();



  // group the numbers between 0 and 10000 into 10 groups
    let mut handles: Vec<JoinHandle<()>> = Vec::with_capacity(usize::try_from(num_of_groups)
                                                            .unwrap());


    let values:Vec<u32> = (0..max_val).map(|x| x).collect();
  
    // println!("{} values", values.len());

  
    let groups:Vec<Vec<u32>> = values.chunks(usize::try_from(max_val / num_of_groups).unwrap())
        .map(|x| x.to_vec())
        .collect();
    
    // println!("{} {} groups", groups.len(), usize::try_from(num_of_groups).unwrap());

  
  for group in groups {
    let func = || {
      for val in group {
        let n:u32 = u32::from(val);
        // println!("{} : {}",val,is_prime(&n));
        is_prime(&n);
      }
    };

    let handle = thread::spawn(func);
    handles.push(handle);
  }
  for n in handles {
    n.join().unwrap();
    // println!("thread done");
  }
    
  println!("group size: {} took : {} millis",num_of_groups, now.elapsed().unwrap().as_millis());

}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.