Hi Folks, my first post here, so greetings and thanks for all the great topics I already looked at I'm searching quite a while now for something that I would call a generics compiler.The Problem: I wrote a NavMesh class that calculates an approximation to the minimal convex polygons between obstacle polygons. It's working quite fine, but sometimes I get floating-point related errors that lead to completely wrong meshes. To solve this I could use additional math to prevent this, but according to the site I found the formulas on it will add about 30% overhead and since I will need to update the mesh in-game I'm considering to change to integer arithmetic completely.Now due to the IMO poor implementation of Java generics, I didn't use them to abstract the base datatype because that would lead to a lot of autoboxing and unnecessary references. So what I imagine is something like this:You have a class Vector<BaseType extends Number> in Vector.java. Now I want some tool that can produce a VectorI / VectorF class that would create the same class, with the same methods and so on just replace the BaseType by int/float (not Integer/Float). Another approach would be some sort of bytecode manipulation tool, that is able to completely replace all Vector<Float> with VectorF, but I think I would prefer the first approach since I would be able to do something like "manual partial specialization".Anyway, as I said I couldn't find something like that.

Thanks for any advice!Greetings,Mene

PS: As you might have guessed I used to do most stuff with C++, so if my idea is just not a good "java-aproach", please let me know

Just write the class using int. If you want to specialise it with different primitive, you're just going to have to bite the bullet and do it manually.

BTW there exist a bunch of pre-built primitive specialisations for the entire collections framework somewhere - we were just talking about it in another thread about map performance... you might want to use some of them.

I read something different, I think in "effective java", but I'll give it a try and if it doesn't work... well find and replace will probably

BTW didn't know the "Java interns Integer objects from -127 to 127"... but if I understood it right it just means, that all Integer instances with a value of e.g. 110 are the same reference (and that is true for any other number in this range)

Thanks, that looks interesting, especially the tool idea sound what I want.But I think I didn't express me clearly enough, when I spoke about Vector I meant the mathematical Vectors that have dot product and so on. E.g. I'd like to replace Java3d's Vector3f with an Vector3<float> and get the exact same results (on a bytecode level) but it'll be more flexible, because I could use it in this manner:

1 2 3 4 5

classNavMesh<REALextendsNumber> {Vector3<REAL> someInternalValue;

publicvoidaddPoint(Vector3<REAL> point);}

And then some tool would process the whole source code and give me:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

classNavMeshf {Vector3fsomeInternalValue;

publicvoidaddPoint(Vector3fpoint);}

classNavMeshd {Vector3dsomeInternalValue;

publicvoidaddPoint(Vector3dpoint);}

classNavMeshi {Vector3isomeInternalValue;

publicvoidaddPoint(Vector3ipoint);}

And replace all occurrences of NavMesh<float> with NavMeshf and so on...

You can do something like this with a bytecode generator, but I have a feeling you're thinking a bit C++ centric here and making an unnecessarily complex task out of it to try and do something in Java which it isn't massively well suited for. IMHO you should probably write whatever library you want to with one kind of primitive type throughout and get all that working, and then maybe worry about other types. The case for Vector<float> and Vector<int> is exactly the sort of thing that unfortunately doesn't map from C++ to Java well at all. Just one of those shortcomings you have to code your way around.

Thanks, that looks interesting, especially the tool idea sound what I want.But I think I didn't express me clearly enough, when I spoke about Vector I meant the mathematical Vectors that have dot product and so on. E.g. I'd like to replace Java3d's Vector3f with an Vector3<float> and get the exact same results (on a bytecode level) but it'll be more flexible, because I could use it in this manner:

1 2 3 4 5

classNavMesh<REALextendsNumber> {Vector3<REAL> someInternalValue;

publicvoidaddPoint(Vector3<REAL> point);}

And then some tool would process the whole source code and give me:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

classNavMeshf {Vector3fsomeInternalValue;

publicvoidaddPoint(Vector3fpoint);}

classNavMeshd {Vector3dsomeInternalValue;

publicvoidaddPoint(Vector3dpoint);}

classNavMeshi {Vector3isomeInternalValue;

publicvoidaddPoint(Vector3ipoint);}

And replace all occurrences of NavMesh<float> with NavMeshf and so on...

your computation/method content will also be very different in some case int vs flot , so you cant use the same code for both (int will need to be scaled befre dividing when float doen't)

Ok, I just wanted to do some micro-benchmarking to see what the compiler/run time optimizer might be able to archive.Again I'm quite disappointed. A friend told me I would be able to use the normal operators (+, -, *, /) if my generic parameter was constrained to Number, but this is not the case. So for serious calculations the code would become totally unreadable.When I first did some java stuff I was quite euphoric about the ease of the language, but these are serious limitations: You're either unmaintainable and probably slow or inflexible.For now I will stick to

Ok, I just wanted to do some micro-benchmarking to see what the compiler/run time optimizer might be able to archive.Again I'm quite disappointed. A friend told me I would be able to use the normal operators (+, -, *, /) if my generic parameter was constrained to Number, but this is not the case. So for serious calculations the code would become totally unreadable.

Agreed, Java's generics with numbers can be a bit misleading. For example you can't cast a Float to an int, you have to turn it into a float first.

You could build some sort of pre-processor which will just generate the class for you. The idea is that when you use Vector<float> it will copy the original generic vector class and then replace all of it's generic parameters with float. With this Vector<int>, Vector<double> and Vector<float> will all generate three seperate classes.

I also considered using bytecode manipulation tools, but that would be almost impossible due to type erasure.If using this approach there still is the problem that you can't use the normal operators. Thus I might end up using a generator which would know that Triangle2f is supposed to be Triangle2<Float> and would generate Triangle2i. The problem here: Triangle2f will contain isInCircumCircle(Vector2f) and isInCircumCircle(Vector2i), thus I need to create Vector2i first... anyway, i guess i'll finish the float implementation first and maybe I'm even better off using some simple regex and copy/replace.If I write a tool I'll let you know, just wanted to know if someone had done some stuff like this before.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org