Skip to content

Commit

Permalink
1,remove struct Index field 'iterator' (#110)
Browse files Browse the repository at this point in the history
2,update SearchFrom function to use new ArchetypeIterator instead of use Index field 'iterator',because in parallel env,this could cause bug:  index out of range

Co-authored-by: walker <shengxuqiang@joylink.club>
  • Loading branch information
soul-walker and walker committed Oct 9, 2023
1 parent 0634ecf commit d9268f9
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions internal/storage/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ func (it *ArchetypeIterator) Next() ArchetypeIndex {

// Index is a structure that indexes archetypes by their component types.
type Index struct {
layouts [][]component.IComponentType
iterator *ArchetypeIterator
layouts [][]component.IComponentType
}

// NewIndex creates a new search index.
func NewIndex() *Index {
return &Index{
layouts: [][]component.IComponentType{},
iterator: &ArchetypeIterator{
current: 0,
},
}
}

Expand All @@ -43,14 +39,16 @@ func (idx *Index) Push(layout *Layout) {

// SearchFrom searches for archetypes that match the given filter from the given index.
func (idx *Index) SearchFrom(f filter.LayoutFilter, start int) *ArchetypeIterator {
idx.iterator.current = 0
idx.iterator.values = []ArchetypeIndex{}
iterator := &ArchetypeIterator{
current: 0,
}
iterator.values = []ArchetypeIndex{}
for i := start; i < len(idx.layouts); i++ {
if f.MatchesLayout(idx.layouts[i]) {
idx.iterator.values = append(idx.iterator.values, ArchetypeIndex(i))
iterator.values = append(iterator.values, ArchetypeIndex(i))
}
}
return idx.iterator
return iterator
}

// Search searches for archetypes that match the given filter.
Expand Down

0 comments on commit d9268f9

Please sign in to comment.