// Code generated by modernc.org/undup from the per-target vec_*.go files; DO NOT EDIT.

//go:build (darwin && amd64) || (darwin && arm64) || (freebsd && 386) || (freebsd && amd64) || (freebsd && arm) || (freebsd && arm64) || (linux && 386) || (linux && amd64) || (linux && arm) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x) || (netbsd && amd64) || (openbsd && amd64) || (openbsd && arm64)

package vec

import (
	"unsafe"

	"modernc.org/libc"
	libsqlite3 "modernc.org/sqlite/lib"
)

func Xvec0Update_Delete(tls *libc.TLS, pVTab uintptr, idValue uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, rc int32
	var p uintptr
	var _ /* chunkDeleted at bp+24 */ int32
	var _ /* chunk_id at bp+8 */ Ti64
	var _ /* chunk_offset at bp+16 */ Ti64
	var _ /* rowid at bp+0 */ Ti64
	_, _, _ = i, p, rc
	p = pVTab
	if (*Tvec0_vtab)(unsafe.Pointer(p)).FpkIsText != 0 {
		rc = Xvec0_rowid_from_id(tls, p, idValue, bp)
		if rc != m_SQLITE_OK {
			return rc
		}
	} else {
		**(**Ti64)(__ccgo_up(bp)) = libsqlite3.Xsqlite3_value_int64(tls, idValue)
	}
	// 1. Find chunk position for given rowid
	// 2. Ensure that validity bit for position is 1, then set to 0
	// 3. Zero out rowid in chunks.rowid
	// 4. Zero out vector data in all vector column chunks
	// 5. Delete value in _rowids table
	// 1. get chunk_id and chunk_offset from _rowids
	rc = Xvec0_get_chunk_position(tls, p, **(**Ti64)(__ccgo_up(bp)), libc.UintptrFromInt32(0), bp+8, bp+16)
	if rc != m_SQLITE_OK {
		return rc
	}
	// 2. clear validity bit
	rc = Xvec0Update_Delete_ClearValidity(tls, p, **(**Ti64)(__ccgo_up(bp + 8)), libc.Uint64FromInt64(**(**Ti64)(__ccgo_up(bp + 16))))
	if rc != m_SQLITE_OK {
		return rc
	}
	// 3. zero out rowid in chunks.rowids
	rc = Xvec0Update_Delete_ClearRowid(tls, p, **(**Ti64)(__ccgo_up(bp + 8)), libc.Uint64FromInt64(**(**Ti64)(__ccgo_up(bp + 16))))
	if rc != m_SQLITE_OK {
		return rc
	}
	// 4. zero out any data in vector chunks tables
	rc = Xvec0Update_Delete_ClearVectors(tls, p, **(**Ti64)(__ccgo_up(bp + 8)), libc.Uint64FromInt64(**(**Ti64)(__ccgo_up(bp + 16))))
	if rc != m_SQLITE_OK {
		return rc
	}
	// 5. delete from _rowids table
	rc = Xvec0Update_Delete_DeleteRowids(tls, p, **(**Ti64)(__ccgo_up(bp)))
	if rc != m_SQLITE_OK {
		return rc
	}
	// 6. delete any auxiliary rows
	if (*Tvec0_vtab)(unsafe.Pointer(p)).FnumAuxiliaryColumns > 0 {
		rc = Xvec0Update_Delete_DeleteAux(tls, p, **(**Ti64)(__ccgo_up(bp)))
		if rc != m_SQLITE_OK {
			return rc
		}
	}
	// 7. delete metadata
	i = 0
	for {
		if !(i < (*Tvec0_vtab)(unsafe.Pointer(p)).FnumMetadataColumns) {
			break
		}
		rc = Xvec0Update_Delete_ClearMetadata(tls, p, i, **(**Ti64)(__ccgo_up(bp)), **(**Ti64)(__ccgo_up(bp + 8)), libc.Uint64FromInt64(**(**Ti64)(__ccgo_up(bp + 16))))
		if rc != m_SQLITE_OK {
			return rc
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	// 8. reclaim chunk if fully empty
	rc = Xvec0Update_Delete_DeleteChunkIfEmpty(tls, p, **(**Ti64)(__ccgo_up(bp + 8)), bp+24)
	if rc != m_SQLITE_OK {
		return rc
	}
	return m_SQLITE_OK
}

func Xvec0Update_Delete_ClearRowid(tls *libc.TLS, p uintptr, chunk_id Ti64, chunk_offset Tu64) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var brc, rc int32
	var _ /* blobChunksRowids at bp+0 */ uintptr
	var _ /* zero at bp+8 */ Ti64
	_, _ = brc, rc
	**(**uintptr)(__ccgo_up(bp)) = libc.UintptrFromInt32(0)
	**(**Ti64)(__ccgo_up(bp + 8)) = 0
	rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, __ccgo_ts+9690, chunk_id, int32(1), bp)
	if rc != m_SQLITE_OK {
		Xvtab_set_error(tls, p, __ccgo_ts+14034, libc.VaList(bp+24, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, chunk_id))
		return int32(m_SQLITE_ERROR)
	}
	rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+8, int32(8), libc.Int32FromUint64(chunk_offset*uint64(8)))
	if rc != m_SQLITE_OK {
		Xvtab_set_error(tls, p, __ccgo_ts+14076, libc.VaList(bp+24, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, chunk_id, chunk_offset))
	}
	brc = libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc != m_SQLITE_OK {
		return rc
	}
	if brc != m_SQLITE_OK {
		Xvtab_set_error(tls, p, __ccgo_ts+14130, libc.VaList(bp+24, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, chunk_id, chunk_offset))
		return brc
	}
	return m_SQLITE_OK
}

// C documentation
//
//	/**
//	 * @brief Extract a vector from a sqlite3_value. Can be a float32, int8, or bit
//	 * vector.
//	 *
//	 * @param value: the sqlite3_value to read from.
//	 * @param vector: Output pointer to vector data.
//	 * @param dimensions: Output number of dimensions
//	 * @param dimensions: Output vector element type
//	 * @param cleanup
//	 * @param pzErrorMessage
//	 * @return int SQLITE_OK on success, error code otherwise
//	 */
func Xvector_from_value(tls *libc.TLS, value uintptr, vector uintptr, dimensions uintptr, element_type uintptr, __ccgo_fp_cleanup uintptr, pzErrorMessage uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc, rc1, rc2, subtype int32
	_, _, _, _ = rc, rc1, rc2, subtype
	subtype = libc.Int32FromUint32(libsqlite3.Xsqlite3_value_subtype(tls, value))
	if !(subtype != 0) || subtype == int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32) || subtype == int32(m_JSON_SUBTYPE) {
		rc = _fvec_from_value(tls, value, vector, dimensions, __ccgo_fp_cleanup, pzErrorMessage)
		if rc == m_SQLITE_OK {
			**(**_VectorElementType)(__ccgo_up(element_type)) = int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32)
		}
		return rc
	}
	if subtype == int32(_SQLITE_VEC_ELEMENT_TYPE_BIT) {
		rc1 = _bitvec_from_value(tls, value, vector, dimensions, __ccgo_fp_cleanup, pzErrorMessage)
		if rc1 == m_SQLITE_OK {
			**(**_VectorElementType)(__ccgo_up(element_type)) = int32(_SQLITE_VEC_ELEMENT_TYPE_BIT)
		}
		return rc1
	}
	if subtype == int32(_SQLITE_VEC_ELEMENT_TYPE_INT8) {
		rc2 = _int8_vec_from_value(tls, value, vector, dimensions, __ccgo_fp_cleanup, pzErrorMessage)
		if rc2 == m_SQLITE_OK {
			**(**_VectorElementType)(__ccgo_up(element_type)) = int32(_SQLITE_VEC_ELEMENT_TYPE_INT8)
		}
		return rc2
	}
	**(**uintptr)(__ccgo_up(pzErrorMessage)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+419, libc.VaList(bp+8, subtype))
	return int32(m_SQLITE_ERROR)
}

func _vec_eachBestIndex(tls *libc.TLS, pVTab uintptr, pIdxInfo uintptr) (r int32) {
	var hasVector, i int32
	var pCons uintptr
	_, _, _ = hasVector, i, pCons
	_ = pVTab
	hasVector = 0
	i = 0
	for {
		if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) {
			break
		}
		pCons = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12
		// printf("i=%d iColumn=%d, op=%d, usable=%d\n", i, pCons->iColumn,
		// pCons->op, pCons->usable);
		switch (*Tsqlite3_index_constraint)(unsafe.Pointer(pCons)).FiColumn {
		case int32(m_VEC_EACH_COLUMN_VECTOR):
			if libc.Int32FromUint8((*Tsqlite3_index_constraint)(unsafe.Pointer(pCons)).Fop) == int32(m_SQLITE_INDEX_CONSTRAINT_EQ) && (*Tsqlite3_index_constraint)(unsafe.Pointer(pCons)).Fusable != 0 {
				hasVector = int32(1)
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = int32(1)
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).Fomit = uint8(1)
			}
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if !(hasVector != 0) {
		return int32(m_SQLITE_CONSTRAINT)
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromInt32(100000)
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(100000)
	return m_SQLITE_OK
}

func _vec_npy_eachBestIndex(tls *libc.TLS, pVTab uintptr, pIdxInfo uintptr) (r int32) {
	var hasInput, i int32
	var pCons uintptr
	_, _, _ = hasInput, i, pCons
	i = 0
	for {
		if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) {
			break
		}
		pCons = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12
		// printf("i=%d iColumn=%d, op=%d, usable=%d\n", i, pCons->iColumn,
		// pCons->op, pCons->usable);
		switch (*Tsqlite3_index_constraint)(unsafe.Pointer(pCons)).FiColumn {
		case int32(m_VEC_NPY_EACH_COLUMN_INPUT):
			if libc.Int32FromUint8((*Tsqlite3_index_constraint)(unsafe.Pointer(pCons)).Fop) == int32(m_SQLITE_INDEX_CONSTRAINT_EQ) && (*Tsqlite3_index_constraint)(unsafe.Pointer(pCons)).Fusable != 0 {
				hasInput = int32(1)
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = int32(1)
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(i)*8))).Fomit = uint8(1)
			}
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if !(hasInput != 0) {
		(*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FzErrMsg = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+3371, 0)
		return int32(m_SQLITE_ERROR)
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromInt32(100000)
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(100000)
	return m_SQLITE_OK
}

type fpos_t = Tfpos_t

const m_BUFSIZ = 1024

const m_ENOTBLK = 15

const m_ETXTBSY = 26

const m_EWOULDBLOCK = "EAGAIN"

const m_MATH_ERREXCEPT = 2

const m_MATH_ERRNO = 1

const m_RAND_MAX = 0x7fffffff

const m__IOFBF = 0

const m__IOLBF = 1

const m__IONBF = 2

const m___SIZEOF_WCHAR_T__ = 4

const m___SIZEOF_WINT_T__ = 4

const m___WCHAR_WIDTH__ = 32

const m___WINT_WIDTH__ = 32

const m___restrict_arr = "restrict"

const m_static_assert = "_Static_assert"
