/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Community System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.htmlcheck.dict;

class DictNode
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final int[] permute_tab =
    { 8, 22, 13, 10, 2, 14, 19, 11, 6, 26, 24, 12, 17, 4, 7, 15, 25, 5, 9, 3, 16, 21, 20, 23, 18, 27 };

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private boolean terminal = false;
  private DictNode[] p = null;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  DictNode()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal functions
   *--------------------------------------------------------------------------------
   */

  private final int permute(char ltr)
  {
    if (ltr=='-')
      return 0;
    else if (ltr=='\'')
      return 1;
    else
      return permute_tab[ltr - 'a'];

  } // end permute

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  final DictNode getRef(char ltr)
  {
    if (p==null)
      return null;
    int x = permute(ltr);
    if (x<p.length)
      return p[x];
    else
      return null;

  } // end getRef

  final void setRef(char ltr, DictNode node)
  {
    int x = permute(ltr);

    if ((p==null) || (x>=p.length))
    { // the internal array needs to be expanded
      DictNode[] new_p = new DictNode[x+1];
      int i;

      if (p!=null)
      { // copy the old portion over, set the new portion
	System.arraycopy(p,0,new_p,0,p.length);
	for (i=p.length; i<x+1; i++)
	  new_p[i] = null;

      } // end if
      else
      { // initialize the whole thing
	for (i=0; i<x+1; i++)
	  new_p[i] = null;

      } // end else

      p = new_p;
      
    } // end if

    p[x] = node;

  } // end setRef

  final boolean isTerminal()
  {
    return terminal;

  } // end isTerminal

  final void setTerminal()
  {
    terminal = true;

  } // end setTerminal

} // end class DictNode
