-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathordered_iterator.go
43 lines (35 loc) · 1.08 KB
/
ordered_iterator.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
package donburi
import "sort"
// OrderedEntryIterator is an iterator for entries from a list of `[]Entity`.
type OrderedEntryIterator[T IOrderable] struct {
current int
entries []*Entry
}
// OrderedEntryIterator is an iterator for entries based on a list of `[]Entity`.
func NewOrderedEntryIterator[T IOrderable](current int, w World, entities []Entity, orderedBy *ComponentType[T]) OrderedEntryIterator[T] {
entLen := len(entities)
entries := make([]*Entry, entLen)
orders := make([]int, entLen)
for i := 0; i < entLen; i++ {
entry := w.Entry(entities[i])
entries[i] = entry
orders[i] = (*orderedBy.Get(entry)).Order()
}
sort.Slice(entries, func(i, j int) bool {
return orders[i] < orders[j]
})
return OrderedEntryIterator[T]{
entries: entries,
current: current,
}
}
// HasNext returns true if there are more entries to iterate over.
func (it *OrderedEntryIterator[T]) HasNext() bool {
return it.current < len(it.entries)
}
// Next returns the next entry.
func (it *OrderedEntryIterator[T]) Next() *Entry {
nextIndex := it.entries[it.current]
it.current++
return nextIndex
}