use VecSet in IntroducedVariables

This commit is contained in:
Folkert 2022-04-17 20:43:45 +02:00
parent 9f453a7ba2
commit 7f3ca4458a
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
4 changed files with 25 additions and 17 deletions

View file

@ -58,8 +58,8 @@ pub struct IntroducedVariables {
pub wildcards: Vec<Loc<Variable>>, pub wildcards: Vec<Loc<Variable>>,
pub lambda_sets: Vec<Variable>, pub lambda_sets: Vec<Variable>,
pub inferred: Vec<Loc<Variable>>, pub inferred: Vec<Loc<Variable>>,
pub named: Vec<NamedVariable>, pub named: VecSet<NamedVariable>,
pub able: Vec<AbleVariable>, pub able: VecSet<AbleVariable>,
pub host_exposed_aliases: MutMap<Symbol, Variable>, pub host_exposed_aliases: MutMap<Symbol, Variable>,
} }
@ -84,7 +84,7 @@ impl IntroducedVariables {
first_seen: var.region, first_seen: var.region,
}; };
self.named.push(named_variable); self.named.insert(named_variable);
} }
pub fn insert_able(&mut self, name: Lowercase, var: Loc<Variable>, ability: Symbol) { pub fn insert_able(&mut self, name: Lowercase, var: Loc<Variable>, ability: Symbol) {
@ -97,7 +97,7 @@ impl IntroducedVariables {
first_seen: var.region, first_seen: var.region,
}; };
self.able.push(able_variable); self.able.insert(able_variable);
} }
pub fn insert_wildcard(&mut self, var: Loc<Variable>) { pub fn insert_wildcard(&mut self, var: Loc<Variable>) {
@ -128,12 +128,7 @@ impl IntroducedVariables {
.extend(other.host_exposed_aliases.clone()); .extend(other.host_exposed_aliases.clone());
self.named.extend(other.named.iter().cloned()); self.named.extend(other.named.iter().cloned());
self.named.sort();
self.named.dedup();
self.able.extend(other.able.iter().cloned()); self.able.extend(other.able.iter().cloned());
self.able.sort();
self.able.dedup();
} }
pub fn union_owned(&mut self, other: Self) { pub fn union_owned(&mut self, other: Self) {
@ -143,8 +138,7 @@ impl IntroducedVariables {
self.host_exposed_aliases.extend(other.host_exposed_aliases); self.host_exposed_aliases.extend(other.host_exposed_aliases);
self.named.extend(other.named); self.named.extend(other.named);
self.named.sort(); self.able.extend(other.able.iter().cloned());
self.named.dedup();
} }
pub fn var_by_name(&self, name: &Lowercase) -> Option<Variable> { pub fn var_by_name(&self, name: &Lowercase) -> Option<Variable> {

View file

@ -344,7 +344,8 @@ pub fn canonicalize_defs<'a>(
let mut named = can_ann.introduced_variables.named; let mut named = can_ann.introduced_variables.named;
for loc_lowercase in vars.iter() { for loc_lowercase in vars.iter() {
match named.iter().position(|nv| nv.name == loc_lowercase.value) { let opt_index = named.iter().position(|nv| nv.name == loc_lowercase.value);
match opt_index {
Some(index) => { Some(index) => {
// This is a valid lowercase rigid var for the type def. // This is a valid lowercase rigid var for the type def.
let named_variable = named.swap_remove(index); let named_variable = named.swap_remove(index);

View file

@ -241,6 +241,18 @@ impl<T: PartialEq> VecSet<T> {
} }
} }
pub fn len(&self) -> usize {
self.elements.len()
}
pub fn is_empty(&self) -> bool {
self.elements.is_empty()
}
pub fn swap_remove(&mut self, index: usize) -> T {
self.elements.swap_remove(index)
}
pub fn insert(&mut self, value: T) -> bool { pub fn insert(&mut self, value: T) -> bool {
if self.elements.contains(&value) { if self.elements.contains(&value) {
true true
@ -273,16 +285,17 @@ impl<T: PartialEq> VecSet<T> {
impl<A: Ord> Extend<A> for VecSet<A> { impl<A: Ord> Extend<A> for VecSet<A> {
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
let mut it = iter.into_iter(); let it = iter.into_iter();
let hint = it.size_hint(); let hint = it.size_hint();
match hint { match hint {
(0, Some(0)) => { (0, Some(0)) => {
// done, do nothing // done, do nothing
} }
(1, Some(1)) => { (1, Some(1)) | (2, Some(2)) => {
let value = it.next().unwrap(); for value in it {
self.insert(value); self.insert(value);
}
} }
_ => { _ => {
self.elements.extend(it); self.elements.extend(it);

View file

@ -40,7 +40,7 @@ use roc_types::solved_types::Solved;
use roc_types::subs::{Subs, VarStore, Variable}; use roc_types::subs::{Subs, VarStore, Variable};
use roc_types::types::{Alias, AliasCommon, TypeExtension}; use roc_types::types::{Alias, AliasCommon, TypeExtension};
use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{HashMap, }; use std::collections::HashMap;
use std::io; use std::io;
use std::iter; use std::iter;
use std::ops::ControlFlow; use std::ops::ControlFlow;