-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented equals operator between tensors
- Loading branch information
1 parent
093faeb
commit 41fdc86
Showing
10 changed files
with
105 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::data_buffer::DataBuffer; | ||
use crate::dtype::RawDataType; | ||
use crate::iterator::flat_iterator::FlatIterator; | ||
use crate::TensorBase; | ||
|
||
impl<B1, T1, B2, T2> PartialEq<TensorBase<B1>> for TensorBase<B2> | ||
where | ||
TensorBase<B1>: FlatIterator<T1>, | ||
TensorBase<B2>: FlatIterator<T2>, | ||
B1: DataBuffer<DType=T1>, | ||
B2: DataBuffer<DType=T2>, | ||
T1: RawDataType, | ||
T2: RawDataType + From<T1>, | ||
{ | ||
fn eq(&self, other: &TensorBase<B1>) -> bool { | ||
if self.shape != other.shape { | ||
false; | ||
} | ||
self.flat_iter().zip(other.flat_iter()).all(|(a, b)| a == b.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::data_buffer::DataBuffer; | ||
use crate::dtype::RawDataType; | ||
use crate::TensorBase; | ||
|
||
#[non_exhaustive] | ||
pub struct BufferIterator<T, I> | ||
where | ||
T: RawDataType, | ||
I: Iterator<Item=isize>, | ||
{ | ||
ptr: *const T, | ||
indices: I, | ||
} | ||
|
||
impl<T, I> BufferIterator<T, I> | ||
where | ||
T: RawDataType, | ||
I: Iterator<Item=isize>, | ||
{ | ||
pub(super) fn from<B>(tensor: &TensorBase<B>, indices: I) -> Self | ||
where | ||
B: DataBuffer<DType=T>, | ||
{ | ||
Self { | ||
ptr: tensor.data.const_ptr(), | ||
indices, | ||
} | ||
} | ||
} | ||
|
||
impl<T, I> Iterator for BufferIterator<T, I> | ||
where | ||
T: RawDataType, | ||
I: Iterator<Item=isize>, | ||
{ | ||
type Item = T; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match self.indices.next() { | ||
None => None, | ||
Some(i) => Some(unsafe { *self.ptr.offset(i) }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,25 @@ | ||
use crate::data_buffer::DataBuffer; | ||
use crate::dtype::RawDataType; | ||
use crate::TensorBase; | ||
use crate::iterator::buffer_iterator::BufferIterator; | ||
use crate::iterator::flat_index_generator::FlatIndexGenerator; | ||
use crate::{Tensor, TensorView}; | ||
use std::ops::Range; | ||
|
||
#[non_exhaustive] | ||
pub struct FlatIterator<T, I> | ||
where | ||
T: RawDataType, | ||
I: Iterator<Item=isize>, | ||
{ | ||
ptr: *const T, | ||
indices: I, | ||
pub trait FlatIterator<T: RawDataType> { | ||
type Indices: Iterator<Item=isize>; | ||
fn flat_iter(&self) -> BufferIterator<T, Self::Indices>; | ||
} | ||
|
||
impl<T, I> FlatIterator<T, I> | ||
where | ||
T: RawDataType, | ||
I: Iterator<Item=isize>, | ||
{ | ||
pub(super) fn from<B>(tensor: &TensorBase<B>, indices: I) -> Self | ||
where | ||
B: DataBuffer<DType=T>, | ||
{ | ||
Self { | ||
ptr: tensor.data.const_ptr(), | ||
indices, | ||
} | ||
impl<T: RawDataType> FlatIterator<T> for Tensor<T> { | ||
type Indices = Range<isize>; | ||
fn flat_iter(&self) -> BufferIterator<T, Self::Indices> { | ||
BufferIterator::from(self, 0..self.size() as isize) | ||
} | ||
} | ||
|
||
impl<T, I> Iterator for FlatIterator<T, I> | ||
where | ||
T: RawDataType, | ||
I: Iterator<Item=isize>, | ||
{ | ||
type Item = T; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match self.indices.next() { | ||
None => None, | ||
Some(i) => Some(unsafe { *self.ptr.offset(i) }) | ||
} | ||
impl<T: RawDataType> FlatIterator<T> for TensorView<T> { | ||
type Indices = FlatIndexGenerator; | ||
fn flat_iter(&self) -> BufferIterator<T, Self::Indices> { | ||
let indices = FlatIndexGenerator::from(&self.shape, &self.stride); | ||
BufferIterator::from(self, indices) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,8 @@ | ||
use crate::dtype::RawDataType; | ||
use crate::iterator::flat_index_iterator::FlatIndexIterator; | ||
use crate::iterator::flat_iterator::FlatIterator; | ||
use crate::{Tensor, TensorView}; | ||
use std::ops::Range; | ||
|
||
pub mod flat_iterator; | ||
pub mod buffer_iterator; | ||
pub(super) mod collapse_contiguous; | ||
pub mod flat_index_iterator; | ||
|
||
impl<T: RawDataType> Tensor<T> { | ||
pub fn flat_iter(&self) -> FlatIterator<T, Range<isize>> { | ||
FlatIterator::from(self, 0..self.size() as isize) | ||
} | ||
} | ||
pub mod flat_index_generator; | ||
pub mod flat_iterator; | ||
|
||
impl<T: RawDataType> TensorView<T> { | ||
pub fn flat_iter(&self) -> FlatIterator<T, FlatIndexIterator> { | ||
let indices = FlatIndexIterator::from(&self.shape, &self.stride); | ||
FlatIterator::from(self, indices) | ||
} | ||
} | ||
pub use flat_iterator::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters