A simple data structure to manage history through circular stack.
-
npm
npm install --save @algorithm.ts/history
-
pnpm
pnpm add @algorithm.ts/history
History is a fixed size stack structure, the main purpose of its design is to reuse space as much as possible on the basis of ordinary historys. Historys usually need to specify a capacity, if the number of elements in the history exceeds the capacity, only the most recent capacity elements are preserved in the history. Other operations are the same as ordinary historys.
-
IHistory: History implements the IHistory interface.Signature Description readonly capacity: numberThe capacity of the history, which also means the maximum elements of the history. readonly size: numberThe count of the elements in the history. readonly name: stringThe name of the history. readonly equals: IEqualsUsed to check if two element in the history are same. backward(step?: number): [element: T | undefined, isBot: boolean]Backward stepsteps and return the present element of the history.clear(): voidClear the history. count(filter: (element: T, index: number) => boolean): numberCount the elements in the history which matched the filter. fork(name: string): IHistory<T>Create a new history from the current one. forward(step?: number): [element: T | undefined, isTop: boolean]Forward stepsteps and return the present from the history.go(index?: number): T | undefinedSet the present index to the given index and return the elements at the index of the history. isBot(): booleanCheck if the present index is at the bottom of the history. isTop(): booleanCheck if the present index is at the top of the history. present(): [element: T | undefined, index: number]Return the present element and present index of the history. push(element: T): thisPush the element to the history. rearrange(filter: (element: T, index: number) => boolean): voidRearrange the history and only keep the elements matched the given filter.updateTop(element: T): voidChange the top element of the history. -
IHistoryPropsexport interface IHistoryProps<T> { /** * The history name. */ readonly name: string /** * Initial capacity of the circular history. */ readonly capacity: number /** * Used to check if two element in the history are same. */ readonly equals?: IEquals<T> }
-
Basic -- CircularHistory
import { History } from '@algorithm.ts/history' const history = new History<{ name: string }>({ name: 'profile', capacity: 100 }) // Append a element to the end of the history. history.push({ name: 'alice' }) // => 0 history.push({ name: 'bob' }) // => 1 history.size // => 2 // Get the front element of the history. history.present() // => [{ name: 'bob' }, 1] history.backward() // => [{ name: 'alice' }, 0] history.backward() // => [{ name: 'alice' }, 0] history.forward() // => [{ name: 'bob' }, 1] history.forward() // => [{ name: 'bob' }, 1]