/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1989-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
#define _IN_SUF_TREE
#include "suftree.h"
/*
** Construct a suffix tree for a source string
** and perform string matching of various input strings.
** This is based on the suffix tree algorithm of E. McCreight.
** I extended the algorithm to remove the restriction that the
** last element of the string has to be different from the rest
** of the string. Note also that since the alphabet is large (256),
** instead of being stored in an array, the children of a node
** are kept in a linked list which is managed by a move-to-front
** heuristic.
**
** For details, see the paper:
** "A Space-Economical Suffix Tree Construction Algorithm"
** E.M. McCreight, JACM, v.23, No.2, 1976, pp.262-272.
**
** BldSuftree returns either NULL or the pointer to the root of the
** tree. In the latter case, the tree can be destroyed by free()-ing
** the root.
**
** Written by Kiem-Phong Vo, 5/20/88.
*/
/* Delete a suffix tree */
{
if(!root)
return;
root -= 1;
while(root)
{
}
}
/* Find a child whose label string starts with a given character */
{
last = 0;
break;
{
/* move-to-front heuristic */
}
return np;
}
/* Get space for tree nodes. */
{
{
if(root)
return 0;
}
/* store where this list is for later deletion */
if(root)
{
;
}
return list+1;
}
/* Build the tree.
** Following are the meaning of a few important variables:
** clocus: contracted locus, this variable defines the
** tree node that points to the longest prefix
** that terminates at a node in the current tree.
** locus: defines a tree node to be constructed that
** points to the longest prefix that can be defined.
** Unless both clocus and locus equal the root,
** we maintain the invariance that clocus is the
** parent of locus.
** link: defines the sublink of the locus of the prefix
** of the previously inserted suffix.
*/
{
register int n;
if(len == 0)
return 0;
/* get initial space for the tree nodes */
root = 0;
/* 2*len+1 is the maximum number of nodes that can be created */
if(n > ALLOCSIZE)
n = ALLOCSIZE;
return 0;
/* make root node */
/* locus and contracted locus of the empty substring */
/* the current match length */
mtlen = 0;
/* the end of the source string */
/* now build the tree */
{
/* prepare for scanning the current suffix */
{
/* define the string to be rescanned */
/* minus the first character of the previous prefix */
{
rescan++;
if(relen > 0)
--relen;
}
}
/* the length of the known-to-be-matched part */
if(mtlen > 0)
--mtlen;
/* use sublink to rescan */
/* rescan */
while(relen > 0)
{
/* find a child of link that starts with the
first character of rescan. We then know that
rescan must match a prefix of that child.
*/
/* clocus will be the parent of the new link */
/* rescan contains LABEL(match) */
{
}
/* rescan is a proper prefix of LABEL(match) */
else
{
{
return 0;
}
/* make an internal node labeled with rescan */
/* adjust label and pointer of old node */
break;
}
}
/* define sublink for the prefix of the last suffix */
/* scan to match as much as possible */
{
/* see if it matches some child of clocus */
break;
/* clocus will be the parent of the new locus */
/* find the extend of the match */
break;
/* the whole node is matched */
{
}
/* found the extended locus of this suffix */
else
{
{
return 0;
}
/* make a new internal node */
/* the new node is the locus for this suffix */
break;
}
}
{
return 0;
}
/* make a new external node for the suffix */
/* hook it in as the first child of locus */
}
return root;
}
/* Given a raw string and a string represented in a suffix tree,
match the string against the tree to find a longest matching
prefix of the string.
Return the length of the match and where it occurs in the
string represented by the tree.
*/
{
register long mlen;
mlen = 0;
while(1)
{
break;
/* find the extent of the match */
break;
/* update the length of the match */
/* prepare for next iteration */
/* see if we have to work any more */
break;
}
if(mlen == 0) /* no match */
*mtchp = 0;
else
{
/* find where the match starts */
}
return mlen;
}