elist_head

package module
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2021 License: MIT Imports: 6 Imported by: 1

README

elist_head

elist_head is fastest embedded doubly linked list like a linux kernel's LIST_HEAD for golang

feature

basic features is the same with lista_encabezado.

  • alternatives for container/list. container/list allocate per set new element. but elist_head is embedded to struct. not over allocatation.
  • lista_encabezado is prev/next normal pointer. elist_head is relative pointer version. relative pointer should not be used in golang. elist_head element should be used in slices.
  • if lista_encabezado 's element store to slice, slice append , element pointer changed . so you must change all efercnce element's prev/next. but elist_head is condition referernce in slice.

require

golang > 1.17

basic usaage

sample is in sample_entry.go

type SampleEntry struct {
	Name string
	Age  int
	ListHead
}

var EmptySampleEntry *SampleEntry = nil

const sampleEntryOffset = unsafe.Offsetof(EmptySampleEntry.ListHead)

func SampleEntryFromListHead(head *elist_head.ListHead) *SampleEntry {
	return (*SampleEntry)(ElementOf(unsafe.Pointer(head), sampleEntryOffset))
}

func (s *SampleEntry) Offset() uintptr {
	return sampleEntryOffset
}

func (s *SampleEntry) PtrListHead() *elist_head.ListHead {
	return &(s.ListHead)
}

func (s *SampleEntry) fromListHead(l *elist_head.ListHead) *SampleEntry {
	return SampleEntryFromListHead(l)
}

func (s *SampleEntry) FromListHead(l *elist_head.ListHead) List {
	return s.fromListHead(l)
}

func (s *SampleEntry) Prev() *S*ampleEntry {
	return s.fromListHead(s.ListHead.Prev())
}
func (s *SampleEntry) Next() *S*ampleEntry {
	return s.fromListHead(s.ListHead.Prev())
}

func (s *SampleEntry) InsertBefore(n *SampleEntry) (err error) {
	_, err = s.ListHead.InsertBefore(&n.ListHead)
	return
}


func main() {


    list := elist_head.NewEmptyList()

    elems := make([]SampleEntry, 10)


    elem := &elems[0]
    elem.Name = "namae dayo"
    elem.age = 10

    elems[1].Name = "namae dayo"
    elems[1].age = 15
    
    // add elem last of list
    list.Tail().InsertBefore(&elem.ListHead)
    // add elems[1] before elemens[0]
    elem.InsertBefore(&elems[1])

    // get 
    entry := EmptySampleEntry.FromListHead(list.Head().Next()).(*SampleEntry)
    name := entry.Name


    // get before element using Prev()
    nEntry := elem.Prev()

}

Documentation

Overview

Package elist_head is like a kernel's LIST_HEAD usage for storing slice/array

Package elist_head is like a kernel's LIST_HEAD usage for storing slice/array

Index

Constants

View Source
const (
	ErrTDeleteFirst = 1 << iota
	ErrTListNil
	ErrTEmpty
	ErrTMarked
	ErrTNextMarked
	ErrTNotAppend
	ErrTNotMarked
	ErrTCasConflictOnMark
	ErrTFirstMarked
	ErrTCasConflictOnAdd
	ErrTOverRetyry
	ErrTNoSafety
	ErrTNoContinous
)

Variables

View Source
var (
	MODE_CONCURRENT      bool = false
	PANIC_NEXT_IS_MARKED bool = false
)
View Source
var (
	ErrDeleteFirst       error = NewError(ErrTDeleteFirst, errors.New("first element cannot delete"))
	ErrListNil           error = NewError(ErrTListNil, errors.New("list is nil"))
	ErrEmpty             error = NewError(ErrTEmpty, errors.New("list is empty"))
	ErrMarked            error = NewError(ErrTMarked, errors.New("element is marked"))
	ErrNextMarked        error = NewError(ErrTNextMarked, errors.New("next element is marked"))
	ErrNotAppend         error = NewError(ErrTNotAppend, errors.New("element cannot be append"))
	ErrNotMarked         error = NewError(ErrTNotMarked, errors.New("elenment cannot be marked"))
	ErrCasConflictOnMark error = NewError(ErrTCasConflictOnMark, errors.New("cas conflict(fail mark)"))
	ErrFirstMarked       error = NewError(ErrTFirstMarked, errors.New("first element is marked"))
	ErrNoSafetyOnAdd     error = NewError(ErrTNoSafety, errors.New("element is not safety to append"))
	ErrNoContinous       error = NewError(ErrTNoContinous, errors.New("element is not continus"))
)

Functions

func Cas

func Cas(target *uintptr, old, new uintptr) bool

func CasIncPointer added in v0.2.4

func CasIncPointer(t *uintptr, same uintptr, moved int) bool

func ElementOf

func ElementOf(head unsafe.Pointer, offset uintptr) unsafe.Pointer

func Error

func Error(oe error, opts ...OptNewError) error

func GetConcurrentMode

func GetConcurrentMode() bool

func IncPointer added in v0.2.2

func IncPointer(t uintptr, moved int) uintptr

func InitAfterSafety

func InitAfterSafety(retry int) func(*ListHead) error

func InitAsEmpty

func InitAsEmpty(head *ListHead, tail *ListHead)

func MarkListHead

func MarkListHead(target *uintptr, old uintptr) bool

func NewEmptyList added in v0.2.3

func NewEmptyList() initedListHead

NewEmptyList ... make Empty List . this has only head and tail terminater.

elist_head require head/tail terminater for list operation.

func OuterPtrs

func OuterPtrs(sHead, sTail unsafe.Pointer, dHead unsafe.Pointer, size int, offset int) (outers []unsafe.Pointer)

func RepaireSliceAfterCopy

func RepaireSliceAfterCopy(sHead, sTail unsafe.Pointer, dHead unsafe.Pointer, size int, offset int) error

func RollbacksharedModeTraverse

func RollbacksharedModeTraverse(prev list_head.TravOpt)

func SharedTrav added in v0.2.2

func SharedTrav(travs ...list_head.TravOpt) []list_head.TravOpt

func StoreListHead

func StoreListHead(dst *unsafe.Pointer, src *ListHead)

Types

type BoolAndError

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

func MakeBoolAndError

func MakeBoolAndError(t bool, e error) BoolAndError
type Head interface {
	Next(opts ...list_head.TravOpt) Head
	DirectNext() Head
	Prev(opts ...list_head.TravOpt) Head
	DirectPrev() Head
	InsertBefore(new Head, opts ...list_head.TravOpt) (Head, error)
	MarkForDelete(opts ...list_head.TravOpt) (err error)
	Empty() bool
	Ptr() unsafe.Pointer
}

type List

type List interface {
	Offset() uintptr
	PtrListHead() *ListHead
	FromListHead(*ListHead) List
}

type ListHead

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

func NewEmpty

func NewEmpty() *ListHead

func NextNoM

func NextNoM(head *ListHead) *ListHead

func PrevNoM

func PrevNoM(head *ListHead) *ListHead

func (*ListHead) Delete

func (head *ListHead) Delete(opts ...func(*ListHead) error) (result *ListHead, e error)

func (*ListHead) DirectNext

func (head *ListHead) DirectNext() *ListHead

func (*ListHead) DirectPrev

func (head *ListHead) DirectPrev() *ListHead

func (*ListHead) Empty

func (head *ListHead) Empty() bool

func (*ListHead) Init

func (head *ListHead) Init()

func (*ListHead) InsertBefore

func (head *ListHead) InsertBefore(new *ListHead, opts ...list_head.TravOpt) (*ListHead, error)

func (*ListHead) IsMarked

func (head *ListHead) IsMarked() bool

func (*ListHead) IsSafety

func (head *ListHead) IsSafety() (bool, error)

func (*ListHead) IsSingle

func (head *ListHead) IsSingle() bool

func (*ListHead) MarkForDelete

func (head *ListHead) MarkForDelete(opts ...list_head.TravOpt) (err error)

func (*ListHead) Next

func (head *ListHead) Next(opts ...list_head.TravOpt) (next *ListHead)

func (*ListHead) OffetNext added in v0.2.2

func (head *ListHead) OffetNext() uintptr

func (*ListHead) OffetPrev added in v0.2.2

func (head *ListHead) OffetPrev() uintptr

func (*ListHead) P

func (head *ListHead) P() string

func (*ListHead) Prev

func (head *ListHead) Prev(opts ...list_head.TravOpt) (next *ListHead)

func (*ListHead) Ptr

func (head *ListHead) Ptr() unsafe.Pointer

func (*ListHead) ReplaceNext added in v0.2.5

func (head *ListHead) ReplaceNext(nextHead *ListHead, nextTail *ListHead, next *ListHead) (err error)

ReplaceNext ... replace next element to new multiple list

type ListHeadError

type ListHeadError struct {
	Type uint16
	Info string
	// contains filtered or unexported fields
}

func NewError

func NewError(t uint16, err error, opts ...OptNewError) *ListHeadError

type ListHeadWithError

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

func ListWithError

func ListWithError(head *ListHead, err error) ListHeadWithError

func (ListHeadWithError) Error

func (le ListHeadWithError) Error() string

func (ListHeadWithError) List

func (le ListHeadWithError) List() *ListHead

type OptNewError

type OptNewError func(e *ListHeadError)

func ErrorInfo

func ErrorInfo(s string) OptNewError

type SampleEntry added in v0.2.3

type SampleEntry struct {
	Name string
	Age  int
	ListHead
}
var EmptySampleEntry *SampleEntry = nil

func SampleEntryFromListHead added in v0.2.3

func SampleEntryFromListHead(head *ListHead) *SampleEntry

func (*SampleEntry) FromListHead added in v0.2.3

func (s *SampleEntry) FromListHead(l *ListHead) List

func (*SampleEntry) InsertBefore added in v0.2.3

func (s *SampleEntry) InsertBefore(n *SampleEntry) (err error)

func (*SampleEntry) Next added in v0.2.3

func (s *SampleEntry) Next() *SampleEntry

func (*SampleEntry) Offset added in v0.2.3

func (s *SampleEntry) Offset() uintptr

func (*SampleEntry) Prev added in v0.2.3

func (s *SampleEntry) Prev() *SampleEntry

func (*SampleEntry) PtrListHead added in v0.2.3

func (s *SampleEntry) PtrListHead() *ListHead

Jump to

Keyboard shortcuts

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