Feature: Now a library as well as a binary

This commit is contained in:
Arlo Filley 2024-05-02 19:08:36 +01:00
parent f54794bdfe
commit 1acbeaf647
3 changed files with 36 additions and 29 deletions

View File

@ -1,8 +1,14 @@
[package] [package]
name = "page_rank" name = "page_rank"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [[bin]]
name = "page_ranker"
path = "src/bin/main.rs"
[lib]
name = "page_rank"
path = "src/lib/lib.rs"
[dependencies] [dependencies]

28
src/bin/main.rs Normal file
View File

@ -0,0 +1,28 @@
use page_rank::Graph;
fn main() {
let mut pages: Graph<char> = Graph::new();
pages.add_page('A');
pages.add_page('B');
pages.add_page('C');
pages.add_page('D');
pages.add_page('E');
pages.add_link('A', 'E');
pages.add_link('B', 'A');
pages.add_link('B', 'D');
pages.add_link('B', 'E');
pages.add_link('C', 'B');
pages.add_link('D', 'C');
pages.add_link('E', 'D');
// println!("{pages:?}");
pages.page_rank(40);
for (page, score) in pages.get_sorted_scores() {
println!("{page} => {score:.2}")
}
}

View File

@ -1,32 +1,5 @@
use std::{collections::HashMap, hash::Hash}; use std::{collections::HashMap, hash::Hash};
fn main() {
let mut pages: Graph<char> = Graph::new();
pages.add_page('A');
pages.add_page('B');
pages.add_page('C');
pages.add_page('D');
pages.add_page('E');
pages.add_link('A', 'E');
pages.add_link('B', 'A');
pages.add_link('B', 'D');
pages.add_link('B', 'E');
pages.add_link('C', 'B');
pages.add_link('D', 'C');
pages.add_link('E', 'D');
// println!("{pages:?}");
pages.page_rank(40);
for (page, score) in pages.get_sorted_scores() {
println!("{page} => {score:.2}")
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Graph<T> { pub struct Graph<T> {
nodes: HashMap<T, Node<T>>, nodes: HashMap<T, Node<T>>,