1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
use crate::BinaryNumber; use is_sorted::IsSorted; use std::collections::HashMap; use std::fmt; use std::ops::{Add, Deref}; mod bitwise_operations; use bitwise_operations::BitwiseZipIter; /// A sparse binary vector. /// /// There are two main variants of a vector, /// the owned one, [`SparseBinVec`](crate::SparseBinVec), and the borrowed one, /// [`SparseBinSlice`](crate::SparseBinSlice). /// Most of the time, you want to create a owned version. /// However, some iterators, such as those defined on [`SparseBinMat`](crate::SparseBinMat) /// returns the borrowed version. #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct SparseBinVecBase<T> { positions: T, length: usize, } pub type SparseBinVec = SparseBinVecBase<Vec<usize>>; pub type SparseBinSlice<'a> = SparseBinVecBase<&'a [usize]>; impl SparseBinVec { /// Creates a new vector with the given length /// and list of non trivial positions. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(5, vec![0, 2]); /// /// assert_eq!(vector.len(), 5); /// assert_eq!(vector.weight(), 2); /// /// ``` /// /// # Panic /// /// Panics if a position is greater or equal to the length. /// /// ```should_panic /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(2, vec![1, 3]); /// ``` pub fn new(length: usize, mut positions: Vec<usize>) -> Self { for position in positions.iter() { if *position >= length { panic!( "position {} is out of bound for length {}", position, length ); } } positions.sort(); Self { positions, length } } /// Creates a vector fill with zeros of the given length. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::zeros(3); /// /// assert_eq!(vector.len(), 3); /// assert_eq!(vector.weight(), 0); /// ``` pub fn zeros(length: usize) -> Self { Self::new(length, Vec::new()) } /// Creates an empty vector. /// /// This allocate minimally, so it is a good placeholder. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::empty(); /// /// assert_eq!(vector.len(), 0); /// assert_eq!(vector.weight(), 0); /// ``` pub fn empty() -> Self { Self { length: 0, positions: Vec::new(), } } /// Converts the sparse binary vector to a `Vec` of /// the non trivial positions. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(3, vec![0, 2]); /// /// assert_eq!(vector.to_positions_vec(), vec![0, 2]); /// ``` pub fn to_positions_vec(self) -> Vec<usize> { self.positions } } impl<'a> SparseBinSlice<'a> { /// Creates a new vector with the given length /// and list of non trivial positions. /// /// This take a mutable reference to the positions in order to sort them. /// If you know the positions are sorted, you can instead use /// [`new_from_sorted`](SparseBinSlice::new_from_sorted). /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinSlice; /// let mut positions = vec![2, 0]; /// let vector = SparseBinSlice::new(5, &mut positions); /// /// assert_eq!(vector.len(), 5); /// assert_eq!(vector.weight(), 2); /// assert_eq!(vector.non_trivial_positions().collect::<Vec<_>>(), vec![0, 2]); /// ``` /// /// # Panic /// /// Panics if a position is greater or equal to the length. /// /// ```should_panic /// # use sparse_bin_mat::SparseBinSlice; /// let vector = SparseBinSlice::new(2, &mut [1, 3]); /// ``` pub fn new(length: usize, positions: &'a mut [usize]) -> Self { for position in positions.iter() { if *position >= length { panic!( "position {} is out of bound for length {}", position, length ); } } positions.sort(); Self { positions, length } } /// Creates a new vector with the given length and a sorted list of non trivial positions. /// //// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinSlice; /// let mut positions = vec![0, 2]; /// let vector = SparseBinSlice::new_from_sorted(5, &positions); /// /// assert_eq!(vector.len(), 5); /// assert_eq!(vector.weight(), 2); /// ``` /// /// # Panics /// /// Panics if the list of positions is unsorted or if a position is greater or equal to the /// length. /// /// ```should_panic /// # use sparse_bin_mat::SparseBinSlice; /// let mut positions = vec![2, 0]; /// let vector = SparseBinSlice::new_from_sorted(5, &positions); /// ``` pub fn new_from_sorted(length: usize, positions: &'a [usize]) -> Self { for position in positions.iter() { if *position >= length { panic!( "position {} is out of bound for length {}", position, length ); } } // Waiting for the is_sorted API to stabilize in std. // https://github.com/rust-lang/rust/issues/53485 if !IsSorted::is_sorted(&mut positions.iter()) { panic!("positions are unsorted"); } Self { length, positions } } } impl<T: Deref<Target = [usize]>> SparseBinVecBase<T> { /// Returns the length (number of elements) of the vector. pub fn len(&self) -> usize { self.length } /// Returns the number of elements with value 1 in the vector. pub fn weight(&self) -> usize { self.positions.len() } /// Returns true if the length of the vector is 0. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns true of all the element in the vector are 0. pub fn is_zero(&self) -> bool { self.weight() == 0 } /// Returns the value at the given position /// or None if the position is out of bound. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(3, vec![0, 2]); /// /// assert_eq!(vector.get(0), Some(1)); /// assert_eq!(vector.get(1), Some(0)); /// assert_eq!(vector.get(2), Some(1)); /// assert_eq!(vector.get(3), None); /// ``` pub fn get(&self, position: usize) -> Option<BinaryNumber> { if position < self.len() { if self.positions.contains(&position) { Some(1) } else { Some(0) } } else { None } } /// Returns true if the value at the given position 0 /// or None if the position is out of bound. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(3, vec![0, 2]); /// /// assert_eq!(vector.is_zero_at(0), Some(false)); /// assert_eq!(vector.is_zero_at(1), Some(true)); /// assert_eq!(vector.is_zero_at(2), Some(false)); /// assert_eq!(vector.is_zero_at(3), None); /// ``` pub fn is_zero_at(&self, position: usize) -> Option<bool> { self.get(position).map(|value| value == 0) } /// Returns true if the value at the given position 1 /// or None if the position is out of bound. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(3, vec![0, 2]); /// /// assert_eq!(vector.is_one_at(0), Some(true)); /// assert_eq!(vector.is_one_at(1), Some(false)); /// assert_eq!(vector.is_one_at(2), Some(true)); /// assert_eq!(vector.is_one_at(3), None); /// ``` pub fn is_one_at(&self, position: usize) -> Option<bool> { self.get(position).map(|value| value == 1) } /// Returns an iterator over all positions where the value is 1. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(5, vec![0, 1, 3]); /// let mut iter = vector.non_trivial_positions(); /// /// assert_eq!(iter.next(), Some(0)); /// assert_eq!(iter.next(), Some(1)); /// assert_eq!(iter.next(), Some(3)); /// assert_eq!(iter.next(), None); /// ``` pub fn non_trivial_positions<'a>(&'a self) -> impl Iterator<Item = usize> + 'a { self.positions.iter().cloned() } /// Returns the concatenation of two vectors. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let left_vector = SparseBinVec::new(3, vec![0, 1]); /// let right_vector = SparseBinVec::new(4, vec![2, 3]); /// /// let concatened = left_vector.concat(&right_vector); /// /// let expected = SparseBinVec::new(7, vec![0, 1, 5, 6]); /// /// assert_eq!(concatened, expected); /// ``` pub fn concat(&self, other: &Self) -> SparseBinVec { let positions = self .non_trivial_positions() .chain(other.non_trivial_positions().map(|pos| pos + self.len())) .collect(); SparseBinVec::new(self.len() + other.len(), positions) } /// Returns a new vector keeping only the given positions. /// /// Positions are relabeled to the fit new number of positions. /// /// # Example /// /// ``` /// use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(5, vec![0, 2, 4]); /// let truncated = SparseBinVec::new(3, vec![0, 2]); /// /// assert_eq!(vector.keep_only_positions(&[0, 1, 2]), truncated); /// assert_eq!(vector.keep_only_positions(&[1, 2]).len(), 2); /// ``` /// /// # Panic /// /// Panics if some positions are out of bound. pub fn keep_only_positions(&self, positions: &[usize]) -> SparseBinVec { for position in positions { if *position >= self.len() { panic!( "position {} is out of bound for length {}", position, self.len() ); } } let old_to_new_positions_map = positions .iter() .enumerate() .map(|(new, old)| (old, new)) .collect::<HashMap<_, _>>(); let new_positions = self .non_trivial_positions() .filter_map(|position| old_to_new_positions_map.get(&position).cloned()) .collect(); SparseBinVec::new(positions.len(), new_positions) } /// Returns a truncated vector where the given positions are removed. /// /// Positions are relabeled to fit the new number of positions. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let vector = SparseBinVec::new(5, vec![0, 2, 4]); /// let truncated = SparseBinVec::new(3, vec![0, 2]); /// /// assert_eq!(vector.without_positions(&[3, 4]), truncated); /// assert_eq!(vector.without_positions(&[1, 2]).len(), 3); /// ``` /// /// # Panic /// /// Panics if some positions are out of bound. pub fn without_positions(&self, positions: &[usize]) -> SparseBinVec { let to_keep: Vec<usize> = (0..self.len()).filter(|x| !positions.contains(x)).collect(); self.keep_only_positions(&to_keep) } /// Returns a view over the vector. pub fn as_view(&self) -> SparseBinSlice { SparseBinSlice { length: self.length, positions: &self.positions, } } /// Returns a slice of the non trivial positions. pub fn as_slice(&self) -> &[usize] { self.positions.as_ref() } /// Returns an owned version of the vector. pub fn to_owned(self) -> SparseBinVec { SparseBinVec { length: self.length, positions: self.positions.to_owned(), } } /// Returns the dot product of two vectors. /// /// # Example /// /// ``` /// # use sparse_bin_mat::SparseBinVec; /// let first = SparseBinVec::new(4, vec![0, 1, 2]); /// let second = SparseBinVec::new(4, vec![1, 2, 3]); /// let third = SparseBinVec::new(4, vec![0, 3]); /// /// assert_eq!(first.dot_with(&second), 0); /// assert_eq!(first.dot_with(&third), 1); /// ``` pub fn dot_with<S: Deref<Target = [usize]>>( &self, other: &SparseBinVecBase<S>, ) -> BinaryNumber { BitwiseZipIter::new(self.as_view(), other.as_view()) .fold(0, |sum, x| sum ^ x.first_row_value * x.second_row_value) } } impl<S, T> Add<&SparseBinVecBase<S>> for &SparseBinVecBase<T> where S: Deref<Target = [usize]>, T: Deref<Target = [usize]>, { type Output = SparseBinVec; fn add(self, other: &SparseBinVecBase<S>) -> SparseBinVec { if self.len() != other.len() { panic!( "vector of length {} can't be added to vector of length {}", self.len(), other.len() ); } let positions = BitwiseZipIter::new(self.as_view(), other.as_view()) .filter_map(|x| { if x.first_row_value ^ x.second_row_value == 1 { Some(x.position) } else { None } }) .collect(); SparseBinVec::new(self.len(), positions) } } impl<T: Deref<Target = [usize]>> fmt::Display for SparseBinVecBase<T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self.positions.deref()) } } #[cfg(test)] mod test { use super::*; #[test] fn positions_are_sorted_on_construction() { let vector = SparseBinVec::new(4, vec![3, 0, 2]); assert_eq!( vector.non_trivial_positions().collect::<Vec<_>>(), vec![0, 2, 3] ) } #[test] fn addition() { let first_vector = SparseBinVec::new(6, vec![0, 2, 4]); let second_vector = SparseBinVec::new(6, vec![0, 1, 2]); let sum = SparseBinVec::new(6, vec![1, 4]); assert_eq!(&first_vector + &second_vector, sum); } #[test] fn panics_on_addition_if_different_length() { let vector_6 = SparseBinVec::new(6, vec![0, 2, 4]); let vector_2 = SparseBinVec::new(2, vec![0]); let result = std::panic::catch_unwind(|| &vector_6 + &vector_2); assert!(result.is_err()); } }