|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe A Sets come in handy when an With sets, adding elements, removing elements, and checking for the existence of an element is fast and simple. You can mix and match the elements in different sets using the supported mathematical set operators: You will see some interesting side effects with different Using the codeThe
You will probably find the
If you are interested in creating your own The example below demonstrates creating new sets and manipulating them using set operators. The currently supported set operators are described briefly in the following table: Set Operators
The example uses using System;
using Iesi.Collections;
namespace RiverDemo
{
class Rivers
{
[STAThread]
static void Main(string[] args)
{
//Use Arrays (which are ICollection objects) to quickly initialize.
Set arizona
= new SortedSet(new string[] {"Colorado River"});
Set california
= new SortedSet(new string[] {"Colorado River", "Sacramento River"});
Set colorado
= new SortedSet(new string[] {"Arkansas River",
"Colorado River", "Green River", "Rio Grande"});
Set kansas
= new SortedSet(new string[] {"Arkansas River", "Missouri River"});
Set nevada
= new SortedSet(new string[] {"Colorado River"});
Set newMexico
= new SortedSet(new string[] {"Rio Grande"});
Set utah
= new SortedSet(new string[] {"Colorado River", "Green River",
"San Juan River"});
//Rivers by region.
Set southWest = colorado | newMexico | arizona | utah;
Set midWest = kansas;
Set west = california | nevada;
//All rivers (at least for the demo).
Set all = southWest | midWest | west;
Print("All rivers:", all);
Print("Rivers in the southwest:", southWest);
Print("Rivers in the west:", west);
Print("Rivers in the midwest:", midWest);
Console.WriteLine();
//Use the '-' operator to subtract the rivers in Colorado from
//the set of all rivers.
Print("Of all rivers, these don't pass through Colorado:",all - colorado);
//Use the '&' operator to find rivers that are in Colorado AND in Utah.
//A river must be present in both states, not just one.
Print("Rivers common to both Colorado and Utah:", colorado & utah);
//use the '^' operator to find rivers that are in Colorado OR Utah,
//but not in both.
Print("Rivers in Colorado and Utah that are not shared by both states:",
colorado ^ utah);
//Use the '&' operator to discover which rivers are present in Arizona,
// California,Colorado, Nevada, and Utah. The river must be present in
// all states to be counted.
Print("Rivers common to Arizona, California, Colorado, Nevada, and Utah:",
arizona & california & colorado & nevada & utah);
//Just to prove to you that operators always return like types, let's do a
//complex Set operation and look at the type of the result:
Console.WriteLine("The type of this complex operation is: " +
((southWest ^ colorado & california) | kansas).GetType().FullName);
}
private static void Print(string title, Set elements)
{
Console.WriteLine(title);
foreach(object o in elements)
{
Console.WriteLine("\t" + o);
Console.WriteLine();
}
}
}
Although there are other kinds of sets available in the library, the example uses What this means is that since we are using Here is the output from running the example: All rivers:
Arkansas River
Colorado River
Green River
Missouri River
Rio Grande
Sacramento River
San Juan River
Rivers in the southwest:
Arkansas River
Colorado River
Green River
Rio Grande
San Juan River
Rivers in the west:
Colorado River
Sacramento River
Rivers in the midwest:
Arkansas River
Missouri River
Of all rivers, these don't pass through Colorado:
Missouri River
Sacramento River
San Juan River
Rivers common to both Colorado and Utah:
Colorado River
Green River
Rivers in Colorado and Utah that are not shared by both states:
Arkansas River
Rio Grande
San Juan River
Rivers common to Arizona, California, Colorado, Nevada, and Utah:
Colorado River
The type of this complex operation is:
Iesi.Collections.SortedSet
Press any key to continue
There is an additional example and a lot more technical information included in the documentation. The source code is pretty easy to follow as well. All the hard work of searching and sorting is performed by classes that are already present in the .NET Framework, so none of the code is particularly difficult or tricky. If you have a question that is not covered by the documentation, it should not take more than 5 or 10 minutes to discover the answer by reading the code. Enjoy!
| |||||||||||||||||||||||||||||||||||||||