tags:

views:

262

answers:

3

I need to build a simple parser for a input type expressed as text.

I have implemented a FSM in Java using two int arrays:

states[][]
actions[][] 

As I was debugging it I realized that it might be easier to use Java Enums as a variable of those types should show up as the value in the debug view in IntelliJ. However, when I started down this path it seemed that the only way to use Enums is to put the state and action tables in a HashMap - or is there a way to make a multi-dimensional array where the dimensions are Enum values? I'd prefer to not take the object creation and performance hit entailed with a HashMap if I can help it.

I was then thinking I might look at Scala to see if it might be a better option.

So, the question is - would Scala be a better language for expressing a tight FSM or am I better off sticking with Java int arrays?

+1  A: 

Multidimensional enum arrays are fine.

Isn't the following working for you (maybe I misunderstood your question):

public class SO {

    public static void main( String[] args ) {
        States[][] array = new States[0][];
    }

    private enum States {
        a,
        b
    }    
}

If you've got big FSM or a lot of them (like you can have in games) and correctly want to dodge needless object creation (and needless GC which can really kill your game) then it's certainly possible to use Java enums to gain some safety and yet have efficient code.

Another good thing is that the "case" statement on enums in Java if I'm not mistaken default on a (fast) table switch (instead of a lame lookup switch).

Webinator
+2  A: 

I heartily recommend that you take a look at Scala--it's a great language, and well worth evaluating. But the problem that you have described is not, alone, a good reason to pick up a new language. If you learn Scala, it is very likely that implementing your FSM parser will be easier than in Java--but learning Scala plus implementing a simple FSM parser is probably not easier than just doing it in Java.

Also, it's not quite clear what you want in detail: do you want to index arrays by Enum value? You can typecast. Do you want arrays to hold Enums? That should just work. Do you want to interpret the inner array as an Enum? Write a wrapper function that creates an object that gives you a view that looks like what you want.

Rex Kerr
Rex - thanks. I've been studying Scala for a while; changing to Scala would be my first serious use of the language. This is not a time-critical effort so I can take the time to explore a solid implementation and learn at the same time.My goal is to end up with an implementation that is very clear and concise while still being as fast as my usual approach in Java with int[][].Specifically what I need is very simple. I have three Enums - States, Actions and Tokens. I need to map:(state,token) -> state(state,token) -> actionand then have a case statement on the action type.
Randy Kahle
@Randy: I still don't understand exactly what you need. How do you know, given a `state (state,token)` what `action` you want to map to? What is the difference between a `(state,token)` pair and a `state (state,token)` representation?
Rex Kerr
+1  A: 

It seems to me your main concern is performance. While there are many reasons one might do this in Scala, making it faster is not one of them. It'll be as fast as Java, if you take care to avoid higher level abstractions, but not faster.

Daniel
I mostly agree, except that it is possible to more nicely hide low-level high-performance code with Scala than with Java--a few implicits and operators in key places make a world of difference.
Rex Kerr
Daniel - thanks. I'm looking for an implementation that will be terse, clear and as fast as a tuned implementation in Java.
Randy Kahle