-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcollection_iter.go
68 lines (60 loc) · 1015 Bytes
/
collection_iter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ecs
import "unsafe"
type Iterator[T any] interface {
Begin() *T
Val() *T
Next() *T
End() bool
}
type Iter[T any] struct {
head unsafe.Pointer
data []T
len int
offset int
pend uintptr
cur *T
curTemp T
eleSize uintptr
readOnly bool
}
func EmptyIter[T any]() Iterator[T] {
return &Iter[T]{}
}
func (i *Iter[T]) End() bool {
if i.offset >= i.len || i.len == 0 {
return true
}
return false
}
func (i *Iter[T]) Begin() *T {
if i.len != 0 {
i.offset = 0
if i.readOnly {
i.curTemp = i.data[0]
i.cur = &i.curTemp
} else {
i.cur = &(i.data[0])
}
}
return i.cur
}
func (i *Iter[T]) Val() *T {
return i.cur
}
func (i *Iter[T]) Next() *T {
i.offset++
i.pend += i.eleSize
if !i.End() {
if i.readOnly {
//i.curTemp = i.data[i.offset]
i.curTemp = *(*T)(unsafe.Add(i.head, i.pend))
i.cur = &i.curTemp
} else {
//i.cur = &(i.data[i.offset])
i.cur = (*T)(unsafe.Add(i.head, i.pend))
}
} else {
i.cur = nil
}
return i.cur
}