<commit_before>// vim:set ts=2 sw=2 et ai ft=go:
// Copyright (c) 2012 <NAME>. See accompanying LICENSE file for
// detailed licensing information.
package main

import (
  "crypto/rand"
  "math/big"
)

const DECK_SIZE = 52

type Deck struct {
  pos   int
  cards [DECK_SIZE]Card
}

// ----- PUBLIC API ---------------------------------------------------------

func NewDeck() *Deck {
  deck := new(Deck)
  deck.pos = 0

  i := 0
  for suit := range suits {
    for rank := range ranks {
      deck.cards[i] = newCard(suits[suit], ranks[rank])
      i++
    }
  }
  return deck
}

//
// Shuffle() uses a Fisher-Yates shuffle:
//
//   http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
//
func (deck *Deck) Shuffle() {
  for i := len(deck.cards) - 1; i > 0; i-- {
    if j := randInt(i + 1); j >= 0 && i != j {
      deck.swap(i, j)
    }
  }
  deck.pos = 0
}

func (deck *Deck) Empty() bool {
  return deck.pos >= len(deck.cards) - 1
}

func (deck *Deck) Deal() (card Card) {
  if deck.Empty() {
    return NoCard
  }
  card = deck.cards[deck.pos]
  deck.pos++
  return card
}

func (deck *Deck) Burn() {
  if !deck.Empty() {
    deck.Deal()
  }
}

// ----- INTERNAL FUNCTIONS -------------------------------------------------

func (deck *Deck) swap(i int, j int) {
  deck.cards[i], deck.cards[j] = deck.cards[j], deck.cards[i]
}

// randInt generates a cryptographically-secure pseudo-random number in the
// range [0,max).
// It returns the generated number on success or -1 if a number could not
// be generated or max was less than 0.
func randInt(max int) int {
  if max < 0 {
    return -1
  }
  m := big.NewInt(int64(max))
  r, e := rand.Int(rand.Reader, m)
  if e != nil {
    return -1
  }
  return int(r.Int64() % int64(max))
}
<commit_msg>should return -1 when max == 0, too<commit_after>// vim:set ts=2 sw=2 et ai ft=go:
// Copyright (c) 2012 <NAME>. See accompanying LICENSE file for
// detailed licensing information.
package main

import (
  "crypto/rand"
  "math/big"
)

const DECK_SIZE = 52

type Deck struct {
  pos   int
  cards [DECK_SIZE]Card
}

// ----- PUBLIC API ---------------------------------------------------------

func NewDeck() *Deck {
  deck := new(Deck)
  deck.pos = 0

  i := 0
  for suit := range suits {
    for rank := range ranks {
      deck.cards[i] = newCard(suits[suit], ranks[rank])
      i++
    }
  }
  return deck
}

//
// Shuffle() uses a Fisher-Yates shuffle:
//
//   http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
//
func (deck *Deck) Shuffle() {
  for i := len(deck.cards) - 1; i > 0; i-- {
    if j := randInt(i + 1); j >= 0 && i != j {
      deck.swap(i, j)
    }
  }
  deck.pos = 0
}

func (deck *Deck) Empty() bool {
  return deck.pos >= len(deck.cards) - 1
}

func (deck *Deck) Deal() (card Card) {
  if deck.Empty() {
    return NoCard
  }
  card = deck.cards[deck.pos]
  deck.pos++
  return card
}

func (deck *Deck) Burn() {
  if !deck.Empty() {
    deck.Deal()
  }
}

// ----- INTERNAL FUNCTIONS -------------------------------------------------

func (deck *Deck) swap(i int, j int) {
  deck.cards[i], deck.cards[j] = deck.cards[j], deck.cards[i]
}

// randInt generates a cryptographically-secure pseudo-random number in the
// range [0,max).
// It returns the generated number on success or -1 if a number could not
// be generated or max was less than or equal to 0.
func randInt(max int) int {
  if max <= 0 {
    return -1
  }
  m := big.NewInt(int64(max))
  r, e := rand.Int(rand.Reader, m)
  if e != nil {
    return -1
  }
  return int(r.Int64() % int64(max))
}

{'fraction_non_alphanumeric': 0.21414427157001414, 'fraction_numerical': 0.01640735502121641, 'mean_word_length': 2.0378006872852232, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6994285', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1268, 'n_words': 383}