Sign in to follow this  

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

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this