github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/union_distinct.go (about)

     1  package arrays
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
     9  )
    10  
    11  // UNION_DISTINCT returns the union of all passed arrays with unique values.
    12  // @param {Any[], repeated} arrays - List of arrays to combine.
    13  // @return {Any[]} - All unique array elements combined in a single array, in any order.
    14  func UnionDistinct(_ context.Context, args ...core.Value) (core.Value, error) {
    15  	err := core.ValidateArgs(args, 2, core.MaxArgs)
    16  
    17  	if err != nil {
    18  		return values.None, err
    19  	}
    20  
    21  	err = core.ValidateType(args[0], types.Array)
    22  
    23  	if err != nil {
    24  		return values.None, err
    25  	}
    26  
    27  	firstArrLen := args[0].(*values.Array).Length()
    28  	result := values.NewArray(len(args) * int(firstArrLen))
    29  	hashes := make(map[uint64]bool)
    30  
    31  	for _, arg := range args {
    32  		err := core.ValidateType(arg, types.Array)
    33  
    34  		if err != nil {
    35  			return values.None, err
    36  		}
    37  
    38  		arr := arg.(*values.Array)
    39  
    40  		arr.ForEach(func(value core.Value, _ int) bool {
    41  			h := value.Hash()
    42  
    43  			_, exists := hashes[h]
    44  
    45  			if !exists {
    46  				hashes[h] = true
    47  				result.Push(value)
    48  			}
    49  
    50  			return true
    51  		})
    52  	}
    53  
    54  	return result, nil
    55  }