// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   DAWG.java

package Scrabble;

import KennethTam.Debug;
import java.io.*;

// Referenced classes of package Scrabble:
//            Edge

public class DAWG
{

    public DAWG(String s)
    {
        try
        {
            System.out.println("DAWG ctor: opening " + s);
            DataInputStream datainputstream = new DataInputStream(new FileInputStream(s));
            _nEdges = datainputstream.readInt();
            _iStart = datainputstream.readInt();
            _aEdges = new Edge[_nEdges];
            for(int i = 0; i < _nEdges; i++)
                _aEdges[i] = new Edge(datainputstream.readInt());

        }
        catch(Exception exception)
        {
            System.out.println(" Exception in creation of DAWG: " + exception);
        }
        System.out.println("DAWG ctor: done");
    }

    public int getStartIndex()
    {
        return _iStart;
    }

    public Edge getEdge(int i)
    {
        return _aEdges[i];
    }

    public synchronized boolean recognizes(String s)
    {
        return recognizes_e(s) != -1;
    }

    public synchronized int recognizes_e(String s)
    {
        int i = s.length();
        byte abyte0[] = new byte[i];
        s.getBytes(0, i, abyte0, 0);
        if(i == 1)
            return matchChar(abyte0[0], _iStart);
        int k = _iStart;
        for(int j = 0; j < i; j++)
        {
            k = matchChar(abyte0[j], k);
            if(k == -1)
                return -1;
        }

        if(_aEdges[k]._t)
            return k;
        else
            return -1;
    }

    public int matchChar(byte byte0, int i)
    {
        byte byte1 = (byte)(byte0 - 65);
        Debug.assert(byte1 >= 0, "Invalid char");
        int j = i;
        boolean flag;
        do
        {
            flag = _aEdges[j]._e;
            if(_aEdges[j]._l == byte1 && _aEdges[j]._d != i)
                return _aEdges[j]._d;
            j++;
        } while(!flag);
        return -1;
    }

    private int _nEdges;
    private int _iStart;
    private Edge _aEdges[];
}
