Checking equal dictionary

  • Thread starter Thread starter Luigi Z
  • Start date Start date
L

Luigi Z

Hi all,
in C# 2.0 I have 2 objects "Voice" with a property like this:

IDictionary<Column, Value> values;

where Column is another class with 2 properties (name and type).

How can I check that two Voice objects have the same "Column"?

Thanks in advance.
 
Hi,

may this sample helps you:


class Program
{
public class Voice
{
public Dictionary<Column, object> Values { get; private set; }

public Voice()
{
Values = new Dictionary<Column, object>();
}
}

public class Column
{
public string Name { get; set; }
public Type Type { get; set; }

public override int GetHashCode()
{
return this.Name.GetHashCode() + this.Type.GetHashCode();
}

public override bool Equals(object obj)
{
Column other = (Column)obj;

return this.Name.Equals(other.Name) &&
this.Type.Equals(other.Type);
}
}

static void Main(string[] args)
{
Voice voice1 = new Voice();
voice1.Values.Add(new Column { Name = "VOICE_1", Type =
typeof(bool) }, false);
voice1.Values.Add(new Column { Name = "VOICE_2", Type =
typeof(int) }, 1);
voice1.Values.Add(new Column { Name = "VOICE_3", Type =
typeof(string) }, "Test #1");

Voice voice2 = new Voice();
voice2.Values.Add(new Column { Name = "VOICE_3", Type =
typeof(string) }, "Test #1");
voice2.Values.Add(new Column { Name = "VOICE_2", Type =
typeof(int) }, 2);
voice2.Values.Add(new Column { Name = "VOICE_5", Type =
typeof(string) }, "Test #2");

Voice voice3 = new Voice();
voice3.Values.Add(new Column { Name = "VOICE_3", Type =
typeof(string) }, "Test #1");
voice3.Values.Add(new Column { Name = "VOICE_2", Type =
typeof(int) }, 2);
voice3.Values.Add(new Column { Name = "VOICE_5", Type =
typeof(string) }, "Test #2");

// Check if a specifield Column is in voices
Console.WriteLine(Contains(new Column { Name = "VOICE_3", Type =
typeof(string) }, voice1, voice2, voice3));

// Get the column which is in all voices
IEnumerable<Column> sameColumnInEachVoice =
GetSameColumnInEachVoice(voice1, voice2, voice3);
}

public static bool Contains(Column columnToFind, params Voice[]
voices)
{
bool contains = true;

for (int i = 0; i < voices.Length; ++i)
{
contains &= voices.Values.ContainsKey(columnToFind);

if (!contains)
return false;
}

return true;
}

public static IEnumerable<Column> GetSameColumnInEachVoice(params
Voice[] voices)
{
Dictionary<Column, int> dict = new Dictionary<Column, int>();

for (int i = 0; i < voices.Length; ++i)
{
foreach (Column column in voices.Values.Keys)
{
if (!dict.ContainsKey(column))
dict.Add(column, 0);

dict[column] += 1;
}
}

foreach (KeyValuePair<Column, int> kvp in dict)
{
if (kvp.Value == voices.Length)
yield return kvp.Key;
}
}
}
 
Markus Zeller said:
Hi,

may this sample helps you:


class Program
{
public class Voice
{
public Dictionary<Column, object> Values { get; private set; }

public Voice()
{
Values = new Dictionary<Column, object>();
}
}

public class Column
{
public string Name { get; set; }
public Type Type { get; set; }

public override int GetHashCode()
{
return this.Name.GetHashCode() + this.Type.GetHashCode();
}

public override bool Equals(object obj)
{
Column other = (Column)obj;

return this.Name.Equals(other.Name) &&
this.Type.Equals(other.Type);
}
}

static void Main(string[] args)
{
Voice voice1 = new Voice();
voice1.Values.Add(new Column { Name = "VOICE_1", Type =
typeof(bool) }, false);
voice1.Values.Add(new Column { Name = "VOICE_2", Type =
typeof(int) }, 1);
voice1.Values.Add(new Column { Name = "VOICE_3", Type =
typeof(string) }, "Test #1");

Voice voice2 = new Voice();
voice2.Values.Add(new Column { Name = "VOICE_3", Type =
typeof(string) }, "Test #1");
voice2.Values.Add(new Column { Name = "VOICE_2", Type =
typeof(int) }, 2);
voice2.Values.Add(new Column { Name = "VOICE_5", Type =
typeof(string) }, "Test #2");

Voice voice3 = new Voice();
voice3.Values.Add(new Column { Name = "VOICE_3", Type =
typeof(string) }, "Test #1");
voice3.Values.Add(new Column { Name = "VOICE_2", Type =
typeof(int) }, 2);
voice3.Values.Add(new Column { Name = "VOICE_5", Type =
typeof(string) }, "Test #2");

// Check if a specifield Column is in voices
Console.WriteLine(Contains(new Column { Name = "VOICE_3", Type =
typeof(string) }, voice1, voice2, voice3));

// Get the column which is in all voices
IEnumerable<Column> sameColumnInEachVoice =
GetSameColumnInEachVoice(voice1, voice2, voice3);
}

public static bool Contains(Column columnToFind, params Voice[]
voices)
{
bool contains = true;

for (int i = 0; i < voices.Length; ++i)
{
contains &= voices.Values.ContainsKey(columnToFind);

if (!contains)
return false;
}

return true;
}

public static IEnumerable<Column> GetSameColumnInEachVoice(params
Voice[] voices)
{
Dictionary<Column, int> dict = new Dictionary<Column, int>();

for (int i = 0; i < voices.Length; ++i)
{
foreach (Column column in voices.Values.Keys)
{
if (!dict.ContainsKey(column))
dict.Add(column, 0);

dict[column] += 1;
}
}

foreach (KeyValuePair<Column, int> kvp in dict)
{
if (kvp.Value == voices.Length)
yield return kvp.Key;
}
}
}


Thanks Markus, now I'm studying your code.

Luigi
 
Back
Top