2018-10-06 03:27:34 +02:00
|
|
|
package arrays
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-14 19:06:27 +02:00
|
|
|
|
2018-10-06 03:27:34 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-06 03:27:34 +02:00
|
|
|
)
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// RemoveValues returns a new array with removed all occurrences of values in a given array.
|
|
|
|
// @param array (Array) - Source array.
|
|
|
|
// @param values (Array) - Target values.
|
|
|
|
// @returns (Array) - A new array with removed all occurrences of values in a given array.
|
2018-10-06 03:27:34 +02:00
|
|
|
func RemoveValues(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[0], types.Array)
|
2018-10-06 03:27:34 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[1], types.Array)
|
2018-10-06 03:27:34 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := args[0].(*values.Array)
|
|
|
|
vals := args[1].(*values.Array)
|
|
|
|
|
|
|
|
result := values.NewArray(int(arr.Length()))
|
|
|
|
lookupTable := make(map[uint64]bool)
|
|
|
|
|
|
|
|
vals.ForEach(func(value core.Value, idx int) bool {
|
|
|
|
lookupTable[value.Hash()] = true
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
arr.ForEach(func(value core.Value, idx int) bool {
|
|
|
|
h := value.Hash()
|
|
|
|
|
|
|
|
_, exists := lookupTable[h]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
result.Push(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|