我想创建一个具有任意索引的 IEnumerable 继承稀疏数组
本文关键字:索引 任意 IEnumerable 继承 数组 创建 一个 | 更新日期: 2023-09-27 17:56:56
我正在尝试创建:
- 具有任意索引和
- 有稀疏数组继承 IEnumerable。
到目前为止,我有索引...IE可数的部分我不知道该怎么办。
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;
}
}
如何让稀疏数组对象在 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;
}
}
最简单的方法是:
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
因为数组的默认比较器是参考比较。由于a
和b
是int[]
的两个实例,它们没有相同的引用。
更改此行为,您必须在字典中指定要如何比较object[]
。