microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/paulimer/src/bits.rs
105lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | pub mod bitblock; |
| 5 | |
| 6 | pub mod standard_types; |
| 7 | pub use standard_types::are_supports_equal; |
| 8 | |
| 9 | mod bitvec; |
| 10 | pub use bitvec::{BitVec, WORD_COUNT_DEFAULT}; |
| 11 | |
| 12 | mod bitview; |
| 13 | pub use bitview::{BitView, MutableBitView}; |
| 14 | |
| 15 | mod index_set; |
| 16 | pub use index_set::{remapped, IndexSet}; |
| 17 | |
| 18 | pub mod bitmatrix; |
| 19 | pub use bitmatrix::BitMatrix; |
| 20 | |
| 21 | // Unary traits, involve only one type |
| 22 | use crate::NeutralElement; |
| 23 | |
| 24 | pub trait BitwiseNeutralElement: |
| 25 | Bitwise |
| 26 | + NeutralElement<NeutralElementType: BitwiseBinaryOps<Self> + NeutralElement + IndexAssignable> |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | pub trait Bitwise { |
| 31 | fn index(&self, index: usize) -> bool; |
| 32 | fn support(&self) -> impl sorted_iter::SortedIterator<Item = usize>; |
| 33 | fn weight(&self) -> usize { |
| 34 | self.support().count() |
| 35 | } |
| 36 | fn parity(&self) -> bool { |
| 37 | (self.weight() % 2) == 1 |
| 38 | } |
| 39 | fn is_zero(&self) -> bool { |
| 40 | self.weight() == 0 |
| 41 | } |
| 42 | fn is_one_bit(&self, index: usize) -> bool { |
| 43 | self.weight() == 1 && self.index(index) |
| 44 | } |
| 45 | fn max_bit_id(&self) -> Option<usize> { |
| 46 | self.support().last() |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub trait IndexAssignable: Bitwise { |
| 51 | fn assign_index(&mut self, index: usize, to: bool); |
| 52 | fn negate_index(&mut self, index: usize); |
| 53 | fn clear_bits(&mut self); |
| 54 | fn set_random(&mut self, num_bits: usize, random_number_generator: &mut impl rand::Rng) { |
| 55 | for j in 0..num_bits { |
| 56 | self.assign_index(j, random_number_generator.gen()); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub trait BorrowAsBitIterator { |
| 62 | type BitIterator<'life>: ExactSizeIterator<Item = bool> |
| 63 | where |
| 64 | Self: 'life; |
| 65 | fn borrow_as_bit_iterator(&self) -> Self::BitIterator<'_>; |
| 66 | } |
| 67 | |
| 68 | // Binary traits, involve two types |
| 69 | |
| 70 | pub trait BitwiseBinaryOps<Other: ?Sized + Bitwise = Self>: Bitwise + IndexAssignable { |
| 71 | fn assign(&mut self, other: &Other); |
| 72 | |
| 73 | fn assign_with_offset(&mut self, other: &Other, start_bit: usize, num_bits: usize) { |
| 74 | for bit_index in 0..num_bits { |
| 75 | self.assign_index(bit_index + start_bit, other.index(bit_index)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn assign_from_interval(&mut self, other: &Other, start_bit: usize, num_bits: usize) { |
| 80 | for bit_index in 0..num_bits { |
| 81 | self.assign_index(bit_index, other.index(start_bit + bit_index)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn bitxor_assign(&mut self, other: &Other); |
| 86 | fn bitand_assign(&mut self, other: &Other); |
| 87 | } |
| 88 | |
| 89 | // TODO: should we eliminate Dot and move `dot` into OverlapWeight ? |
| 90 | pub trait Dot<Other: ?Sized = Self> { |
| 91 | fn dot(&self, other: &Other) -> bool; |
| 92 | } |
| 93 | |
| 94 | pub trait OverlapWeight<Other: ?Sized = Self> { |
| 95 | fn and_weight(&self, other: &Other) -> usize; |
| 96 | fn or_weight(&self, other: &Other) -> usize; |
| 97 | } |
| 98 | |
| 99 | pub trait FromBits<Other> { |
| 100 | fn from_bits(other: &Other) -> Self; |
| 101 | } |
| 102 | |
| 103 | //TODO: Should BitVec, BitView, MutableBitView, BitBlock thoroughly implement standard traits such as : BitXor, BitAnd, BitXorAssign, BitAndAssign, Index, FromIterator<bool>, Display ? |
| 104 | pub mod bitchunk; |
| 105 | pub mod tiny_matrix; |
| 106 | |