Navigation
Links

Powered by Squarespace

Entries in c# (12)

Wednesday
10Dec2008

Sequence membership

Given two sorted sequences, this method returns the intersection, a-b, and b-a, using a single iteration over both sequences. The return type is an IEnumerable<KeyValuePair<T, int>>, where:

  • only in A = -1

  • in both = 0

  • only in B = 1


/// <summary>
/// Return an int for every element in sortedSetA and sortedSetB specifying objects
/// that belong to A but not to B (-1), objects which are both in A and in B (0), and objects
/// that belong to B but not A (1).
/// </summary>
/// <remarks>This method is an O(n+m) operation when the two sets have different members, where n
/// is the count of <paramref name="sortedSetA"/> and m is the count of <paramref name="sortedSetB"/>.
/// Otherwise the operation approaches O(n+m-n) where m is the count of the larger set and n is the
/// smaller set.
/// </remarks>
/// <typeparam name="T">Type of element in each set</typeparam>
/// <param name="setA">A set where each element is of type <typeparamref name="T"/></param>
/// <param name="setB">A set where each element is of type <typeparamref name="T"/></param>
/// <returns>Returns A \ B (-1), the intersection of A and B (0), and B \ A (1)</returns>
public static IEnumerable<KeyValuePair<T, int>> Membership<T> (IEnumerable<T> sortedSetA, IEnumerable<T> sortedSetB) {
return Membership<T> (sortedSetA, sortedSetB, Comparer<T>.Default);
}
/// <summary>
/// Return a KeyValuePair<int, T> for every element in sortedSetA and sortedSetB where the int
/// specifies membership and T is the element. The int specifies objects that belong to A but
/// not to B (-1), objects which are both in A and in B (0), and objects that belong to B but
/// not A (1).
/// </summary>
/// <remarks>This method is an O(n+m) operation when the two sets have different members, where n
/// is the count of <paramref name="sortedSetA"/> and m is the count of <paramref name="sortedSetB"/>.
/// Otherwise the operation approaches O(n+m-n) where m is the count of the larger set and n is the
/// smaller set.
/// </remarks>
/// <typeparam name="T">Type of element in each set</typeparam>
/// <param name="setA">A set where each element is of type <typeparamref name="T"/></param>
/// <param name="setB">A set where each element is of type <typeparamref name="T"/></param>
/// <param name="comparer"><see cref="IComparer"/> used to sort the sets</param>
/// <returns>Returns A \ B (-1), the intersection of A and B (0), and B \ A (1)</returns>
public static IEnumerable<KeyValuePair<T, int>> Membership<T> (IEnumerable<T> sortedSetA, IEnumerable<T> sortedSetB, IComparer<T> comparer) {
const int onlyA = -1;
const int both = 0;
const int onlyB = 1;

if (sortedSetA == null)
throw new ArgumentNullException ("sortedSetA");
if (sortedSetB == null)
throw new ArgumentNullException ("sortedSetB");
if (comparer == null)
throw new ArgumentNullException ("comparer");

IEnumerator<T> enumeratorA = sortedSetA.GetEnumerator ();
IEnumerator<T> enumeratorB = sortedSetB.GetEnumerator ();

bool nextA = enumeratorA.MoveNext ();
bool nextB = enumeratorB.MoveNext ();

// default value for type T
T a = default (T);
// default value for type T
T b = default (T);

// if both collections have a value
if (nextA & nextB) {
// get current value
a = enumeratorA.Current;
// get current value
b = enumeratorB.Current;

do {
// Compare a to b: is a < b, a == b, or a > b ?
int val = comparer.Compare (a, b);
// a == b
if (val == 0) {
// return both collections have the value a
yield return new KeyValuePair<T, int> (a, both);
// if collection a can move next
if (nextA = enumeratorA.MoveNext ())
// get the next a
a = enumeratorA.Current;
// if collection b can move next
if (nextB = enumeratorB.MoveNext ())
// get the next b
b = enumeratorB.Current;
}
// a < b
else if (val < 0) {
// return only collection a has the value a
yield return new KeyValuePair<T, int> (a, onlyA);
// if collection a can move next
if (nextA = enumeratorA.MoveNext ())
// get the next a
a = enumeratorA.Current;
}
// a > b
else {
// return only collection b has the value b
yield return new KeyValuePair<T, int> (b, onlyB);
// if collection b can move next
if (nextB = enumeratorB.MoveNext ())
// get the next b
b = enumeratorB.Current;
}
// loop while there are values for both collections
} while (nextA & nextB);
}
// if collection a has more values
if (nextA) {
// return only collection a has the value a
yield return new KeyValuePair<T, int> (a, onlyA);
// if collection a can move next
while (enumeratorA.MoveNext ()) {
// return only collection a has the value a
yield return new KeyValuePair<T, int> (enumeratorA.Current, onlyA);
}
}
// if collection b has more values
else if (nextB) {
// return only collection b has the value b
yield return new KeyValuePair<T, int> (b, onlyB);
// if collection b can move next
while (enumeratorB.MoveNext ()) {
// return only collection b has the value b
yield return new KeyValuePair<T, int> (enumeratorB.Current, onlyB);
}
}
}
Tuesday
07Oct2008

c# Language request for properties

Auto-Implemented properties are great, until you need to implement custom get or set logic. I love the ability to make the field an implementation detail, until you need to implement the property. What if you could continue to hide the field, like this:
public string FirstName {
get {
// do more stuff, like lazy init the field
if (field == null)
field = "Unknown";
return field;
}
set {
field = value;
}
}
A new keyword, field, references the compiler generated property’s backing field. Also, for readonly fields:
public readonly string Id {
get {
return field;
}
set { // private is implied
// do more stuff, like don't accept null
if (value == null)
value = string.Empty;
field = value;
}
}
Where the property would need to be set in the constructor (like read-only fields) as the backing field would be marked read-only. For simplicity, the implementation would use simple property syntax while the compiled output would involve a read-only field, a truly read-only property and a constructor that executes the property's set body via a static method call each time the field is set in the constructor.

Please rate and validate this sugestion at the MSDN Microsoft Product Feedback Center.
Friday
30Nov2007

LINQ - WHERE X IN (…)

I couldn't figure out a way to perform the equivalent of WHERE Column1 IN ('A', 'B', 'C') in LINQ, where ('A', 'B', 'C') would represent an IEnumerable<T> where T is the type of Column1. So I thought it was an excellent time to write an extension method that would generate a dynamic expression tree that would add AND (Column1 == "A" OR Column1 = "B" OR ...) to the LINQ query. So I wrote the following code:
public static IQueryable<TSource> WhereIn<TSource, TKey> (
this IQueryable<TSource> source1,
Expression<Func<TSource, TKey>> keySelector,
IEnumerable<TKey> source2) {
if (null == source1)
throw new ArgumentNullException ("source1");
if (null == keySelector)
throw new ArgumentNullException ("keySelector");
if (null == source2)
throw new ArgumentNullException ("source2");
Expression where = null;
foreach (TKey value in source2) {
Expression equal = Expression.Equal (
keySelector.Body,
Expression.Constant (value, typeof (TKey))
);
if (null == where)
where = equal;
else
where = Expression.OrElse (where, equal);
}
return source1.Where<TSource> (
Expression.Lambda<Func<TSource, bool>> (
where, keySelector.Parameters));
}
An example of the usage:
var q = (from u in db.Users
where u.LastLogin > new DateTime (2007, 5, 1)
orderby u.LastLogin descending
select new { u.FirstName, u.LastName, u.UserName, u.LastLogin }
).WhereIn (u => u.UserName, new string[] { "A", "B", "C" });
A day later, I found the right way that will actually generate "WHERE X IN (...)" in LINQ to SQL thanks to Mark Blomsma.
Thursday
15Mar2007

Internal NullReferenceException in HttpWebRequest when using a CachePolicy

Setting the CachePolicy to the default HttpRequestCacheLevel causes an exception internal to HttpWebRequest when a web application is running with an application pool configured to use a custom identity. For example:
HttpWebRequest request = …;
request.CachePolicy =
new System.Net.Cache.HttpRequestCachePolicy (
System.Net.Cache.HttpRequestCacheLevel.Default);
request.GetResponse ();

GetResponse () throws a WebException: “The request was aborted: The request was canceled.” Inner exception shows that the private method CheckCacheUpdateOnResponse () of System.Net.HttpWebRequest encounters a NullReferenceException.

A “workaround” would involve loading the user’s profile. For example, if the application pool custom identity is DOMAIN\MyWebApp, log in to the server as MyWebApp. The error goes away – until the profile is unloaded.

This problem does not occur when the web application is using the default application pool identity, NetworkService, because I believe that profile is already loaded by default.

Another bug report is somewhat similar to this but only manifests after a few thousand requests. These are probably symptoms of a much larger bug (or design flaw – http request caching is currently implemented through IE’s cache).

Update: Microsoft has reproduced this bug and will be addressing it in a future release.
Wednesday
14Mar2007

What happened here?

Here's a number of inconsistencies with exception constructors:

The message parameter is first and paramName is second for one of ArgumentException's constructors.
public ArgumentException(string message, string paramName);

These exceptions inherit ArgumentException:

  • In one of ArgumentOutOfRangeException's constructors, paramName is first and message is second.
    public ArgumentOutOfRangeException(string paramName, string message);
  • In one of InvalidEnumArgumentException's constructors, argumentName is used to represent the same value as paramName. This is an overload that doesn't include the message parameter, which is normal.public InvalidEnumArgumentException(string argumentName, int invalidValue, Type enumClass);
  • In one of DuplicateWaitObjectException's constructors, parameterName is first, represents the same value as paramName, and message is second.
    public DuplicateWaitObjectException(string parameterName, string message);


Questions:

  1. Why are there three names used to represent paramName?

  2. Why was the parameter order changed in the child classes?