common/ptr: add Advance() method (for allowing relative jumps in forge code)
This commit is contained in:
parent
e76da8533d
commit
419bd2617b
4 changed files with 27 additions and 0 deletions
|
@ -13,6 +13,7 @@ type Ptr[V any] interface {
|
||||||
Set(V) Ptr[V]
|
Set(V) Ptr[V]
|
||||||
Size() int
|
Size() int
|
||||||
Started() bool
|
Started() bool
|
||||||
|
Advance(int) Ptr[V]
|
||||||
Seek(int) Ptr[V]
|
Seek(int) Ptr[V]
|
||||||
Reset() Ptr[V]
|
Reset() Ptr[V]
|
||||||
Clone() Ptr[V]
|
Clone() Ptr[V]
|
||||||
|
|
|
@ -67,6 +67,17 @@ func (p *acc[V]) Started() bool {
|
||||||
return p.started
|
return p.started
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *acc[V]) Advance(i int) Ptr[V] {
|
||||||
|
if i == 0 {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
if p.started && i > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
p.started = i >= 0
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
func (p *acc[V]) Seek(i int) Ptr[V] {
|
func (p *acc[V]) Seek(i int) Ptr[V] {
|
||||||
p.started = true
|
p.started = true
|
||||||
return p
|
return p
|
||||||
|
|
|
@ -68,6 +68,19 @@ func (p *sptr[V]) Started() bool {
|
||||||
return p.offset >= 0
|
return p.offset >= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *sptr[V]) Advance(i int) Ptr[V] {
|
||||||
|
p.offset += i
|
||||||
|
l := len(*p.seq)
|
||||||
|
if p.offset >= l {
|
||||||
|
p.offset = l - 1
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if p.offset < -1 {
|
||||||
|
p.offset = -1
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
func (p *sptr[V]) Seek(i int) Ptr[V] {
|
func (p *sptr[V]) Seek(i int) Ptr[V] {
|
||||||
p.offset = i
|
p.offset = i
|
||||||
return p
|
return p
|
||||||
|
|
|
@ -119,6 +119,8 @@ func PtrSliceTest(t *testing.T) {
|
||||||
t.AssertEqual(sp1.Value(), 99)
|
t.AssertEqual(sp1.Value(), 99)
|
||||||
sp1.Next()
|
sp1.Next()
|
||||||
t.AssertEqual(sp1.Value(), 12)
|
t.AssertEqual(sp1.Value(), 12)
|
||||||
|
sp1.Advance(-1)
|
||||||
|
t.AssertEqual(sp1.Value(), 99)
|
||||||
sp1.Reset()
|
sp1.Reset()
|
||||||
t.AssertEqual(sp1.Next().Value(), 11)
|
t.AssertEqual(sp1.Next().Value(), 11)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue