简体   繁体   English

C#相当于Java的FloatBuffer / ShortBuffer?

[英]C# equivalent to Java's FloatBuffer/ShortBuffer?

I am wondering, is there an equivalent in C# to the following code? 我想知道,C#中是否有与以下代码相同的内容?

ShortBuffer sb = BufferUtils.createShortBuffer(Cubie.indexData.length);
sb.put(Cubie.indexData).flip();

Gl.BufferData(BufferTarget.ElementArrayBuffer, sb, BufferUsageHint.StaticDraw);
Gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

First of all this might be a little bump, but answering in case someone else comes around here or that OP may still need something like this. 首先,这可能是一个小小的颠簸,但回答以防其他人来到这里或OP可能仍然需要这样的东西。

I wrapped an array into a buffer class and tried to match ShortBuffer . 我将一个数组包装到缓冲类中,并尝试匹配ShortBuffer Of course this is not a complete implementation of it, so there might still be some stuff missing or stuff that'd have to be implemented. 当然,这不是它的完整实现,因此可能仍然存在一些缺失的东西或者必须实现的东西。

I made it generic in case you might want to have different type of buffers and not just short. 如果您想要使用不同类型的缓冲区而不是简短的缓冲区,我会将它设为通用的。

Here is the Buffer<T> class. 这是Buffer<T>类。

    /// <summary>
    /// A generic buffer.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Buffer<T>
    {
        /// <summary>
        /// The internal buffer.
        /// </summary>
        protected T[] _array;

        /// <summary>
        /// Creates a new generic buffer.
        /// </summary>
        /// <param name="array">The array.</param>
        public Buffer(T[] array)
        {
            _array = new T[array.Length];
            Array.Copy(array, 0, _array, 0, _array.Length);
        }

        /// <summary>
        /// Creates a new generic buffer.
        /// </summary>
        /// <param name="capacity">The capacity.</param>
        public Buffer(int capacity)
        {
            _array = new T[capacity];
            // 0 filling for value-types
            for (int i = 0; i < _array.Length; i++)
                _array[i] = default(T);
        }

        /// <summary>
        /// Gets the first value of the buffer.
        /// </summary>
        public T First
        {
            get { return _array[0]; }
        }

        /// <summary>
        /// Gets the last value of the buffer.
        /// </summary>
        public T Last
        {
            get { return _array[_array.Length - 1]; }
        }

        /// <summary>
        /// Gets the length of the buffer.
        /// </summary>
        public int Length
        {
            get { return _array.Length; }
        }

        /// <summary>
        /// Gets values in the form of an array from a specific index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="length">The length.</param>
        /// <returns>Returns the values in the form of an array.</returns>
        public T[] Get(int index, int length)
        {
            var array = new T[length];
            Array.Copy(_array, index, array, 0, length);
            return array;
        }

        /// <summary>
        /// Gets a copy of the internal array.
        /// </summary>
        /// <returns>Returns the copied array.</returns>
        public T[] Get()
        {
            var array = new T[_array.Length];
            Array.Copy(_array, 0, array, 0, array.Length);
            return array;
        }

        /// <summary>
        /// Gets a value by an index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <returns>Returns the value.</returns>
        public T GetValue(int index)
        {
            return _array[index];
        }

        /// <summary>
        /// Puts a value into the beginning of the buffer.
        /// Note: Put does not replace the first value.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Put(T value)
        {
            var array = new T[_array.Length + 1];
            Array.Copy(_array, 0, array, 1, _array.Length);
            array[0] = value;

            _array = array;
        }

        /// <summary>
        /// Puts a value into a specific index of the buffer.
        /// Note: Put does not replace the value at the specific index.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="index">The index.</param>
        public void Put(T value, int index)
        {
            if (index == 0)
            {
                Put(value);
            }
            else if (index == _array.Length - 1)
            {
                Append(value);
            }
            else
            {
                var array = new T[_array.Length + 1];
                array[index] = value;

                Array.Copy(_array, 0, array, 0, index);
                Array.Copy(_array, index, array, index + 1, _array.Length - index);

                _array = array;
            }
        }

        /// <summary>
        /// Inserts a value at a specific index of the buffer.
        /// Note: Insert replaces the current value at the index.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="index">The index.</param>
        public void Insert(T value, int index)
        {
            _array[index] = value;
        }

        /// <summary>
        /// Appends a value to the buffer.
        /// </summary>
        /// <param name="value">The value to append.</param>
        public void Append(T value)
        {
            var array = new T[_array.Length + 1];
            Array.Copy(_array, 0, array, 0, _array.Length);
            array[array.Length - 1] = value;

            _array = array;
        }

        /// <summary>
        /// Puts an array into the beginning of the buffer.
        /// Note: Put does not replace the first values of the buffer.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Put(T[] value)
        {
            var array = new T[_array.Length + value.Length];
            Array.Copy(value, 0, array, 0, value.Length);
            Array.Copy(_array, 0, array, value.Length, _array.Length);

            _array = array;
        }

        /// <summary>
        /// Puts an array into a specific index of the buffer.
        /// Note: Put does not replace the array at the specific index.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="index">The index.</param>
        public void Put(T[] value, int index)
        {
            if (index == 0)
            {
                Put(value);
            }
            else if (index == _array.Length - 1)
            {
                Append(value);
            }
            else
            {
                var array = new T[_array.Length + value.Length];

                Array.Copy(value, 0, array, index, value.Length);
                Array.Copy(_array, 0, array, 0, index);
                Array.Copy(_array, index, array, index + value.Length, _array.Length - index);

                _array = array;
            }
        }

        /// <summary>
        /// Appends an array to the buffer.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Append(T[] value)
        {
            var array = new T[_array.Length + value.Length];
            Array.Copy(_array, 0, array, 0, _array.Length);
            Array.Copy(value, 0, array, _array.Length, value.Length);

            _array = array;
        }

        /// <summary>
        /// Puts a buffer into the beginning of the buffer.
        /// Note: Put does not replace the first values of the buffer.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Put(Buffer<T> value)
        {
            Put(value._array);
        }

        /// <summary>
        /// Puts a buffer into a specific index of the buffer.
        /// Note: Put does not replace the buffer at the specific index.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="index">The index.</param>
        public void Put(Buffer<T> value, int index)
        {
            Put(value._array, index);
        }

        /// <summary>
        /// Appends a buffer to the buffer.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Append(Buffer<T> value)
        {
            Append(value._array);
        }

        /// <summary>
        /// Gets the bytes of the buffer.
        /// </summary>
        /// <returns>Returns a newly created byte array of the buffer.</returns>
        public byte[] GetBytes()
        {
            var buffer = new byte[_array.Length * Marshal.SizeOf(typeof(T))];
            Buffer.BlockCopy(_array, 0, buffer, 0, buffer.Length);
            return buffer;
        }
    }

Here is an example implementation on how to implement ShortBuffer 以下是有关如何实现ShortBuffer的示例实现

    /// <summary>
    /// A 16 bit integer buffer.
    /// </summary>
    public class Int16Buffer : Buffer<short>
    {
        /// <summary>
        /// Creates a new instance of Int16Buffer.
        /// </summary>
        /// <param name="array">The array to build a buffer upon.</param>
        public Int16Buffer(short[] array)
            : base(array) { }

        /// <summary>
        /// Creates a new instance of Int16Buffer.
        /// </summary>
        /// <param name="capacity">The start capacity of the buffer.</param>
        public Int16Buffer(int capacity)
            : base(capacity) { }
    }

Of couse you can also just do 你也可以这样做

// Creates a buffer of 10 short elements ...
var shortBuffer = new Buffer<short>(10);

At last here is some example usage. 最后这里是一些示例用法。

var buffer1 = new Int16Buffer(new short[] { 1, 2, 4, 5 });
buffer1.Put(0);
buffer1.Put(3, 3);
buffer1.Append(6);

var buffer2 = new Int16Buffer(new short[] { 1, 2, 4, 5 });
buffer2.Put(0);
buffer2.Put(3, 3);
buffer2.Append(6);

buffer1.Put(buffer2);

var buffer3 = new Int16Buffer(new short[] { 1, 2, 4, 5 });
buffer3.Put(0);
buffer3.Put(3, 3);
buffer3.Append(6);

buffer1.Put(buffer3, 7);

foreach (var b in buffer1.Get())
{
    Console.WriteLine(b);
}

You can get output here: 你可以在这里得到输出:

ShortBuffer Example ShortBuffer示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM