microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/paulimer/src/bits/bitview.rs
76lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use super::bitvec::WORD_COUNT_DEFAULT; |
| 5 | use super::standard_types::BitsPerBlock; |
| 6 | use super::BitVec; |
| 7 | use crate::bits::bitblock::Word; |
| 8 | |
| 9 | #[derive(Eq, Hash)] |
| 10 | pub struct BitView<'life, const WORD_COUNT: usize = WORD_COUNT_DEFAULT> { |
| 11 | pub blocks: &'life [[Word; WORD_COUNT]], |
| 12 | } |
| 13 | |
| 14 | // Should we use convention <TypeName>Mutable or Mutable<TypeName> ? |
| 15 | #[derive(Eq, Hash)] |
| 16 | pub struct MutableBitView<'life, const WORD_COUNT: usize = WORD_COUNT_DEFAULT> { |
| 17 | pub blocks: &'life mut [[Word; WORD_COUNT]], |
| 18 | } |
| 19 | |
| 20 | impl<const WORD_COUNT: usize> BitView<'_, WORD_COUNT> { |
| 21 | #[must_use] |
| 22 | pub fn len(&self) -> usize { |
| 23 | self.blocks.len() * <[Word; WORD_COUNT]>::BITS_PER_BLOCK |
| 24 | } |
| 25 | |
| 26 | #[must_use] |
| 27 | pub fn is_empty(&self) -> bool { |
| 28 | self.blocks.len() == 0 |
| 29 | } |
| 30 | |
| 31 | #[must_use] |
| 32 | pub fn top(&self) -> u64 { |
| 33 | self.blocks[0][0] |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<const WORD_COUNT: usize> MutableBitView<'_, WORD_COUNT> { |
| 38 | #[must_use] |
| 39 | pub fn len(&self) -> usize { |
| 40 | self.blocks.len() * <[Word; WORD_COUNT]>::BITS_PER_BLOCK |
| 41 | } |
| 42 | |
| 43 | #[must_use] |
| 44 | pub fn is_empty(&self) -> bool { |
| 45 | self.blocks.len() == 0 |
| 46 | } |
| 47 | |
| 48 | #[must_use] |
| 49 | pub fn top(&self) -> u64 { |
| 50 | self.blocks[0][0] |
| 51 | } |
| 52 | |
| 53 | pub fn top_mut(&mut self) -> &mut u64 { |
| 54 | &mut self.blocks[0][0] |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl<'life, const WORD_COUNT: usize> From<BitView<'life, WORD_COUNT>> for BitVec<WORD_COUNT> { |
| 59 | fn from(value: BitView<'life, WORD_COUNT>) -> Self { |
| 60 | Self::from_view(&value) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl<'life, const WORD_COUNT: usize> From<&'life BitVec<WORD_COUNT>> for BitVec<WORD_COUNT> { |
| 65 | fn from(value: &'life BitVec<WORD_COUNT>) -> Self { |
| 66 | value.clone() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl<'life, const WORD_COUNT: usize> From<MutableBitView<'life, WORD_COUNT>> |
| 71 | for BitVec<WORD_COUNT> |
| 72 | { |
| 73 | fn from(value: MutableBitView<'life, WORD_COUNT>) -> Self { |
| 74 | Self::from_view_mut(&value) |
| 75 | } |
| 76 | } |
| 77 | |