Skip to content

Commit

Permalink
Merge pull request #320 from tdejager/refactor/name-to-pool
Browse files Browse the repository at this point in the history
feat: move name type to pool
  • Loading branch information
tdejager authored Sep 7, 2023
2 parents f9b20fe + 09ecf32 commit f57a585
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
4 changes: 0 additions & 4 deletions crates/rattler_libsolv_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ pub use mapping::Mapping;

/// Version is a name and a version specification.
pub trait VersionTrait: Display {
/// The name of the package associated with this record.
/// TODO: Can we move this to the Pool?
type Name: Display + Sized + Hash + Eq + Clone;

/// The version associated with this record.
type Version: Display + Ord + Clone;

Expand Down
21 changes: 11 additions & 10 deletions crates/rattler_libsolv_rs/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::hash::Hash;

use crate::arena::Arena;
use crate::id::{NameId, RepoId, SolvableId, VersionSetId};
use crate::mapping::Mapping;
use crate::solvable::{PackageSolvable, Solvable};
use crate::{VersionSet, VersionTrait};
use crate::VersionSet;

/// A pool that stores data related to the available packages
///
/// Because it stores solvables, it contains references to `PackageRecord`s (the `'a` lifetime comes
/// from the original `PackageRecord`s)
pub struct Pool<VS: VersionSet> {
pub struct Pool<VS: VersionSet, Name: Hash + Eq = String> {
/// All the solvables that have been registered
pub(crate) solvables: Arena<SolvableId, Solvable<VS::V>>,

/// The total amount of registered repos
total_repos: u32,

/// Interned package names
package_names: Arena<NameId, <VS::V as VersionTrait>::Name>,
package_names: Arena<NameId, Name>,

/// Map from package names to the id of their interned counterpart
pub(crate) names_to_ids: HashMap<<VS::V as VersionTrait>::Name, NameId>,
pub(crate) names_to_ids: HashMap<Name, NameId>,

/// Map from interned package names to the solvables that have that name
pub(crate) packages_by_name: Mapping<NameId, Vec<SolvableId>>,
Expand All @@ -41,7 +42,7 @@ pub struct Pool<VS: VersionSet> {
pub(crate) match_spec_to_forbidden: Mapping<VersionSetId, Vec<SolvableId>>,
}

impl<VS: VersionSet> Default for Pool<VS> {
impl<VS: VersionSet, Name: Hash + Eq> Default for Pool<VS, Name> {
fn default() -> Self {
let mut solvables = Arena::new();
solvables.alloc(Solvable::new_root());
Expand All @@ -62,7 +63,7 @@ impl<VS: VersionSet> Default for Pool<VS> {
}
}

impl<VS: VersionSet> Pool<VS> {
impl<VS: VersionSet, Name: Hash + Eq + Clone> Pool<VS, Name> {
/// Creates a new [`Pool`]
pub fn new() -> Self {
Self::default()
Expand Down Expand Up @@ -133,9 +134,9 @@ impl<VS: VersionSet> Pool<VS> {
}

/// Interns a package name into the `Pool`, returning its `NameId`
pub fn intern_package_name<N>(&mut self, name: N) -> NameId
pub fn intern_package_name<NValue>(&mut self, name: NValue) -> NameId
where
N: Into<<VS::V as VersionTrait>::Name>,
NValue: Into<Name>,
{
match self.names_to_ids.entry(name.into()) {
Entry::Occupied(e) => *e.get(),
Expand All @@ -152,14 +153,14 @@ impl<VS: VersionSet> Pool<VS> {
}

/// Lookup the package name id associated to the provided name
pub fn lookup_package_name(&self, name: &<VS::V as VersionTrait>::Name) -> Option<NameId> {
pub fn lookup_package_name(&self, name: &Name) -> Option<NameId> {
self.names_to_ids.get(name).copied()
}

/// Returns the package name associated to the provided id
///
/// Panics if the package name is not found in the pool
pub fn resolve_package_name(&self, name_id: NameId) -> &<VS::V as VersionTrait>::Name {
pub fn resolve_package_name(&self, name_id: NameId) -> &Name {
&self.package_names[name_id]
}

Expand Down
15 changes: 7 additions & 8 deletions crates/rattler_libsolv_rs/src/solver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,6 @@ mod test {
}

impl VersionTrait for Pack {
type Name = String;
type Version = u32;

fn version(&self) -> Self::Version {
Expand All @@ -974,7 +973,7 @@ mod test {
}

impl Nameable for Pack {
type Name = <Pack as VersionTrait>::Name;
type Name = String;
fn name(&self) -> &Self::Name {
&self.0
}
Expand All @@ -988,7 +987,7 @@ mod test {
}

impl Nameable for Spec {
type Name = <<Spec as VersionSet>::V as VersionTrait>::Name;
type Name = String;
fn name(&self) -> &String {
&self.name
}
Expand Down Expand Up @@ -1080,8 +1079,8 @@ mod test {
/// packages: packages and its dependencies to add to the pool
fn pool<VS>(packages: &[(&str, Vec<&str>)]) -> Pool<VS>
where
VS: VersionSet + Nameable<Name = <VS::V as VersionTrait>::Name> + FromStr,
VS::V: FromStr + Nameable<Name = <VS::V as VersionTrait>::Name>,
VS: VersionSet + Nameable<Name = String> + FromStr,
VS::V: FromStr + Nameable<Name = String>,
<VS as FromStr>::Err: Debug,
<<VS as VersionSet>::V as FromStr>::Err: Debug,
{
Expand All @@ -1099,8 +1098,8 @@ mod test {
dependencies: &Vec<&str>,
constrains: &Vec<&str>,
) where
VS: VersionSet + Nameable<Name = <VS::V as VersionTrait>::Name> + FromStr,
VS::V: FromStr + Nameable<Name = <VS::V as VersionTrait>::Name>,
VS: VersionSet + Nameable<Name = String> + FromStr,
VS::V: FromStr + Nameable<Name = String>,
<VS as FromStr>::Err: Debug,
<<VS as VersionSet>::V as FromStr>::Err: Debug,
{
Expand All @@ -1126,7 +1125,7 @@ mod test {
}

/// Install the given version sets
fn install<VS: VersionSet + FromStr + Nameable<Name = <VS::V as VersionTrait>::Name>>(
fn install<VS: VersionSet + FromStr + Nameable<Name = String>>(
pool: &mut Pool<VS>,
version_sets: &[&str],
) -> SolveJobs
Expand Down
1 change: 0 additions & 1 deletion crates/rattler_solve/src/libsolv_rs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ impl<'a> Display for SolverPackageRecord<'a> {
}

impl<'a> VersionTrait for SolverPackageRecord<'a> {
type Name = String;
type Version = rattler_conda_types::Version;

fn version(&self) -> Self::Version {
Expand Down

0 comments on commit f57a585

Please sign in to comment.