public void EnumerateJaggedTest()
        {
            int[][][] input =
            {
                new int[][] { new[] { 0 }, new[] { 1 }, new[] { 2 } },
                new int[][] { new[] { 1 }, new[] { 2 }, new[] { 3 } }
            };

            int[] expected = { 0, 1, 2, 1, 2, 3 };

            List <int> actual = new List <int>();

            foreach (object obj in Jagged.Enumerate(input, new int[] { 2, 3, 1 }))
            {
                actual.Add((int)obj);
            }

            Assert.IsTrue(expected.IsEqual(actual.ToArray()));

            actual.Clear();
            foreach (int obj in Jagged.Enumerate <int>(input, new int[] { 2, 3, 1 }))
            {
                actual.Add(obj);
            }

            Assert.IsTrue(expected.IsEqual(actual.ToArray()));
        }
Ejemplo n.º 2
0
        private static string GetDtypeFromType(Array array, out Type type, out int bytes)
        {
            type = array.GetInnerMostType();

            bytes = 1;

            if (type == typeof(String))
            {
                foreach (String s in Jagged.Enumerate <String>(array))
                {
                    if (s.Length > bytes)
                    {
                        bytes = s.Length;
                    }
                }
            }
            else if (type == typeof(bool))
            {
                bytes = 1;
            }
            else
            {
#pragma warning disable 618 // SizeOf would be Obsolete
                bytes = Marshal.SizeOf(type);
#pragma warning restore 618 // SizeOf would be Obsolete
            }

            if (type == typeof(bool))
            {
                return("|b1");
            }
            if (type == typeof(Byte))
            {
                return("|i1");
            }
            if (type == typeof(Int16))
            {
                return("<i2");
            }
            if (type == typeof(Int32))
            {
                return("<i4");
            }
            if (type == typeof(Int64))
            {
                return("<i8");
            }
            if (type == typeof(String))
            {
                return("|S" + bytes);
            }

            throw new NotSupportedException();
        }
Ejemplo n.º 3
0
        private static ulong writeStringMatrix(BinaryWriter reader, Array matrix, int bytes, int[] shape)
        {
            var buffer = new byte[bytes];
            var empty  = new byte[bytes];

            empty[0] = byte.MinValue;
            for (int i = 1; i < empty.Length; i++)
            {
                empty[i] = byte.MaxValue;
            }

            ulong writtenBytes = 0;

            unsafe
            {
                fixed(byte *b = buffer)
                {
                    foreach (String s in Jagged.Enumerate <String>(matrix, shape))
                    {
                        if (s != null)
                        {
                            int c = 0;
                            for (int i = 0; i < s.Length; i++)
                            {
                                b[c++] = (byte)s[i];
                            }
                            for (; c < buffer.Length; c++)
                            {
                                b[c] = byte.MinValue;
                            }

                            reader.Write(buffer, 0, bytes);
                        }
                        else
                        {
                            reader.Write(empty, 0, bytes);
                        }

#if NETSTANDARD1_4
                        writtenBytes += (ulong)buffer.Length;
#else
                        writtenBytes += (ulong)buffer.LongLength;
#endif
                    }
                }
            }

            return(writtenBytes);
        }
Ejemplo n.º 4
0
        private static ulong writeValueJagged(BinaryWriter reader, Array matrix, int bytes, int[] shape)
        {
            int last = shape[shape.Length - 1];

            byte[] buffer = new byte[bytes * last];
            int[]  first  = shape.Get(0, -1);

            ulong writtenBytes = 0;

            foreach (Array arr in Jagged.Enumerate <Array>(matrix, first))
            {
                Array.Clear(buffer, arr.Length, buffer.Length - buffer.Length);
                Buffer.BlockCopy(arr, 0, buffer, 0, buffer.Length);
                reader.Write(buffer, 0, buffer.Length);

                writtenBytes += (ulong)buffer.LongLength;
            }

            return(writtenBytes);
        }