Flow
This commit is contained in:
parent
cb125bf64e
commit
d55e5fdb68
5 changed files with 57 additions and 8 deletions
|
@ -4,7 +4,7 @@ use std::cmp::Reverse;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
struct Adjacencies<'a> {
|
pub struct Adjacencies<'a> {
|
||||||
edges: &'a HashMap<Address, Vec<Edge>>,
|
edges: &'a HashMap<Address, Vec<Edge>>,
|
||||||
lazy_adjacencies: HashMap<Node, HashMap<Node, U256>>,
|
lazy_adjacencies: HashMap<Node, HashMap<Node, U256>>,
|
||||||
capacity_adjustments: HashMap<Node, HashMap<Node, U256>>,
|
capacity_adjustments: HashMap<Node, HashMap<Node, U256>>,
|
||||||
|
@ -42,12 +42,12 @@ impl<'a> Adjacencies<'a> {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn adjust_capacity(&mut self, from: Node, to: Node, adjustment: U256) {
|
pub fn adjust_capacity(&mut self, from: &Node, to: &Node, adjustment: U256) {
|
||||||
*self
|
*self
|
||||||
.capacity_adjustments
|
.capacity_adjustments
|
||||||
.entry(from)
|
.entry(from.clone())
|
||||||
.or_default()
|
.or_default()
|
||||||
.entry(to)
|
.entry(to.clone())
|
||||||
.or_default() += adjustment;
|
.or_default() += adjustment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
mod adjacencies;
|
use crate::types::{Address};
|
||||||
|
|
||||||
use crate::types::{Address, Edge, U256};
|
mod adjacencies;
|
||||||
|
mod flow;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||||
enum Node {
|
enum Node {
|
||||||
Node(Address),
|
Node(Address),
|
||||||
TokenEdge(Address, Address),
|
TokenEdge(Address, Address),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub use crate::flow::flow::compute_flow;
|
||||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -1,8 +1,21 @@
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
|
||||||
mod flow;
|
mod flow;
|
||||||
mod io;
|
mod io;
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
|
use types::Address;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let edges = io::read_edges_binary(&String::from("./edges.dat")).expect("Error loading edges.");
|
let args: Vec<String> = env::args().collect();
|
||||||
|
if args.len() != 4 {
|
||||||
|
panic!("Expected three arguments");
|
||||||
|
}
|
||||||
|
let (from_str, to_str, edges_file) = (&args[1], &args[2], &args[3]);
|
||||||
|
|
||||||
|
println!("Computing flow {from_str} -> {to_str} using {edges_file}");
|
||||||
|
let edges = io::read_edges_binary(edges_file).expect("Error loading edges.");
|
||||||
print!("Read {} edges", edges.len());
|
print!("Read {} edges", edges.len());
|
||||||
|
flow::compute_flow(&Address::from(from_str.as_str()), &Address::from(to_str.as_str()), &edges);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,3 +6,15 @@ impl From<[u8; 20]> for Address {
|
||||||
Address(item)
|
Address(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Address {
|
||||||
|
fn from(item: &str) -> Self {
|
||||||
|
assert!(item.starts_with("0x"));
|
||||||
|
assert!(item.len() == 2 + 20 * 2);
|
||||||
|
let mut data = [0u8; 20];
|
||||||
|
for i in (2..item.len()).step_by(2) {
|
||||||
|
data[i / 2 - 1] = u8::from_str_radix(&item[i..i + 2], 16).unwrap();
|
||||||
|
}
|
||||||
|
Address(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
use std::ops::{Add, AddAssign};
|
use std::ops::{Add, AddAssign, Neg, Sub, SubAssign};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct U256([u128; 2]);
|
pub struct U256([u128; 2]);
|
||||||
|
@ -70,6 +70,27 @@ impl AddAssign for U256 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Neg for U256 {
|
||||||
|
type Output = Self;
|
||||||
|
fn neg(self) -> Self {
|
||||||
|
let result = U256([!self.0[0], !self.0[1]]);
|
||||||
|
result + U256::from(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub for U256 {
|
||||||
|
type Output = Self;
|
||||||
|
fn sub(self, rhs: Self) -> Self {
|
||||||
|
self + (-rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SubAssign for U256 {
|
||||||
|
fn sub_assign(&mut self, rhs: Self) {
|
||||||
|
*self = *self - rhs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Display for U256 {
|
impl Display for U256 {
|
||||||
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
|
||||||
if self.0[0] == 0 {
|
if self.0[0] == 0 {
|
||||||
|
|
Loading…
Reference in a new issue