Documentation
¶
Overview ¶
Package history provides data structures with undo functionality.
This package includes implementations of Map, Set, and Slice data structures that support undo operations. It also provides a Recorder interface for managing undo actions.
The main components of this package are:
- Map: A generic map that supports undo operations.
- Set: A generic set that supports undo operations.
- Slice: A generic slice that supports undo operations.
- Recorder: An interface for managing undo actions.
- BaseRecorder: A basic implementation of the Recorder interface.
Each data structure (Map, Set, and Slice) is designed to work with a Recorder, which keeps track of changes and allows for undoing operations. The BaseRecorder provides a simple implementation of the Recorder interface that can be used with any of the data structures.
Example usage:
recorder := &history.BaseRecorder{} myMap := history.NewMap[string, int](recorder, 10) myMap.Set("key", 42) value, _ := myMap.Get("key") fmt.Println(value) // Output: 42 recorder.Undo() _, ok := myMap.Get("key") fmt.Println(ok) // Output: false
This package is useful for scenarios where you need to maintain a history of changes and potentially revert them, such as in text editors, game state management, or any application where an undo feature is desired.
Index ¶
- type Action
- type BaseRecorder
- type Map
- func (m Map[K, V]) All() iter.Seq2[K, V]
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Clone(recorder Recorder) *Map[K, V]
- func (m *Map[K, V]) Contains(k K) bool
- func (m *Map[K, V]) Get(k K) (v V, ok bool)
- func (m Map[K, V]) Keys() iter.Seq[K]
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Range(f func(K, V) bool) bool
- func (m *Map[K, V]) Remove(k K) (v V, removed bool)
- func (m *Map[K, V]) Set(k K, v V) bool
- func (m Map[K, V]) String() string
- func (m Map[K, V]) Values() iter.Seq[V]
- type Recorder
- type Set
- func (s *Set[K]) Add(k K) (added bool)
- func (s Set[K]) All() iter.Seq[K]
- func (s *Set[K]) Clear()
- func (s *Set[K]) Clone(recorder Recorder) *Set[K]
- func (s *Set[K]) Contains(k K) bool
- func (s *Set[K]) Len() int
- func (s *Set[K]) Range(f func(K) bool) bool
- func (s *Set[K]) Remove(k K) (removed bool)
- func (s Set[K]) String() string
- type Slice
- func (s Slice[T]) All() iter.Seq2[int, T]
- func (s *Slice[T]) Append(elements ...T)
- func (s Slice[T]) Backward() iter.Seq2[int, T]
- func (s *Slice[T]) BinarySearch(target T, cmp func(T, T) int) (int, bool)
- func (s *Slice[T]) Cap() int
- func (s *Slice[T]) Clear()
- func (s *Slice[T]) Clip()
- func (s *Slice[T]) Clone(recorder Recorder) *Slice[T]
- func (s *Slice[T]) Compare(target *Slice[T], cmp func(T, T) int) int
- func (s *Slice[T]) Contains(target T, eq func(T, T) bool) bool
- func (s *Slice[T]) Get(i int) T
- func (s *Slice[T]) Grow(n int)
- func (s *Slice[T]) Insert(i int, elements ...T)
- func (s *Slice[T]) Len() int
- func (s *Slice[T]) RemoveAt(i int) T
- func (s *Slice[T]) RemoveFirst(value T, eq func(a, b T) bool) bool
- func (s *Slice[T]) Reverse()
- func (s *Slice[T]) Set(i int, x T)
- func (s Slice[T]) String() string
- func (s Slice[T]) Values() iter.Seq[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action interface {
// Undo reverts the changes made by this action.
Undo()
}
Action represents a single undoable action.
func ValueUndoAction ¶
ValueUndoAction creates a new Action for undoing changes to a value of any type.
type BaseRecorder ¶
type BaseRecorder struct {
// contains filtered or unexported fields
}
BaseRecorder implements the Recorder interface using a slice of Actions.
func (*BaseRecorder) Len ¶
func (r *BaseRecorder) Len() int
Len returns the number of actions in the recorder.
func (*BaseRecorder) PushAction ¶
func (r *BaseRecorder) PushAction(a Action)
PushAction appends a new Action to the BaseRecorder.
func (*BaseRecorder) Undo ¶
func (r *BaseRecorder) Undo() bool
Undo reverts the last action in the recorder. It returns true if an action was undone.
func (*BaseRecorder) UndoAll ¶
func (r *BaseRecorder) UndoAll() (n int)
UndoAll reverts all actions in reverse order and clears the recorder. It returns the number of actions undone.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a generic map with keys of type K and values of type V that supports undo operations.
func NewMap ¶
func NewMap[K comparable, V any](recorder Recorder, size int) *Map[K, V]
NewMap creates a new Map with the given recorder and initial size.
func (*Map[K, V]) Range ¶
Range calls f sequentially for each key and value in the Map. If f returns false, Range stops the iteration.
func (*Map[K, V]) Remove ¶
Remove removes a key-value pair from the Map. It returns the removed value and a boolean indicating if the key was present.
func (*Map[K, V]) Set ¶
Set adds or updates a key-value pair in the Map. It returns true if an existing entry was updated.
type Recorder ¶
type Recorder interface { // PushAction adds a new undo action to the recorder. PushAction(a Action) // UndoAll reverts all actions in reverse order and clears the recorder. // It returns the number of actions undone. UndoAll() (n int) // Undo reverts the last action in the recorder. // It returns true if an action was undone. Undo() bool }
Recorder defines the interface for adding undo actions and performing undo operations.
type Set ¶
type Set[K comparable] struct { // contains filtered or unexported fields }
Set is a generic set with elements of type K that supports undo operations.
func NewSet ¶
func NewSet[K comparable](recorder Recorder, size int) *Set[K]
NewSet creates a new Set with the given recorder and initial size.
func (*Set[K]) Add ¶
Add adds an element to the Set. It returns true if the element was not already present.
func (*Set[K]) Range ¶
Range calls f sequentially for each element in the Set. If f returns false, Range stops the iteration.
type Slice ¶
type Slice[T any] struct { // contains filtered or unexported fields }
Slice contains a slice data with recorder
func (*Slice[T]) Append ¶
func (s *Slice[T]) Append(elements ...T)
Append appends elements to the slice.
func (*Slice[T]) BinarySearch ¶
BinarySearch searches for target in the sorted slice and returns its index and a bool indicating if it was found.
func (*Slice[T]) Clip ¶
func (s *Slice[T]) Clip()
Clip reduces the slice's capacity to match its length.
func (*Slice[T]) Compare ¶
Compare compares the slice with another slice using the provided comparison function.
func (*Slice[T]) Grow ¶
Grow increases the slice's capacity, if necessary, to guarantee space for n more elements.
func (*Slice[T]) RemoveFirst ¶
RemoveFirst removes the first occurrence of the specified value from the slice. It returns true if the value was found and removed.
func (*Slice[T]) Reverse ¶
func (s *Slice[T]) Reverse()
Reverse reverses the order of elements in the slice.