Working with NSort and RSS.NET

In this part of our continuing series on working with code libraries, you will learn about NSort and RSS.NET. This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
August 28, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

4.8 Sorting Algorithms in C Sharp with NSort

The NSort Library is a collection of sort methods utilizing customizable swap and compare methods. You can select an appropriate sort algorithm based on the data requirements, and, for complex data types, you can implement a custom comparer. You can also customize the swap method to accommodate any special requirements when swapping two data elements and easily exchange one sort algorithm for another using the ISort interface.

NSort Library at a Glance

Tool

NSort Library

Version covered

1.0

Home page

http://www.codeproject.com/csharp/cssorters.asp

Power Tools page

http://www.windevpowertools.com/tools/148

Summary

A collection of sorting algorithms in an extensible framework

License type

Unknown

Online resources

Code Project article at http://www.codeproject.com/csharp/cssorters.asp,forum

Supported Frameworks

.NET 1.1, 2.0

Getting Started

NSort’s sorting algorithms were written in C# with version 1.1 of the .NET Framework. They can also be used with C# 2.0 and version 2.0 of the .NET Framework, but generics are not supported. A separate version supporting generics is available from http://www.projectdistributor.net/Projects/Project.aspx?projectId=108.

To use the sorting algorithms, add a reference to the NSort assembly to your project’s assembly references. Alternatively, you can add the NSort.csproj file to your solution.

Using NSort

The NSort Library provides 15 different sort algorithms:

  1. Bidirectional Bubble Sort
  2. Bubble Sort
  3. Combo Sort 11
  4. Double Storage Merge Sort
  5. Fast Quick Sorter
  6. Heap Sort
  7. In Place Merge Sort
  8. Insertion Sort
  9. Odd/Even Transport Sort
  10. Quick Sort
  11. Quick Sort with Bubble Sort
  12. Selection Sort
  13. Shaker Sort
  14. Shear Sort
  15. Shell Sort

These sorters vary in best-and worst-case sort times and in memory usage. Unfortunately, determining the optimal sorter isn’t always easy. It’s not just a matter of choosing one with the “best”worst-case sort time and the least amount of memory utilization. Performance depends on the size of your list and on how much of the data is already sorted. One sorter may perform very well under one set of conditions and poorly under a different set. For this reason, Quick Sort with Bubble Sort often makes a good default sorter. This sorter reverts to a Bubble Sort for small sets of data, which is more efficient than a Quick Sort. For large sets, it uses the Quick Sort algorithm first, then applies the Bubble Sort on the smaller fragments.

Using the sorters is straightforward. Decide which sorter you wish to use and instantiate it. Here’s an example that requests a Bubble Sort:

  private ISorter sorter = new BubbleSorter();

Next, create the list of items to be sorted. TheSort()method takes anIListrepresenting an enumerable collection. For example, to sort a set of random numbers:

  Random rnd = new Random();
  int[] list = new int[1000];

  for(int i = 0;i<list.Length;++i)
  {

    list[i] = rnd.Next();
  }

  ArrayList mylist=new ArrayList(list);
  sorter.Sort(mylist);

The default constructor for each of the sorters instantiates theComparableComparer andDefaultSwapclasses. These handle all value types and any class or struct implementingIComparable.

You can also customize the comparer and swapper by implementingIComparerandISwapclasses. All the sorters have an additional constructor:

  public xxx(IComparer comparer, ISwap swapper)

This constructor can be used to specify your own compare and swap algorithms. This is a very useful feature of these classes, allowing you to sort not just value types, but also classes and structs that do not implementIComparable. Conversely, you can implementIComparableon your own classes to support custom comparisons.

Many of the sorters in NSort are ported from sorting demonstration code in Java, available at http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html.

For more information on sorting, refer to John Robbins’s MSDN article “Make Your Apps Fly with the New Enterprise Performance Tool,”available at http:// msdn.microsoft.com/msdnmag/issues/04/12/EnterprisePerformance/default.aspx. You can also find an overview of sorting on Wikipedia at http://en.wikipedia.org/wiki/ Sort_algorithms.

Getting Support

NSort is supported via the tool’s Code Project article at http://www.codeproject.com/ csharp/cssorters.asp and at http://www.marcclifton.com.

Problems or questions can be posted in the comments section for the Code Project article or in the forum at Marc Clifton’s web site.

 

NSort Library in a Nutshell

NSort is a useful collection of sorting algorithms that enables you to select a sorter suitable
for your requirements. The algorithms are very flexible, as the core comparer supports
classes implementing IComparable and can be overridden for classes that don’t support IComparable. You can also override the default swap algorithm to handle custom
swapping of objects and values.

—Marc Clifton, cocreator of NSort

4.9 Creating RSS Feeds with RSS.NET

RSS stands for Real Simple Syndication (or Rich Site Summary, depending on which version you are using). It is a simple XML format that is used to publish content for consumption by various applications. RSS has taken the technology world by storm, rapidly going from being a fringe technology to being supported by almost every major Internet site, including Yahoo!, Google, and even NYTimes.com.

RSS is a relatively simple XML format, so implementing and exposing RSS feeds from your application is fairly straightforward. RSS.NET is an open source library that makes it even easier to publish RSS feeds by giving you a simple object model so you don’t have to actually work with XML. (Who wants to work with XML, anyway?) RSS.NET also handles the small differences between the various versions of RSS, including 0.90, 0.91, 0.92, and 2.0.1.

RSS.NET at a Glance

Tool

RSS.NET

Version covered

.86

Home page

http://www.rssdotnet.com

Power Tools page

http://www.windevpowertools.com/tools/150

Summary

A simple library written in C# to make publishing and consuming RSS feeds

 

even easier

License type

MIT License

Online resources

Bug and feature request trackers

Supported Frameworks

.NET 1.1 natively; convert and recompile for .NET 2.0

Related tools in this book

RSS Toolkit

Getting Started

RSS.NET is still a work in progress, so binaries have not yet been released. This means you will need to download and compile the source yourself. The best way to do so is to select the Nightly Build link from the tool’s home page, which points to a tar archive of the most recent source. You can also connect directly to the CVS repository to get the latest code if that link disappears.

The existing code is for .NET 1.1, but you can recompile it in .NET 2.0 without so much as a compiler warning by simply converting the project to Visual Studio 2005.

Using RSS.NET

Once you have successfully built the project in your Framework of choice, you just need to add a reference to the compiled assembly to your application. You can then start writing code to publish and consume RSS feeds.

Creating an RSS feed

RSS.NET provides an easy-to-work-with object model for creating RSS feeds. To get started, first create a new ASP.NET project in Visual Studio 2005. Then add a new HTTP handler, using the Add New Item -> Generic Handler menu command.

The handler’sProcessRequest()method is where you’ll be doing all your work. The first step is to create a newRssFeed object. This is where you actually write your RSS out to the response:

  RssFeed feed = new RssFeed();

Next, create a newRssChannelobject and tell it what you want to publish. TheRssChannel acts as a heading for the feed and contains the actual news items in itsItemscollection (you’ll populate it shortly):

  RssChannel rssChannel = new RssChannel();
  rssChannel.Description = "Sample RSS Feed for WDPT";
 
rssChannel.Link = new Uri(http://www.windevpowertools.com);
  rssChannel.Title = "Windows Developer Power Tools Feed";

The consuming application uses the channel description, link, and title to let readers know a little bit about your feed and what it contains.

Now you can create anRssItem, which is the actual meat of the RSS feed:

  RssItem rssItem = new RssItem();
  rssItem.Title = "First Item Title";
  rssItem.Description = "Hello World from RSS";
  rssItem.Link = new Uri(http://www.windevpowertools.com);
  rssItem.PubDate = new DateTime(2006, 4, 23);

The title identifies the item to your consumer’s application. The description is the actual body of the item, which could be a news item, blog post, etc. You also need to set the link and publication date for the item.

Next, create a newRssGuid(globally unique identifier) object, which will be used to uniquely identify the item. The main use of this GUID is to provide consuming
applications with a way to keep track of what posts they have already seen. For instance, an aggregator will want to keep track of what posts the user has read and not show the posts as new each time the feed is updated. Simply create theRssGuidobject, set a couple of properties, and then assign it to theGuidproperty of theRssItem:

  RssGuid rssGuid = new RssGuid();
  rssGuid.PermaLink = true;
  rssGuid.Name = http://www.windevpowertools.com/sampleItem;
  rssItem.Guid = rssGuid;

One important property to note is thePermaLink property. SettingPermaLinktotruetells the consumer that the GUID you are using is an actual URL to the item, as opposed to just a globally unique identifier.

Once you’ve done all that, add the item to the channel and set itsLastBuildDateproperty:

  rssChannel.Items.Add(rssItem);
  rssChannel.LastBuildDate = rssChannel.Items.LatestPubDate();

Some consumers check this property before parsing your entire feed to see whether any new updates have been added, so it’s important to avoid setting this to the current date and time.

You can now add the channel to theRssFeedobject that you created earlier:

  rssFeed.Channels.Add(rssChannel);

Then, set the response type using the HTTP context’sContentTypeproperty, write the RSS out to the response, and close the response:

  rssFeed.Channels.Add(rssChannel);
  context.Response.ContentType = "text/xml";
 
rssFeed.Write(context.Response.OutputStream);
  context.Response.End();

You can now point your browser to your test page, and the following RSS will be served up:

  <?xml version="1.0" encoding="iso-8859-1" ?>
  <rss version="2.0">
   
<channel>
      <title>Windows Developer Power Tools Feed</title>
      <description>Sample RSS Feed for WDPT</description> 
      <link>http:// www.windevpowertools.com/</link>
      <lastBuildDate>Sun, 23 Apr 2006 00:00:00 GMT</lastBuildDate> 
      <docs>http:// backend.userland.com/rss</docs>
      <generator>RSS.NET: http://www.rssdotnet.com/</generator>
      <item>
       
<title>First Item</title>
        <description>Hello World from RSS</description>
        <link>http:// www.windevpowertools.com/</link>
        <guid isPermaLink="true">http:// www.windevpowertools.com/sampleItem</guid>
        <pubDate>Sun, 23 Apr 2006 00:00:00 GMT</pubDate>
     
</item>
    </channel>
  </rss>

RSS.NET makes it very simple to publish RSS feeds without writing XML. It also lets you avoid having to deal with escaping text or with minor differences between RSS versions.

Reading RSS feeds with RSS.NET

Reading RSS feeds with RSS.NET is even easier than writing them. The object model is exactly the same, just reversed. To read an RSS feed, simply call the static Read() method on the RssFeed object:

  RssFeed rssFeed = RssFeed.Read(http://www.dotavery.com/blog/rss.aspx);

TheRssFeed object is now populated with the channels and items from the supplied RSS feed. You can display the items in a grid for users, save them to a database, or even modify an item and rewrite the entire feed back out to the response.

Getting Support

Support is limited to filing bug reports at the tool’s SourceForge site (http:// sourceforge.net/projects/rss-net/). However, with the source available, you can fix any bugs you happen to come across.

RSS.NET in a Nutshell

RSS.NET is a great little library that makes working with RSS a little bit easier and cleaner. You don’t need it to use RSS, but it helps and encourages good object-oriented programming. The project has been sitting in beta status for some time, but even in its
current state, it is still very useful.

Please check back tomorrow for the continuation of this article.

blog comments powered by Disqus
BRAINDUMP ARTICLES

- Microsoft Windows 8 Committed to Cloud Compu...
- Independent Developers Favor Windows Phone 7
- Dell Introduces VMware-based Cloud
- Microsoft and Skype Agree to Acquisition Deal
- Transfer Contacts in Microsoft Outlook
- Zune`s Next Steps
- Safari Books Online Review
- Does Microsoft Get Touch Screens Now?
- Microsoft`s Record Quarterly Earnings Not En...
- Basic Operations and Registers in Assembly
- Assembly Coding within Visual C/C++ IDE
- New Microsoft Office Coming with a Twist
- Microsoft`s FUSE Labs Unveils Spindex Social...
- HP Slate with Windows 7: Dead or Alive?
- Windows Phone 7 Mobile OS to Rival Android a...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials