Navigation
Links

Powered by Squarespace
« Implementing tail with .NET & Rx | Main | c# Language request for properties »
Wednesday
Dec102008

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);
}
}
}

Reader Comments (1)

Manchester United to Lampard? Don't mention it, take my! January 8th Beijing time, nike tiempo according to the latest British media reports, Chelsea has decided to abandon the team captain, adidas adipure Lampard of 33 years old, although the adidas predator lamp and the team has 1 and a half years contract, but if a team like united with a cheque to buy, mizuno wave cup the blues would definitely be happy in your.

This year's winter transfer window opens for over a week, lionel messi soccer cleats although the Premier League giants are the current halt the troops and wait, wayne rooney cleats but with them about the rumours and many, including Lampard wear nike tiempo legend iv going to buy. Regarding this matter, many nike mercurial vapor superfly iii British media have been reported, the United boss Ferguson"nike total 90 laser iv regular course of official duties" were denied, Sir Alex Ferguson wear nike ctr360 maestri ii elite denied mainly based on two points: first, adidas f50 adizero trx United are not in the winter transfer window to introduce players; second, adidas adipower predator trx Chelsea will agree to sell the Lampard, especially to direct title adidas adipure iv sl rivals Manchester united.

Manchester United don't want to buy on January, is the Soccer cleats main transfer market the lack of suitable players, from the media that Nike Total 90 Laser IV Scholes may return the rescue in the news, the Reds midfielder is Nike Mercurial Superfly III short-handed. As for Chelsea to Lampard's attitude, not like Ferguson wear Nike Tiempo Legend IV would like to, the British" Daily Mirror" reports, the Nike CTR360 Maestri II Blues have decided to give up the 33 year-old veteran midfielder, first they will respect Adidas F50 Adizero TRX Lampard's existing contract, but if there are teams offer buy, Chelsea will have to let go, after all, Adidas Adipower Predator TRX young boa the tactical system, the role of Lampard has been getting smaller and Adidas Adipure IV SL smaller.

Since 2001 since moving to Chelsea, Lampard wear Mizuno Supersonic Wave II has been the team 's midfield unshakable, but as the season boa, arrival, Adidas Adipower Predator TRX the situation has been changing turn the world upside Nike Mercurial Vapor Superfly III FG down, the group stages of the Champions League at the end of round and Valencia battle of life and death, Nike Mercurial Vapor Superfly Safari III the body health of Lampard for the first time since 2003, the Nike CTR360 Maestri II Elite absence of the team 's key and, later in the and Tottenham, Manchester City team of World War II, Nike Tiempo Legend IV Elite he also missed the first episode. Now the boa, to locate Lampard, hope he is in after bench play Nike Total 90 Laser III a key role, but the lineup, more often, by Ramirez Meirelles and Luo Mei, these young players Nike Total 90 Laser IV occupy.

But for Lampard, just half a season there is such a huge gap, Adidas Adipure IV this is really hard to accept him, Adidas Adipure IV SL and in the team gradually become marginalized people, also let him in the England team is clouded, Adidas adipower Predator 33 year old lamp also aspire to the main identity for this summer's European Championship, Adidas F50 AdiZero Prime but if the club they did not play the main force, how do you expect Capello to ensure his position? nike ctr360 maestri ii This move has become the best choice, in fact Lampard and many suitors, nike total 90 in addition to Manchester United, American occupation big alliance of Losangeles the Milky wear nike ctr360 way and the Red Bull New York also are in good faith.

In addition to the status of the team down, nike mercurial another comeback has accelerated the departure of Lampard -- this is Garner midfielder Essien. didier drogba cleats Since last summer, since the injury, Essien has been out of date, and now he was back, cristiano ronaldo cleats according to the" Daily Mirror" the latest reports, Essien wear cheap mizuno soccer cleats will on Monday participate in the home and the Albion reserve team football, boa, hoping to let cheap puma soccer cleats him slowly to get match feeling, remaining in the first half of the season, cheap soccer cleats manager also hope Garner can be restored to its former peak.

January 8, 2012 | Unregistered Commentersoccer cleats

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>