I'm working on getting FarseerPhysics to compile in MonoTouch. It works fine when I use the HashSet in System.Collections.Generic, however Farseer has its own Hashset class it uses for Xbox 360 and Windows Phone, so I thought it would makes sense to also include that hashset for IPHONE.
This is the Farseer hashset code:
#if WINDOWS_PHONE || XBOX || IPHONE
//TODO: FIX
using System;
using System.Collections;
using System.Collections.Generic;
namespace FarseerPhysics.Common
{
public class HashSet<T> : ICollection<T>
{
private Dictionary<T, short> _dict;
public HashSet(int capacity)
{
_dict = new Dictionary<T, short>(capacity);
}
public HashSet()
{
_dict = new Dictionary<T, short>();
}
// Methods
#region ICollection<T> Members
public void Add(T item)
{
// We don't care for the value in dictionary, Keys matter.
_dict.Add(item, 0);
}
public void Clear()
{
_dict.Clear();
}
public bool Contains(T item)
{
return _dict.ContainsKey(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(T item)
{
return _dict.Remove(item);
}
public IEnumerator<T> GetEnumerator()
{
return _dict.Keys.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _dict.Keys.GetEnumerator();
}
// Properties
public int Count
{
get { return _dict.Keys.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
#endregion
}
}
#endif
They're used as such:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using FarseerPhysics.Collision;
using FarseerPhysics.Common;
using FarseerPhysics.Controllers;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Dynamics.Joints;
using Microsoft.Xna.Framework;
class World
{
(...)
private HashSet<Body> _bodyAddList = new HashSet<Body>();
private HashSet<Body> _bodyRemoveList = new HashSet<Body>();
private HashSet<Joint> _jointAddList = new HashSet<Joint>();
private HashSet<Joint> _jointRemoveList = new HashSet<Joint>();
}
There are two problems when I add IPHONE to the #if in the Farseer hashset class file.
The first is I get an error in the declarations where the compiler says HashSet is an ambigous reference between System.Collections.Generic.HashSet and FarseerPhysics.Common.HashSet. This error does not occur in Visual Studios compiler. I suspect this is because MonoTouch does implement Hashset wherein Xbox 360 and Windows Phone .Net APIs don't have either. Not too sure why there is no hashset for either of those but I suspect it would be best for me to use Farseers versio of hashset.
The other problem is that if I explicitly set the declaration to use FarseerPhysics.Common.Hashset (i.e. new FarseerPhysics.Common.HashSet();) on running the app on an iPhone device I get the error
'Attempting to JIT compile method 'System.Collections.Generic.Dictionary'2:.ctor()' while running with --aot-only.\n'
I should also point out this error does not occur in the simulator, only on an actual device.
The first issue, with the ambiguous reference, is because now you have two classes called HashSet that are being used by your class, and you're not specifying which one you want. You can remove the using System.Collections.Generic;
line, or add a using HashSet = FarseerPhysics.Common.HashSet;
statement to the top of the file. That will make the compiler know which one specifically to use.
The JIT compilation error you're getting is one of a few limitations of monotouch: you can't really use value types in dictionary keys, because of the way the mono compiler will try to instantiate a comparer object. For more info, look here: http://monotouch.net/Documentation/Limitations (search for "value types as dictionary keys").
To work around this issue, you need to implement the IEqualityComparer interface in a new type and provide an instance of that type to the Dictionary(IEqualityComparer) constructor.