sliceutil: helpers for mid-slice insert/delete.
Manipulating the hand, deck, etc. is going to use these operations a lot.
This commit is contained in:
@ -31,8 +31,8 @@ func (d *Deck[C]) Len() int {
|
||||
return len(d.cards)
|
||||
}
|
||||
|
||||
// Insert puts a card at a specific location in the Deck. The card previously
|
||||
// at that location and all locations after are shifted one card later.
|
||||
// Insert puts one or more cards at a specific location in the Deck. Cards
|
||||
// at that location and all locations after are shifted deeper into the deck.
|
||||
// Negative indexes are counted from the bottom of the deck. BottomOfDeck is
|
||||
// a sentinel value for the bottommost position; -1 is one card above.
|
||||
//
|
||||
@ -44,7 +44,7 @@ func (d *Deck[C]) Len() int {
|
||||
// WarningTopClamped. Like all warnings, these can be safely ignored and the
|
||||
// program is in a well-defined state, but you may want to check for them
|
||||
// if you expect some other behavior.
|
||||
func (d *Deck[C]) Insert(idx int, card Card[C]) error {
|
||||
func (d *Deck[C]) Insert(idx int, card ...Card[C]) error {
|
||||
var errs ErrorCollector
|
||||
// Calculate actual target index.
|
||||
switch {
|
||||
@ -60,14 +60,7 @@ func (d *Deck[C]) Insert(idx int, card Card[C]) error {
|
||||
idx += d.Len()
|
||||
}
|
||||
// remaining case: 0 <= idx <= d.Len(), which is a normal forward insert index.
|
||||
|
||||
// Place new card on bottom and "bubble" into position.
|
||||
// Takes O(N) time. If this turns out to be a problem, implement a more
|
||||
// efficient data structure.
|
||||
d.cards = append(d.cards, card)
|
||||
for i := len(d.cards) - 1; i > idx; i-- {
|
||||
d.cards[i], d.cards[i-1] = d.cards[i-1], d.cards[i]
|
||||
}
|
||||
d.cards = InsertInto(d.cards, idx, card...)
|
||||
return errs.Emit()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user