Documentation
¶
Overview ¶
utility functions for dealing with files
utility functions for math. the Go standard library has some of these as well, but in a lot of situations they are too generic and weighed down by unnecessary checks and the like. the functions here are expected to be run in tight loops, so we strip out unneeded cruft. other things are just useful functions that I made up as I needed them.
utility functions for generating random numbers, sequences, etc.
utility functions for handling strings, text, runes, etc.
Index ¶
- func Abs[T constraints.Signed](value T) T
- func Clamp[T constraints.Ordered](value, min, max T) T
- func CycleClamp(value, min, max int) int
- func CycleClampWithOverflow(val, min, max int) (int, int)
- func GetFileList(directory_path, extension string) (files []string, err error)
- func Lerp[T constraints.Integer | constraints.Float](start, end T, val, steps int) T
- func LoremIpsum(words int) string
- func PickOne[S ~[]E, E any](slice S) E
- func Pow(value, exponent int) int
- func RoundFloatToInt(f float64) int
- func WalkSubTrees[T TreeType[T]](node T, fn func(T), predicates ...func(T) bool)
- func WalkTree[T TreeType[T]](node T, fn func(T), predicates ...func(T) bool)
- func WrapText(text string, width int, maxlines ...int) (lines []string)
- type Set
- func (s *Set[E]) Add(elems ...E)
- func (s *Set[E]) AddSet(s2 Set[E])
- func (s Set[E]) Contains(elem E) bool
- func (s Set[E]) ContainsAll(elems ...E) bool
- func (s Set[E]) ContainsAny(elems ...E) bool
- func (s Set[E]) Count() int
- func (s Set[E]) EachElement() iter.Seq[E]
- func (s Set[E]) Equals(s2 Set[E]) bool
- func (s Set[E]) Intersection(s2 Set[E]) (intersection Set[E])
- func (s *Set[E]) Remove(elems ...E)
- func (s *Set[E]) RemoveAll()
- func (s *Set[E]) RemoveSet(s2 Set[E])
- func (s Set[E]) Union(s2 Set[E]) (union Set[E])
- type State
- type StateID
- type StateMachine
- type TreeNode
- func (t *TreeNode[T]) AddChild(node T)
- func (t *TreeNode[T]) AddChildren(nodes ...T)
- func (t TreeNode[T]) ChildCount() int
- func (t *TreeNode[T]) Deparent()
- func (t TreeNode[T]) GetChildren() []T
- func (t TreeNode[T]) GetParent() T
- func (t TreeNode[T]) GetSelf() T
- func (t *TreeNode[T]) Init(self T)
- func (t *TreeNode[T]) RemoveChild(node T)
- type TreeType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clamp ¶
func Clamp[T constraints.Ordered](value, min, max T) T
Clamp checks if min <= val <= max. If val < min, returns min. If val > max, returns max. Otherwise returns val.
func CycleClamp ¶
CycleClamp is like clamp but instead of clamping at the endpoints, it overflows/underflows back to the other side of the range. This range of the function is INCLUSIVE of min and max, so min <= val <= max.
func CycleClampWithOverflow ¶
CycleClampWithOverflow is like CycleClamp but also returns the number of overflow cycles. negative for underflow, 0 for none, positive for overflow. NOTE: this function kind of doesn't work as expected since it is inclusive at the end points. THINK: should this just be removed? it's pretty niche.
func GetFileList ¶
GetFileList returns a list of all files in the provided directory. If ext is provided, it only includes files with that extension.
func Lerp ¶
func Lerp[T constraints.Integer | constraints.Float](start, end T, val, steps int) T
Lerp linearly interpolates a range (start-end) over (steps) intervals, and returns the (val)th step.
func LoremIpsum ¶
Returns a string of lorem ipsum test text with the requested number of words.
func PickOne ¶
func PickOne[S ~[]E, E any](slice S) E
Picks a random element from the slice and returns it.
func Pow ¶
Pow is an integer power function. Doesn't ~~do~~ negative exponents. Totally does 0 though.
func RoundFloatToInt ¶
RoundFloatToInt rounds a float to an int in the way you'd expect. It's the way I expect anyways.
func WalkSubTrees ¶
Walks the tree, calling function f on all subnodes in the tree below the provided node. This is identical to Walktree, except it doesn't call f on the root node.
func WalkTree ¶
Walks the tree, starting from the provided root node. The function f is called in a depth-first manner, on leaf nodes first and then up. The type T must be specified, inferring it doesn't really seem to work. Optionally takes any number of predicates. If any are provided, they are called for each node before continuing to traverse the tree and if any return false, stops the walk for that node and all of its children. THINK: should there be non-depth-first versions?
Types ¶
type Set ¶ added in v0.2.0
type Set[E comparable] struct { // contains filtered or unexported fields }
Set is a container that holds elements of a type E. Elements can be added and removed, and duplicate adds are no-ops.
func (*Set[E]) AddSet ¶ added in v0.2.0
Adds the elements of another set to the Set. Operationally this is the same as a Union, except it works in-place.
func (Set[E]) ContainsAll ¶ added in v0.2.0
func (Set[E]) ContainsAny ¶ added in v0.2.0
func (Set[E]) EachElement ¶ added in v0.2.0
func (Set[E]) Intersection ¶ added in v0.2.0
type State ¶ added in v0.2.0
type State struct { OnEnter func(previous_state StateID) // Callback run when state is entered. OnLeave func(next_state StateID) // Callback run when state is left. }
A State is an element of a state machine that defines the behaviour that occurs when states are changed. Both OnEnter and OnLeave callbacks are optional.
type StateMachine ¶ added in v0.2.0
type StateMachine struct { // OnStateChange is a callback that is run whenever the state machine changes state. OnStateChange func(previous, next StateID) // contains filtered or unexported fields }
A StateMachine allows you to register different states, one of which will be the CurrentState. You can change to another state, and the StateMachine will run any callbacks as appropriate. See the ChangeState() function for specifics. One of the basic fundamentals of a state machine is that all states are mutually exclusive; only one state will be active at a time. Use this in cases where something can be one of multiple states and you need to define behaviour for swapping between them. StateMachines do not need to be initialized and default to being in the state STATE_NONE, which has no callbacks. You can use this as a sort of default state if you like, or define your own and change away on setup, never to return again...
func (*StateMachine) ChangeState ¶ added in v0.2.0
func (sm *StateMachine) ChangeState(next StateID)
ChangeState is the heart of the StateMachine. If next is a valid StateID, and is not the same as the current state, a change will be initiated. Callbacks are called in this order: 1) The Current State's OnLeave() 2) The StateMachine's OnStateChange() 3) The Next State's OnEnter() Of course all of these are optional, they do not need to be explicitly set. NOTE: callbacks CANNOT contain more state changes inside! this can cause an infinite loop if you're not careful which will make Tyumi cry. If this is detected it will throw an error and all but the initial call to ChangeState will fail.
func (StateMachine) CurrentState ¶ added in v0.2.0
func (sm StateMachine) CurrentState() StateID
CurrentState retrieves the ID for the currently active state.
func (*StateMachine) RegisterState ¶ added in v0.2.0
func (sm *StateMachine) RegisterState(new_state State) StateID
RegisterState adds a state to the state machine, and returns an ID that you can use to refer to the state. Note that once set up States can't be modified so don't bother retaining raw State objects, just use the IDs. THINK: maybe mutable state objects would be useful? not sure how but... maybe?
type TreeNode ¶
type TreeNode[T tree] struct {
// contains filtered or unexported fields
}
TreeNode[T] implements the TreeType[T] generic interface. This object can stand freeform, or can be embedded in other type to give them tree functionality. Remember that the node is not usable unless it has been initialized with Init. Failing to do this will crash your whole gig and make the computer have a headache. So don't do that.
func (*TreeNode[T]) AddChildren ¶
func (t *TreeNode[T]) AddChildren(nodes ...T)
func (TreeNode[T]) ChildCount ¶
func (TreeNode[T]) GetChildren ¶
func (t TreeNode[T]) GetChildren() []T
func (*TreeNode[T]) Init ¶
func (t *TreeNode[T]) Init(self T)
Initializes the node with a reference to the object in the tree that it represents. If this is not done the tree cannot work.
func (*TreeNode[T]) RemoveChild ¶
func (t *TreeNode[T]) RemoveChild(node T)
type TreeType ¶
type TreeType[T tree] interface { GetParent() T GetChildren() []T GetSelf() T AddChild(child T) AddChildren(children ...T) RemoveChild(child T) // contains filtered or unexported methods }
TreeType[T] is the interface for trees of objects T (duh). Ts can be added as children to other Ts, and as such a tree structure is built. Trees ensure all contained items are unique (no object in the can have 2 parents).