Documentation
¶
Overview ¶
Copyright Consensys Software Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
SPDX-License-Identifier: Apache-2.0
Copyright Consensys Software Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
SPDX-License-Identifier: Apache-2.0
Copyright Consensys Software Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
SPDX-License-Identifier: Apache-2.0
Copyright Consensys Software Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
SPDX-License-Identifier: Apache-2.0
Copyright Consensys Software Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
SPDX-License-Identifier: Apache-2.0
Copyright Consensys Software Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
SPDX-License-Identifier: Apache-2.0
Index ¶
- func BigEndianCopy(src []byte, srcOffset uint, dst []byte, dstOffset uint, nbits uint)
- func BigEndianRead(src []byte, bitoffset uint) bool
- func BigEndianWrite(val bool, src []byte, bitoffset uint)
- func BytesRequiredFor(bitwidth uint) uint
- func DecodeArray[T any](bitwidth uint, bytes []byte, decoder func([]byte) T) ([]T, uint)
- func LittleEndianCopy(src []byte, srcOffset uint, dst []byte, dstOffset uint, nbits uint)
- func LittleEndianRead(src []byte, bitoffset uint) bool
- func LittleEndianWrite(val bool, src []byte, bitoffset uint)
- func NewBuffer(bitwidth uint) []byte
- func Width(bound uint) uint
- type Predicate
- type Reader
- type Set
- func (p *Set) Clone() Set
- func (p *Set) Contains(val uint) bool
- func (p *Set) Count() uint
- func (p *Set) Get(i uint) bool
- func (p *Set) Insert(val uint)
- func (p *Set) InsertAll(vals ...uint)
- func (p *Set) Iter() iter.Iterator[uint]
- func (p *Set) Remove(val uint)
- func (p *Set) Set(i uint, v bool)
- func (p *Set) String() string
- func (p *Set) Union(bits Set) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BigEndianCopy ¶
BigEndianCopy copies n bits starting a given bit offset from a given byte array source into a given destination (at a given offset) assuming a little endian layout of bytes. For example, consider the array [0x90,0x7] which is [0b10010000,0b00000111]. Then, the bit offsets can be viewed as follows:
+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 00| 01| 02| 03| 04| 05| 06| 07| | 08| 09| 10| 11| 12| 13| 14| 15|
Now, consider copying 8 bits starting at offset 3. This represents the following bits:
+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | | | | X | X | X | X | X | | X | X | X | | | | | | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 00| 01| 02| 03| 04| 05| 06| 07| | 08| 09| 10| 11| 12| 13| 14| 15|
As such, we see how the big end treatment of bytes impacts the bits which are copied.
func BigEndianRead ¶
BigEndianRead reads the bit at a given bit offset out of an array of bytes arranged in big endian format. So, for example, reading bit 0 from the byte 0b0111_11111 returns 0, but reading bit 7 returns 1.
func BigEndianWrite ¶
BigEndianWrite writes a bit to a given bit offset in an array of bytes arranged in big endian format. So, for example, writing 1 at offset 15 into an array [0x00,0x00] yields [0x00,0x01].
func BytesRequiredFor ¶
BytesRequiredFor returns the minimum number of bytes required to hold the given bitwidth. For example, the number of bytes to hold a u16 is 2 bytes, whilst the minimum required to hold a u17 is 3 bytes.
func DecodeArray ¶
DecodeArray decodes the given set of bytes into an array of arbitrary fixed-width values. Observe that values are assumed to be packed tightly (i.e. without any padding). Consider the following input byte array:
| 00 | 01 | 02 | 03 | +------+------+------+------+ | 0x31 | 0xf0 | 0x0e | 0x1d |
Then, decoding this into a u4 array will produce the following:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | +-----+-----+-----+-----+-----+-----+-----+-----+ | 0x3 | 0x1 | 0xf | 0x0 | 0x0 | 0xe | 0x1 | 0xd |
Finally, the number of unused (i.e. remaining) bits is returned (which will be zero if (len(bytes)*8)%bitwidth == 0).
NOTE: the array parsed into the decoder function is reused across different elements and, hence, the decoder function should clone it if necessary.
func LittleEndianCopy ¶
LittleEndianCopy copies n bits starting a given bit offset from a given byte array source into a given destination (at a given offset) assuming a little endian layout of bytes. For example, consider the array [0x90,0x7] which is [0b10010000,0b00000111]. Then, the bit offsets can be viewed as follows:
+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 07| 06| 05| 04| 03| 02| 01| 00| | 15| 14| 13| 12| 11| 10| 09| 08|
Now, consider copying 8 bits starting at offset 3. This represents the following bits:
+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | X | X | X | X | X | | | | | | | | | | X | X | X | +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+ | 07| 06| 05| 04| 03| 02| 01| 00| | 15| 14| 13| 12| 11| 10| 09| 08|
As such, we see how the little end treatment of bytes impacts the bits which are copied.
func LittleEndianRead ¶
LittleEndianRead reads the bit at a given bit offset out of an array of bytes arranged in little endian format. So, for example, reading bit 0 from the byte 0b0111_11111 returns 1, but reading bit 7 returns 0.
func LittleEndianWrite ¶
LittleEndianWrite writes a bit to a given bit offset in an array of bytes arranged in little endian format. So, for example, writing 1 at offset 15 into an array [0x00,0x00] yields [0x00,0x80].
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader provides a mechanism for reading bits from a given array of bytes, where the least significant bits are read first. For example, consider sequence of bytes [0x9f,0x05] can be views as the following bit sequence:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 || 8 | 9 | A | B | C | D | E | F | +===+===+===+===+===+===+===+===++===+===+===+===+===+===+===+===+ | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 || 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | | | | | | | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
The above illustrates the outcome from reading 7 bits. In such case, the value 0b0011111 is written into the target buffer.
func (*Reader) BigEndianReadInto ¶
BigEndianReadInto reads the n least significant bits from the underlying array into a given target array, returning the total number of bytes affected. This assume a big endian encoding of bytes.
func (*Reader) LittleEndianReadInto ¶
LittleEndianReadInto reads the n least significant bits from the underlying array into a given target array, returning the total number of bytes affected. This assume a little endian encoding of bytes.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set provides a straightforward bitset implementation. That is, a set of (unsigned) integer values implemented as an array of bits.
func (*Set) Clone ¶
Clone creates a true copy of this bitset which ensures no aliasing between this set and the result.