-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Work in progress ordered query iteration * Fastest yet * Forgot this * Update query.go * New approach * New iteration system * Improve documentation * Fix a crash * Support ordered queries
- Loading branch information
Showing
7 changed files
with
216 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters