Improvements to internal/history

This commit is contained in:
Sasha Koshka 2024-09-06 00:12:05 -04:00
parent ac1a952b40
commit 63ad06e214

View File

@ -4,7 +4,7 @@ import "time"
// History stores a stack of items, always keeping the bottom-most one. It must // History stores a stack of items, always keeping the bottom-most one. It must
// be created using the NewHistory constructor, otherwise it will be invalid. // be created using the NewHistory constructor, otherwise it will be invalid.
type History[T any] struct { type History[T comparable] struct {
max int max int
stack []T stack []T
topIndex int topIndex int
@ -14,7 +14,7 @@ type History[T any] struct {
// NewHistory creates a new History. The initial item will be on the bottom, and // NewHistory creates a new History. The initial item will be on the bottom, and
// it will remain there until the History overflows and chooses the item after // it will remain there until the History overflows and chooses the item after
// it to be the initial item. // it to be the initial item.
func NewHistory[T any] (initial T, max int) *History[T] { func NewHistory[T comparable] (initial T, max int) *History[T] {
return &History[T] { return &History[T] {
max: max, max: max,
stack: []T { initial }, stack: []T { initial },
@ -29,14 +29,28 @@ func (this *History[T]) Top () T {
// Swap replaces the most recent item with another. // Swap replaces the most recent item with another.
func (this *History[T]) Swap (item T) { func (this *History[T]) Swap (item T) {
this.topTime = time.Now() this.topTime = time.Now()
this.SwapSilently(item)
}
// SwapSilently replaces the most recent item with another without updating the
// time.
func (this *History[T]) SwapSilently (item T) {
this.stack[this.topIndex] = item this.stack[this.topIndex] = item
} }
// Push pushes a new item onto the stack. // Push pushes a new item onto the stack. If the stack overflows (becomes bigger
// than the specified max value), the initial item is removed and the one on top
// of it takes its place.
func (this *History[T]) Push (item T) { func (this *History[T]) Push (item T) {
this.topTime = time.Now() this.topTime = time.Now()
if this.Top() != item {
this.topIndex ++ this.topIndex ++
this.stack = append(this.stack[:this.topIndex], item) this.stack = append(this.stack[:this.topIndex], item)
}
if len(this.stack) > this.max {
this.stack = this.stack[1:]
}
} }
// PushWeak replaces the most recent item if it was added recently (sooner than // PushWeak replaces the most recent item if it was added recently (sooner than