C++ Set to C# List... ?

Started by
2 comments, last by TheUnbeliever 16 years, 11 months ago
If anyone has seen Erin Catto's Box2D, then you know the code I'm trying to convert to C#. Here's the only part I'm having trouble with. I'm not familier with Set's in C++, so can someone tell me exactly what this code is doing, when it's being used, and possibly why... ?

// This is used by std::set
inline bool operator < (const Arbiter& a1, const Arbiter& a2)
{
	if (a1.body1 < a2.body1)
		return true;

	if (a1.body1 == a2.body1 && a1.body2 < a2.body2)
		return true;

	return false;
}

Advertisement
Short Answer: It's a comparison function that defines the order which Arbiters are stored in a set.

Long answer: A C++ set is a dictionary that is also sorted. When moving the code to C# you'll need to examine the C++ code to see if the sorted behaviour is begin used. If it is, you'll need a sorted list, if not a dictionary.
Awesome. I know it's being sorted, so I'll look into sorted lists. Thanks :D

And actually... how would I see if the "sorting behaviour" is being used?
Quote:Original post by Nitage
If it is, you'll need a sorted list, if not a dictionary.


Or a SortedDictionary, depending on how it's being used.

Quote:MSDN SortedDictionary page
The SortedDictionary generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this respect, it is similar to the SortedList generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:

SortedList uses less memory than SortedDictionary.

SortedDictionary has faster insertion and removal operations for unsorted data: O(log n) as opposed to O(n) for SortedList.

If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary.


EDIT: Oops, I misread that as "If not, you'll need a sorted list, not a dictionary", sorry. However, this may still be relevant so I'll let it stand.

[Edited by - TheUnbeliever on May 19, 2007 7:10:17 AM]
[TheUnbeliever]

This topic is closed to new replies.

Advertisement