microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.25.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/paulimer/src/bits.rs

105lines · 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 for j in 0..num_bits {
56 self.assign_index(j, random_number_generator.gen());
57 }
58 }
59}
60
61pub 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
70pub 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 ?
90pub trait Dot<Other: ?Sized = Self> {
91 fn dot(&self, other: &Other) -> bool;
92}
93
94pub trait OverlapWeight<Other: ?Sized = Self> {
95 fn and_weight(&self, other: &Other) -> usize;
96 fn or_weight(&self, other: &Other) -> usize;
97}
98
99pub 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 ?
104pub mod bitchunk;
105pub mod tiny_matrix;
106