Skip to content

Commit

Permalink
use Arc for record values
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Nov 8, 2021
1 parent f650fd4 commit 2e9d10a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/builtins/maps.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::Arc;

use cirru_edn::EdnKwd;
use im_ternary_tree::TernaryTreeList;

use crate::builtins::records::find_in_fields;
use crate::primes::{Calcit, CalcitErr, CalcitItems, CrListWrap};

use crate::util::number::is_even;

use im_ternary_tree::TernaryTreeList;

pub fn call_new_map(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
if is_even(xs.len()) {
let n = xs.len() >> 1;
Expand Down Expand Up @@ -69,7 +70,7 @@ pub fn call_merge(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
Ok(Calcit::Map(zs))
}
(Some(Calcit::Record(name, fields, values)), Some(Calcit::Map(ys))) => {
let mut new_values = values.to_owned();
let mut new_values = (**values).to_owned();
for (k, v) in ys {
match k {
Calcit::Str(s) | Calcit::Symbol { sym: s, .. } => match find_in_fields(fields, &EdnKwd::from(s)) {
Expand All @@ -83,7 +84,7 @@ pub fn call_merge(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
a => return CalcitErr::err_str(format!("invalid field key: {}", a)),
}
}
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), new_values))
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), Arc::new(new_values)))
}
(Some(a), Some(b)) => CalcitErr::err_str(format!("expected 2 maps, got: {} {}", a, b)),
(_, _) => CalcitErr::err_str(format!("expected 2 arguments, got: {:?}", xs)),
Expand Down
18 changes: 9 additions & 9 deletions src/builtins/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn new_record(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
prev = x.to_owned()
}
}
Ok(Calcit::Record(name_id.to_owned(), Arc::new(fields), values))
Ok(Calcit::Record(name_id.to_owned(), Arc::new(fields), Arc::new(values)))
}
pub fn call_record(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
let args_size = xs.len();
Expand All @@ -63,7 +63,7 @@ pub fn call_record(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
if size != def_fields.len() {
return CalcitErr::err_str(format!("unexpected size in &%{{}}, {} .. {}", size, def_fields.len()));
}
let mut values: Vec<Calcit> = v0.to_owned();
let mut values: Vec<Calcit> = (**v0).to_owned();

for idx in 0..size {
let k_idx = idx * 2 + 1;
Expand All @@ -85,7 +85,7 @@ pub fn call_record(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
}
}

Ok(Calcit::Record(name.to_owned(), def_fields.to_owned(), values))
Ok(Calcit::Record(name.to_owned(), def_fields.to_owned(), Arc::new(values)))
} else {
CalcitErr::err_str(format!("&%{{}} expected pairs, got: {:?}", xs))
}
Expand Down Expand Up @@ -125,7 +125,7 @@ pub fn record_from_map(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
return CalcitErr::err_str(format!("field mismatch: {} {} in {:?} {:?}", k, fields[idx], fields, pairs));
}
}
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), values))
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), Arc::new(values)))
}
(a, b) => CalcitErr::err_str(format!("&record:from-map expected a record and a map, got {} {}", a, b)),
}
Expand Down Expand Up @@ -237,17 +237,17 @@ pub fn assoc(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
(Some(Calcit::Record(name, fields, values)), Some(a), Some(b)) => match a {
Calcit::Str(s) | Calcit::Symbol { sym: s, .. } => match find_in_fields(fields, &EdnKwd::from(s)) {
Some(pos) => {
let mut new_values = values.to_owned();
let mut new_values = (**values).to_owned();
new_values[pos] = b.to_owned();
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), new_values))
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), Arc::new(new_values)))
}
None => CalcitErr::err_str(format!("invalid field `{}` for {:?}", s, fields)),
},
Calcit::Keyword(s) => match find_in_fields(fields, s) {
Some(pos) => {
let mut new_values = values.to_owned();
let mut new_values = (**values).to_owned();
new_values[pos] = b.to_owned();
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), new_values))
Ok(Calcit::Record(name.to_owned(), fields.to_owned(), Arc::new(new_values)))
}
None => CalcitErr::err_str(format!("invalid field `{}` for {:?}", s, fields)),
},
Expand Down Expand Up @@ -325,5 +325,5 @@ fn extend_record_field(
_ => return CalcitErr::err_str("expected record name"),
};

Ok(Calcit::Record(new_name_id, Arc::new(next_fields), next_values))
Ok(Calcit::Record(new_name_id, Arc::new(next_fields), Arc::new(next_values)))
}
2 changes: 1 addition & 1 deletion src/data/edn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub fn edn_to_calcit(x: &Edn) -> Calcit {
fields.push(v.0.to_owned());
values.push(edn_to_calcit(&v.1));
}
Calcit::Record(name.to_owned(), Arc::new(fields), values)
Calcit::Record(name.to_owned(), Arc::new(fields), Arc::new(values))
}
Edn::Buffer(buf) => Calcit::Buffer(buf.to_owned()),
}
Expand Down
2 changes: 1 addition & 1 deletion src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub enum Calcit {
List(CalcitItems),
Set(rpds::HashTrieSetSync<Calcit>),
Map(rpds::HashTrieMapSync<Calcit, Calcit>),
Record(EdnKwd, Arc<Vec<EdnKwd>>, Vec<Calcit>), // usize of keyword id
Record(EdnKwd, Arc<Vec<EdnKwd>>, Arc<Vec<Calcit>>), // usize of keyword id
Proc(Arc<str>),
Macro {
name: Arc<str>, // name
Expand Down

0 comments on commit 2e9d10a

Please sign in to comment.