shufflesharding

package
v0.0.0-...-d88c8b5 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const MaxHashBits = 60

MaxHashBits is the max bit length which can be used from hash value. If we use all bits of hash value, the critical(last) card shuffled by Dealer will be uneven to 2:3 (first half:second half) at most, in order to reduce this unevenness to 32:33, we set MaxHashBits to 60 here.

Variables

This section is empty.

Functions

func RequiredEntropyBits

func RequiredEntropyBits(deckSize, handSize int) int

RequiredEntropyBits makes a quick and slightly conservative estimate of the number of bits of hash value that are consumed in shuffle sharding a deck of the given size to a hand of the given size. The result is meaningful only if 1 <= handSize <= deckSize <= 1<<26.

Types

type Dealer

type Dealer struct {
	// contains filtered or unexported fields
}

Dealer contains some necessary parameters and provides some methods for shuffle sharding. Dealer is thread-safe.

func NewDealer

func NewDealer(deckSize, handSize int) (*Dealer, error)

NewDealer will create a Dealer with the given deckSize and handSize, will return error when deckSize or handSize is invalid as below. 1. deckSize or handSize is not positive 2. handSize is greater than deckSize 3. deckSize is impractically large (greater than 1<<26) 4. required entropy bits of deckSize and handSize is greater than MaxHashBits

func (*Dealer) Deal

func (d *Dealer) Deal(hashValue uint64, pick func(int))

Deal shuffles a card deck and deals a hand of cards, using the given hashValue as the source of entropy. The deck size and hand size are properties of the Dealer. This function synchronously makes sequential calls to pick, one for each dealt card. Each card is identified by an integer in the range [0, deckSize). For example, for deckSize=128 and handSize=4 this function might call pick(14); pick(73); pick(119); pick(26).

func (*Dealer) DealIntoHand

func (d *Dealer) DealIntoHand(hashValue uint64, hand []int) []int

DealIntoHand shuffles and deals according to the Dealer's parameters, using the given hashValue as the source of entropy and then returns the dealt cards as a slice of `int`. If `hand` has the correct length as Dealer's handSize, it will be used as-is and no allocations will be made. If `hand` is nil or too small, it will be extended (performing an allocation). If `hand` is too large, a sub-slice will be returned.

Jump to

Keyboard shortcuts

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