microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fc18cc36fceb28438976110616dfee7b8b01bd85

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/paulimer/src/bits.rs

106lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4pub mod bitblock;
5
6pub mod standard_types;
7pub use standard_types::are_supports_equal;
8
9mod bitvec;
10pub use bitvec::{BitVec, WORD_COUNT_DEFAULT};
11
12mod bitview;
13pub use bitview::{BitView, MutableBitView};
14
15mod index_set;
16pub use index_set::{remapped, IndexSet};
17
18pub mod bitmatrix;
19pub use bitmatrix::BitMatrix;
20
21// Unary traits, involve only one type
22use crate::NeutralElement;
23
24pub trait BitwiseNeutralElement:
25 Bitwise
26 + NeutralElement<NeutralElementType: BitwiseBinaryOps<Self> + NeutralElement + IndexAssignable>
27{
28}
29
30pub 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
50pub 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 use rand::RngExt;
56 for j in 0..num_bits {
57 self.assign_index(j, random_number_generator.random());
58 }
59 }
60}
61
62pub trait BorrowAsBitIterator {
63 type BitIterator<'life>: ExactSizeIterator<Item = bool>
64 where
65 Self: 'life;
66 fn borrow_as_bit_iterator(&self) -> Self::BitIterator<'_>;
67}
68
69// Binary traits, involve two types
70
71pub trait BitwiseBinaryOps<Other: ?Sized + Bitwise = Self>: Bitwise + IndexAssignable {
72 fn assign(&mut self, other: &Other);
73
74 fn assign_with_offset(&mut self, other: &Other, start_bit: usize, num_bits: usize) {
75 for bit_index in 0..num_bits {
76 self.assign_index(bit_index + start_bit, other.index(bit_index));
77 }
78 }
79
80 fn assign_from_interval(&mut self, other: &Other, start_bit: usize, num_bits: usize) {
81 for bit_index in 0..num_bits {
82 self.assign_index(bit_index, other.index(start_bit + bit_index));
83 }
84 }
85
86 fn bitxor_assign(&mut self, other: &Other);
87 fn bitand_assign(&mut self, other: &Other);
88}
89
90// TODO: should we eliminate Dot and move `dot` into OverlapWeight ?
91pub trait Dot<Other: ?Sized = Self> {
92 fn dot(&self, other: &Other) -> bool;
93}
94
95pub trait OverlapWeight<Other: ?Sized = Self> {
96 fn and_weight(&self, other: &Other) -> usize;
97 fn or_weight(&self, other: &Other) -> usize;
98}
99
100pub trait FromBits<Other> {
101 fn from_bits(other: &Other) -> Self;
102}
103
104//TODO: Should BitVec, BitView, MutableBitView, BitBlock thoroughly implement standard traits such as : BitXor, BitAnd, BitXorAssign, BitAndAssign, Index, FromIterator<bool>, Display ?
105pub mod bitchunk;
106pub mod tiny_matrix;
107