// ArrayOperand converts x to an array. If the cast fails, a descriptive
// error is returned.
188975
func ArrayOperand(x ast.Value, pos int) (ast.Array, error) {
	a, ok := x.(ast.Array)
	if !ok {
		return nil, NewOperandTypeErr(pos, x, "array")
	}
	return a, nil
}
1284133
func arraySet(u C.uintptr_t, i C.int, v C.xpc_object_t) {
	a := *(*Array)(unsafe.Pointer(uintptr(u)))
	a[i] = xpcToGo(v)
}
1047851
func NumberOperand(x ast.Value, pos int) (ast.Number, error) {
	n, ok := x.(ast.Number)
	if !ok {
		return ast.Number(""), NewOperandTypeErr(pos, x, "number")
	}
	return n, nil
}
510943
func intersectArrays(orig, tgt []string) bool {
	for _, i := range orig {
		for _, x := range tgt {
			if i == x {
				return true
			}
		}
	}
	return false
}
732251
func (fa *Float64Array) Float64ArrayOr(or []float64) []float64 {
	switch {
	case fa == nil:
		return or
	case !fa.Valid:
		return or
	default:
		return fa.Float64Array
	}
}
1057160
func And(inner ...Condition) Condition {
	if len(inner) == 1 {
		return inner[0]
	}

	return Condition{
		Type:  ConditionAnd,
		Inner: inner,
	}
}
433971
func (v *VARIANT) ToArray() *SafeArrayConversion {
	if v.VT != VT_SAFEARRAY {
		if v.VT&VT_ARRAY == 0 {
			return nil
		}
	}
	var safeArray *SafeArray = (*SafeArray)(unsafe.Pointer(uintptr(v.Val)))
	return &SafeArrayConversion{safeArray}
}
255264
func (ua *Uint32Array) Uint32ArrayOr(or []uint32) []uint32 {
	switch {
	case ua == nil:
		return or
	case !ua.Valid:
		return or
	default:
		return ua.Uint32Array
	}
}
766815
func ArrayOfFunction(env *Zlisp, name string,
	args []Sexp) (Sexp, error) {

	if len(args) != 2 {
		return SexpNull, fmt.Errorf("insufficient arguments to ([size] regtype) array constructor. use: " +
			"([size...] a-regtype)\n")
	}
	sz := 0
	Q("args = %#v in ArrayOfFunction", args)
	switch ar := args[1].(type) {
	case *SexpArray:
		if len(ar.Val) == 0 {
			return SexpNull, fmt.Errorf("at least one size must be specified in array constructor; e.g. ([size ...] regtype)")
		}
		asInt, isInt := ar.Val[0].(*SexpInt)
		if !isInt {
			return SexpNull, fmt.Errorf("size must be an int (not %T) in array constructor; e.g. ([size ...] regtype)", ar.Val[0])
		}
		sz = int(asInt.Val)
		// TODO: implement multiple dimensional arrays (matrixes etc).
	default:
		return SexpNull, fmt.Errorf("at least one size must be specified in array constructor; e.g. ([size ...] regtype)")
	}

	var rt *RegisteredType
	switch arg := args[0].(type) {
	case *RegisteredType:
		rt = arg
	case *SexpHash:
		rt = arg.GoStructFactory
	default:
		return SexpNull, fmt.Errorf("argument tx in (%s x) was not regtype, "+
			"instead type %T displaying as '%v' ",
			name, arg, arg.SexpString(nil))
	}

	//Q("arrayOf arg = '%s' with type %T", args[0].SexpString(nil), args[0])

	derivedType := reflect.ArrayOf(sz, rt.TypeCache)
	arrayRt := NewRegisteredType(func(env *Zlisp, h *SexpHash) (interface{}, error) {
		return reflect.New(derivedType), nil
	})
	arrayRt.DisplayAs = fmt.Sprintf("(%s %s)", name, rt.DisplayAs)
	arrayName := "arrayOf" + rt.RegisteredName
	GoStructRegistry.RegisterUserdef(arrayRt, false, arrayName)
	return arrayRt, nil
}
908752
func arrayFirst(input interface{}) interface{} {
	if input == nil {
		return nil
	}

	arr := reflect.ValueOf(input)

	if arr.Len() == 0 {
		return nil
	}

	return arr.Index(0).Interface()
}
34718
func GetOperand(p plannercore.LogicalPlan) Operand {
	switch p.(type) {
	case *plannercore.LogicalJoin:
		return OperandJoin
	case *plannercore.LogicalAggregation:
		return OperandAggregation
	case *plannercore.LogicalProjection:
		return OperandProjection
	case *plannercore.LogicalSelection:
		return OperandSelection
	case *plannercore.LogicalApply:
		return OperandApply
	case *plannercore.LogicalMaxOneRow:
		return OperandMaxOneRow
	case *plannercore.LogicalTableDual:
		return OperandTableDual
	case *plannercore.DataSource:
		return OperandDataSource
	case *plannercore.LogicalUnionScan:
		return OperandUnionScan
	case *plannercore.LogicalUnionAll:
		return OperandUnionAll
	case *plannercore.LogicalSort:
		return OperandSort
	case *plannercore.LogicalTopN:
		return OperandTopN
	case *plannercore.LogicalLock:
		return OperandLock
	case *plannercore.LogicalLimit:
		return OperandLimit
	default:
		return OperandUnsupported
	}
}
616338
func (a *API) OperationList() []*Operation {
	list := make([]*Operation, len(a.Operations))
	for i, n := range a.OperationNames() {
		list[i] = a.Operations[n]
	}
	return list
}
1158909
func subArrayUsage(arrayType reflect.Type, fieldName string) string {
	// Convert plural field names to singular.  Only works for English.
	singularFieldName := fieldName
	if strings.HasSuffix(fieldName, "ies") {
		singularFieldName = strings.TrimSuffix(fieldName, "ies")
		singularFieldName = singularFieldName + "y"
	} else if strings.HasSuffix(fieldName, "es") {
		singularFieldName = strings.TrimSuffix(fieldName, "es")
	} else if strings.HasSuffix(fieldName, "s") {
		singularFieldName = strings.TrimSuffix(fieldName, "s")
	}

	elemType := arrayType.Elem()
	switch elemType.Kind() {
	case reflect.String:
		return fmt.Sprintf("[%q,...]", singularFieldName)

	case reflect.Struct:
		return fmt.Sprintf("[%s,...]", subStructUsage(elemType))
	}

	// Fall back to simply showing the field name in array syntax.
	return fmt.Sprintf(`[%s,...]`, singularFieldName)
}
1059782
func arrayToUpper(parts []string) []string {
	for i, part := range parts {
		parts[i] = strings.ToUpper(part[0:1]) + part[1:]
	}
	return parts
}
828963
func IntArray(name string, values []int) FieldOpt {
	inType := inTypeNull
	var writeItem func(*eventData, int)
	switch unsafe.Sizeof(values[0]) {
	case 4:
		inType = inTypeInt32
		writeItem = func(ed *eventData, item int) { ed.writeInt32(int32(item)) }
	case 8:
		inType = inTypeInt64
		writeItem = func(ed *eventData, item int) { ed.writeInt64(int64(item)) }
	default:
		panic("Unsupported int size")
	}

	return func(em *eventMetadata, ed *eventData) {
		em.writeArray(name, inType, outTypeDefault, 0)
		ed.writeUint16(uint16(len(values)))
		for _, v := range values {
			writeItem(ed, v)
		}
	}
}
373564
func unionArrayRun(a, b *Container) *Container {
	statsHit("union/ArrayRun")
	if b.n == maxContainerVal+1 {
		return b.Clone()
	}
	output := NewContainerRun(nil)
	aa, rb := a.array(), b.runs()
	na, nb := len(aa), len(rb)
	var vb interval16
	var va uint16
	for i, j := 0, 0; i < na || j < nb; {
		if i < na {
			va = aa[i]
		}
		if j < nb {
			vb = rb[j]
		}
		if i < na && (j >= nb || va < vb.start) {
			output.n += output.runAppendInterval(interval16{start: va, last: va})
			i++
		} else {
			output.n += output.runAppendInterval(vb)
			j++
		}
	}
	if output.n < ArrayMaxSize {
		output.runToArray()
	} else if len(output.runs()) > runMaxSize {
		output.runToBitmap()
	}
	return output
}
69818
func NewArrayIterator(s Slice) (*ArrayIterator, error) {
	if !s.IsArray() {
		return nil, InvalidTypeError{"Expected Array slice"}
	}
	size, err := s.Length()
	if err != nil {
		return nil, WithStack(err)
	}
	i := &ArrayIterator{
		s:        s,
		position: 0,
		size:     size,
	}
	if size > 0 {
		i.current, err = s.At(0)
		if err != nil {
			return nil, WithStack(err)
		}
	}
	return i, nil
}