mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-16 11:37:36 +02:00
ec2d6a659b
* #9 Added 'APPEND' function * #9 Added 'FIRST' function * #9 Added 'FLATTEN' function * #9 Added 'INTERSECTION' function * #9 Added 'LAST' function * #9 Added 'MINUS' function * #9 Added 'NTH' function * #9 Added 'OUTERSECTION' function * #9 Added 'POP' function * #9 Added 'POSITION' function * #9 Added 'PUSH' function * Fixed nil pointer exception in value parser * #9 Added 'REMOVE_NTH' function * #9 Added 'REMOVE_VALUE' function * #9 Added 'REMOVE_VALUES' function * #9 Added 'REVERSE' function * #9 Added 'SHIFT' function * #9 Added 'SLICE' function * Removed meme * #9 Added 'SORTED' function * #9 Added SORTED_UNIQUE function * #9 Added 'UNION' function * #9 Added 'UNION_DISTINCT' function * #9 Added 'UNIQUE' function * #9 Added 'UNSHIFT' function * #9 Made more strict optional arg validation * #9 Fixed linting errors
81 lines
1.3 KiB
Go
81 lines
1.3 KiB
Go
package collections
|
|
|
|
import (
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
)
|
|
|
|
type (
|
|
UniqueIterator struct {
|
|
src Iterator
|
|
hashes map[uint64]bool
|
|
value core.Value
|
|
key core.Value
|
|
err error
|
|
}
|
|
)
|
|
|
|
func NewUniqueIterator(src Iterator) (*UniqueIterator, error) {
|
|
if src == nil {
|
|
return nil, core.Error(core.ErrMissedArgument, "source")
|
|
}
|
|
|
|
return &UniqueIterator{
|
|
src: src,
|
|
hashes: make(map[uint64]bool),
|
|
}, nil
|
|
}
|
|
|
|
func (iterator *UniqueIterator) HasNext() bool {
|
|
if !iterator.src.HasNext() {
|
|
return false
|
|
}
|
|
|
|
iterator.doNext()
|
|
|
|
if iterator.err != nil {
|
|
return false
|
|
}
|
|
|
|
if !core.IsNil(iterator.value) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (iterator *UniqueIterator) Next() (core.Value, core.Value, error) {
|
|
return iterator.value, iterator.key, iterator.err
|
|
}
|
|
|
|
func (iterator *UniqueIterator) doNext() {
|
|
// reset state
|
|
iterator.err = nil
|
|
iterator.value = nil
|
|
iterator.key = nil
|
|
|
|
// iterate over source until we find a non-unique item
|
|
for iterator.src.HasNext() {
|
|
val, key, err := iterator.src.Next()
|
|
|
|
if err != nil {
|
|
iterator.err = err
|
|
|
|
return
|
|
}
|
|
|
|
h := val.Hash()
|
|
|
|
_, exists := iterator.hashes[h]
|
|
|
|
if exists {
|
|
continue
|
|
}
|
|
|
|
iterator.hashes[h] = true
|
|
iterator.key = key
|
|
iterator.value = val
|
|
|
|
return
|
|
}
|
|
}
|