Files
obitools4/pkg/obitax/iterator.go

104 lines
2.6 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obitax
2024-11-16 05:59:41 +01:00
// ITaxon represents an iterator for traversing Taxon instances.
// It provides methods to retrieve the next Taxon and check if the iteration is finished.
2024-11-14 19:10:23 +01:00
type ITaxon struct {
2024-11-16 05:59:41 +01:00
source chan *Taxon // Channel to receive Taxon instances
current *Taxon // Current Taxon instance
finished bool // Indicates if the iteration is finished
p_finished *bool // Pointer to the finished status
2022-01-13 23:27:39 +01:00
}
2024-11-16 05:59:41 +01:00
// NewITaxon creates a new ITaxon iterator instance and initializes its fields.
2024-11-14 19:10:23 +01:00
func NewITaxon() *ITaxon {
i := ITaxon{
source: make(chan *Taxon),
current: nil,
finished: false,
2024-11-16 05:59:41 +01:00
p_finished: nil,
}
2022-01-13 23:27:39 +01:00
i.p_finished = &i.finished
return &i
}
2024-11-16 05:59:41 +01:00
// Iterator creates a new ITaxon iterator for the TaxonSet.
// It starts a goroutine to send Taxon instances from the set to the iterator's source channel.
2024-11-14 19:10:23 +01:00
func (set *TaxonSet) Iterator() *ITaxon {
i := NewITaxon()
2022-01-13 23:27:39 +01:00
go func() {
2024-11-08 09:48:16 +01:00
for _, t := range set.set {
2024-11-14 19:10:23 +01:00
i.source <- &Taxon{
Taxonomy: set.taxonomy,
Node: t,
}
2022-01-13 23:27:39 +01:00
}
close(i.source)
}()
return i
}
2024-11-16 05:59:41 +01:00
// Iterator creates a new ITaxon iterator for the TaxonSlice.
// It starts a goroutine to send Taxon instances from the slice to the iterator's source channel.
2024-11-14 19:10:23 +01:00
func (set *TaxonSlice) Iterator() *ITaxon {
i := NewITaxon()
2022-01-13 23:27:39 +01:00
go func() {
2024-11-08 09:48:16 +01:00
for _, t := range set.slice {
2024-11-14 19:10:23 +01:00
i.source <- &Taxon{
Taxonomy: set.taxonomy,
Node: t,
}
2022-01-13 23:27:39 +01:00
}
close(i.source)
}()
return i
}
2024-11-16 05:59:41 +01:00
// Iterator creates a new ITaxon iterator for the Taxonomy's nodes.
func (taxonomy *Taxonomy) Iterator() *ITaxon {
return taxonomy.nodes.Iterator()
2022-01-13 23:27:39 +01:00
}
2024-11-16 05:59:41 +01:00
// Next advances the iterator to the next Taxon instance.
// It returns true if there is a next Taxon, and false if the iteration is finished.
2024-11-14 19:10:23 +01:00
func (iterator *ITaxon) Next() bool {
2022-01-13 23:27:39 +01:00
if *(iterator.p_finished) {
return false
}
next, ok := (<-iterator.source)
if ok {
iterator.current = next
return true
}
iterator.current = nil
*iterator.p_finished = true
return false
}
2024-11-16 05:59:41 +01:00
// Get returns the current Taxon instance pointed to by the iterator.
// You must call 'Next' before calling 'Get' to retrieve the next instance.
2024-11-14 19:10:23 +01:00
func (iterator *ITaxon) Get() *Taxon {
2022-01-13 23:27:39 +01:00
return iterator.current
}
2024-11-16 05:59:41 +01:00
// Finished returns true if no more data is available from the iterator.
2024-11-14 19:10:23 +01:00
func (iterator *ITaxon) Finished() bool {
2022-01-13 23:27:39 +01:00
return *iterator.p_finished
}
2024-11-16 05:59:41 +01:00
// Split creates a new ITaxon iterator that shares the same source channel
// and finished status as the original iterator.
2024-11-14 19:10:23 +01:00
func (iterator *ITaxon) Split() *ITaxon {
return &ITaxon{
source: iterator.source,
current: nil,
finished: false,
p_finished: iterator.p_finished,
2022-01-13 23:27:39 +01:00
}
}