mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
refactor roc_collections
This commit is contained in:
parent
9343c6802a
commit
b59d33a1d5
11 changed files with 108 additions and 106 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::env::Env;
|
use crate::env::Env;
|
||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
use roc_collections::all::{ImMap, MutMap, MutSet, SendMap, VecSet};
|
use roc_collections::{ImMap, MutMap, MutSet, SendMap, VecSet};
|
||||||
use roc_module::ident::{Ident, Lowercase, TagName};
|
use roc_module::ident::{Ident, Lowercase, TagName};
|
||||||
use roc_module::symbol::{IdentIds, ModuleId, Symbol};
|
use roc_module::symbol::{IdentIds, ModuleId, Symbol};
|
||||||
use roc_parse::ast::{AssignedField, ExtractSpaces, Pattern, Tag, TypeAnnotation, TypeHeader};
|
use roc_parse::ast::{AssignedField, ExtractSpaces, Pattern, Tag, TypeAnnotation, TypeHeader};
|
||||||
|
|
|
@ -10,8 +10,7 @@ use crate::pattern::{bindings_from_patterns, canonicalize_def_header_pattern, Pa
|
||||||
use crate::procedure::References;
|
use crate::procedure::References;
|
||||||
use crate::scope::create_alias;
|
use crate::scope::create_alias;
|
||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
use roc_collections::all::ImSet;
|
use roc_collections::{default_hasher, ImEntry, ImMap, ImSet, MutMap, MutSet, SendMap};
|
||||||
use roc_collections::all::{default_hasher, ImEntry, ImMap, MutMap, MutSet, SendMap};
|
|
||||||
use roc_module::ident::Lowercase;
|
use roc_module::ident::Lowercase;
|
||||||
use roc_module::symbol::Symbol;
|
use roc_module::symbol::Symbol;
|
||||||
use roc_parse::ast;
|
use roc_parse::ast;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::env::Env;
|
||||||
use crate::expr::{ClosureData, Expr, Recursive};
|
use crate::expr::{ClosureData, Expr, Recursive};
|
||||||
use crate::pattern::Pattern;
|
use crate::pattern::Pattern;
|
||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
use roc_collections::all::{SendMap, VecSet};
|
use roc_collections::{SendMap, VecSet};
|
||||||
use roc_module::called_via::CalledVia;
|
use roc_module::called_via::CalledVia;
|
||||||
use roc_module::ident::TagName;
|
use roc_module::ident::TagName;
|
||||||
use roc_module::symbol::Symbol;
|
use roc_module::symbol::Symbol;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::procedure::References;
|
use crate::procedure::References;
|
||||||
use roc_collections::all::{MutMap, VecSet};
|
use roc_collections::{MutMap, VecSet};
|
||||||
use roc_module::ident::{Ident, Lowercase, ModuleName};
|
use roc_module::ident::{Ident, Lowercase, ModuleName};
|
||||||
use roc_module::symbol::{IdentIds, ModuleId, ModuleIds, Symbol};
|
use roc_module::symbol::{IdentIds, ModuleId, ModuleIds, Symbol};
|
||||||
use roc_problem::can::{Problem, RuntimeError};
|
use roc_problem::can::{Problem, RuntimeError};
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::num::{
|
||||||
use crate::pattern::{canonicalize_pattern, Pattern};
|
use crate::pattern::{canonicalize_pattern, Pattern};
|
||||||
use crate::procedure::References;
|
use crate::procedure::References;
|
||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
use roc_collections::all::{MutMap, MutSet, SendMap, VecSet};
|
use roc_collections::{MutMap, MutSet, SendMap, VecSet};
|
||||||
use roc_module::called_via::CalledVia;
|
use roc_module::called_via::CalledVia;
|
||||||
use roc_module::ident::{ForeignSymbol, Lowercase, TagName};
|
use roc_module::ident::{ForeignSymbol, Lowercase, TagName};
|
||||||
use roc_module::low_level::LowLevel;
|
use roc_module::low_level::LowLevel;
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crate::operator::desugar_def;
|
||||||
use crate::pattern::Pattern;
|
use crate::pattern::Pattern;
|
||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use roc_collections::all::{MutMap, SendMap, VecSet};
|
use roc_collections::{MutMap, SendMap, VecSet};
|
||||||
use roc_module::ident::Lowercase;
|
use roc_module::ident::Lowercase;
|
||||||
use roc_module::ident::{Ident, TagName};
|
use roc_module::ident::{Ident, TagName};
|
||||||
use roc_module::symbol::{IdentIds, ModuleId, ModuleIds, Symbol};
|
use roc_module::symbol::{IdentIds, ModuleId, ModuleIds, Symbol};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::expr::Expr;
|
use crate::expr::Expr;
|
||||||
use crate::pattern::Pattern;
|
use crate::pattern::Pattern;
|
||||||
use roc_collections::all::VecSet;
|
use roc_collections::VecSet;
|
||||||
use roc_module::symbol::Symbol;
|
use roc_module::symbol::Symbol;
|
||||||
use roc_region::all::{Loc, Region};
|
use roc_region::all::{Loc, Region};
|
||||||
use roc_types::subs::Variable;
|
use roc_types::subs::Variable;
|
||||||
|
|
|
@ -220,99 +220,3 @@ macro_rules! mut_map {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
|
||||||
pub struct VecSet<T> {
|
|
||||||
elements: Vec<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Default for VecSet<T> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
elements: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: PartialEq> VecSet<T> {
|
|
||||||
pub fn with_capacity(capacity: usize) -> Self {
|
|
||||||
Self {
|
|
||||||
elements: Vec::with_capacity(capacity),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
if self.elements.contains(&value) {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
self.elements.push(value);
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn contains(&self, value: &T) -> bool {
|
|
||||||
self.elements.contains(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(&mut self, value: &T) {
|
|
||||||
match self.elements.iter().position(|x| x == value) {
|
|
||||||
None => {
|
|
||||||
// just do nothing
|
|
||||||
}
|
|
||||||
Some(index) => {
|
|
||||||
self.elements.swap_remove(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
|
||||||
self.elements.iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A: Ord> Extend<A> for VecSet<A> {
|
|
||||||
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
|
|
||||||
let it = iter.into_iter();
|
|
||||||
let hint = it.size_hint();
|
|
||||||
|
|
||||||
match hint {
|
|
||||||
(0, Some(0)) => {
|
|
||||||
// done, do nothing
|
|
||||||
}
|
|
||||||
(1, Some(1)) | (2, Some(2)) => {
|
|
||||||
for value in it {
|
|
||||||
self.insert(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
self.elements.extend(it);
|
|
||||||
|
|
||||||
self.elements.sort();
|
|
||||||
self.elements.dedup();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> IntoIterator for VecSet<T> {
|
|
||||||
type Item = T;
|
|
||||||
|
|
||||||
type IntoIter = std::vec::IntoIter<T>;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
self.elements.into_iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,3 +4,7 @@
|
||||||
|
|
||||||
pub mod all;
|
pub mod all;
|
||||||
pub mod soa;
|
pub mod soa;
|
||||||
|
mod vec_set;
|
||||||
|
|
||||||
|
pub use all::{default_hasher, BumpMap, ImEntry, ImMap, ImSet, MutMap, MutSet, SendMap};
|
||||||
|
pub use vec_set::VecSet;
|
||||||
|
|
95
compiler/collections/src/vec_set.rs
Normal file
95
compiler/collections/src/vec_set.rs
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct VecSet<T> {
|
||||||
|
elements: Vec<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for VecSet<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
elements: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PartialEq> VecSet<T> {
|
||||||
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
elements: Vec::with_capacity(capacity),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if self.elements.contains(&value) {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
self.elements.push(value);
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contains(&self, value: &T) -> bool {
|
||||||
|
self.elements.contains(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(&mut self, value: &T) {
|
||||||
|
match self.elements.iter().position(|x| x == value) {
|
||||||
|
None => {
|
||||||
|
// just do nothing
|
||||||
|
}
|
||||||
|
Some(index) => {
|
||||||
|
self.elements.swap_remove(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||||
|
self.elements.iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Ord> Extend<A> for VecSet<A> {
|
||||||
|
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
|
||||||
|
let it = iter.into_iter();
|
||||||
|
let hint = it.size_hint();
|
||||||
|
|
||||||
|
match hint {
|
||||||
|
(0, Some(0)) => {
|
||||||
|
// done, do nothing
|
||||||
|
}
|
||||||
|
(1, Some(1)) | (2, Some(2)) => {
|
||||||
|
for value in it {
|
||||||
|
self.insert(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.elements.extend(it);
|
||||||
|
|
||||||
|
self.elements.sort();
|
||||||
|
self.elements.dedup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> IntoIterator for VecSet<T> {
|
||||||
|
type Item = T;
|
||||||
|
|
||||||
|
type IntoIter = std::vec::IntoIter<T>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.elements.into_iter()
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ use roc_can::abilities::AbilitiesStore;
|
||||||
use roc_can::constraint::{Constraint as ConstraintSoa, Constraints};
|
use roc_can::constraint::{Constraint as ConstraintSoa, Constraints};
|
||||||
use roc_can::def::Declaration;
|
use roc_can::def::Declaration;
|
||||||
use roc_can::module::{canonicalize_module_defs, Module};
|
use roc_can::module::{canonicalize_module_defs, Module};
|
||||||
use roc_collections::all::{default_hasher, BumpMap, MutMap, MutSet, VecSet};
|
use roc_collections::{default_hasher, BumpMap, MutMap, MutSet, VecSet};
|
||||||
use roc_constrain::module::{
|
use roc_constrain::module::{
|
||||||
constrain_builtin_imports, constrain_module, ExposedByModule, ExposedForModule,
|
constrain_builtin_imports, constrain_module, ExposedByModule, ExposedForModule,
|
||||||
ExposedModuleTypes,
|
ExposedModuleTypes,
|
||||||
|
@ -1110,7 +1110,7 @@ pub fn load<'a>(
|
||||||
) -> Result<LoadResult<'a>, LoadingProblem<'a>> {
|
) -> Result<LoadResult<'a>, LoadingProblem<'a>> {
|
||||||
// When compiling to wasm, we cannot spawn extra threads
|
// When compiling to wasm, we cannot spawn extra threads
|
||||||
// so we have a single-threaded implementation
|
// so we have a single-threaded implementation
|
||||||
if cfg!(target_family = "wasm") {
|
if true || cfg!(target_family = "wasm") {
|
||||||
load_single_threaded(
|
load_single_threaded(
|
||||||
arena,
|
arena,
|
||||||
load_start,
|
load_start,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue