2018-10-06 03:27:34 +02:00
|
|
|
package arrays
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
import (
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
2018-10-06 03:27:34 +02:00
|
|
|
|
|
|
|
func NewLib() map[string]core.Function {
|
|
|
|
return map[string]core.Function{
|
|
|
|
"APPEND": Append,
|
|
|
|
"FIRST": First,
|
|
|
|
"FLATTEN": Flatten,
|
|
|
|
"INTERSECTION": Intersection,
|
|
|
|
"LAST": Last,
|
|
|
|
"MINUS": Minus,
|
|
|
|
"NTH": Nth,
|
|
|
|
"OUTERSECTION": Outersection,
|
|
|
|
"POP": Pop,
|
|
|
|
"POSITION": Position,
|
|
|
|
"PUSH": Push,
|
|
|
|
"REMOVE_NTH": RemoveNth,
|
|
|
|
"REMOVE_VALUE": RemoveValue,
|
|
|
|
"REMOVE_VALUES": RemoveValues,
|
|
|
|
"SHIFT": Shift,
|
|
|
|
"SLICE": Slice,
|
|
|
|
"SORTED": Sorted,
|
|
|
|
"SORTED_UNIQUE": SortedUnique,
|
|
|
|
"UNION": Union,
|
|
|
|
"UNION_DISTINCT": UnionDistinct,
|
|
|
|
"UNIQUE": Unique,
|
|
|
|
"UNSHIFT": Unshift,
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
func ToUniqueArray(arr *values.Array) *values.Array {
|
|
|
|
hashTable := make(map[uint64]bool)
|
|
|
|
result := values.NewArray(int(arr.Length()))
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
arr.ForEach(func(item core.Value, _ int) bool {
|
|
|
|
h := item.Hash()
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
_, exists := hashTable[h]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
hashTable[h] = true
|
|
|
|
result.Push(item)
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
return true
|
|
|
|
})
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
return result
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|