algorithm/dirtiable

Simple module that provides the concept of a variable that can retain it's prior state but be used normally.

All Dirtiables are basically tuple wrappers around a given value. I'm pretty sure I covered all the general needs but if anyone has any further requests or would like to submit more please feel free to open an issue.

Types

Dirtiable[T] = ref object
  value, altered: T
  isDirty: bool
CountingDirtiable[T] = ref object
  value, altered: T
  isDirty: bool
  changes: int
A Dirtiable that simply tracks how many changes it has recieved
HistoryDirtiable[T] = ref object
  value, altered: T
  isDirty: bool
  changelog: seq[T]
A Dirtiable that tracks the previous changes made to it

Funcs

func dirtify[T](data: T): Dirtiable[T]
Wrap the target data in a dirtiable object
func count_dirtify[T](data: T): CountingDirtiable
Wrap the target data in a counting dirtiable object
func histo_dirtify[T](data: T): HistoryDirtiable
Wrap the target data in a history dirtiable object
func get[T](self: SomeDirtiable[T]): T
Get the current value from the dirtiable
func `^=`[T](self: var SomeDirtiable[T]; newValue: T)
Equality operator. Applies the new value to the Dirtiable's altered one, and does all the internal work to make the various Dirtiable types work.
func `^=`[T](newValue: var T; self: SomeDirtiable[T])
Equality operator. Gets the current running value and returns it
func `!^=`[T](self: var SomeDirtiable[T]; newValue: T)
Sets the value of the dirtiable and applies it.
func `!^=`[T](newValue: var T; self: var SomeDirtiable[T])
First applies the dirtiable's changes and then sets newValue to it.
func `^==`[T](self: SomeDirtiable[T]; value: T): bool
Equality test that uses the altered value if it is available, otherwise uses the original one
func `~=`[T](self: SomeDirtiable[T]; value: T): bool
Inequality test that uses the altered value if it is available, otherwise uses the original one
func apply(self: var SomeDirtiable)
Updates the value with the altered version
func getapply[T](self: var SomeDirtiable[T]): T
Updates the value with the altered version and returns it
func cancel(self: var SomeDirtiable)
Resets the altered value to the original version
func undo(self: var HistoryDirtiable)
Reapplies the change before last and removes it from the history
func clear(self: var HistoryDirtiable)
Clears the history of a tracked Dirtiable var
func `~=`[T](self: SomeDirtiable[T]; value: T): bool
Inequality test that uses the altered value if it is available, otherwise uses the original one
func `^=`[T](newValue: var T; self: SomeDirtiable[T])
Equality operator. Gets the current running value and returns it
func `^=`[T](self: var SomeDirtiable[T]; newValue: T)
Equality operator. Applies the new value to the Dirtiable's altered one, and does all the internal work to make the various Dirtiable types work.
func `^==`[T](self: SomeDirtiable[T]; value: T): bool
Equality test that uses the altered value if it is available, otherwise uses the original one

Templates

template `~=`[T](value: T; self: SomeDirtiable[T]): bool
template `^==`[T](value: T; self: SomeDirtiable[T]): bool
template `~=`[T](value: T; self: SomeDirtiable[T]): bool
template `^==`[T](value: T; self: SomeDirtiable[T]): bool