bits

package
v3.0.1-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NumBytes = 48 // bytes
	NumBits  = NumBytes * 8
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bitmap

type Bitmap [NumBytes]byte

Bitmap is a generalized representation of an identifier or data that can be sorted, compared fast. Used by the DHT package as a way to handle the unique identifiers of a DHT node.

func Closest

func Closest(target Bitmap, bitmaps ...Bitmap) Bitmap

Closest returns the closest bitmap to target. if no bitmaps are provided, target itself is returned

func FromBigP

func FromBigP(b *big.Int) Bitmap

func FromBytes

func FromBytes(data []byte) (Bitmap, error)

FromBytes returns a bitmap as long as the byte array is of a specific length specified in the parameters.

func FromBytesP

func FromBytesP(data []byte) Bitmap

FromBytesP returns a bitmap as long as the byte array is of a specific length specified in the parameters otherwise it wil panic.

func FromHex

func FromHex(hexStr string) (Bitmap, error)

FromHex returns a bitmap by converting the hex string to bytes and creating from bytes as long as the byte array is of a specific length specified in the parameters

func FromHexP

func FromHexP(hexStr string) Bitmap

FromHexP returns a bitmap by converting the hex string to bytes and creating from bytes as long as the byte array is of a specific length specified in the parameters otherwise it wil panic.

func FromShortHex

func FromShortHex(hexStr string) (Bitmap, error)

FromShortHex returns a bitmap by converting the hex string to bytes, adding the leading zeros prefix to the hex string and creating from bytes as long as the byte array is of a specific length specified in the parameters

func FromShortHexP

func FromShortHexP(hexStr string) Bitmap

FromShortHexP returns a bitmap by converting the hex string to bytes, adding the leading zeros prefix to the hex string and creating from bytes as long as the byte array is of a specific length specified in the parameters otherwise it wil panic.

func FromString

func FromString(data string) (Bitmap, error)

FromString returns a bitmap by converting the string to bytes and creating from bytes as long as the byte array is of a specific length specified in the parameters

func FromStringP

func FromStringP(data string) Bitmap

FromStringP returns a bitmap by converting the string to bytes and creating from bytes as long as the byte array is of a specific length specified in the parameters otherwise it wil panic.

func MaxP

func MaxP() Bitmap

MaxP returns a bitmap with all bits set to 1

func Rand

func Rand() Bitmap

Rand generates a cryptographically random bitmap with the confines of the parameters specified.

func RandInRangeP

func RandInRangeP(low, high Bitmap) Bitmap

RandInRangeP generates a cryptographically random bitmap and while it is greater than the high threshold bitmap will subtract the diff between high and low until it is no longer greater that the high.

func (Bitmap) Add

func (b Bitmap) Add(other Bitmap) Bitmap

Add returns a bitmap that treats both bitmaps as numbers and adding them together. Since the size of a bitmap is limited, an overflow is possible when adding bitmaps.

func (Bitmap) And

func (b Bitmap) And(other Bitmap) Bitmap

And returns a comparison bitmap, that for each byte returns the AND true table result

func (Bitmap) BString

func (b Bitmap) BString() string

BString returns the bitmap as a string of 0s and 1s

func (Bitmap) Big

func (b Bitmap) Big() *big.Int

func (Bitmap) Closer

func (b Bitmap) Closer(x, y Bitmap) bool

Closer returns true if dist(b,x) < dist(b,y)

func (Bitmap) Cmp

func (b Bitmap) Cmp(other Bitmap) int

Cmp compares b and other and returns:

-1 if b < other
 0 if b == other
+1 if b > other

func (Bitmap) Copy

func (b Bitmap) Copy() Bitmap

Copy returns a duplicate value for the bitmap.

func (Bitmap) Equals

func (b Bitmap) Equals(other Bitmap) bool

Equals returns true if every byte in bitmap are equal, false otherwise

func (Bitmap) Get

func (b Bitmap) Get(n int) bool

Get returns the binary bit at the position passed.

func (Bitmap) Hex

func (b Bitmap) Hex() string

Hex returns a hexadecimal representation of the bitmap.

func (Bitmap) HexShort

func (b Bitmap) HexShort() string

HexShort returns a hexadecimal representation of the first 4 bytes.

func (Bitmap) HexSimplified

func (b Bitmap) HexSimplified() string

HexSimplified returns the hexadecimal representation with all leading 0's removed

func (Bitmap) MarshalBencode

func (b Bitmap) MarshalBencode() ([]byte, error)

MarshalBencode implements the Marshaller(bencode)/Message interface.

func (Bitmap) Not

func (b Bitmap) Not() Bitmap

Not returns a complimentary bitmap that is an inverse. So b.NOT.NOT = b

func (Bitmap) Or

func (b Bitmap) Or(other Bitmap) Bitmap

Or returns a comparison bitmap, that for each byte returns the OR true table result

func (Bitmap) Prefix

func (b Bitmap) Prefix(n int, one bool) Bitmap

Prefix returns a copy of b with the first n bits set to 1 (if `one` is true) or 0 (if `one` is false) https://stackoverflow.com/a/23192263/182709

func (Bitmap) PrefixLen

func (b Bitmap) PrefixLen() int

PrefixLen returns the number of leading 0 bits

func (Bitmap) RawString

func (b Bitmap) RawString() string

func (Bitmap) Set

func (b Bitmap) Set(n int, one bool) Bitmap

Set sets the binary bit at the position passed.

func (Bitmap) String

func (b Bitmap) String() string

func (Bitmap) Sub

func (b Bitmap) Sub(other Bitmap) Bitmap

Sub returns a bitmap that treats both bitmaps as numbers and subtracts then via the inverse of the other and adding then together a + (-b). Negative bitmaps are not supported so other must be greater than this.

func (Bitmap) Suffix

func (b Bitmap) Suffix(n int, one bool) Bitmap

Suffix returns a copy of b with the last n bits set to 1 (if `one` is true) or 0 (if `one` is false) https://stackoverflow.com/a/23192263/182709

func (*Bitmap) UnmarshalBencode

func (b *Bitmap) UnmarshalBencode(encoded []byte) error

UnmarshalBencode implements the Marshaller(bencode)/Message interface.

func (Bitmap) Xor

func (b Bitmap) Xor(other Bitmap) Bitmap

Xor returns a diff bitmap. If they are equal, the returned bitmap will be all 0's. If 100% unique the returned bitmap will be all 1's.

type Range

type Range struct {
	Start Bitmap
	End   Bitmap
}

Range has a start and end

func MaxRange

func MaxRange() Range

func (Range) Contains

func (r Range) Contains(b Bitmap) bool

func (Range) IntervalP

func (r Range) IntervalP(n, num int) Range

IntervalP divides the range into `num` intervals and returns the `n`th one intervals are approximately the same size, but may not be exact because of rounding issues the first interval always starts at the beginning of the range, and the last interval always ends at the end

func (Range) IntervalSize

func (r Range) IntervalSize() *big.Int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL