go-scopes/common/ptr/ptr.go

39 lines
803 B
Go

// Package ptr provides a generic interface for accessing an ordered
// collection of values sequentially, updating it and appending to it
// using a pointer (or cursor) type that keeps track of the current
// position. There are also implementations based on single
// scalar values, object attributes, or accessors.
package ptr
type Ptr[V any] interface {
Append(V) Ptr[V]
Insert(V, int) Ptr[V]
Next() Ptr[V]
Value() V
Set(V) Ptr[V]
Size() int
Started() bool
Advance(int) Ptr[V]
Seek(int) Ptr[V]
Reset() Ptr[V]
Clone() Ptr[V]
New() Ptr[V]
Copy() Ptr[V]
}
// generic functions
func New[V any](p Ptr[V]) Ptr[V] {
np := p.Clone()
np.Reset()
return np
}
func Copy[V any](p, np Ptr[V]) Ptr[V] {
op := p.New()
for op.Next() != nil {
np.Append(op.Value())
}
np.Reset()
return np
}