Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a data structure that holds sets of data in .NET?

I'm looking for a data structure similar to a dictionary that returns the set of all related items to a key.

For example, I would use it like this:

var data = new FancyDataStructure();

data.Add(new string[] {"Elizabeth", "Liz", "Betty"});
data.Add(new string[] {"Bob", "Robert", "Rob"});

string[] alternateNames1 = data["Betty"];
string[] alternateNames2 = data["Liz"]

In this instance, alternateNames1 would be an array containing "Liz" and "Elizabeth", and alternateNames2 would be an array containing "Elizabeth" and "Betty."

I don't want to reinvent this, but I couldn't find any examples of such a structure.

Update

Thank you to those that have written back with suggestions. Many people have suggested using some version of Dictionary<string, IEnumerable<string>>. Currently I am using this approach, but it doesn't actually fulfill the requirement without being horribly difficult to maintain. Every value in every list needs to be able to function as a key to every other value ever added to it in a set.

Thus, given the following:

data.Add(new string[] {"Elizabeth", "Liz"}
data.Add(new string[] {"Liz", "Betty"}
alternates = data["Betty"];

I would expect alternates to now contain "Elizabeth," and "Liz."

It looks as though I might just have to build such a structure to suit my needs. Keep the ideas coming though!

Brian

like image 925
Brian Vallelunga Avatar asked Feb 09 '10 23:02

Brian Vallelunga


People also ask

What is set data structure in C#?

In C#, struct is the value type data type that represents data structures. It can contain a parameterized constructor, static constructor, constants, fields, methods, properties, indexers, operators, events, and nested types.

Which data structure can hold data?

Arrays is the data structure which cannot store the non-homogeneous data elements.

What is data structure in net?

NET data structures are in System. Collections namespace. There are type libraries such as PowerCollections which offer additional data structures. To get a thorough understanding of data structures, consult resources such as CLRS.

Which data structure is used in set?

A set is a data structure that stores unique elements of the same type in a sorted order. Each value is a key, which means that we access each value using the value itself. With arrays, on the other hand, we access each value by its position in the container (the index). Accordingly, each value in a set must be unique.


1 Answers

Your problem sounds like it is really a graphing problem. Think of the names as nodes and membership in the set as the edges. From this standpoint, you would want a data structure that handles sparse graphs well, such as an adjacency list. This is, of course, similary to what you are already doing with a Dictionary<string, IEnumerable<string>> but thinking about it in this way might lead you to some helpful implementations and algorithms.

like image 151
Dolphin Avatar answered Dec 05 '22 08:12

Dolphin