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

More methods and traits for la_arena::ArenaMap #12956

Merged
merged 2 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion crates/hir-def/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl HasChildSource<LocalTypeOrConstParamId> for GenericDefId {
if let GenericDefId::TraitId(id) = *self {
let trait_ref = id.lookup(db).source(db).value;
let idx = idx_iter.next().unwrap();
params.insert(idx, Either::Right(trait_ref))
params.insert(idx, Either::Right(trait_ref));
}

if let Some(generic_params_list) = generic_params_list {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub(crate) fn field_visibilities_query(
let resolver = variant_id.module(db).resolver(db);
let mut res = ArenaMap::default();
for (field_id, field_data) in var_data.fields().iter() {
res.insert(field_id, field_data.visibility.resolve(db, &resolver))
res.insert(field_id, field_data.visibility.resolve(db, &resolver));
}
Arc::new(res)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ pub(crate) fn field_types_query(
let ctx =
TyLoweringContext::new(db, &resolver).with_type_param_mode(ParamLoweringMode::Variable);
for (field_id, field_data) in var_data.fields().iter() {
res.insert(field_id, make_binders(db, &generics, ctx.lower_ty(&field_data.type_ref)))
res.insert(field_id, make_binders(db, &generics, ctx.lower_ty(&field_data.type_ref)));
}
Arc::new(res)
}
Expand Down
50 changes: 48 additions & 2 deletions lib/la-arena/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,42 @@ impl<T, V> ArenaMap<Idx<T>, V> {
Self { v: Vec::with_capacity(capacity), _ty: PhantomData }
}

/// Reserves capacity for at least additional more elements to be inserted in the map.
pub fn reserve(&mut self, additional: usize) {
self.v.reserve(additional);
}

/// Clears the map, removing all elements.
pub fn clear(&mut self) {
self.v.clear();
}

/// Shrinks the capacity of the map as much as possible.
pub fn shrink_to_fit(&mut self) {
let min_len = self.v.iter().rposition(|slot| slot.is_some()).map_or(0, |i| i + 1);
self.v.truncate(min_len);
self.v.shrink_to_fit();
}

/// Returns whether the map contains a value for the specified index.
pub fn contains_idx(&self, idx: Idx<T>) -> bool {
matches!(self.v.get(Self::to_idx(idx)), Some(Some(_)))
}

/// Removes an index from the map, returning the value at the index if the index was previously in the map.
pub fn remove(&mut self, idx: Idx<T>) -> Option<V> {
self.v.get_mut(Self::to_idx(idx))?.take()
}

/// Inserts a value associated with a given arena index into the map.
pub fn insert(&mut self, idx: Idx<T>, t: V) {
///
/// If the map did not have this index present, None is returned.
/// Otherwise, the value is updated, and the old value is returned.
pub fn insert(&mut self, idx: Idx<T>, t: V) -> Option<V> {
let idx = Self::to_idx(idx);

self.v.resize_with((idx + 1).max(self.v.len()), || None);
self.v[idx] = Some(t);
self.v[idx].replace(t)
}

/// Returns a reference to the value associated with the provided index
Expand Down Expand Up @@ -94,6 +124,22 @@ impl<T, V> Default for ArenaMap<Idx<V>, T> {
}
}

impl<T, V> Extend<(Idx<V>, T)> for ArenaMap<Idx<V>, T> {
fn extend<I: IntoIterator<Item = (Idx<V>, T)>>(&mut self, iter: I) {
iter.into_iter().for_each(move |(k, v)| {
self.insert(k, v);
});
}
}

impl<T, V> FromIterator<(Idx<V>, T)> for ArenaMap<Idx<V>, T> {
fn from_iter<I: IntoIterator<Item = (Idx<V>, T)>>(iter: I) -> Self {
let mut this = Self::new();
this.extend(iter);
this
}
}

/// A view into a single entry in a map, which may either be vacant or occupied.
///
/// This `enum` is constructed from the [`entry`] method on [`ArenaMap`].
Expand Down