arr_rs/alphanumeric/operations/compare.rs
1use crate::{
2 alphanumeric::prelude::*,
3 core::prelude::*,
4 errors::prelude::*,
5};
6use crate::core::types::compare::CompareOpType;
7
8/// `ArrayTrait` - Alphanumeric Array operations
9pub trait ArrayStringCompare<N: Alphanumeric> where Self: Sized + Clone {
10
11 /// Return (self == other) element-wise
12 ///
13 /// # Arguments
14 ///
15 /// * `other` - array to perform the operation with
16 ///
17 /// # Examples
18 ///
19 /// ```
20 /// use arr_rs::prelude::*;
21 ///
22 /// let expected = Array::flat(vec![true, false]);
23 /// let arr = Array::flat(vec!["aaa".to_string(), "bbbxx".to_string()]);
24 /// let other = Array::flat(vec!["aaa".to_string(), "bbbbb".to_string()]).unwrap();
25 /// assert_eq!(expected, arr.equal(&other));
26 /// ```
27 ///
28 /// # Errors
29 ///
30 /// may returns `ArrayError`
31 fn equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError>;
32
33 /// Return (self != other) element-wise
34 ///
35 /// # Arguments
36 ///
37 /// * `other` - array to perform the operation with
38 ///
39 /// # Examples
40 ///
41 /// ```
42 /// use arr_rs::prelude::*;
43 ///
44 /// let expected = Array::flat(vec![false, true]);
45 /// let arr = Array::flat(vec!["aaa".to_string(), "bbbxx".to_string()]);
46 /// let other = Array::flat(vec!["aaa".to_string(), "bbbbb".to_string()]).unwrap();
47 /// assert_eq!(expected, arr.not_equal(&other));
48 /// ```
49 ///
50 /// # Errors
51 ///
52 /// may returns `ArrayError`
53 fn not_equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError>;
54
55 /// Return (self >= other) element-wise
56 ///
57 /// # Arguments
58 ///
59 /// * `other` - array to perform the operation with
60 ///
61 /// # Examples
62 ///
63 /// ```
64 /// use arr_rs::prelude::*;
65 ///
66 /// let expected = Array::flat(vec![true, false, true]);
67 /// let arr = Array::flat(vec!["aaa".to_string(), "aaa".to_string(), "bbbxx".to_string()]);
68 /// let other = Array::flat(vec!["aaa".to_string(), "aba".to_string(), "bbbbb".to_string()]).unwrap();
69 /// assert_eq!(expected, arr.greater_equal(&other));
70 /// ```
71 ///
72 /// # Errors
73 ///
74 /// may returns `ArrayError`
75 fn greater_equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError>;
76
77 /// Return (self <= other) element-wise
78 ///
79 /// # Arguments
80 ///
81 /// * `other` - array to perform the operation with
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use arr_rs::prelude::*;
87 ///
88 /// let expected = Array::flat(vec![true, true, false]);
89 /// let arr = Array::flat(vec!["aaa".to_string(), "aaa".to_string(), "bbbxx".to_string()]);
90 /// let other = Array::flat(vec!["aaa".to_string(), "aba".to_string(), "bbbbb".to_string()]).unwrap();
91 /// assert_eq!(expected, arr.less_equal(&other));
92 /// ```
93 ///
94 /// # Errors
95 ///
96 /// may returns `ArrayError`
97 fn less_equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError>;
98
99 /// Return (self > other) element-wise
100 ///
101 /// # Arguments
102 ///
103 /// * `other` - array to perform the operation with
104 ///
105 /// # Examples
106 ///
107 /// ```
108 /// use arr_rs::prelude::*;
109 ///
110 /// let expected = Array::flat(vec![false, false, true]);
111 /// let arr = Array::flat(vec!["aaa".to_string(), "aaa".to_string(), "bbbxx".to_string()]);
112 /// let other = Array::flat(vec!["aaa".to_string(), "aba".to_string(), "bbbbb".to_string()]).unwrap();
113 /// assert_eq!(expected, arr.greater(&other));
114 /// ```
115 ///
116 /// # Errors
117 ///
118 /// may returns `ArrayError`
119 fn greater(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError>;
120
121 /// Return (self < other) element-wise
122 ///
123 /// # Arguments
124 ///
125 /// * `other` - array to perform the operation with
126 ///
127 /// # Examples
128 ///
129 /// ```
130 /// use arr_rs::prelude::*;
131 ///
132 /// let expected = Array::flat(vec![false, true, false]);
133 /// let arr = Array::flat(vec!["aaa".to_string(), "aaa".to_string(), "bbbxx".to_string()]);
134 /// let other = Array::flat(vec!["aaa".to_string(), "aba".to_string(), "bbbbb".to_string()]).unwrap();
135 /// assert_eq!(expected, arr.less(&other));
136 /// ```
137 ///
138 /// # Errors
139 ///
140 /// may returns `ArrayError`
141 fn less(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError>;
142
143 /// Performs element-wise comparison of two string arrays using the comparison operator specified by `cmp_op`
144 ///
145 /// # Arguments
146 ///
147 /// * `other` - array to perform the operation with
148 /// * `cmp_op` - Type of comparison: {“<”, “<=”, “==”, “>=”, “>”, “!=”}
149 ///
150 /// # Examples
151 ///
152 /// ```
153 /// use arr_rs::prelude::*;
154 ///
155 /// let arr = Array::flat(vec!["aaa".to_string(), "bbbxx".to_string()]);
156 /// let other = Array::flat(vec!["aaa".to_string(), "bbbbb".to_string()]).unwrap();
157 ///
158 /// let expected = Array::flat(vec![true, false]);
159 /// assert_eq!(expected, arr.compare(&other, "=="));
160 ///
161 /// let expected = Array::flat(vec![false, true]);
162 /// assert_eq!(expected, arr.compare(&other, "!="));
163 ///
164 /// let expected = Array::flat(vec![true, true]);
165 /// assert_eq!(expected, arr.compare(&other, ">="));
166 ///
167 /// let expected = Array::flat(vec![true, false]);
168 /// assert_eq!(expected, arr.compare(&other, "<="));
169 ///
170 /// let expected = Array::flat(vec![false, true]);
171 /// assert_eq!(expected, arr.compare(&other, ">"));
172 ///
173 /// let expected = Array::flat(vec![false, false]);
174 /// assert_eq!(expected, arr.compare(&other, "<"));
175 /// ```
176 ///
177 /// # Errors
178 ///
179 /// may returns `ArrayError`
180 fn compare(&self, other: &Array<N>, cmp_op: impl CompareOpType) -> Result<Array<bool>, ArrayError>;
181}
182
183impl <N: Alphanumeric> ArrayStringCompare<N> for Array<N> {
184
185 fn equal(&self, other: &Self) -> Result<Array<bool>, ArrayError> {
186 let broadcasted = self.broadcast(other)?;
187 let elements = broadcasted.clone().into_iter()
188 .map(|tuple| tuple.0._equal(tuple.1))
189 .collect();
190 Array::new(elements, broadcasted.get_shape()?)
191 }
192
193 fn not_equal(&self, other: &Self) -> Result<Array<bool>, ArrayError> {
194 let broadcasted = self.broadcast(other)?;
195 let elements = broadcasted.clone().into_iter()
196 .map(|tuple| tuple.0._not_equal(tuple.1))
197 .collect();
198 Array::new(elements, broadcasted.get_shape()?)
199 }
200
201 fn greater_equal(&self, other: &Self) -> Result<Array<bool>, ArrayError> {
202 let broadcasted = self.broadcast(other)?;
203 let elements = broadcasted.clone().into_iter()
204 .map(|tuple| tuple.0._greater_equal(tuple.1))
205 .collect();
206 Array::new(elements, broadcasted.get_shape()?)
207 }
208
209 fn less_equal(&self, other: &Self) -> Result<Array<bool>, ArrayError> {
210 let broadcasted = self.broadcast(other)?;
211 let elements = broadcasted.clone().into_iter()
212 .map(|tuple| tuple.0._less_equal(tuple.1))
213 .collect();
214 Array::new(elements, broadcasted.get_shape()?)
215 }
216
217 fn greater(&self, other: &Self) -> Result<Array<bool>, ArrayError> {
218 let broadcasted = self.broadcast(other)?;
219 let elements = broadcasted.clone().into_iter()
220 .map(|tuple| tuple.0._greater(tuple.1))
221 .collect();
222 Array::new(elements, broadcasted.get_shape()?)
223 }
224
225 fn less(&self, other: &Self) -> Result<Array<bool>, ArrayError> {
226 let broadcasted = self.broadcast(other)?;
227 let elements = broadcasted.clone().into_iter()
228 .map(|tuple| tuple.0._less(tuple.1))
229 .collect();
230 Array::new(elements, broadcasted.get_shape()?)
231 }
232
233 fn compare(&self, other: &Self, cmp_op: impl CompareOpType) -> Result<Array<bool>, ArrayError> {
234 let cmp_op = cmp_op.parse_type()?;
235 match cmp_op {
236 CompareOp::Equals => self.equal(other),
237 CompareOp::NotEquals => self.not_equal(other),
238 CompareOp::GreaterEqual => self.greater_equal(other),
239 CompareOp::LessEqual => self.less_equal(other),
240 CompareOp::Greater => self.greater(other),
241 CompareOp::Less => self.less(other),
242 }
243 }
244}
245
246impl <N: Alphanumeric> ArrayStringCompare<N> for Result<Array<N>, ArrayError> {
247
248 fn equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError> {
249 self.clone()?.equal(other)
250 }
251
252 fn not_equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError> {
253 self.clone()?.not_equal(other)
254 }
255
256 fn greater_equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError> {
257 self.clone()?.greater_equal(other)
258 }
259
260 fn less_equal(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError> {
261 self.clone()?.less_equal(other)
262 }
263
264 fn greater(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError> {
265 self.clone()?.greater(other)
266 }
267
268 fn less(&self, other: &Array<N>) -> Result<Array<bool>, ArrayError> {
269 self.clone()?.less(other)
270 }
271
272 fn compare(&self, other: &Array<N>, cmp_op: impl CompareOpType) -> Result<Array<bool>, ArrayError> {
273 self.clone()?.compare(other, cmp_op)
274 }
275}