1use crate::complex::{c64, c128};
5use crate::dtype::{DType, Element};
6use half::{bf16, f16};
7
8#[derive(Clone, PartialEq, Debug)]
10pub enum Buffer {
11 Bool(Vec<bool>),
13 I8(Vec<i8>),
15 I16(Vec<i16>),
17 I32(Vec<i32>),
19 I64(Vec<i64>),
21 U8(Vec<u8>),
23 U16(Vec<u16>),
25 U32(Vec<u32>),
27 U64(Vec<u64>),
29 F16(Vec<f16>),
31 BF16(Vec<bf16>),
33 F32(Vec<f32>),
35 F64(Vec<f64>),
37 C64(Vec<c64>),
39 C128(Vec<c128>),
41}
42
43#[macro_export]
47macro_rules! with_buffer {
48 ($buf:expr, |$v:ident| $body:expr) => {
49 match $buf {
50 $crate::Buffer::Bool($v) => $body,
51 $crate::Buffer::I8($v) => $body,
52 $crate::Buffer::I16($v) => $body,
53 $crate::Buffer::I32($v) => $body,
54 $crate::Buffer::I64($v) => $body,
55 $crate::Buffer::U8($v) => $body,
56 $crate::Buffer::U16($v) => $body,
57 $crate::Buffer::U32($v) => $body,
58 $crate::Buffer::U64($v) => $body,
59 $crate::Buffer::F16($v) => $body,
60 $crate::Buffer::BF16($v) => $body,
61 $crate::Buffer::F32($v) => $body,
62 $crate::Buffer::F64($v) => $body,
63 $crate::Buffer::C64($v) => $body,
64 $crate::Buffer::C128($v) => $body,
65 }
66 };
67}
68
69#[macro_export]
72macro_rules! with_dtype {
73 ($dtype:expr, |$t:ident| $body:expr) => {
74 match $dtype {
75 $crate::DType::Bool => {
76 type $t = bool;
77 $body
78 }
79 $crate::DType::I8 => {
80 type $t = i8;
81 $body
82 }
83 $crate::DType::I16 => {
84 type $t = i16;
85 $body
86 }
87 $crate::DType::I32 => {
88 type $t = i32;
89 $body
90 }
91 $crate::DType::I64 => {
92 type $t = i64;
93 $body
94 }
95 $crate::DType::U8 => {
96 type $t = u8;
97 $body
98 }
99 $crate::DType::U16 => {
100 type $t = u16;
101 $body
102 }
103 $crate::DType::U32 => {
104 type $t = u32;
105 $body
106 }
107 $crate::DType::U64 => {
108 type $t = u64;
109 $body
110 }
111 $crate::DType::F16 => {
112 type $t = $crate::f16;
113 $body
114 }
115 $crate::DType::BF16 => {
116 type $t = $crate::bf16;
117 $body
118 }
119 $crate::DType::F32 => {
120 type $t = f32;
121 $body
122 }
123 $crate::DType::F64 => {
124 type $t = f64;
125 $body
126 }
127 $crate::DType::C64 => {
128 type $t = $crate::c64;
129 $body
130 }
131 $crate::DType::C128 => {
132 type $t = $crate::c128;
133 $body
134 }
135 }
136 };
137}
138
139impl Buffer {
140 pub fn dtype(&self) -> DType {
142 with_buffer!(self, |v| element_dtype(v))
143 }
144
145 pub fn len(&self) -> usize {
147 with_buffer!(self, |v| v.len())
148 }
149
150 pub fn is_empty(&self) -> bool {
152 self.len() == 0
153 }
154
155 pub fn convert(&self, dtype: DType) -> Buffer {
157 with_buffer!(self, |v| with_dtype!(dtype, |U| Buffer::from_vec(
158 v.iter()
159 .map(|&x| crate::cast::<_, U>(x))
160 .collect::<Vec<U>>()
161 )))
162 }
163
164 pub fn from_vec<T: Element>(v: Vec<T>) -> Buffer {
166 T::wrap(v)
167 }
168
169 pub fn as_slice<T: Element>(&self) -> Option<&[T]> {
171 T::view(self)
172 }
173}
174
175fn element_dtype<T: Element>(_: &[T]) -> DType {
176 T::DTYPE
177}
178
179pub trait BufferElement: Sized {
181 fn wrap(v: Vec<Self>) -> Buffer;
183 fn view(b: &Buffer) -> Option<&[Self]>;
185}
186
187macro_rules! impl_buffer_element {
188 ($($t:ty => $v:ident;)*) => {$(
189 impl BufferElement for $t {
190 fn wrap(v: Vec<Self>) -> Buffer {
191 Buffer::$v(v)
192 }
193 fn view(b: &Buffer) -> Option<&[Self]> {
194 match b {
195 Buffer::$v(v) => Some(v),
196 _ => None,
197 }
198 }
199 }
200 )*};
201}
202
203impl_buffer_element! {
204 bool => Bool; i8 => I8; i16 => I16; i32 => I32; i64 => I64;
205 u8 => U8; u16 => U16; u32 => U32; u64 => U64;
206 f16 => F16; bf16 => BF16; f32 => F32; f64 => F64; c64 => C64; c128 => C128;
207}