HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Word lookup code optimization

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codelookupwordoptimization

Problem

I have a Java class which accepts some data and reads it in as a raster of characters x by y. The class is used then to look for occurrences of a certain word in any direction of the raster and store them in a multidimensional array.

My code works, but I would like to see the code optimized because to me it looks too cumbersome with all the if-else's.

...
private static final int NORTH = 0;
private static final int NORTH_EAST = 1;
private static final int EAST = 2;
private static final int SOUTH_EAST = 3;
private static final int SOUTH = 4;
private static final int SOUTH_WEST = 5;
private static final int WEST = 6;
private static final int NORTH_WEST = 7;

/** The raster char[yIndex][xIndex] */
private char[][] raster;

/**
 * Starts looking through the raster to find occurrences of the given word.
 * @param word the word to look for
 */
public void searchWord(String word) {
    if (!searched || !this.word.equals(word)) {
        this.word = word;
        this.count = 0;
        int w = raster[0].length, h = raster.length;
        for (int y=0; ylookFor.
 * The word is searched horizontally, vertically and diagonally, forward and reverse.
 *  
 * @param x start looking at this x-coordinate
 * @param y start looking at this y-coordinate
 * @param w width of the raster
 * @param h height of the raster
 * @param lookFor characters of the word to look for
 * @return the occurrences found for this word:
 *         int [occurrenceIndex][characterIndex][x,y coordinate] 
 */
private int [][][] search(int x, int y, int w, int h, char [] lookFor) {
    int len = lookFor.length, occurrences[][][] = new int [8][len][];
    for (int j=0; j= 0,
            east  = x + len = 0,
            south = y + len  items = new ArrayList();
    for (int [][] dirArray : occurrences) {
        if (dirArray != null) {
            items.add(dirArray);
        }
    }
    return (int[][][]) items.toArray(new int [][][] {});
}
...

Solution

You could theoretically make it nicer by introducing Iterator<> concept into the code. It would make your code more optimal from readability point of view but I doubt it would make the code more optimal from execution speed point of view. Your Iterator<> interface derivates would always move to the next character in given direction. Basically, the ifs would be hidden behind the iterators.

If using Iterator<> seems too heavy, you could abstract the position increments and direction into an array of (spatial) vectors. E.g., iteration increment in the north direction could be represented by i * {0, +1}. This way you could transform the if/else chain into iteration over an array of such vectors.

I would also introduce a class for the point/coordinate instead of using two element array.

All of this could improve the abstraction and readability. It is up to you to decide if it is worth it though.

Context

StackExchange Code Review Q#10774, answer score: 2

Revisions (0)

No revisions yet.