feat: initial parallel impl

This commit is contained in:
2025-06-08 17:27:43 +08:00
parent 3797544fc2
commit 7293cb9f75
18 changed files with 529 additions and 934 deletions

View File

@@ -7,74 +7,94 @@ use hashbrown::{HashMap, HashSet};
use itertools::Itertools;
use crate::engine::Engine;
use crate::error::{Error, Result};
use crate::ty::public::Symbol;
use super::super::public as p;
use super::Value;
#[repr(transparent)]
#[derive(Constructor, Clone, PartialEq)]
pub struct AttrSet<'gc> {
data: HashMap<EcoString, Value<'gc>>,
pub struct AttrSet {
data: HashMap<EcoString, Value>,
}
impl<'gc> From<HashMap<EcoString, Value<'gc>>> for AttrSet<'gc> {
fn from(data: HashMap<EcoString, Value<'gc>>) -> Self {
impl From<HashMap<EcoString, Value>> for AttrSet {
fn from(data: HashMap<EcoString, Value>) -> Self {
Self { data }
}
}
impl<'gc> Deref for AttrSet<'gc> {
type Target = HashMap<EcoString, Value<'gc>>;
impl Deref for AttrSet {
type Target = HashMap<EcoString, Value>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<'gc> AttrSet<'gc> {
impl AttrSet {
pub fn with_capacity(cap: usize) -> Self {
AttrSet {
data: HashMap::with_capacity(cap),
}
}
pub fn push_attr_force(&mut self, sym: EcoString, val: Value<'gc>) {
pub fn push_attr_force(&mut self, sym: EcoString, val: Value) {
self.data.insert(sym, val);
}
pub fn push_attr(&mut self, sym: EcoString, val: Value<'gc>) {
pub fn push_attr(&mut self, sym: EcoString, val: Value) {
if self.data.get(&sym).is_some() {
todo!()
}
self.data.insert(sym, val);
}
pub fn select(&self, sym: &EcoString) -> Option<Value<'gc>> {
self.data.get(sym).cloned()
pub fn select(&self, mut path: impl DoubleEndedIterator<Item = Result<EcoString>>) -> Result<Value> {
// .ok_or_else(|| Error::EvalError())),
let mut data = &self.data;
let last = path.nth_back(0).unwrap();
for item in path {
let item = item?;
let Some(Value::AttrSet(attrs)) = data.get(&item) else {
return Err(Error::EvalError(format!("{} not found", Symbol::from(item))))
};
data = attrs.as_inner();
}
let last = last?;
data.get(&last).cloned().ok_or_else(|| Error::EvalError(format!("{} not found", Symbol::from(last))))
}
pub fn has_attr(&self, sym: &EcoString) -> bool {
self.data.get(sym).is_some()
pub fn has_attr(&self, path: impl IntoIterator<Item = Result<EcoString>>) -> Result<bool> {
let mut data = &self.data;
for item in path {
let Some(Value::AttrSet(attrs)) = data.get(&item?) else {
return Ok(false)
};
data = attrs.as_inner();
}
Ok(true)
}
pub fn update(&mut self, other: &AttrSet<'gc>) {
pub fn update(&mut self, other: &AttrSet) {
for (k, v) in other.data.iter() {
self.push_attr_force(k.clone(), v.clone())
}
}
pub fn as_inner(&self) -> &HashMap<EcoString, Value<'gc>> {
pub fn as_inner(&self) -> &HashMap<EcoString, Value> {
&self.data
}
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<EcoString, Value<'gc>>> {
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<EcoString, Value>> {
unsafe { std::mem::transmute(self) }
}
pub fn from_inner(data: HashMap<EcoString, Value<'gc>>) -> Self {
pub fn from_inner(data: HashMap<EcoString, Value>) -> Self {
Self { data }
}
pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool {
pub fn eq_impl(&self, other: &AttrSet) -> bool {
self.data.iter().len() == other.data.iter().len()
&& std::iter::zip(
self.data.iter().sorted_by(|(a, _), (b, _)| a.cmp(b)),
@@ -83,7 +103,7 @@ impl<'gc> AttrSet<'gc> {
.all(|((_, v1), (_, v2))| v1.eq_impl(v2))
}
pub fn to_public(&self, engine: &'gc Engine, seen: &mut HashSet<Value<'gc>>) -> p::Value {
pub fn to_public(&self, engine: &Engine, seen: &mut HashSet<Value>) -> p::Value {
p::Value::AttrSet(p::AttrSet::new(
self.data
.iter()