2

私は作成しようとしています:

  • 任意のインデックスとスパース配列
  • スパース配列にIEnumerableを継承させます。

これまでのところ、インデックス付けがあります...IEnumerableの部分は何をすべきかわかりません。

class SparseArray : IEnumerable
{
    private Dictionary<object[], object> Items = new Dictionary<object[], object>();
    private int Counter = -1;
    public object this[params object[] Key]
    {
        set { Items.Add(Key, value); }
        get { object B; bool Tau = Items.TryGetValue(Key, out B); return B; }
    }
    public IEnumerator GetEnumerable()
    {
        ///
    }
}
class Program
{
    static void Main(string[] args)
    {
        uint W = 5;
        Potato P = new Potato(W, "Spudz");
        Potato Q = new Potato(W+2, "Super Spudz");
        SparseArray Pile = new SparseArray();
        Pile[1,2,3,4,5,Q] = "String";
        Pile["String",P] = "Strung";
        Pile[1.2030,"pi"] = true;
        foreach ( object Val in Pile)
        {
            Console.WriteLine(Val);
        }
        Console.ReadKey();
    }
}
struct Potato
{
    private string _Name;
    private uint _Weight;
    public string Name
    {   set { _Name = value; }
        get { return _Name; }
    }
    public uint Weight
    {
        set { _Weight = value; }
        get { return _Weight; }
    }
    public Potato(uint weight, string name)
    {
        _Weight = weight;
        _Name = name;
    }
}

Sparse Arrayオブジェクトをforeachループ内でディクショナリをループさせるにはどうすればよいですか?

更新を編集

皆さんからの入力のおかげで、コードを少し修正しました。

public class ObjectArrayComparer : IEqualityComparer<object[]>
{
    // Determines whether x and y are equal or not
    public bool Equals(object[] x, object[] y)
    {
        return object.ReferenceEquals(x, y) // Returns true if they are the same array instance
            || (x != null && y != null && x.SequenceEqual(y));  // Returns true if x and y are not null and have the same elements (order dependent)
    }

    // Function that allow to fastly determine if an element is in a set of not.        
    // This function must have the following property:
    //   x.Equals(y) implies GetHashCode(x) == GetHashCode(y)
    public int GetHashCode(object[] obj)
    {
        if (obj == null)
            return 0;

        // Unchecked sum of the Hash Codes of all elements in obj
        return unchecked(obj.Select(o => o != null ? o.GetHashCode() : 0).Aggregate(0, (a, b) => a + b));
    }
}
class SparseArray : IEnumerable
{
    private Dictionary<object[], object> Items = new Dictionary<object[], object>(new ObjectArrayComparer());
    private int Counter = -1;
    public object this[params object[] Key]
    {
        set { Items.Add(Key, value); }
        get { 
                object B;
                if (Items.TryGetValue(Key, out B) == true)
                {
                    return Items[Key];
                }
                else
                {
                    //In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).
                    //So If the key does not exist, return null.   
                    return null;
                }

            }
        //get { object B; bool Tau = Items.TryGetValue(Key, out B); return B; }
    }
    public IEnumerator GetEnumerator()
    {
        return Items.Values.GetEnumerator();
    }
}
class Program
{
    static void Main(string[] args)
    {
        uint W = 5;
        Potato P = new Potato(W, "Spudz");
        Potato Q = new Potato(W+2, "Super Spudz");
        SparseArray Pile = new SparseArray();
        Pile[1,2,3,4,5,Q] = "String";
        Pile["String",P] = "Strung";
        Pile[1.2030,"pi"] = true;
        foreach ( object Val in Pile)
        {
            Console.WriteLine(Val);
        }
        Console.WriteLine(Pile[1.2030, "pi"]);
        Console.WriteLine(Pile["String", P]);
        Console.WriteLine(Pile["String", P]);
        Console.WriteLine(Pile["String", Q]);
        Console.ReadKey();
    }
}
struct Potato
{
    private string _Name;
    private uint _Weight;
    public string Name
    {   set { _Name = value; }
        get { return _Name; }
    }
    public uint Weight
    {
        set { _Weight = value; }
        get { return _Weight; }
    }
    public Potato(uint weight, string name)
    {
        _Weight = weight;
        _Name = name;
    }
}
4

1 に答える 1

2

最も簡単な方法は次のとおりです。

public IEnumerator GetEnumerator()
{
    return Items.GetEnumerator();
}

または、値のみが必要な場合:

public IEnumerator GetEnumerator()
{
    return Items.Values.GetEnumerator();
}

辞書にカスタム比較ツールを使用することを忘れないでください。

 private Dictionary<object[], object> Items = new Dictionary<object[], object>(new ObjectArrayComparer());

と:

public class ObjectArrayComparer : IEqualityComparer<object[]>
{
    // Determines whether x and y are equal or not
    public bool Equals(object[] x, object[] y)
    {
        return object.ReferenceEquals(x, y) // Returns true if they are the same array instance
            || (x != null && y != null && x.SequenceEqual(y));  // Returns true if x and y are not null and have the same elements (order dependent)
    }

    // Function that allow to fastly determine if an element is in a set of not.        
    // This function must have the following property:
    //   x.Equals(y) implies GetHashCode(x) == GetHashCode(y)
    public int GetHashCode(object[] obj)
    {
        if (obj == null)
            return 0;

        // Unchecked sum of the Hash Codes of all elements in obj
        return unchecked(obj.Select(o => o != null ? o.GetHashCode() : 0).Aggregate(0, (a, b) => a + b));
    }
}

なんで?

これを試して:

var a = new int[] { 0 };
var b = new int[] { 0 };
Console.WriteLine(a == b);  // Returns false

配列のデフォルトの比較子は参照比較であるためです。abはの2つのインスタンスであるためint[]、同じ参照はありません。

この動作を変更するには、辞書で、を比較する方法を指定する必要がありますobject[]

于 2013-01-31T16:55:39.603 に答える