Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO_STD #370

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ debug = true
[dependencies]
fixedbitset = { version = "0.3.0", default-features = false }
quickcheck = { optional = true, version = "0.8", default-features = false }
indexmap = { version = "1.0.2" }
indexmap = { version = "1.5.1" }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
hashbrown = { version = "0.8.2", features = ["ahash"]}

[dev-dependencies]
rand = "0.5.5"
rand = { version = "0.7", default-features = false, features = ["alloc"] }
odds = { version = "0.2.19" }
defmac = "0.1"
itertools = { version = "0.8", default-features = false }

[features]
default = ["graphmap", "stable_graph", "matrix_graph"]
default = [ "std", "graphmap", "stable_graph", "matrix_graph" ]
std = []
graphmap = []
serde-1 = ["serde", "serde_derive"]
stable_graph = []
Expand Down
3 changes: 2 additions & 1 deletion src/algo/dominators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
//! dominates **C** and **C** dominates **B**.

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet, hash_map::Iter};
use std::hash::Hash;

use crate::lib::*;

use crate::visit::{DfsPostOrder, GraphBase, IntoNeighbors, Visitable, Walker};

/// The dominance relation for some graph and root.
Expand Down
5 changes: 3 additions & 2 deletions src/algo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
pub mod dominators;

use std::cmp::min;
use std::collections::{BinaryHeap, HashMap};

use crate::prelude::*;

use crate::lib::*;

use super::graph::IndexType;
use super::unionfind::UnionFind;
use super::visit::{
Expand Down Expand Up @@ -846,7 +847,7 @@ where
red.visit(start);
let mut blue = g.visit_map();

let mut stack = ::std::collections::VecDeque::new();
let mut stack = VecDeque::new();
stack.push_front(start);

while let Some(node) = stack.pop_front() {
Expand Down
4 changes: 2 additions & 2 deletions src/astar.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{BinaryHeap, HashMap};

use std::hash::Hash;

use crate::lib::*;

use super::visit::{EdgeRef, GraphBase, IntoEdges, VisitMap, Visitable};
use crate::scored::MinScored;

Expand Down
3 changes: 3 additions & 0 deletions src/csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::marker::PhantomData;
use std::ops::{Index, IndexMut, Range};
use std::slice::Windows;


use crate::lib::*;

use crate::visit::{Data, GraphProp, IntoEdgeReferences, NodeCount};
use crate::visit::{EdgeRef, GraphBase, IntoEdges, IntoNeighbors, NodeIndexable};
use crate::visit::{IntoNodeIdentifiers, NodeCompactIndexable, Visitable};
Expand Down
4 changes: 4 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use crate::visit::{Data, NodeCount, NodeIndexable, Reversed};
use crate::EdgeType;
use crate::Graph;


use crate::lib::*;


trait_template! {
/// Access node and edge weights (associated data).
pub trait DataMap : Data {
Expand Down
5 changes: 2 additions & 3 deletions src/dijkstra.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{BinaryHeap, HashMap};

use std::hash::Hash;

use crate::lib::*;

use super::visit::{EdgeRef, IntoEdges, VisitMap, Visitable};
use crate::algo::Measure;
use crate::scored::MinScored;
Expand Down
3 changes: 3 additions & 0 deletions src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use crate::visit::{
NodeIndexable, NodeRef,
};


use crate::lib::*;

/// `Dot` implements output to graphviz .dot format for a graph.
///
/// Formatting and options are rather simple, this is mostly intended
Expand Down
2 changes: 2 additions & 0 deletions src/graph_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::mem::size_of;
use std::ops::{Index, IndexMut, Range};
use std::slice;

use crate::lib::*;

use crate::{Directed, Direction, EdgeType, Incoming, IntoWeightedEdge, Outgoing, Undirected};

use crate::iter_format::{DebugMap, IterFormatExt, NoPretty};
Expand Down
3 changes: 3 additions & 0 deletions src/graph_impl/stable_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::mem::size_of;
use std::ops::{Index, IndexMut};
use std::slice;

#[cfg(not(feature = "std"))]
use crate::lib::*;

use crate::{Directed, Direction, EdgeType, Graph, Incoming, Outgoing, Undirected};

use crate::iter_format::{DebugMap, IterFormatExt, NoPretty};
Expand Down
14 changes: 8 additions & 6 deletions src/graphmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use std::marker::PhantomData;
use std::ops::{Deref, Index, IndexMut};
use std::slice::Iter;

use crate::{Directed, Direction, EdgeType, Incoming, Outgoing, Undirected};
use crate::lib::*;

use crate::{Directed, Direction, EdgeType, Incoming, Outgoing, Undirected};
use crate::graph::node_index;
use crate::graph::Graph;
use crate::visit::{IntoEdgeReferences, IntoEdges, NodeCompactIndexable};
Expand Down Expand Up @@ -58,8 +59,8 @@ pub type DiGraphMap<N, E> = GraphMap<N, E, Directed>;
/// Depends on crate feature `graphmap` (default).
#[derive(Clone)]
pub struct GraphMap<N, E, Ty> {
nodes: IndexMap<N, Vec<(N, CompactDirection)>>,
edges: IndexMap<(N, N), E>,
nodes: IndexMap<N, Vec<(N, CompactDirection)>, RandomState>,
edges: IndexMap<(N, N), E, RandomState>,
ty: PhantomData<Ty>,
}

Expand Down Expand Up @@ -108,8 +109,8 @@ where
/// Create a new `GraphMap` with estimated capacity.
pub fn with_capacity(nodes: usize, edges: usize) -> Self {
GraphMap {
nodes: IndexMap::with_capacity(nodes),
edges: IndexMap::with_capacity(edges),
nodes: IndexMap::with_capacity_and_hasher(nodes, RandomState::default()),
edges: IndexMap::with_capacity_and_hasher(edges, RandomState::default()),
ty: PhantomData,
}
}
Expand Down Expand Up @@ -577,10 +578,11 @@ where
Ty: EdgeType,
{
from: N,
edges: &'a IndexMap<(N, N), E>,
edges: &'a IndexMap<(N, N), E, RandomState>,
iter: Neighbors<'a, N, Ty>,
}


impl<'a, N, E, Ty> Iterator for Edges<'a, N, E, Ty>
where
N: 'a + NodeTrait,
Expand Down
3 changes: 3 additions & 0 deletions src/isomorphism.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use fixedbitset::FixedBitSet;
use std::marker;


use crate::lib::*;

use super::graph::{Graph, IndexType, NodeIndex};
use super::{EdgeType, Incoming};

Expand Down
3 changes: 2 additions & 1 deletion src/k_shortest_path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{BinaryHeap, HashMap};

use crate::lib::*;

use std::hash::Hash;

Expand Down
61 changes: 61 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@
//! * **matrix_graph** -
//! Defaults on. Enables [`MatrixGraph`](./matrix_graph/struct.MatrixGraph.html).
//!
#![cfg_attr(not(feature = "std"), no_std)]
#![doc(html_root_url = "https://docs.rs/petgraph/0.4/")]

#[cfg(not(feature = "std"))]
extern crate core as std;

#[cfg(not(feature = "std"))]
extern crate alloc;

extern crate fixedbitset;
#[cfg(feature = "graphmap")]
extern crate indexmap;
Expand Down Expand Up @@ -173,6 +180,60 @@ pub mod graph {
#[cfg(feature = "stable_graph")]
pub use crate::graph_impl::stable_graph;


pub mod lib {
#[cfg(not(feature = "std"))]
pub use alloc::string::{String, ToString};
#[cfg(feature = "std")]
pub use std::string::{String, ToString};

#[cfg(not(feature = "std"))]
pub use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
pub use alloc::{format, vec};
#[cfg(feature = "std")]
pub use std::vec::Vec;

#[cfg(not(feature = "std"))]
pub use alloc::boxed::Box;
#[cfg(feature = "std")]
pub use std::boxed::Box;

#[cfg(not(feature = "std"))]
pub use alloc::rc::Rc;
#[cfg(feature = "std")]
pub use std::rc::Rc;

#[cfg(not(feature = "std"))]
pub use alloc::sync::{Arc, Weak};
#[cfg(feature = "std")]
pub use std::sync::Arc;


#[cfg(feature = "std")]
pub use std::collections::hash_map::RandomState;
#[cfg(not(feature = "std"))]
pub use hashbrown::hash_map::DefaultHashBuilder as RandomState;


#[cfg(feature = "std")]
pub use std::collections::{hash_map::Entry::{Occupied, Vacant}, HashSet, hash_map::Iter};
#[cfg(not(feature = "std"))]
pub use hashbrown::{ HashSet, hash_map::{ HashMap, Entry::{Occupied, Vacant}, Iter}};
Copy link
Member

@bluss bluss May 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap is used in the public API - this PR would mean that public API changes breakingly based on a cargo feature, and that's not an allowed usage of cargo features in general. This can't be integrated with this design, unfortunately.

The whole approach to HashMap needs to change somehow. I don't know if using hashbrown is necessary. Keep it simple and incremental, is it possible to support compiling without HashMap?


#[cfg(feature = "std")]
pub use std::collections::{BinaryHeap, HashMap,VecDeque};
#[cfg(not(feature = "std"))]
pub use alloc::collections::{BinaryHeap, VecDeque};

zendurix marked this conversation as resolved.
Show resolved Hide resolved





}


macro_rules! copyclone {
($name:ident) => {
impl Clone for $name {
Expand Down
8 changes: 5 additions & 3 deletions src/matrix_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use indexmap::IndexSet;

use fixedbitset::FixedBitSet;

use crate::lib::*;

use crate::{Directed, Direction, EdgeType, IntoWeightedEdge, Outgoing, Undirected};

use crate::graph::NodeIndex as GraphNodeIndex;
Expand Down Expand Up @@ -848,15 +850,15 @@ fn ensure_len<T: Default>(v: &mut Vec<T>, size: usize) {
struct IdStorage<T> {
elements: Vec<Option<T>>,
upper_bound: usize,
removed_ids: IndexSet<usize>,
removed_ids: IndexSet<usize, RandomState>,
}

impl<T> IdStorage<T> {
fn with_capacity(capacity: usize) -> Self {
IdStorage {
elements: Vec::with_capacity(capacity),
upper_bound: 0,
removed_ids: IndexSet::new(),
removed_ids: IndexSet::with_capacity_and_hasher(0, RandomState::default()),
}
}

Expand Down Expand Up @@ -922,7 +924,7 @@ impl<T> IndexMut<usize> for IdStorage<T> {

struct IdIterator<'a> {
upper_bound: usize,
removed_ids: &'a IndexSet<usize>,
removed_ids: &'a IndexSet<usize, RandomState>,
current: Option<usize>,
}

Expand Down
9 changes: 7 additions & 2 deletions src/simple_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{
iter::{from_fn, FromIterator},
};

use crate::lib::*;

use indexmap::IndexSet;

use crate::{
Expand Down Expand Up @@ -39,7 +41,7 @@ where
let min_length = min_intermediate_nodes + 1;

// list of visited nodes
let mut visited: IndexSet<G::NodeId> = IndexSet::from_iter(Some(from));
let mut visited: IndexSet<G::NodeId, RandomState> = IndexSet::from_iter(Some(from));
// list of childs of currently exploring path nodes,
// last elem is list of childs of last visited node
let mut stack = vec![graph.neighbors_directed(from, Outgoing)];
Expand Down Expand Up @@ -84,7 +86,7 @@ where

#[cfg(test)]
mod test {
use std::{collections::HashSet, iter::FromIterator};
use crate::lib::*;

use itertools::assert_equal;

Expand Down Expand Up @@ -121,6 +123,7 @@ mod test {
vec![0, 3, 4, 5],
];

#[cfg(feature = "std")]
println!("{}", Dot::new(&graph));
let actual_simple_paths_0_to_5: HashSet<Vec<_>> =
all_simple_paths(&graph, 0u32.into(), 5u32.into(), 0, None)
Expand All @@ -138,6 +141,7 @@ mod test {
let graph = DiGraph::<i32, i32, _>::from_edges(&[(0, 1), (2, 1)]);

let expexted_simple_paths_0_to_1 = &[vec![0usize, 1]];
#[cfg(feature = "std")]
println!("{}", Dot::new(&graph));
let actual_simple_paths_0_to_1: Vec<Vec<_>> =
all_simple_paths(&graph, 0u32.into(), 1u32.into(), 0, None)
Expand All @@ -152,6 +156,7 @@ mod test {
fn test_no_simple_paths() {
let graph = DiGraph::<i32, i32, _>::from_edges(&[(0, 1), (2, 1)]);

#[cfg(feature = "std")]
println!("{}", Dot::new(&graph));
let actual_simple_paths_0_to_2: Vec<Vec<_>> =
all_simple_paths(&graph, 0u32.into(), 2u32.into(), 0, None)
Expand Down
1 change: 1 addition & 0 deletions src/unionfind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::graph::IndexType;
use std::cmp::Ordering;
use crate::lib::*;

/// `UnionFind<K>` is a disjoint-set data structure. It tracks set membership of *n* elements
/// indexed from *0* to *n - 1*. The scalar type is `K` which must be an unsigned integer type.
Expand Down
3 changes: 2 additions & 1 deletion src/visit/filter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::prelude::*;

use fixedbitset::FixedBitSet;
use std::collections::HashSet;
use std::marker::PhantomData;

use crate::lib::*;

use crate::data::DataMap;
use crate::visit::{Data, NodeCompactIndexable, NodeCount};
use crate::visit::{
Expand Down
4 changes: 3 additions & 1 deletion src/visit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ pub use self::dfsvisit::*;
pub use self::traversal::*;

use fixedbitset::FixedBitSet;
use std::collections::HashSet;

use crate::lib::*;

use std::hash::{BuildHasher, Hash};

use super::{graph, EdgeType};
Expand Down
3 changes: 2 additions & 1 deletion src/visit/traversal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{GraphRef, IntoNodeIdentifiers, Reversed};
use super::{IntoNeighbors, IntoNeighborsDirected, VisitMap, Visitable};
use crate::Incoming;
use std::collections::VecDeque;

use crate::lib::*;

/// Visit nodes of a graph in a depth-first-search (DFS) emitting nodes in
/// preorder (when they are first discovered).
Expand Down