C# - 稳定的,有界的数组

时间:2013-10-27 04:33:38

标签: c# typedef pascal

我正在将用Pascal编写的游戏(以16位编译)移植到C#(因此它将在比XP更新的机器上运行)。根据我收集的内容,在Pascal中,可以通过以下语法在单元/程序的类型部分中键入define:

type
    BaseArrayPtr = ^BaseArray;
    BaseArray = array [1 .. 5, 1 .. 5] of Integer;

    SubArray = array [0 .. 3] of BaseArray;

我还认为,不幸的是,在C#中输入define是不可能的。但是,我正在尝试解决方法。到目前为止,这就是我所拥有的:

BoundedArray.cs:

using System;
using System.Collections;

namespace test
{
    abstract class BoundedArray<T>
    {
        public BoundedArray()
        {
            m_data = null;
        }

        public T this[params int[] index]
        {
            get
            {
                if (index.Length != m_data.Rank)
                    throw new IndexOutOfRangeException();

                return (T) m_data.GetValue(index);
            }
            set
            {
                if (index.Length != m_data.Rank)
                    throw new IndexOutOfRangeException();

                m_data.SetValue(value, index);
            }
        }

        protected void SetAttributes(int[] lowerBounds, int[] lengths)
        {
            if (lengths.Length != lowerBounds.Length)
                throw new ArgumentException();

            m_lowerBounds = lowerBounds;
            m_lengths = lengths;

            m_data = Array.CreateInstance(typeof(T), m_lengths, m_lowerBounds);
            m_data.Initialize(); // Should (but doesn't) initialize every element in m_data
        }

        Array m_data;
        int[] m_lengths;
        int[] m_lowerBounds;
    }
}

test.cs中:

using System;

namespace test
{
    class Program
    {
        public static int[] ints(params int[] values)
        {
            return values;
        }

        class BaseArray : BoundedArray<int>
        {
            public BaseArray()
            {
                SetAttributes(ints(2, 2), ints(1, 2));
            }
        }

        class SubArray : BoundedArray<BaseArray>
        {
            public SubArray()
            {
                SetAttributes(ints(4), ints(2));
            }
        }

        static void Main(string[] args)
        {
            SubArray subArray = new SubArray();

            Console.Read();
        }
    }
}

我已检查baseArray,默认值m_data为零,因为它们是int s。但是,在subArray中,m_data的默认值为null - BaseArray中数组内的subArray实例由于某种原因尚未初始化。如何运行默认构造函数?

编辑:目前真正的问题是为什么m_data.Initialize();方法中SetAttributes没有初始化m_data中的所有元素? The documentation on MSDN似乎表明它应该......

编辑: 所以我认为问题在于System.Array.Initialize仅适用于价值类型。由于类是C#中的引用类型,System.Array.Initialize不执行任何操作。所以我必须找到一种方法来初始化一个可变维度,长度和下限的引用类型数组。

3 个答案:

答案 0 :(得分:0)

你有一个单维数组SubArray,它包含BaseArray个对象,这些对象是二维的整数数组。代替Pascal type,您可以定义一个自定义C#类,它将覆盖indexer operator以提供完全相同的行为。

<强> EDITED 所以,在Pascal你有这个:

type
    BaseArrayPtr = ^BaseArray;
    BaseArray = array [1 .. 5, 1 .. 5] of Integer;

    SubArray = array [0 .. 3] of BaseArray;

也许我误解了这个问题,但在C#中,下面的内容并不完全相同?

public class BaseArray
{
    int[,] m_array = new int[5, 5];

    static void CheckBounds(int x, int y)
    {
        if (x < 1 || x > 5 || y < 1 || y > 5)
            throw new IndexOutOfRangeException();
    }

    public int this[int x, int y]
    {
        get 
        {
            CheckBounds(x, y);
            return m_array[x-1, y-1]; 
        }
        set 
        {
            CheckBounds(x, y);
            m_array[x-1, y-1] = value; 
        }
    }
}

public class SubArray
{
    BaseArray[] m_array = new BaseArray[4];

    public BaseArray this[int x]
    {
        get { return m_array[x]; }
        set { m_array[x] = value; }
    }
}

答案 1 :(得分:0)

我做了一些更改,当您想要创建SubArray的实例时,您应该将BaseArray作为要初始化的数据源传递。

据我了解,您要将值从BaseArray设置为SubArray

这是我的工作:

<强> BoundedArray.cs

 abstract class BoundedArray<T>
{
    public BoundedArray()
    {
        m_data = null;
    }

    public int[] Lengths;
    public int[] LowerBounds;

    public void CreateInstance()
    {
        if (Lengths.Length != LowerBounds.Length)
            throw new Exception("Incorrect number of lengths or lower bounds.");

        m_data = Array.CreateInstance(typeof(T), Lengths, LowerBounds);
    }
    public void CreateInstance(Array source)
    {
        if (Lengths.Length != LowerBounds.Length)
            throw new Exception("Incorrect number of lengths or lower bounds.");

        m_data = Array.CreateInstance(typeof(T), Lengths, LowerBounds);

        /************************************************************************/
        /*    Now you should find the value of BaseArray and set it to m_data                                                                     */
        /************************************************************************/


    }
    public T this[params int[] index]
    {
        get
        {
            if (index.Length != m_data.Rank)
                throw new IndexOutOfRangeException();

            return (T)m_data.GetValue(index);
        }
        set
        {
            if (index.Length != m_data.Rank)
                throw new IndexOutOfRangeException();

            m_data.SetValue(value, index);
        }
    }
    public Array GetData()
    {
        return m_data;
    }
    Array m_data;
}

<强> test.cs中

class Program
{
    public static int[] ints(params int[] values)
    {
        return values;
    }

    class BaseArray : BoundedArray<int>
    {
        public BaseArray()
        {
            Lengths = ints(1, 2);
            LowerBounds = ints(2, 2);

            CreateInstance();
        }
    }

    class SubArray : BoundedArray<BaseArray>
    {
        public SubArray(BaseArray arr)
        {
            Lengths = ints(2);
            LowerBounds = ints(4);

            CreateInstance(arr.GetData());
        }
    }

    static void Main(string[] args)
    {
        BaseArray baseArray = new BaseArray();
        SubArray subArray = new SubArray(baseArray);

        Console.Read();
    }
}

答案 2 :(得分:0)

我已经回答了一次我自己的问题,但我想出了一个更多更好地实现了我的答案。

以下是此解决方案的内容:

  1. SetAttributes必须在基于BoundedArray
  2. 的类的默认构造函数中运行一次
  3. SetAttributes期间,我收集了当前BoundedArray子类中所有索引的锯齿状二维数组
  4. 我通过调用Activator.CreateInstance并为每个索引分配一个
  5. 来创建模板类型的实例

    其他注意事项:

    • 现在设置属性需要一个int[] s的可变长度数组,而不是两个int[] s。以前,它采取了下限和长度,但我意识到只需要使用int[] s更低的上限,然后使用LINQ查询来检查是否有任何不是't pair
    • 我创建了一个名为IntArray的静态类,SetAttributes test.cs
    • 广泛使用
    • 我试图尽可能多地抛出有用的错误,因为我可能最终会使用这段代码
    • 我觉得Combinations(int[][] list1, int[] list2)可能是找到解决方案最大改进的地方。我愿意接受有关如何改进所有代码的建议

    所以,不用多说,我的完整解决方案:

    <强> BoundedArray.cs

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    namespace test
    {
        static class IntArray
        {
            public static int[] FromValues(params int[] values)
            {
                return values;
            }
    
            public static int[] Sequence(int from, int length)
            {
                if (from < 0 || length < 1)
                    throw new ArgumentException();
    
                return Enumerable.Range(from, length).ToArray();
            }
    
            public static int[][] Combinations(int[] list1, int[] list2)
            {
                return Combinations(list1.Select(i => new int[] { i }).ToArray(), list2);
            }
    
            public static int[][] Combinations(int[][] list1, int[] list2)
            {
                List<List<int>> result = new List<List<int>>();
    
                for (int i = 0; i < list1.Length; i++)
                {
                    for (int j = 0; j < list2.Length; j++)
                        result.Add(((int[]) list1.GetValue(i)).Concat(new int[] { list2[j] }).ToList());
                }
    
                return result.Select(i => i.ToArray()).ToArray();
            }
        }
    
        abstract class BoundedArray<T>
        {
            public BoundedArray()
            {
                m_data = null;
            }
    
            public Array Value
            {
                get { return m_data; }
            }
    
            public T this[params int[] index]
            {
                get
                {
                    if (index.Length != m_data.Rank)
                        throw new IndexOutOfRangeException();
    
                    return (T) m_data.GetValue(index);
                }
                set
                {
                    if (index.Length != m_data.Rank)
                        throw new IndexOutOfRangeException();
    
                    m_data.SetValue(value, index);
                }
            }
    
            protected void SetAttributes(params int[][] values)
            {
                // Make sure all of the values are pairs
                if (values.Where(i => i.Length != 2).ToArray().Length > 0)
                    throw new ArgumentException("Input arrays must be of length 2.");
    
                int[] lowerBounds = values.Select(i => i[0]).ToArray();
                int[] lengths = values.Select(i => i[1] - i[0] + 1).ToArray();
    
                m_data = Array.CreateInstance(typeof(T), lengths, lowerBounds);
    
                int[][] indices = (lowerBounds.Length != 1) ?
                    IntArray.Combinations(IntArray.Sequence(lowerBounds[0], lengths[0]), IntArray.Sequence(lowerBounds[1], lengths[1]))
                    : IntArray.Sequence(lowerBounds[0], lengths[0]).Select(i => new int[] { i }).ToArray();
    
                for (int i = 2; i < lowerBounds.Length; i++)
                    indices = IntArray.Combinations(indices, IntArray.Sequence(lowerBounds[i], lengths[i]));
    
                for (int i = 0; i < indices.Length; i++)
                    m_data.SetValue(Activator.CreateInstance(typeof(T)), indices[i]);
            }
    
            Array m_data;
        }
    }
    

    <强> test.cs中

    using System;
    
    namespace test
    {
        class Program
        {
            // *** Examples of what you can do with BoundedArray ***
    
            // Multi-dimensional, bounded base array
            class BaseArray : BoundedArray<int>
            {
                public BaseArray()
                {
                    SetAttributes(IntArray.FromValues(2, 3), IntArray.FromValues(2, 4));
                }
            }
    
            // One-dimensional, bounded subclass array
            class SubArray : BoundedArray<BaseArray>
            {
                public SubArray()
                {
                    SetAttributes(IntArray.FromValues(4, 6));
                }
            }
    
            static void Main(string[] args)
            {
                // Initializations used for testing purposes
                BaseArray baseArray = new BaseArray();
                SubArray subArray = new SubArray();
    
                // Example of assignment
                baseArray[3, 4] = 3;
                subArray[4][2, 3] = 4;
    
                subArray[4][2] = 3; // Weakness: compiles, but causes IndexOutOfRangeException
    
                Console.Read();
            }
        }
    }
    

    Thougts?