history

package
v0.0.0-...-89aa834 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 5 Imported by: 0

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

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

func ValueUndoAction[T any](ptr *T, old T) Action

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]) All

func (m Map[K, V]) All() iter.Seq2[K, V]

All returns an iterator over key-value pairs in the map

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear removes all elements from the Map.

func (*Map[K, V]) Clone

func (m *Map[K, V]) Clone(recorder Recorder) *Map[K, V]

Clone creates a deep copy of the Map with a new recorder.

func (*Map[K, V]) Contains

func (m *Map[K, V]) Contains(k K) bool

Contains checks if the Map contains the given key.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(k K) (v V, ok bool)

Get retrieves the value for a key in the Map.

func (Map[K, V]) Keys

func (m Map[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over the map keys

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of elements in the Map.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(K, V) bool) bool

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

func (m *Map[K, V]) Remove(k K) (v V, removed bool)

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

func (m *Map[K, V]) Set(k K, v V) bool

Set adds or updates a key-value pair in the Map. It returns true if an existing entry was updated.

func (Map[K, V]) String

func (m Map[K, V]) String() string

String returns a string representation of the Map.

func (Map[K, V]) Values

func (m Map[K, V]) Values() iter.Seq[V]

Values returns an iterator over the map values

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

func (s *Set[K]) Add(k K) (added bool)

Add adds an element to the Set. It returns true if the element was not already present.

func (Set[K]) All

func (s Set[K]) All() iter.Seq[K]

All returns an iterator over keys in the set

func (*Set[K]) Clear

func (s *Set[K]) Clear()

Clear removes all elements from the Set.

func (*Set[K]) Clone

func (s *Set[K]) Clone(recorder Recorder) *Set[K]

Clone creates a deep copy of the Set with a new recorder.

func (*Set[K]) Contains

func (s *Set[K]) Contains(k K) bool

Contains checks if the Set contains the given element.

func (*Set[K]) Len

func (s *Set[K]) Len() int

Len returns the number of elements in the Set.

func (*Set[K]) Range

func (s *Set[K]) Range(f func(K) bool) bool

Range calls f sequentially for each element in the Set. If f returns false, Range stops the iteration.

func (*Set[K]) Remove

func (s *Set[K]) Remove(k K) (removed bool)

Remove removes an element from the Set. It returns true if the element was present.

func (Set[K]) String

func (s Set[K]) String() string

String returns a string representation of the Set.

type Slice

type Slice[T any] struct {
	// contains filtered or unexported fields
}

Slice contains a slice data with recorder

func NewSlice

func NewSlice[T any](recorder Recorder, len, cap int) *Slice[T]

NewSlice creates a new slice with the given recorder, length, and capacity.

func (Slice[T]) All

func (s Slice[T]) All() iter.Seq2[int, T]

All returns an iterator over index-value pairs in the slice

func (*Slice[T]) Append

func (s *Slice[T]) Append(elements ...T)

Append appends elements to the slice.

func (Slice[T]) Backward

func (s Slice[T]) Backward() iter.Seq2[int, T]

Backward returns an iterator over index-value pairs in the slice,

func (*Slice[T]) BinarySearch

func (s *Slice[T]) BinarySearch(target T, cmp func(T, T) int) (int, bool)

BinarySearch searches for target in the sorted slice and returns its index and a bool indicating if it was found.

func (*Slice[T]) Cap

func (s *Slice[T]) Cap() int

Cap returns the capacity of the slice.

func (*Slice[T]) Clear

func (s *Slice[T]) Clear()

Clear removes all elements from the slice.

func (*Slice[T]) Clip

func (s *Slice[T]) Clip()

Clip reduces the slice's capacity to match its length.

func (*Slice[T]) Clone

func (s *Slice[T]) Clone(recorder Recorder) *Slice[T]

Clone creates a deep copy of the Slice with a new recorder.

func (*Slice[T]) Compare

func (s *Slice[T]) Compare(target *Slice[T], cmp func(T, T) int) int

Compare compares the slice with another slice using the provided comparison function.

func (*Slice[T]) Contains

func (s *Slice[T]) Contains(target T, eq func(T, T) bool) bool

Contains reports whether target is present in the slice.

func (*Slice[T]) Get

func (s *Slice[T]) Get(i int) T

Get returns the i-th element of the slice.

func (*Slice[T]) Grow

func (s *Slice[T]) Grow(n int)

Grow increases the slice's capacity, if necessary, to guarantee space for n more elements.

func (*Slice[T]) Insert

func (s *Slice[T]) Insert(i int, elements ...T)

Insert inserts elements at i-th position of the slice.

func (*Slice[T]) Len

func (s *Slice[T]) Len() int

Len returns the length of the slice.

func (*Slice[T]) RemoveAt

func (s *Slice[T]) RemoveAt(i int) T

RemoveAt removes the i-th element from the slice and returns it.

func (*Slice[T]) RemoveFirst

func (s *Slice[T]) RemoveFirst(value T, eq func(a, b T) bool) bool

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.

func (*Slice[T]) Set

func (s *Slice[T]) Set(i int, x T)

Set sets the i-th element of the slice to x.

func (Slice[T]) String

func (s Slice[T]) String() string

String returns a string representation of the Slice.

func (Slice[T]) Values

func (s Slice[T]) Values() iter.Seq[T]

Values returns an iterator that yields the slice elements in order.

Jump to

Keyboard shortcuts

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