-
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.
- Loading branch information
1 parent
69ba10e
commit 86be050
Showing
3 changed files
with
94 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::data_buffer::{DataOwned, DataView}; | ||
use crate::dtype::RawDataType; | ||
use std::ops::Index; | ||
use std::ptr::NonNull; | ||
|
||
pub trait DataBuffer: Index<usize> { | ||
type DType: RawDataType; | ||
|
||
fn len(&self) -> usize; | ||
|
||
fn ptr(&self) -> NonNull<Self::DType>; | ||
|
||
fn const_ptr(&self) -> *const Self::DType; | ||
|
||
fn to_view(&self) -> DataView<Self::DType>; | ||
|
||
// fn clone(&self) -> DataOwned<Self::DType>; | ||
} | ||
|
||
// Two kinds of data buffers | ||
// DataOwned: owns its data & responsible for cleaning it up | ||
// DataView: reference to data owned by another buffer | ||
|
||
impl<T: RawDataType> DataBuffer for DataOwned<T> { | ||
type DType = T; | ||
|
||
fn len(&self) -> usize { | ||
self.len | ||
} | ||
|
||
fn ptr(&self) -> NonNull<T> { | ||
self.ptr | ||
} | ||
|
||
fn const_ptr(&self) -> *const T { | ||
self.ptr.as_ptr() | ||
} | ||
|
||
fn to_view(&self) -> DataView<T> { | ||
let ptr = self.ptr; | ||
let len = self.len; | ||
DataView { ptr, len } | ||
} | ||
} | ||
impl<T: RawDataType> DataBuffer for DataView<T> { | ||
type DType = T; | ||
|
||
fn len(&self) -> usize { | ||
self.len | ||
} | ||
|
||
fn ptr(&self) -> NonNull<T> { | ||
self.ptr | ||
} | ||
|
||
fn const_ptr(&self) -> *const T { | ||
self.ptr.as_ptr() | ||
} | ||
|
||
fn to_view(&self) -> DataView<T> { | ||
(*self).clone() | ||
} | ||
} |
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,70 +1,56 @@ | ||
pub(super) mod clone; | ||
pub(super) mod data_owned; | ||
pub(super) mod data_view; | ||
pub(super) mod buffer; | ||
|
||
pub(super) use crate::data_buffer::buffer::DataBuffer; | ||
pub(super) use crate::data_buffer::data_owned::DataOwned; | ||
pub(super) use crate::data_buffer::data_view::DataView; | ||
|
||
use crate::tensor::dtype::RawDataType; | ||
|
||
use std::ops::Index; | ||
use std::ptr::NonNull; | ||
|
||
pub trait DataBuffer: Index<usize> { | ||
type DType: RawDataType; | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::data_buffer::DataOwned; | ||
|
||
fn len(&self) -> usize; | ||
#[test] | ||
fn from_vector() { | ||
let arr = DataOwned::from(vec![0, 50, 100]); | ||
assert_eq!(arr.len(), &3); | ||
|
||
fn ptr(&self) -> NonNull<Self::DType>; | ||
let arr = DataOwned::from(vec![vec![50], vec![50], vec![50]]); | ||
assert_eq!(arr.len(), &3); | ||
|
||
fn const_ptr(&self) -> *const Self::DType; | ||
let arr = DataOwned::from(vec![vec![vec![50]], vec![vec![50]]]); | ||
assert_eq!(arr.len(), &2); | ||
|
||
fn to_view(&self) -> DataView<Self::DType>; | ||
|
||
// fn clone(&self) -> DataOwned<Self::DType>; | ||
} | ||
|
||
// Two kinds of data buffers | ||
// DataOwned: owns its data & responsible for cleaning it up | ||
// DataView: reference to data owned by another buffer | ||
|
||
impl<T: RawDataType> DataBuffer for DataOwned<T> { | ||
type DType = T; | ||
|
||
fn len(&self) -> usize { | ||
self.len | ||
} | ||
|
||
fn ptr(&self) -> NonNull<T> { | ||
self.ptr | ||
let arr = DataOwned::from(vec![vec![vec![50, 50, 50]], vec![vec![50, 50, 50]]]); | ||
assert_eq!(arr.len(), &6); | ||
} | ||
|
||
fn const_ptr(&self) -> *const T { | ||
self.ptr.as_ptr() | ||
} | ||
#[test] | ||
fn from_array() { | ||
let arr = DataOwned::from([500, 50, 100]); | ||
assert_eq!(arr.len(), &3); | ||
|
||
fn to_view(&self) -> DataView<T> { | ||
let ptr = self.ptr; | ||
let len = self.len; | ||
DataView { ptr, len } | ||
} | ||
} | ||
impl<T: RawDataType> DataBuffer for DataView<T> { | ||
type DType = T; | ||
let arr = DataOwned::from([[500], [50], [100]]); | ||
assert_eq!(arr.len(), &3); | ||
|
||
fn len(&self) -> usize { | ||
self.len | ||
} | ||
let arr = DataOwned::from([[[500], [50], [30]], [[50], [0], [0]]]); | ||
assert_eq!(arr.len(), &6); | ||
|
||
fn ptr(&self) -> NonNull<T> { | ||
self.ptr | ||
let arr = DataOwned::from([[[50, 50]], [[50, 50]]]); | ||
assert_eq!(arr.len(), &4); | ||
} | ||
|
||
fn const_ptr(&self) -> *const T { | ||
self.ptr.as_ptr() | ||
} | ||
#[test] | ||
fn from_inhomogeneous_vector() { | ||
let arr = DataOwned::from(vec![vec![50, 50], vec![50]]); | ||
assert_eq!(arr.len(), &3); | ||
|
||
fn to_view(&self) -> DataView<T> { | ||
(*self).clone() | ||
let arr = DataOwned::from(vec![vec![vec![50, 50]], vec![vec![50]], vec![vec![50]]]); | ||
assert_eq!(arr.len(), &4); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.