using System; using System.Buffers; using System.Runtime.InteropServices; namespace LLama.Native { /// /// Contains an array of LLamaTokenData, potentially sorted. /// public struct LLamaTokenDataArray { /// /// The LLamaTokenData /// public readonly Memory data; /// /// Indicates if `data` is sorted /// public readonly bool sorted; /// /// Create a new LLamaTokenDataArray /// /// /// public LLamaTokenDataArray(Memory tokens, bool isSorted = false) { data = tokens; sorted = isSorted; } } /// /// Contains a pointer to an array of LLamaTokenData which is pinned in memory. /// [StructLayout(LayoutKind.Sequential)] public struct LLamaTokenDataArrayNative { /// /// A pointer to an array of LlamaTokenData /// /// Memory must be pinned in place for all the time this LLamaTokenDataArrayNative is in use public IntPtr data; /// /// Number of LLamaTokenData in the array /// public ulong size; /// /// Indicates if the items in the array are sorted /// public bool sorted { get => Convert.ToBoolean(_sorted); set => _sorted = Convert.ToSByte(value); } private sbyte _sorted; /// /// Create a new LLamaTokenDataArrayNative around the data in the LLamaTokenDataArray /// /// Data source /// Created native array /// A memory handle, pinning the data in place until disposed public static MemoryHandle Create(LLamaTokenDataArray array, out LLamaTokenDataArrayNative native) { var handle = array.data.Pin(); unsafe { native = new LLamaTokenDataArrayNative { data = new IntPtr(handle.Pointer), size = (ulong)array.data.Length, sorted = array.sorted }; } return handle; } } }