Esempio n. 1
0
        private void Compare <T>(ISpanComparer comparer, int size) where T : struct, IComparable <T>
        {
            Random rng = new Random();

            for (int i = 0; i < 100; ++i)
            {
                byte[] leftData  = new byte[32];
                byte[] rightData = new byte[32];
                rng.NextBytes(leftData);
                rng.NextBytes(rightData);

                T a = MemoryMarshal.Cast <byte, T>(leftData.AsSpan())[0];
                T b = MemoryMarshal.Cast <byte, T>(rightData.AsSpan())[0];

                var leftSpan  = leftData.AsSpan().Slice(0, size);
                var rightSpan = rightData.AsSpan().Slice(0, size);

                Assert.AreEqual(
                    a.CompareTo(b),
                    comparer.Compare(leftSpan, rightSpan));

                Assert.AreEqual(
                    b.CompareTo(a),
                    comparer.Compare(rightSpan, leftSpan));

                Assert.AreEqual(0, comparer.Compare(leftSpan, leftSpan));
                Assert.AreEqual(0, comparer.Compare(rightSpan, rightSpan));
            }
        }
        public static void SortVector(
            Span <byte> buffer,
            int vectorUOffset,
            int vtableIndex,
            int?keyInlineSize,
            ISpanComparer comparer)
        {
            checked
            {
                int vectorStartOffset = vectorUOffset + (int)ScalarSpanReader.ReadUInt(buffer.Slice(vectorUOffset));
                int vectorLength      = (int)ScalarSpanReader.ReadUInt(buffer.Slice(vectorStartOffset));
                int index0Position    = vectorStartOffset + sizeof(int);

                // Traverse the vector and figure out the offsets of all the keys.
                // Store that in some local data, hopefully on the stack. 512 is somewhat arbitrary, but we want to avoid stack overflows.
                Span <(int offset, int length, int tableOffset)> keyOffsets =
                    vectorLength < 512
                    ? stackalloc (int, int, int)[vectorLength]