Skip to content

Commit

Permalink
Merge pull request #41 from l-monninger/main
Browse files Browse the repository at this point in the history
Adds an example of bfs with inlines.
  • Loading branch information
l-monninger authored Jul 13, 2023
2 parents 2d203be + 3831943 commit a241bc4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/citest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
container:
image: public.ecr.aws/c4i6k4r8/movement-dev:latest
steps:
- run: ./abin/citest.sh
- run: ./bin/maintainer/citest.sh
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
85 changes: 60 additions & 25 deletions examples/movement/data_structures/sources/graph.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

module ds_std::graph {
// use std::vector;
use std::vector;
use std::option::{ Self, Option };
// use std::bcs;
use ds_std::oa_hash_map::{ Self, OaHashMap, Entry };
Expand Down Expand Up @@ -56,33 +56,49 @@ module ds_std::graph {

}


/*public fun dfs_preorder<V>(
public inline fun bfs<V : copy + drop>(
graph : &Graph<V>,
path : &mut vector<
Entry<
u64, // store the which direction we left off at
Option<Entry<V, V>> // store the value
>
>,
direction : u64
): (u64, vector<Option<Entry<V, V>>>, Option<Entry<V, V>>) {
let len = vector::length(path);
if (len == 0) {
return (0, vector::empty(), option::none())
};
if (direction > len){ // I've we reached the end of this depth, pop back
vector::pop_back(&mut path);
}
let last = vector::borrow(path, len - 1);
return last
start : &V,
f: |&V|
){

let queue = vector::empty<V>();
vector::push_back(&mut queue, *start);
let visited = oa_hash_map::new<V, bool>(graph.size);

while (!vector::is_empty(&queue)) {

let node = vector::remove(&mut queue, 0);
if (oa_hash_map::contains(&visited, &node)) {
continue
};

f(&node);
oa_hash_map::set(&mut visited, node, true);

if (!oa_hash_map::contains(&graph.adj, &node)) {
continue
};

let adj = borrow_adj(graph, &node);
let i = 0;
loop {
let (ni, value) = oa_hash_map::iter_next(adj, i);
if (option::is_none(value)) {
break
};
i = ni;
let value = oa_hash_map::get_value(option::borrow(value));
if (!oa_hash_map::contains(&visited, value)) {
vector::push_back(&mut queue, *value);
};
};

}*/
};


}

#[test]
public fun test_insert(){

Expand Down Expand Up @@ -142,5 +158,24 @@ module ds_std::graph {

}

#[test]
public fun test_bfs_next() {


let count = 0;
let graph = new<u8>(10);
let la = 1;
let ny = 2;
let sf = 3;
insert_adj(&mut graph, la, ny);
insert_adj(&mut graph, la, sf);
bfs(&graph, &la, |node| {
let _ = node;
count = count + 1;
});
assert!(count == 3, 99);

}


}

0 comments on commit a241bc4

Please sign in to comment.