Skip to content

Commit

Permalink
refactor: make Kind smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Apr 22, 2021
1 parent 01008c6 commit f7ab7cc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
50 changes: 27 additions & 23 deletions src/internal/incompatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ enum Kind<P: Package, V: Version> {
/// Initial incompatibility aiming at picking the root package for the first decision.
NotRoot(P, V),
/// There are no versions in the given range for this package.
NoVersions(P, Range<V>),
NoVersions(P),
/// Dependencies of the package are unavailable for versions in that range.
UnavailableDependencies(P, Range<V>),
UnavailableDependencies(P),
/// Incompatibility coming from the dependencies of a given package.
FromDependencyOf(P, Range<V>, P, Range<V>),
FromDependencyOf(P, P),
/// Derived from two causes. Stores cause ids.
DerivedFrom(IncompId<P, V>, IncompId<P, V>),
}
Expand Down Expand Up @@ -84,24 +84,23 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
/// Create an incompatibility to remember
/// that a given range does not contain any version.
pub fn no_versions(package: P, term: Term<V>) -> Self {
let range = match &term {
Term::Positive(r) => r.clone(),
Term::Negative(_) => panic!("No version should have a positive term"),
};
assert!(term.is_positive(), "No version should have a positive term");
Self {
package_terms: SmallMap::One([(package.clone(), term)]),
kind: Kind::NoVersions(package, range),
kind: Kind::NoVersions(package),
}
}

/// Create an incompatibility to remember
/// that a package version is not selectable
/// because its list of dependencies is unavailable.
pub fn unavailable_dependencies(package: P, version: V) -> Self {
let range = Range::exact(version);
Self {
package_terms: SmallMap::One([(package.clone(), Term::Positive(range.clone()))]),
kind: Kind::UnavailableDependencies(package, range),
package_terms: SmallMap::One([(
package.clone(),
Term::Positive(Range::exact(version)),
)]),
kind: Kind::UnavailableDependencies(package),
}
}

Expand All @@ -114,7 +113,7 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
(package.clone(), Term::Positive(range1.clone())),
(p2.clone(), Term::Negative(range2.clone())),
]),
kind: Kind::FromDependencyOf(package, range1, p2.clone(), range2.clone()),
kind: Kind::FromDependencyOf(package, p2.clone()),
}
}

Expand Down Expand Up @@ -239,7 +238,8 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
shared_ids: &Set<Id<Self>>,
store: &Arena<Self>,
) -> DerivationTree<P, V> {
match &store[self_id].kind {
let val = &store[self_id];
match &val.kind {
Kind::DerivedFrom(id1, id2) => {
let cause1 = Self::build_derivation_tree(*id1, shared_ids, store);
let cause2 = Self::build_derivation_tree(*id2, shared_ids, store);
Expand All @@ -254,18 +254,22 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
Kind::NotRoot(package, version) => {
DerivationTree::External(External::NotRoot(package.clone(), version.clone()))
}
Kind::NoVersions(package, range) => {
DerivationTree::External(External::NoVersions(package.clone(), range.clone()))
Kind::NoVersions(package) => DerivationTree::External(External::NoVersions(
package.clone(),
val.package_terms[package].as_range().clone(),
)),
Kind::UnavailableDependencies(package) => {
DerivationTree::External(External::UnavailableDependencies(
package.clone(),
val.package_terms[package].as_range().clone(),
))
}
Kind::UnavailableDependencies(package, range) => DerivationTree::External(
External::UnavailableDependencies(package.clone(), range.clone()),
),
Kind::FromDependencyOf(package, range, dep_package, dep_range) => {
Kind::FromDependencyOf(package, dep_package) => {
DerivationTree::External(External::FromDependencyOf(
package.clone(),
range.clone(),
val.package_terms[package].as_range().clone(),
dep_package.clone(),
dep_range.clone(),
val.package_terms[dep_package].as_range().clone(),
))
}
}
Expand Down Expand Up @@ -305,12 +309,12 @@ pub mod tests {
let mut store = Arena::new();
let i1 = store.alloc(Incompatibility {
package_terms: SmallMap::Two([("p1", t1.clone()), ("p2", t2.negate())]),
kind: Kind::UnavailableDependencies("0", Range::any())
kind: Kind::UnavailableDependencies("0")
});

let i2 = store.alloc(Incompatibility {
package_terms: SmallMap::Two([("p2", t2), ("p3", t3.clone())]),
kind: Kind::UnavailableDependencies("0", Range::any())
kind: Kind::UnavailableDependencies("0")
});

let mut i3 = Map::default();
Expand Down
9 changes: 8 additions & 1 deletion src/internal/small_map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::type_aliases::Map;
use std::hash::Hash;
use std::{hash::Hash, ops::Index};

#[derive(Debug, Clone)]
pub enum SmallMap<K, V> {
Expand Down Expand Up @@ -146,6 +146,13 @@ impl<K, V> SmallMap<K, V> {
}
}

impl<K: PartialEq + Eq + Hash, V> Index<&K> for SmallMap<K, V> {
type Output = V;
fn index(&self, key: &K) -> &V {
&self.get(key).unwrap()
}
}

impl<K: Eq + Hash + Clone, V: Clone> SmallMap<K, V> {
pub fn as_map(&self) -> Map<K, V> {
match self {
Expand Down
8 changes: 8 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ impl<V: Version> Term<V> {
_ => panic!("Negative term cannot unwrap positive range"),
}
}

/// Unwrap the range contains in the term.
pub(crate) fn as_range(&self) -> &Range<V> {
match self {
Self::Positive(range) => range,
Self::Negative(range) => range,
}
}
}

/// Set operations with terms.
Expand Down

0 comments on commit f7ab7cc

Please sign in to comment.