Development on a Shoestring

Archive for March, 2008

Comparing two arrays (or IEnumerables) in C#

Much to my surprise I found that .NET 3.5 doesn’t seem to have a native method for comparing two arrays or collections of any type.  The LINQ extension methods offer a whole lot of added functionality for IEnumerable<T> collections, but not native comparison.  The Equals method is still the base object.Equals method that does a reference equality (i.e. Are these two the same object?) not a value equality (do they contain the same values?)

So, finding myself needing an equality comparison between two arrays, I’ve written the following extension method:

   1: /// <summary>
   2: /// Checks whether a collection is the same as another collection
   3: /// </summary>
   4: /// <param name="value">The current instance object</param>
   5: /// <param name="compareList">The collection to compare with</param>
   6: /// <param name="comparer">The comparer object to use to compare each item in the collection.  If null uses EqualityComparer(T).Default</param>
   7: /// <returns>True if the two collections contain all the same items in the same order</returns>
   8: public static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList, IEqualityComparer<TSource> comparer)
   9: {
  10:     if (value == compareList)
  11:     {
  12:         return true;
  13:     }
  14:     else if (value == null || compareList == null)
  15:     {
  16:         return false;
  17:     }
  18:     else
  19:     {
  20:         if (comparer == null)
  21:         {
  22:             comparer = EqualityComparer<TSource>.Default;
  23:         }
  24:  
  25:         IEnumerator<TSource> enumerator1 = value.GetEnumerator();
  26:         IEnumerator<TSource> enumerator2 = compareList.GetEnumerator();
  27:  
  28:         bool enum1HasValue = enumerator1.MoveNext();
  29:         bool enum2HasValue = enumerator2.MoveNext();
  30:  
  31:         try
  32:         {
  33:             while (enum1HasValue && enum2HasValue)
  34:             {
  35:                 if (!comparer.Equals(enumerator1.Current, enumerator2.Current))
  36:                 {
  37:                     return false;
  38:                 }
  39:  
  40:                 enum1HasValue = enumerator1.MoveNext();
  41:                 enum2HasValue = enumerator2.MoveNext();
  42:             }
  43:  
  44:             return !(enum1HasValue || enum2HasValue);
  45:         }
  46:         finally
  47:         {
  48:             if (enumerator1 != null) enumerator1.Dispose();
  49:             if (enumerator2 != null) enumerator2.Dispose();
  50:         }
  51:     }
  52: }
  53:  
  54: public static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList)
  55: {
  56:     return IsEqualTo(value, compareList, null);
  57: }
  58:  
  59: public static bool IsEqualTo(this IEnumerable value, IEnumerable compareList)
  60: {
  61:     return IsEqualTo<object>(value.OfType<object>(), compareList.OfType<object>());
  62: }

Updated: Jugen (see comment below) made some quality suggestions that I’ve used to improve the code here.  To see the state of the code when the comment was made, see here.

It gives you an extension methods on any collection that implements IEnumerable<T>.  There is an optional parameter of type IEqualityComparer<TSource> which if not null will be used to compare each item in the collections.  Otherwise it will use the default comparer for TSource. It will also work for untyped IEnumerable collections, the overloaded method passes the collections through to the IsEqualTo<TSource> method with object as the TSource.  This is really just there for backwards compatibility.

As far as speed goes, I ran a test with 2 collections of 10,000,000 (I stopped there because I started getting out of memory exceptions when I was populating the test collections after that!) items & it took 0.89 seconds, so I think that’d do for most scenarios.  If you want to use this code you can grab a copy of it here.

Sweet.  I didn’t see this before, but Firebug has a new beta version too, which means that it works in Firefox 3 beta 4.  Grab it from the releases page under the Firebug 1.1 Betas heading.  Possibly the single most useful Firefox plugin for me, the fact that it wasn’t working in the beta releases of FF3 was what was stopping me from switching to the beta. No more excuses now, the Adblock Plus plugin even works.

Mozilla Firefox 3 beta 4 available

Mozilla has released the 4th beta of the upcoming Firefox 3 browser.  Check out the review of the new beta by Mozilla Links.  Updates in this release:

  • Improvements to the user interface: better search support in the Download Manager, ability to zoom entire page or just the text, continuing look and feel improvements on Windows Vista, Windows XP, Mac OS X and Linux.
  • Richer personalization through: location bar that uses an algorithm based on site visit recency and frequency (called “frecency”) to provide better matches against your history and bookmarks for URLs and page titles, as well as an adaptive learning algorithm which tunes itself to your browsing habits.
  • Improved platform features such as: support for HTML5’s window.postMessage and window.messageEvent, JavaScript 1.8 improvements, and offline data storage for web applications.
  • Performance improvements: changes to our JavaScript engine as well as profile guided optimization resulted in significant gains over previous releases in the popular SunSpider test from Apple, web applications like Google Mail and Zoho Office run much faster, and continued improvements to memory usage drastically reduce the amount of memory consumed over long web browsing sessions.

There’s a link to the download site at the bottom of this page.

It has also been announced that there will be a 5th beta release, with the code freeze on the 18th of March, which means the beta should be available around March 31st. Or they may release it on 1st April just to screw with everyone :)

Google adds site search underneath site links

imageI hadn’t noticed this before, but Google has added a site search box underneath the site links in their results.  

Not sure what criteria they’re using to generate this, because it doesn’t work for every site.  The SMH gets it, but Drive doesn’t.  It seems to be based on sites that are listed on Google News and their size.  That’s a total guess just based on the couple of dozen of searches I’ve checked out.

Not sure how I missed this seeing as it was posted on the official blog site.  The official word on what sites it will show up for is:

This feature will now occur when we detect a high probability that a user wants more refined search results within a specific site. Like the rest of our snippets, the sites that display the site search box are chosen algorithmically based on metrics that measure how useful the search box is to users

Not sure what that means in real terms, but as I said, I’ve only seen it show up for sites that exist in Google News.  That may simply be coincidental though, as people are often searching those sites too.

Update: Strike my theory. The search box shows up for gimp.org & ubuntu.org & they’re not in Google News.  I guess it’s just down to the pigeons then.

Windows Update doesn’t work in IE8

So I installed IE8 and checked out a few sites.  Plenty of sites look a bit strange, but possibly most surprising is that the windows update site doesn’t work:

image

But but 8 is later than 5 isn’t it?????

Internet Explorer 8 public beta available

Microsoft has released the first public beta of IE8.  The release was announced at the keynote of Mix in Las Vegas (which apparently is full of cool stuff).

Grab the beta now & check out the noise about it on TechMeme.

Update: Ha! Check out the latest article on the IEBlog:

Although we said that IE8 Beta 1 passes the ACID2 test, some of you may be seeing results like the image above; we thought we should explain what’s going on. IE8 passes the official ACID2 test hosted on http://www.webstandards.org/files/acid2/test.html. (Note, this seems to be a popular destination at the moment. You may have trouble reaching the site.)There are also a number of copies of this test around the net. One popular copy that I’ve seen of late is http://acid2.acidtests.org/

People didn’t wait long to start fact checking them!

Update 2: Note, you may need to install this hotfix if you haven’t already: http://www.microsoft.com/downloads/details.aspx?FamilyID=c1ac48ad-f4f9-4b4b-9cb5-460593b052cc&displaylang=en

Subscribe by email

Enter your email address:

Delivered by FeedBurner


Elsewhere

Liked 28 stories

That's how I roll

Artists blaming iTunes for changing music tastes. I got news for these idiots, iTunes only responded to forces in the marketplace. If you want to sell an album, make one worth buying.

FUCKEN PARTY!!!! [pic]

Here’s a small gallery of awesome monsters and aliens made entirely in LEGO!

Ten commandments for developers

Is It Real or Fake?

Why we bill by the hour

IE8 beta2 released

Stargate: Universe has a Go! New series starts 2009

Celebrate haircare pioneer who overcame discrimination to become a millionare...by discriminating against boys of course.

Theft vs. piracy vs. file sharing? Nonono...

Duke Nukem Forever Is In Testing, 'Several Hours" Playable

ACLU doesn't defend the Civil liberties of just ANYONE...

Your chance of getting out alive [Pic]

Girls Just Wanna Have Fun

Vote up if you are a secret Reptilian Overlord who intends to keep humans in perpetual servitude and wish the global shadow governments would chill the fuck out.

Password must be 6 letters: yes. Must contain a numeral: yes. Must not say your bank is pants: WTF?

Appeal Court Overrules Family Court's Shakedown of Dad

Only Chinese performers were skilled, disciplined and obedient enough to lay on the sort of song and dance display seen on Sunday at the Olympics

It’s time to put a deadline on dropping IE6

Zero Punctuation: Braid

An essay on how "Star Trek" ignored fundamental principles of engineering.

Foam Monster wants his hand back... [PIC]

M.C. Escher worked for Blizzard! [pic]

Ahoy Smegheads! a brand new, 1 hour Red Dwarf episode had been commissioned by BBC Worldwide

Overplaying Cards [comic]

Is anything worth its weight in gold?

The Dirty Secret of Clean Energy- "While generating it is getting easier, moving it to market is not."

Friday 13:51

Whohoo! I just got into the stackoverflow beta!

Tuesday 18:53

Top Rated Posts

Verse of the Day

Lift up your eyes on high and see: who created these? He who brings out their host by number, calling them all by name, by the greatness of his might, and because he is strong in power not one is missing. (Isaiah 40:26, ESV) (Listen)

Networks & References