casset

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 2 Imported by: 1

README

casset_logo

Casset is double linked endless memory library.

Always generate new space automatically.

Memory hold length, front, back and current location
Element hold value belong memory address and next, previous elements address.

Installation

go get github.com/rytsh/casset

Usage

// create a memory with a first value
// value could be anything
l := casset.NewMemory[any]().Init(casset.NewElement[any]("My First Element")).GetFront()
l.Next("second element").Next(3.14).Next(struct{ v string }{v: "4th element"})

// for e := l.GetMemory().GetFront(); e != nil; e = e.GetNextElement() {
// 	fmt.Println(e.GetValue())
// }

for e := range l.GetMemory().Range() {
    fmt.Println(e.GetValue())
}

// Output:
// My First Element
// second element
// 3.14
// {4th element}

Documentation

Overview

Package casset help you to create memory on double linked list.

Example
package main

import (
	"fmt"

	"github.com/rytsh/casset"
)

func main() {
	// create a memory with a first value
	// value could be anything
	l := casset.NewMemory[any]().GetFront().SetValue("My First Element")
	l.Next("second element").Next(3.14).Next(struct{ v string }{v: "4th element"})

	// for e := l.GetMemory().GetFront(); e != nil; e = e.GetNextElement() {
	// 	fmt.Println(e.GetValue())
	// }

	for e := range l.GetMemory().Range() {
		fmt.Println(e.GetValue())
	}

}
Output:

My First Element
second element
3.14
{4th element}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

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

Element is an struct of double-linked list.

Example
package main

import (
	"fmt"

	"github.com/rytsh/casset"
)

func main() {
	e := casset.NewElement(1234)
	e.Next(5678).Next(91011)

	for v := e; v != nil; v = v.GetNextElement() {
		fmt.Println(v.GetValue())
	}

}
Output:

1234
5678
91011

func (*Element[T]) Delete

func (e *Element[T]) Delete() IElement[T]

Delete this element, reconnect prev and next if exist. When deleting current element, it will set current element to last or next element.

func (*Element[T]) GetMemory

func (e *Element[T]) GetMemory() IMemory[T]

func (*Element[T]) GetNextElement

func (e *Element[T]) GetNextElement() IElement[T]

func (*Element[T]) GetPrevElement

func (e *Element[T]) GetPrevElement() IElement[T]

func (*Element[T]) GetValue

func (e *Element[T]) GetValue() T

func (*Element[T]) Next

func (e *Element[T]) Next(v T) IElement[T]

Next generate new element with argument and return new element.

func (*Element[T]) Prev

func (e *Element[T]) Prev(v T) IElement[T]

Prev generate new element with argument and return new element.

func (*Element[T]) SetMemory

func (e *Element[T]) SetMemory(m IMemory[T]) IElement[T]

func (*Element[T]) SetNextElement

func (e *Element[T]) SetNextElement(element IElement[T]) IElement[T]

SetNextElement set next element.

func (*Element[T]) SetPrevElement

func (e *Element[T]) SetPrevElement(element IElement[T]) IElement[T]

SetPrevElement set previous element.

func (*Element[T]) SetValue

func (e *Element[T]) SetValue(v T) IElement[T]

type IElement

type IElement[T any] interface {
	// GetMemory return memory of this element.
	GetMemory() IMemory[T]
	// SetMemory set memory of this element.
	SetMemory(m IMemory[T]) IElement[T]
	// GetValue return value of this element.
	GetValue() T
	// GetNextElement return next element of this element.
	GetNextElement() IElement[T]
	// GetPrevElement return prev element of this element.
	GetPrevElement() IElement[T]
	// SetValue set value of this element.
	SetValue(v T) IElement[T]
	// SetNextElement set next element.
	SetNextElement(IElement[T]) IElement[T]
	// SetPrevElement set prev element.
	SetPrevElement(IElement[T]) IElement[T]
	// Delete this element, reconnect prev and next if exist.
	Delete() IElement[T]
	// Next return next element if exist or generate new element with argument and return new element.
	Next(T) IElement[T]
	// Prev return prev element if exist or generate new element with argument and return new element.
	Prev(T) IElement[T]
}

func NewElement

func NewElement[T any](v T) IElement[T]

type ILen

type ILen interface {
	Value() big.Int
	Set(func(*big.Int) *big.Int) ILen
	// Cmp compares x and y on current element and returns:
	//
	//   -1 if x <  y
	//    0 if x == y
	//   +1 if x >  y
	Cmp(y *big.Int) int
}

type IMemory

type IMemory[T any] interface {
	// Clear remove all elements.
	Clear() IMemory[T]
	// RemoveRange remove range of elements including e1 and e2.
	// If e1 is nil, start from front.
	// If e2 is nil, end at back.
	// Both e1 and e2 are nil, remove all.
	RemoveRange(e1, e2 IElement[T])
	GetLen() ILen
	GetFront() IElement[T]
	SetFront(e IElement[T])
	GetBack() IElement[T]
	SetBack(e IElement[T])
	Range() iter.Seq[IElement[T]]
}

func NewMemory

func NewMemory[T any]() IMemory[T]

NewMemory return new empty memory. Before use, you must call Init method.

type Len

type Len struct {
	// contains filtered or unexported fields
}

func NewLen

func NewLen() *Len

func (*Len) Cmp

func (l *Len) Cmp(y *big.Int) int

func (*Len) Set added in v0.1.1

func (l *Len) Set(f func(*big.Int) *big.Int) ILen

func (*Len) String added in v0.2.0

func (l *Len) String() string

func (*Len) Value added in v0.2.0

func (l *Len) Value() big.Int

type Memory

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

Memory is main struct of linked list.

func (*Memory[T]) Clear added in v0.2.1

func (m *Memory[T]) Clear() IMemory[T]

func (*Memory[T]) GetBack

func (m *Memory[T]) GetBack() IElement[T]

func (*Memory[T]) GetFront

func (m *Memory[T]) GetFront() IElement[T]

func (*Memory[T]) GetLen

func (m *Memory[T]) GetLen() ILen

func (*Memory[T]) Range added in v0.2.0

func (m *Memory[T]) Range() iter.Seq[IElement[T]]

func (*Memory[T]) RemoveRange added in v0.2.0

func (m *Memory[T]) RemoveRange(e1, e2 IElement[T])

Remove remove range of elements. If elements not inside of memory, nothing change.

func (*Memory[T]) SetBack

func (m *Memory[T]) SetBack(e IElement[T])

func (*Memory[T]) SetFront

func (m *Memory[T]) SetFront(e IElement[T])

Jump to

Keyboard shortcuts

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