bv/bit_vec/
impls.rs

1use {BlockType, Bits, BitsMut, BitsPush, BitSliceable, BitSlice, BitSliceMut};
2use super::BitVec;
3use iter::BlockIter;
4use storage::Address;
5
6use traits::get_masked_block;
7
8use range_compat::*;
9
10use std::cmp::Ordering;
11use std::fmt;
12use std::hash::{Hash, Hasher};
13
14impl<Block: BlockType> Bits for BitVec<Block> {
15    type Block = Block;
16
17    fn bit_len(&self) -> u64 {
18        self.len()
19    }
20
21    fn get_bit(&self, position: u64) -> bool {
22        assert!( position < self.len(),
23                 "BitVec::get_bit: out of bounds" );
24        let address = Address::new::<Block>(position);
25        // We know this is safe because we just did a bounds check.
26        let block = unsafe { self.bits.get_block(address.block_index) };
27        block.get_bit(address.bit_offset)
28    }
29
30    fn get_block(&self, position: usize) -> Block {
31        get_masked_block(self, position)
32    }
33
34    fn get_raw_block(&self, position: usize) -> Block {
35        assert!( position < self.block_len(),
36                 "BitVec::get_block: out of bounds" );
37        // We know this is safe because we just did a bounds check.
38        unsafe { self.bits.get_block(position) }
39    }
40}
41
42impl<Block: BlockType> BitsMut for BitVec<Block> {
43    fn set_bit(&mut self, position: u64, value: bool) {
44        assert!( position < self.len(),
45                 "BitVec::set_bit: out of bounds" );
46        let address = Address::new::<Block>(position);
47        // We know this is safe because we just did a bounds check.
48        let old_block = unsafe { self.bits.get_block(address.block_index) };
49        let new_block = old_block.with_bit(address.bit_offset, value);
50        unsafe { self.bits.set_block(address.block_index, new_block); }
51    }
52
53    fn set_block(&mut self, position: usize, value: Block) {
54        assert!( position < self.block_len(),
55                 "BitVec::set_block: out of bounds" );
56        // We know this is safe because we just did a bounds check, and
57        // position is in bounds. This may set extra bits in the last
58        // block, but that's okay because such bits are never observed.
59        unsafe {
60            self.bits.set_block(position, value);
61        }
62    }
63}
64
65impl<Block: BlockType> BitsPush for BitVec<Block> {
66    fn push_bit(&mut self, value: bool) {
67        self.push(value);
68    }
69
70    fn pop_bit(&mut self) -> Option<bool> {
71        self.pop()
72    }
73
74    fn align_block(&mut self, value: bool) {
75        let keep_bits = Block::mod_nbits(self.len);
76        if keep_bits > 0 {
77            // We know the next line doesn't overflow because if
78            // there are bits to keep then there must be at least
79            // one blck.
80            let last_index = self.block_len() - 1;
81            // Then this is safe because `last_index` is the index
82            // of the last block, which certainly exists at this point.
83            unsafe {
84                let old_last = self.bits.get_block(last_index);
85                let new_last = if value {
86                    old_last | !Block::low_mask(keep_bits)
87                } else {
88                    old_last & Block::low_mask(keep_bits)
89                };
90                self.bits.set_block(last_index, new_last);
91            }
92            self.len += (Block::nbits() - keep_bits) as u64;
93        }
94    }
95
96    fn push_block(&mut self, value: Block) {
97        self.align_block(false);
98        self.block_reserve(1);
99        self.len += Block::nbits() as u64;
100        let last = self.block_len() - 1;
101        self.set_block(last, value);
102    }
103}
104
105impl<'a, Block: BlockType> BitSliceable<Range<u64>> for &'a BitVec<Block> {
106    type Slice = BitSlice<'a, Block>;
107
108    fn bit_slice(self, range: Range<u64>) -> BitSlice<'a, Block> {
109        self.as_slice().bit_slice(range)
110    }
111}
112
113impl<'a, Block: BlockType> BitSliceable<Range<u64>> for &'a mut BitVec<Block> {
114    type Slice = BitSliceMut<'a, Block>;
115
116    fn bit_slice(self, range: Range<u64>) -> BitSliceMut<'a, Block> {
117        self.as_mut_slice().bit_slice(range)
118    }
119}
120
121#[cfg(inclusive_range)]
122impl<'a, Block: BlockType> BitSliceable<RangeInclusive<u64>> for &'a BitVec<Block> {
123    type Slice = BitSlice<'a, Block>;
124
125    fn bit_slice(self, range: RangeInclusive<u64>) -> BitSlice<'a, Block> {
126        self.as_slice().bit_slice(range)
127    }
128}
129
130#[cfg(inclusive_range)]
131impl<'a, Block: BlockType> BitSliceable<RangeInclusive<u64>> for &'a mut BitVec<Block> {
132    type Slice = BitSliceMut<'a, Block>;
133
134    fn bit_slice(self, range: RangeInclusive<u64>) -> BitSliceMut<'a, Block> {
135        self.as_mut_slice().bit_slice(range)
136    }
137}
138
139impl<'a, Block: BlockType> BitSliceable<RangeFrom<u64>> for &'a BitVec<Block> {
140    type Slice = BitSlice<'a, Block>;
141
142    fn bit_slice(self, range: RangeFrom<u64>) -> BitSlice<'a, Block> {
143        self.as_slice().bit_slice(range)
144    }
145}
146
147impl<'a, Block: BlockType> BitSliceable<RangeFrom<u64>> for &'a mut BitVec<Block> {
148    type Slice = BitSliceMut<'a, Block>;
149
150    fn bit_slice(self, range: RangeFrom<u64>) -> BitSliceMut<'a, Block> {
151        self.as_mut_slice().bit_slice(range)
152    }
153}
154
155impl<'a, Block: BlockType> BitSliceable<RangeTo<u64>> for &'a BitVec<Block> {
156    type Slice = BitSlice<'a, Block>;
157
158    fn bit_slice(self, range: RangeTo<u64>) -> BitSlice<'a, Block> {
159        self.as_slice().bit_slice(range)
160    }
161}
162
163impl<'a, Block: BlockType> BitSliceable<RangeTo<u64>> for &'a mut BitVec<Block> {
164    type Slice = BitSliceMut<'a, Block>;
165
166    fn bit_slice(self, range: RangeTo<u64>) -> BitSliceMut<'a, Block> {
167        self.as_mut_slice().bit_slice(range)
168    }
169}
170
171#[cfg(inclusive_range)]
172impl<'a, Block: BlockType> BitSliceable<RangeToInclusive<u64>> for &'a BitVec<Block> {
173    type Slice = BitSlice<'a, Block>;
174
175    fn bit_slice(self, range: RangeToInclusive<u64>) -> BitSlice<'a, Block> {
176        self.as_slice().bit_slice(range)
177    }
178}
179
180#[cfg(inclusive_range)]
181impl<'a, Block: BlockType> BitSliceable<RangeToInclusive<u64>> for &'a mut BitVec<Block> {
182    type Slice = BitSliceMut<'a, Block>;
183
184    fn bit_slice(self, range: RangeToInclusive<u64>) -> BitSliceMut<'a, Block> {
185        self.as_mut_slice().bit_slice(range)
186    }
187}
188
189impl<'a, Block: BlockType> BitSliceable<RangeFull> for &'a BitVec<Block> {
190    type Slice = BitSlice<'a, Block>;
191
192    fn bit_slice(self, _: RangeFull) -> BitSlice<'a, Block> {
193        self.as_slice()
194    }
195}
196
197impl<'a, Block: BlockType> BitSliceable<RangeFull> for &'a mut BitVec<Block> {
198    type Slice = BitSliceMut<'a, Block>;
199
200    fn bit_slice(self, _: RangeFull) -> BitSliceMut<'a, Block> {
201        self.as_mut_slice()
202    }
203}
204
205impl_index_from_bits! {
206    impl[Block: BlockType] Index<u64> for BitVec<Block>;
207}
208
209impl<Other: Bits> PartialEq<Other> for BitVec<Other::Block> {
210    fn eq(&self, other: &Other) -> bool {
211        BlockIter::new(self) == BlockIter::new(other)
212    }
213}
214
215impl<Block: BlockType> PartialOrd for BitVec<Block> {
216    fn partial_cmp(&self, other: &BitVec<Block>) -> Option<Ordering> {
217        let iter1 = BlockIter::new(self);
218        let iter2 = BlockIter::new(other);
219        iter1.partial_cmp(iter2)
220    }
221}
222
223impl<Block: BlockType> Eq for BitVec<Block> {}
224
225impl<Block: BlockType> Ord for BitVec<Block> {
226    fn cmp(&self, other: &Self) -> Ordering {
227        let iter1 = BlockIter::new(self);
228        let iter2 = BlockIter::new(other);
229        iter1.cmp(iter2)
230    }
231}
232
233impl<Block: BlockType + Hash> Hash for BitVec<Block> {
234    fn hash<H: Hasher>(&self, state: &mut H) {
235        self.as_slice().hash(state);
236    }
237}
238
239impl<Block: BlockType> fmt::Debug for BitVec<Block> {
240    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
241        self.as_slice().fmt(f)
242    }
243}
244
245impl<Block: BlockType> From<Box<[Block]>> for BitVec<Block> {
246    fn from(bb: Box<[Block]>) -> Self {
247        let len = Block::mul_nbits(bb.len());
248        BitVec {
249            bits: bb.into(),
250            len,
251        }
252    }
253}
254
255impl<Block: BlockType> From<Vec<Block>> for BitVec<Block> {
256    fn from(vec: Vec<Block>) -> Self {
257        vec.into_boxed_slice().into()
258    }
259}