Skip to content

Commit

Permalink
refactor(vector): remove unused RefCell from Rep
Browse files Browse the repository at this point in the history
  • Loading branch information
sebffischer authored Oct 17, 2024
1 parent e3e874c commit e4ddc02
Show file tree
Hide file tree
Showing 8 changed files with 1,189 additions and 1,557 deletions.
3 changes: 2 additions & 1 deletion src/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Named vectors were added and can e.g. be constructed via `[a = 1, b = 2]`
* The `is_null()` primitive was added
* Setting a list value to `null` actually sets it to `null` and does not remove it.
* Stricter recycling rule are enforced (@98):
* Stricter recycling rules are enforced (@98):
Vectorized operations on two vectors `v1` and `v2` now requires either of:
* One of the vectors has length 1 and the other vector's length is not zero.
* The vectors have the same length.
Expand All @@ -20,6 +20,7 @@
This included a considerable refactor.
* Iterating over references of a `Rep<T>` was made much simpler and new methods were added
and unused ones removed.
* The `RepType` struct that was introduced in 0.4.0 was removed again (#189).

## Notable Bugs Addressed

Expand Down
5 changes: 3 additions & 2 deletions src/callable/primitive/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ impl Callable for PrimitiveC {
};

if let Some(names) = names {
v.set_names(names.into())
Ok(Obj::Vector(v.set_names(names.into())))
} else {
Ok(Obj::Vector(v))
}
Ok(Obj::Vector(v))
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/callable/primitive/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::error::*;
use crate::formals;
use crate::internal_err;
use crate::lang::*;
use crate::object::reptype::RepType;
use crate::object::rep::Rep;
use crate::object::*;

/// Calculate a Sum of Elements
Expand Down Expand Up @@ -40,7 +40,7 @@ impl Callable for PrimitiveSum {
let (_, ellipsis) = self.match_arg_exprs(args, stack)?;

if ellipsis.is_empty() {
return EvalResult::Ok(Obj::Vector(Vector::from(RepType::from(vec![0.0]))));
return EvalResult::Ok(Obj::Vector(Vector::from(Rep::from(vec![0.0]))));
}

let objects: Vec<Obj> = force_promises(ellipsis, stack)?
Expand Down Expand Up @@ -78,8 +78,8 @@ impl Callable for PrimitiveSum {
for x in repr.inner().borrow().iter() {
match *x {
OptionNA::NA => {
let rep: RepType<OptionNA<f64>> =
RepType::from(vec![OptionNA::NA]);
let rep: Rep<OptionNA<f64>> =
Rep::from(vec![OptionNA::NA]);
return EvalResult::Ok(Obj::Vector(Vector::from(rep)));
}
OptionNA::Some(x) => sum += x as i32 as f64,
Expand All @@ -90,8 +90,8 @@ impl Callable for PrimitiveSum {
for x in repr.inner().borrow().iter() {
match *x {
OptionNA::NA => {
let rep: RepType<OptionNA<f64>> =
RepType::from(vec![OptionNA::NA]);
let rep: Rep<OptionNA<f64>> =
Rep::from(vec![OptionNA::NA]);
return EvalResult::Ok(Obj::Vector(Vector::from(rep)));
}
OptionNA::Some(x) => sum += x as f64,
Expand All @@ -102,8 +102,8 @@ impl Callable for PrimitiveSum {
for x in repr.inner().borrow().iter() {
match *x {
OptionNA::NA => {
let rep: RepType<OptionNA<f64>> =
RepType::from(vec![OptionNA::NA]);
let rep: Rep<OptionNA<f64>> =
Rep::from(vec![OptionNA::NA]);
return EvalResult::Ok(Obj::Vector(Vector::from(rep)));
}
OptionNA::Some(x) => sum += x,
Expand All @@ -116,7 +116,7 @@ impl Callable for PrimitiveSum {
_ => return internal_err!(),
}
}
EvalResult::Ok(Obj::Vector(Vector::from(RepType::from(vec![sum]))))
EvalResult::Ok(Obj::Vector(Vector::from(Rep::from(vec![sum]))))
} else {
let mut sum: i32 = 0;

Expand All @@ -128,8 +128,8 @@ impl Callable for PrimitiveSum {
for x in repr.inner().borrow().iter() {
match *x {
OptionNA::NA => {
let rep: RepType<OptionNA<i32>> =
RepType::from(vec![OptionNA::NA]);
let rep: Rep<OptionNA<i32>> =
Rep::from(vec![OptionNA::NA]);
return EvalResult::Ok(Obj::Vector(Vector::from(rep)));
}
OptionNA::Some(x) => sum += x as i32,
Expand All @@ -140,8 +140,8 @@ impl Callable for PrimitiveSum {
for x in repr.inner().borrow().iter() {
match *x {
OptionNA::NA => {
let rep: RepType<OptionNA<i32>> =
RepType::from(vec![OptionNA::NA]);
let rep: Rep<OptionNA<i32>> =
Rep::from(vec![OptionNA::NA]);
return EvalResult::Ok(Obj::Vector(Vector::from(rep)));
}
OptionNA::Some(x) => sum += x,
Expand All @@ -154,7 +154,7 @@ impl Callable for PrimitiveSum {
_ => return internal_err!(),
}
}
EvalResult::Ok(Obj::Vector(Vector::from(RepType::from(vec![sum]))))
EvalResult::Ok(Obj::Vector(Vector::from(Rep::from(vec![sum]))))
}
}
}
Expand Down
40 changes: 8 additions & 32 deletions src/object/vector/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use crate::object::CowObj;
use crate::object::Obj;

use super::coercion::CoercibleInto;
use super::rep::IterableValues;
use super::rep::Rep;
use super::reptype::IterableValues;
use super::reptype::RepType;
use super::subset::Subset;
use super::types::*;

Expand Down Expand Up @@ -139,13 +138,14 @@ impl Vector {
}
}

pub fn set_names(&self, names: CowObj<Vec<Character>>) {
pub fn set_names(&self, names: CowObj<Vec<Character>>) -> Self {
use super::Vector::*;
match self {
Vector::Character(x) => x.set_names(names),
Vector::Logical(x) => x.set_names(names),
Vector::Integer(x) => x.set_names(names),
Vector::Double(x) => x.set_names(names),
};
Character(x) => Character(x.set_names(names)),
Logical(x) => Logical(x.set_names(names)),
Integer(x) => Integer(x.set_names(names)),
Double(x) => Double(x.set_names(names)),
}
}

pub fn try_get(&self, index: Obj) -> EvalResult {
Expand Down Expand Up @@ -323,30 +323,6 @@ impl From<CowObj<Vec<Character>>> for Vector {
}
}

impl From<RepType<Double>> for Vector {
fn from(x: RepType<Double>) -> Self {
Vector::Double(x.into())
}
}

impl From<RepType<Integer>> for Vector {
fn from(x: RepType<Integer>) -> Self {
Vector::Integer(x.into())
}
}

impl From<RepType<Logical>> for Vector {
fn from(x: RepType<Logical>) -> Self {
Vector::Logical(x.into())
}
}

impl From<RepType<Character>> for Vector {
fn from(x: RepType<Character>) -> Self {
Vector::Character(x.into())
}
}

impl From<Rep<Double>> for Vector {
fn from(x: Rep<Double>) -> Self {
Vector::Double(x)
Expand Down
1 change: 0 additions & 1 deletion src/object/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
pub mod coercion;
pub mod iterators;
pub mod rep;
pub mod reptype;
pub mod types;

mod subsets;
Expand Down
Loading

0 comments on commit e4ddc02

Please sign in to comment.