1use Bits;
2use BlockType;
3use iter::BlockIter;
4
5#[derive(Debug, Clone)]
11pub struct BitConcat<T, U>(T, U);
12
13impl<T, U> BitConcat<T, U> {
14 pub (crate) fn new(bits1: T, bits2: U) -> Self {
15 BitConcat(bits1, bits2)
16 }
17}
18
19impl<T, U> Bits for BitConcat<T, U>
20 where T: Bits,
21 U: Bits<Block = T::Block> {
22
23 type Block = T::Block;
24
25 fn bit_len(&self) -> u64 {
26 self.0.bit_len() + self.1.bit_len()
27 }
28
29 fn get_bit(&self, position: u64) -> bool {
30 let len0 = self.0.bit_len();
31 if position < len0 {
32 self.0.get_bit(position)
33 } else {
34 self.1.get_bit(position - len0)
35 }
36 }
37
38 fn get_block(&self, position: usize) -> Self::Block {
39 let start_bit = Self::Block::mul_nbits(position);
40 let count = Self::Block::block_bits(self.bit_len(), position);
41 let limit_bit = start_bit + count as u64;
42
43 let len0 = self.0.bit_len();
44 if limit_bit <= len0 {
45 self.0.get_block(position)
46 } else if start_bit < len0 {
47 let block1 = self.0.get_raw_block(position);
48 let block2 = self.1.get_raw_block(0);
49 let size1 = (len0 - start_bit) as usize;
50 let size2 = count - size1;
51 block1.get_bits(0, size1) |
52 (block2.get_bits(0, size2) << size1)
53 } else {
54 self.1.get_bits(start_bit - len0, count)
55 }
56 }
57}
58
59impl<T, U, V> PartialEq<V> for BitConcat<T, U>
60 where T: Bits,
61 U: Bits<Block = T::Block>,
62 V: Bits<Block = T::Block> {
63
64 fn eq(&self, other: &V) -> bool {
65 BlockIter::new(self) == BlockIter::new(other)
66 }
67}
68
69impl_index_from_bits! {
70 impl[T: Bits, U: Bits<Block = T::Block>] Index<u64> for BitConcat<T, U>;
71}
72
73impl_bit_sliceable_adapter! {
74 impl[T: Bits, U: Bits<Block = T::Block>] BitSliceable for BitConcat<T, U>;
75 impl['a, T: Bits, U: Bits<Block = T::Block>] BitSliceable for &'a BitConcat<T, U>;
76}
77