Skip to content
Closed
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: 0 additions & 2 deletions src/callable/primitive/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl Callable for PrimitiveC {
// lets first see what we're aiming to build.
let ty: u8 = vals
.values
.borrow()
.iter()
.map(|(_, v)| match v {
Obj::Null => 0,
Expand All @@ -76,7 +75,6 @@ impl Callable for PrimitiveC {
// otherwise, try to collapse vectors into same type
let ret = vals
.values
.borrow()
.iter()
.map(|(_, r)| match r {
Obj::Vector(Vector::Logical(_)) => Vector::from(Vec::<Logical>::new()),
Expand Down
1 change: 0 additions & 1 deletion src/callable/primitive/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl Callable for PrimitiveNames {
Function(..) => Ok(Null), // return formals?
List(x) => {
Ok(x.values
.borrow()
.iter()
.map(|(k, _)| match k {
Some(name) => OptionNA::Some(name.clone()),
Expand Down
5 changes: 2 additions & 3 deletions src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::object::*;
use crate::parser::LocalizedParser;
use crate::parser::ParseResult;
use crate::session::{Session, SessionParserConfig};
use std::collections::HashSet;
use hashbrown::HashSet;

use core::fmt;
use std::fmt::Display;
Expand Down Expand Up @@ -229,7 +229,6 @@ impl Obj {
match self {
Obj::List(v) => v
.values
.borrow()
.iter()
.find(|(k, _)| *k == Some(String::from(name)))
.map(|(_, v)| v.clone()),
Expand Down Expand Up @@ -1170,7 +1169,7 @@ pub fn assert_formals(session: &Session, formals: ExprList) -> Result<ExprList,
}
}

Ok(formals)
Ok(formals.clone())
}

#[cfg(test)]
Expand Down
6 changes: 2 additions & 4 deletions src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ impl PartialEq for Obj {
match (self, other) {
(Obj::Null, Obj::Null) => true,
(Obj::List(l), Obj::List(r)) => {
let lb = l.values.borrow();
let rb = r.values.borrow();
let liter = lb.iter();
let riter = rb.iter();
let liter = l.values.iter();
let riter = r.values.iter();
liter
.zip(riter)
.all(|((lk, lv), (rk, rv))| lk == rk && lv == rv)
Expand Down
2 changes: 1 addition & 1 deletion src/object/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Environment {
}

pub fn append(&self, l: List) {
for (key, value) in l.values.borrow().iter() {
for (key, value) in l.values.iter() {
if let Some(name) = key {
self.values.borrow_mut().insert(name.clone(), value.clone());
}
Expand Down
19 changes: 6 additions & 13 deletions src/object/vector/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ pub enum Vector {

impl Clone for Vector {
fn clone(&self) -> Self {
self.lazy_copy()
match self {
Vector::Double(v) => Vector::Double(v.clone()),
Vector::Character(v) => Vector::Character(v.clone()),
Vector::Integer(v) => Vector::Integer(v.clone()),
Vector::Logical(v) => Vector::Logical(v.clone()),
}
}
}

Expand All @@ -69,18 +74,6 @@ impl Vector {
}
}

/// Create a lazy copy of the vector.
/// When mutating vectors, the internal data only needs to be copied when there is more than
/// one such lazy copy.
pub fn lazy_copy(&self) -> Self {
match self {
Vector::Double(v) => Vector::Double(v.clone()),
Vector::Character(v) => Vector::Character(v.clone()),
Vector::Integer(v) => Vector::Integer(v.clone()),
Vector::Logical(v) => Vector::Logical(v.clone()),
}
}

pub fn try_get(&self, index: Obj) -> EvalResult {
let err =
Error::Other("Vector index cannot be coerced into a valid indexing type.".to_string());
Expand Down
77 changes: 56 additions & 21 deletions src/object/vector/rep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use super::reptype::RepTypeIter;
use super::subset::Subset;
use super::types::*;
use super::{OptionNA, Pow, VecPartialCmp};
use crate::object::CowObj;
use crate::object::ViewMut;
use crate::object::{CowObj, Obj, ViewMut};

/// Vector Representation
///
Expand All @@ -18,25 +17,31 @@ use crate::object::ViewMut;
#[derive(Debug, PartialEq)]
pub struct Rep<T: Clone>(pub RefCell<RepType<T>>);

impl<T: Clone + AtomicMode + Default> Clone for Rep<T> {
impl<T: Clone + Default> Clone for Rep<T> {
fn clone(&self) -> Self {
match self.borrow().clone() {
RepType::Subset(v, s) => Rep(RefCell::new(RepType::Subset(v.clone(), s.clone()))),
}
}
}

impl<T: Clone + AtomicMode + Default> ViewMut for Rep<T> {
impl<T: Clone + Default> ViewMut for Rep<T> {
fn view_mut(&self) -> Self {
Self(RefCell::new(self.borrow().view_mut()))
}
}

impl<T: ViewMut + Default + Clone> Rep<T> {
pub fn get_inner_mut(&self, index: usize) -> Option<T> {
self.0.borrow().get_inner_mut(index)
}
}

impl<T> Rep<T>
where
T: AtomicMode + Clone + Default,
T: Clone + Default,
{
fn borrow(&self) -> Ref<RepType<T>> {
pub fn borrow(&self) -> Ref<RepType<T>> {
self.0.borrow()
}
fn materialize_inplace(&self) -> &Self {
Expand All @@ -47,6 +52,14 @@ where
self
}

/// Try to get mutable access to the internal vector through the passed closure.
pub fn with_inner_mut<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut Vec<T>) -> R,
{
self.0.borrow().with_inner_mut(f)
}

pub fn materialize(&self) -> Self {
self.borrow().materialize().into()
}
Expand Down Expand Up @@ -101,6 +114,10 @@ where
x.map(|x| x.into())
}

pub fn get_inner(&self, index: usize) -> Option<T> {
self.borrow().get_inner(index)
}

pub fn assign(&mut self, value: Self) -> Self {
self.0.borrow_mut().assign(value.0.into_inner()).into()
}
Expand All @@ -109,19 +126,31 @@ where
/// Internally, this is defined by the [crate::object::coercion::AtomicMode]
/// implementation of the vector's element type.
///
pub fn is_double(&self) -> bool {
pub fn is_double(&self) -> bool
where
T: AtomicMode,
{
T::is_double()
}
/// See [Self::is_double] for more information
pub fn is_logical(&self) -> bool {
pub fn is_logical(&self) -> bool
where
T: AtomicMode,
{
T::is_logical()
}
/// See [Self::is_double] for more information
pub fn is_integer(&self) -> bool {
pub fn is_integer(&self) -> bool
where
T: AtomicMode,
{
T::is_integer()
}
/// See [Self::is_double] for more information
pub fn is_character(&self) -> bool {
pub fn is_character(&self) -> bool
where
T: AtomicMode,
{
T::is_character()
}

Expand Down Expand Up @@ -151,7 +180,7 @@ where
///
pub fn as_mode<Mode>(&self) -> Rep<Mode>
where
T: CoercibleInto<Mode>,
T: CoercibleInto<Mode> + AtomicMode,
Mode: Clone,
{
Rep(RefCell::new(self.borrow().as_mode()))
Expand All @@ -160,31 +189,31 @@ where
/// See [Self::as_mode] for more information
pub fn as_logical(&self) -> Rep<Logical>
where
T: CoercibleInto<Logical>,
T: CoercibleInto<Logical> + AtomicMode,
{
self.as_mode::<Logical>()
}

/// See [Self::as_mode] for more information
pub fn as_integer(&self) -> Rep<Integer>
where
T: CoercibleInto<Integer>,
T: CoercibleInto<Integer> + AtomicMode,
{
self.as_mode::<Integer>()
}

/// See [Self::as_mode] for more information
pub fn as_double(&self) -> Rep<Double>
where
T: CoercibleInto<Double>,
T: CoercibleInto<Double> + AtomicMode,
{
self.as_mode::<Double>()
}

/// See [Self::as_mode] for more information
pub fn as_character(&self) -> Rep<Character>
where
T: CoercibleInto<Character>,
T: CoercibleInto<Character> + AtomicMode,
{
self.as_mode::<Character>()
}
Expand All @@ -210,14 +239,14 @@ where
.vectorized_partial_cmp(other.0.into_inner())
}

fn get_inner(&self, index: usize) -> Option<T> {
self.borrow().get_inner(index)
pub fn iter(&self) -> RepIter<T> {
self.clone().into_iter()
}
}

impl<T> Default for Rep<T>
where
T: AtomicMode + Clone + Default,
T: Clone + Default,
{
fn default() -> Self {
Rep(RefCell::new(RepType::default()))
Expand All @@ -228,7 +257,7 @@ pub struct RepIter<T: Clone>(RepTypeIter<T>);

impl<T> IntoIterator for Rep<T>
where
T: AtomicMode + Clone + Default,
T: Clone + Default,
{
type Item = T;
type IntoIter = RepIter<T>;
Expand All @@ -240,7 +269,7 @@ where

impl<T> Iterator for RepIter<T>
where
T: AtomicMode + Clone + Default,
T: Clone + Default,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -250,7 +279,7 @@ where

impl<T> From<RepType<T>> for Rep<T>
where
T: AtomicMode + Clone + Default,
T: Clone + Default,
{
fn from(rep: RepType<T>) -> Self {
Rep(RefCell::new(rep))
Expand All @@ -273,6 +302,12 @@ where
}
}

impl From<Vec<(Option<String>, Obj)>> for Rep<(Option<String>, Obj)> {
fn from(value: Vec<(Option<String>, Obj)>) -> Self {
Rep(RefCell::new(value.into()))
}
}

impl From<Vec<OptionNA<f64>>> for Rep<Double> {
fn from(value: Vec<OptionNA<f64>>) -> Self {
Rep(RefCell::new(value.into()))
Expand Down
Loading