2018-06-14 17:48:02 +02:00
|
|
|
package bits
|
2018-03-07 02:15:44 +01:00
|
|
|
|
|
|
|
import (
|
2018-03-24 00:18:00 +01:00
|
|
|
"crypto/rand"
|
2018-03-07 02:15:44 +01:00
|
|
|
"encoding/hex"
|
2018-06-15 04:30:37 +02:00
|
|
|
"math/big"
|
2018-05-30 03:38:55 +02:00
|
|
|
"strconv"
|
2018-06-14 17:48:02 +02:00
|
|
|
"strings"
|
2018-05-30 03:38:55 +02:00
|
|
|
|
2019-01-09 23:37:29 +01:00
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
2018-06-14 17:48:02 +02:00
|
|
|
|
2018-03-24 00:18:00 +01:00
|
|
|
"github.com/lyoshenka/bencode"
|
2018-03-07 02:15:44 +01:00
|
|
|
)
|
|
|
|
|
2018-05-24 19:05:05 +02:00
|
|
|
// TODO: http://roaringbitmap.org/
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
const (
|
|
|
|
NumBytes = 48 // bytes
|
|
|
|
NumBits = NumBytes * 8
|
|
|
|
)
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// 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.
|
2018-06-14 17:48:02 +02:00
|
|
|
type Bitmap [NumBytes]byte
|
2018-03-07 02:15:44 +01:00
|
|
|
|
2018-06-19 19:47:13 +02:00
|
|
|
func (b Bitmap) RawString() string {
|
2018-04-03 19:38:01 +02:00
|
|
|
return string(b[:])
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 19:47:13 +02:00
|
|
|
func (b Bitmap) String() string {
|
|
|
|
return b.Hex()
|
2018-06-15 04:30:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
// BString returns the bitmap as a string of 0s and 1s
|
|
|
|
func (b Bitmap) BString() string {
|
2018-06-14 17:48:02 +02:00
|
|
|
var s string
|
|
|
|
for _, byte := range b {
|
|
|
|
s += strconv.FormatInt(int64(byte), 2)
|
2018-05-19 19:05:30 +02:00
|
|
|
}
|
2018-06-14 17:48:02 +02:00
|
|
|
return s
|
2018-05-19 19:05:30 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Hex returns a hexadecimal representation of the bitmap.
|
2018-04-05 22:05:28 +02:00
|
|
|
func (b Bitmap) Hex() string {
|
2018-04-03 19:38:01 +02:00
|
|
|
return hex.EncodeToString(b[:])
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// HexShort returns a hexadecimal representation of the first 4 bytes.
|
2018-04-05 22:05:28 +02:00
|
|
|
func (b Bitmap) HexShort() string {
|
2018-04-03 19:38:01 +02:00
|
|
|
return hex.EncodeToString(b[:4])
|
2018-03-29 03:05:27 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// HexSimplified returns the hexadecimal representation with all leading 0's removed
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) HexSimplified() string {
|
|
|
|
simple := strings.TrimLeft(b.Hex(), "0")
|
|
|
|
if simple == "" {
|
|
|
|
simple = "0"
|
|
|
|
}
|
|
|
|
return simple
|
|
|
|
}
|
|
|
|
|
2018-06-19 19:47:13 +02:00
|
|
|
func (b Bitmap) Big() *big.Int {
|
|
|
|
i := new(big.Int)
|
|
|
|
i.SetString(b.Hex(), 16)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-06-25 21:48:57 +02:00
|
|
|
// Cmp compares b and other and returns:
|
|
|
|
//
|
|
|
|
// -1 if b < other
|
|
|
|
// 0 if b == other
|
|
|
|
// +1 if b > other
|
|
|
|
//
|
|
|
|
func (b Bitmap) Cmp(other Bitmap) int {
|
2018-03-07 02:15:44 +01:00
|
|
|
for k := range b {
|
2018-06-25 21:48:57 +02:00
|
|
|
if b[k] < other[k] {
|
|
|
|
return -1
|
|
|
|
} else if b[k] > other[k] {
|
|
|
|
return 1
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-25 21:48:57 +02:00
|
|
|
return 0
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-25 21:48:57 +02:00
|
|
|
// Closer returns true if dist(b,x) < dist(b,y)
|
|
|
|
func (b Bitmap) Closer(x, y Bitmap) bool {
|
|
|
|
return x.Xor(b).Cmp(y.Xor(b)) < 0
|
2018-05-19 19:05:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 21:48:57 +02:00
|
|
|
// Equals returns true if every byte in bitmap are equal, false otherwise
|
|
|
|
func (b Bitmap) Equals(other Bitmap) bool {
|
|
|
|
return b.Cmp(other) == 0
|
2018-05-19 19:05:30 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Copy returns a duplicate value for the bitmap.
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Copy() Bitmap {
|
|
|
|
var ret Bitmap
|
|
|
|
copy(ret[:], b[:])
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// 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.
|
2018-04-05 22:05:28 +02:00
|
|
|
func (b Bitmap) Xor(other Bitmap) Bitmap {
|
|
|
|
var ret Bitmap
|
2018-03-07 02:15:44 +01:00
|
|
|
for k := range b {
|
|
|
|
ret[k] = b[k] ^ other[k]
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// And returns a comparison bitmap, that for each byte returns the AND true table result
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) And(other Bitmap) Bitmap {
|
|
|
|
var ret Bitmap
|
|
|
|
for k := range b {
|
|
|
|
ret[k] = b[k] & other[k]
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Or returns a comparison bitmap, that for each byte returns the OR true table result
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Or(other Bitmap) Bitmap {
|
|
|
|
var ret Bitmap
|
|
|
|
for k := range b {
|
|
|
|
ret[k] = b[k] | other[k]
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Not returns a complimentary bitmap that is an inverse. So b.NOT.NOT = b
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Not() Bitmap {
|
|
|
|
var ret Bitmap
|
|
|
|
for k := range b {
|
|
|
|
ret[k] = ^b[k]
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Bitmap) add(other Bitmap) (Bitmap, bool) {
|
|
|
|
var ret Bitmap
|
|
|
|
carry := false
|
2018-06-14 17:48:02 +02:00
|
|
|
for i := NumBits - 1; i >= 0; i-- {
|
2018-05-19 19:05:30 +02:00
|
|
|
bBit := getBit(b[:], i)
|
|
|
|
oBit := getBit(other[:], i)
|
|
|
|
setBit(ret[:], i, bBit != oBit != carry)
|
|
|
|
carry = (bBit && oBit) || (bBit && carry) || (oBit && carry)
|
|
|
|
}
|
|
|
|
return ret, carry
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// 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.
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Add(other Bitmap) Bitmap {
|
|
|
|
ret, carry := b.add(other)
|
|
|
|
if carry {
|
2018-06-14 17:48:02 +02:00
|
|
|
panic("overflow in bitmap addition. limited to " + strconv.Itoa(NumBits) + " bits.")
|
2018-05-19 19:05:30 +02:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// 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.
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Sub(other Bitmap) Bitmap {
|
2018-06-25 21:48:57 +02:00
|
|
|
if b.Cmp(other) < 0 {
|
2018-05-30 03:38:55 +02:00
|
|
|
// ToDo: Why is this not supported? Should it say not implemented? BitMap might have a generic use case outside of dht.
|
2018-05-19 19:05:30 +02:00
|
|
|
panic("negative bitmaps not supported")
|
|
|
|
}
|
2018-06-14 17:48:02 +02:00
|
|
|
complement, _ := other.Not().add(FromShortHexP("1"))
|
2018-05-19 19:05:30 +02:00
|
|
|
ret, _ := b.add(complement)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Get returns the binary bit at the position passed.
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Get(n int) bool {
|
|
|
|
return getBit(b[:], n)
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Set sets the binary bit at the position passed.
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Set(n int, one bool) Bitmap {
|
|
|
|
ret := b.Copy()
|
|
|
|
setBit(ret[:], n, one)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-03-07 02:15:44 +01:00
|
|
|
// PrefixLen returns the number of leading 0 bits
|
2018-04-05 22:05:28 +02:00
|
|
|
func (b Bitmap) PrefixLen() int {
|
2018-03-07 02:15:44 +01:00
|
|
|
for i := range b {
|
|
|
|
for j := 0; j < 8; j++ {
|
|
|
|
if (b[i]>>uint8(7-j))&0x1 != 0 {
|
|
|
|
return i*8 + j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-14 17:48:02 +02:00
|
|
|
return NumBits
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:05:30 +02:00
|
|
|
// Prefix returns a copy of b with the first n bits set to 1 (if `one` is true) or 0 (if `one` is false)
|
2018-05-13 22:02:46 +02:00
|
|
|
// https://stackoverflow.com/a/23192263/182709
|
2018-05-19 19:05:30 +02:00
|
|
|
func (b Bitmap) Prefix(n int, one bool) Bitmap {
|
|
|
|
ret := b.Copy()
|
2018-05-13 22:02:46 +02:00
|
|
|
|
|
|
|
Outer:
|
|
|
|
for i := range ret {
|
|
|
|
for j := 0; j < 8; j++ {
|
|
|
|
if i*8+j < n {
|
2018-05-19 19:05:30 +02:00
|
|
|
if one {
|
|
|
|
ret[i] |= 1 << uint(7-j)
|
|
|
|
} else {
|
|
|
|
ret[i] &= ^(1 << uint(7-j))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Suffix returns a copy of b with the last n bits set to 1 (if `one` is true) or 0 (if `one` is false)
|
2018-05-19 19:05:30 +02:00
|
|
|
// https://stackoverflow.com/a/23192263/182709
|
|
|
|
func (b Bitmap) Suffix(n int, one bool) Bitmap {
|
|
|
|
ret := b.Copy()
|
|
|
|
|
|
|
|
Outer:
|
|
|
|
for i := len(ret) - 1; i >= 0; i-- {
|
|
|
|
for j := 7; j >= 0; j-- {
|
2018-06-14 17:48:02 +02:00
|
|
|
if i*8+j >= NumBits-n {
|
2018-05-19 19:05:30 +02:00
|
|
|
if one {
|
|
|
|
ret[i] |= 1 << uint(7-j)
|
|
|
|
} else {
|
|
|
|
ret[i] &= ^(1 << uint(7-j))
|
|
|
|
}
|
2018-05-13 22:02:46 +02:00
|
|
|
} else {
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// MarshalBencode implements the Marshaller(bencode)/Message interface.
|
2018-04-05 22:05:28 +02:00
|
|
|
func (b Bitmap) MarshalBencode() ([]byte, error) {
|
2018-04-04 17:43:27 +02:00
|
|
|
str := string(b[:])
|
|
|
|
return bencode.EncodeBytes(str)
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// UnmarshalBencode implements the Marshaller(bencode)/Message interface.
|
2018-04-05 22:05:28 +02:00
|
|
|
func (b *Bitmap) UnmarshalBencode(encoded []byte) error {
|
2018-03-08 01:49:33 +01:00
|
|
|
var str string
|
|
|
|
err := bencode.DecodeBytes(encoded, &str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-14 17:48:02 +02:00
|
|
|
if len(str) != NumBytes {
|
2018-04-04 17:43:27 +02:00
|
|
|
return errors.Err("invalid bitmap length")
|
2018-04-03 19:38:01 +02:00
|
|
|
}
|
2018-03-08 01:49:33 +01:00
|
|
|
copy(b[:], str)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
// FromBytes returns a bitmap as long as the byte array is of a specific length specified in the parameters.
|
|
|
|
func FromBytes(data []byte) (Bitmap, error) {
|
2018-04-05 22:05:28 +02:00
|
|
|
var bmp Bitmap
|
|
|
|
|
|
|
|
if len(data) != len(bmp) {
|
|
|
|
return bmp, errors.Err("invalid bitmap of length %d", len(data))
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
copy(bmp[:], data)
|
2018-04-05 22:05:28 +02:00
|
|
|
return bmp, nil
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
// FromBytesP returns a bitmap as long as the byte array is of a specific length specified in the parameters
|
2018-05-30 03:38:55 +02:00
|
|
|
// otherwise it wil panic.
|
2018-06-14 17:48:02 +02:00
|
|
|
func FromBytesP(data []byte) Bitmap {
|
|
|
|
bmp, err := FromBytes(data)
|
2018-04-05 22:05:28 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-07 02:15:44 +01:00
|
|
|
return bmp
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
//FromString returns a bitmap by converting the string to bytes and creating from bytes as long as the byte array
|
2018-05-30 03:38:55 +02:00
|
|
|
// is of a specific length specified in the parameters
|
2018-06-14 17:48:02 +02:00
|
|
|
func FromString(data string) (Bitmap, error) {
|
|
|
|
return FromBytes([]byte(data))
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
//FromStringP returns a bitmap by converting the string to bytes and creating from bytes as long as the byte array
|
2018-05-30 03:38:55 +02:00
|
|
|
// is of a specific length specified in the parameters otherwise it wil panic.
|
2018-06-14 17:48:02 +02:00
|
|
|
func FromStringP(data string) Bitmap {
|
|
|
|
bmp, err := FromString(data)
|
2018-04-05 22:05:28 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bmp
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
//FromHex returns a bitmap by converting the hex string to bytes and creating from bytes as long as the byte array
|
2018-05-30 03:38:55 +02:00
|
|
|
// is of a specific length specified in the parameters
|
2018-06-14 17:48:02 +02:00
|
|
|
func FromHex(hexStr string) (Bitmap, error) {
|
2018-03-07 02:15:44 +01:00
|
|
|
decoded, err := hex.DecodeString(hexStr)
|
2018-04-05 22:05:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return Bitmap{}, errors.Err(err)
|
|
|
|
}
|
2018-06-14 17:48:02 +02:00
|
|
|
return FromBytes(decoded)
|
2018-04-05 22:05:28 +02:00
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
//FromHexP returns a bitmap by converting the hex string to bytes and creating from bytes as long as the byte array
|
2018-05-30 03:38:55 +02:00
|
|
|
// is of a specific length specified in the parameters otherwise it wil panic.
|
2018-06-14 17:48:02 +02:00
|
|
|
func FromHexP(hexStr string) Bitmap {
|
|
|
|
bmp, err := FromHex(hexStr)
|
2018-03-07 02:15:44 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-05 22:05:28 +02:00
|
|
|
return bmp
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
//FromShortHex returns a bitmap by converting the hex string to bytes, adding the leading zeros prefix to the
|
2018-05-30 03:38:55 +02:00
|
|
|
// hex string and creating from bytes as long as the byte array is of a specific length specified in the parameters
|
2018-06-14 17:48:02 +02:00
|
|
|
func FromShortHex(hexStr string) (Bitmap, error) {
|
|
|
|
return FromHex(strings.Repeat("0", NumBytes*2-len(hexStr)) + hexStr)
|
2018-05-19 19:05:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
//FromShortHexP returns a bitmap by converting the hex string to bytes, adding the leading zeros prefix to the
|
2018-05-30 03:38:55 +02:00
|
|
|
// 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.
|
2018-06-14 17:48:02 +02:00
|
|
|
func FromShortHexP(hexStr string) Bitmap {
|
|
|
|
bmp, err := FromShortHex(hexStr)
|
2018-05-19 19:05:30 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bmp
|
|
|
|
}
|
|
|
|
|
2018-06-15 04:30:37 +02:00
|
|
|
func FromBigP(b *big.Int) Bitmap {
|
|
|
|
return FromShortHexP(b.Text(16))
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:20:15 +02:00
|
|
|
// MaxP returns a bitmap with all bits set to 1
|
2018-06-15 04:30:37 +02:00
|
|
|
func MaxP() Bitmap {
|
2018-06-19 19:47:13 +02:00
|
|
|
return FromHexP(strings.Repeat("f", NumBytes*2))
|
2018-06-15 04:30:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
// Rand generates a cryptographically random bitmap with the confines of the parameters specified.
|
|
|
|
func Rand() Bitmap {
|
2018-04-05 22:05:28 +02:00
|
|
|
var id Bitmap
|
2018-03-24 00:18:00 +01:00
|
|
|
_, err := rand.Read(id[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-03-07 02:15:44 +01:00
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
2018-05-19 19:05:30 +02:00
|
|
|
|
2018-06-14 17:48:02 +02:00
|
|
|
// RandInRangeP generates a cryptographically random bitmap and while it is greater than the high threshold
|
2018-05-30 03:38:55 +02:00
|
|
|
// bitmap will subtract the diff between high and low until it is no longer greater that the high.
|
2018-06-14 17:48:02 +02:00
|
|
|
func RandInRangeP(low, high Bitmap) Bitmap {
|
2018-05-19 19:05:30 +02:00
|
|
|
diff := high.Sub(low)
|
2018-06-14 17:48:02 +02:00
|
|
|
r := Rand()
|
2018-06-25 21:48:57 +02:00
|
|
|
for r.Cmp(diff) > 0 {
|
2018-05-19 19:05:30 +02:00
|
|
|
r = r.Sub(diff)
|
|
|
|
}
|
2018-05-30 03:38:55 +02:00
|
|
|
//ToDo - Adding the low at this point doesn't gurantee it will be within the range. Consider bitmaps as numbers and
|
|
|
|
// I have a range of 50-100. If get to say 60, and add 50, I would be at 110. Should protect against this?
|
2018-05-19 19:05:30 +02:00
|
|
|
return r.Add(low)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBit(b []byte, n int) bool {
|
|
|
|
i := n / 8
|
|
|
|
j := n % 8
|
|
|
|
return b[i]&(1<<uint(7-j)) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBit(b []byte, n int, one bool) {
|
|
|
|
i := n / 8
|
|
|
|
j := n % 8
|
|
|
|
if one {
|
|
|
|
b[i] |= 1 << uint(7-j)
|
|
|
|
} else {
|
|
|
|
b[i] &= ^(1 << uint(7-j))
|
|
|
|
}
|
|
|
|
}
|
2018-06-25 21:48:57 +02:00
|
|
|
|
2018-08-31 02:20:15 +02:00
|
|
|
// Closest returns the closest bitmap to target. if no bitmaps are provided, target itself is returned
|
2018-06-25 21:48:57 +02:00
|
|
|
func Closest(target Bitmap, bitmaps ...Bitmap) Bitmap {
|
|
|
|
if len(bitmaps) == 0 {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
|
|
|
var closest *Bitmap
|
|
|
|
for _, b := range bitmaps {
|
|
|
|
if closest == nil || target.Closer(b, *closest) {
|
|
|
|
closest = &b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *closest
|
|
|
|
}
|