gob/collection/chain.go

55 lines
805 B
Go

package collection
import (
. "gitea.zaclys.com/bvaudour/gob/option"
)
type chain[T any] struct {
value T
next *chain[T]
}
func prevChain[T any](value T, next *chain[T]) *chain[T] {
return &chain[T]{
value: value,
next: next,
}
}
func nextChain[T any](value T, prev Option[*chain[T]]) *chain[T] {
c := &chain[T]{
value: value,
}
if p, ok := prev.Get(); ok {
p.next = c
}
return c
}
type dualChain[T any] struct {
value T
prev *dualChain[T]
next *dualChain[T]
}
func newDual[T any](e T) *dualChain[T] {
return &dualChain[T]{
value: e,
}
}
func link[T any](o1, o2 Option[*dualChain[T]]) {
if c1, ok := o1.Get(); ok {
if c2, ok := o2.Get(); ok {
c1.next = c2
c2.prev = c1
} else {
c1.next = nil
}
} else if c2, ok := o2.Get(); ok {
c2.prev = nil
}
}