// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package array

import (
	
	
	
	

	
	
	
	
	
	
)

type Float16Builder struct {
	builder

	data    *memory.Buffer
	rawData []float16.Num
}

func ( memory.Allocator) *Float16Builder {
	 := &Float16Builder{builder: builder{mem: }}
	.refCount.Add(1)
	return 
}

func ( *Float16Builder) () arrow.DataType { return arrow.FixedWidthTypes.Float16 }

// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
func ( *Float16Builder) () {
	debug.Assert(.refCount.Load() > 0, "too many releases")

	if .refCount.Add(-1) == 0 {
		if .nullBitmap != nil {
			.nullBitmap.Release()
			.nullBitmap = nil
		}
		if .data != nil {
			.data.Release()
			.data = nil
			.rawData = nil
		}
	}
}

func ( *Float16Builder) ( float16.Num) {
	.Reserve(1)
	.UnsafeAppend()
}

func ( *Float16Builder) ( float16.Num) {
	bitutil.SetBit(.nullBitmap.Bytes(), .length)
	.rawData[.length] = 
	.length++
}

func ( *Float16Builder) () {
	.Reserve(1)
	.UnsafeAppendBoolToBitmap(false)
}

func ( *Float16Builder) ( int) {
	for  := 0;  < ; ++ {
		.AppendNull()
	}
}

func ( *Float16Builder) () {
	.Reserve(1)
	.UnsafeAppend(float16.Num{})
}

func ( *Float16Builder) ( int) {
	for  := 0;  < ; ++ {
		.AppendEmptyValue()
	}
}

func ( *Float16Builder) ( bool) {
	if  {
		bitutil.SetBit(.nullBitmap.Bytes(), .length)
	} else {
		.nulls++
	}
	.length++
}

// AppendValues will append the values in the v slice. The valid slice determines which values
// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
// all values in v are appended and considered valid.
func ( *Float16Builder) ( []float16.Num,  []bool) {
	if len() != len() && len() != 0 {
		panic("len(v) != len(valid) && len(valid) != 0")
	}

	if len() == 0 {
		return
	}

	.Reserve(len())
	if len() > 0 {
		arrow.Float16Traits.Copy(.rawData[.length:], )
	}
	.unsafeAppendBoolsToBitmap(, len())
}

func ( *Float16Builder) ( int) {
	.builder.init()

	.data = memory.NewResizableBuffer(.mem)
	 := arrow.Uint16Traits.BytesRequired()
	.data.Resize()
	.rawData = arrow.Float16Traits.CastFromBytes(.data.Bytes())
}

// Reserve ensures there is enough space for appending n elements
// by checking the capacity and calling Resize if necessary.
func ( *Float16Builder) ( int) {
	.reserve(, .Resize)
}

// Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(),
// additional memory will be allocated. If n is smaller, the allocated memory may reduced.
func ( *Float16Builder) ( int) {
	 := 
	if  < minBuilderCapacity {
		 = minBuilderCapacity
	}

	if .capacity == 0 {
		.init()
	} else {
		.resize(, .init)
		.data.Resize(arrow.Float16Traits.BytesRequired())
		.rawData = arrow.Float16Traits.CastFromBytes(.data.Bytes())
	}
}

// NewArray creates a Float16 array from the memory buffers used by the builder and resets the Float16Builder
// so it can be used to build a new array.
func ( *Float16Builder) () arrow.Array {
	return .NewFloat16Array()
}

// NewFloat16Array creates a Float16 array from the memory buffers used by the builder and resets the Float16Builder
// so it can be used to build a new array.
func ( *Float16Builder) () ( *Float16) {
	 := .newData()
	 = NewFloat16Data()
	.Release()
	return
}

func ( *Float16Builder) () ( *Data) {
	 := arrow.Float16Traits.BytesRequired(.length)
	if  > 0 &&  < .data.Len() {
		// trim buffers
		.data.Resize()
	}
	 = NewData(arrow.FixedWidthTypes.Float16, .length, []*memory.Buffer{.nullBitmap, .data}, nil, .nulls, 0)
	.reset()

	if .data != nil {
		.data.Release()
		.data = nil
		.rawData = nil
	}

	return
}

func ( *Float16Builder) ( string) error {
	if  == NullValueStr {
		.AppendNull()
		return nil
	}
	,  := strconv.ParseFloat(, 32)
	if  != nil {
		.AppendNull()
		return 
	}
	.Append(float16.New(float32()))
	return nil
}

func ( *Float16Builder) ( *json.Decoder) error {
	,  := .Token()
	if  != nil {
		return 
	}

	switch v := .(type) {
	case float64:
		.Append(float16.New(float32()))
	case string:
		,  := strconv.ParseFloat(, 32)
		if  != nil {
			return 
		}
		// this will currently silently truncate if it is too large
		.Append(float16.New(float32()))
	case json.Number:
		,  := .Float64()
		if  != nil {
			return 
		}
		.Append(float16.New(float32()))
	case nil:
		.AppendNull()
	default:
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(),
			Type:   reflect.TypeOf(float16.Num{}),
			Offset: .InputOffset(),
		}
	}
	return nil
}

func ( *Float16Builder) ( *json.Decoder) error {
	for .More() {
		if  := .UnmarshalOne();  != nil {
			return 
		}
	}
	return nil
}

// UnmarshalJSON will add values to this builder from unmarshalling the
// array of values. Currently values that are larger than a float16 will
// be silently truncated.
func ( *Float16Builder) ( []byte) error {
	 := json.NewDecoder(bytes.NewReader())
	,  := .Token()
	if  != nil {
		return 
	}

	if ,  := .(json.Delim); ! ||  != '[' {
		return fmt.Errorf("float16 builder must unpack from json array, found %s", )
	}

	return .Unmarshal()
}