#ifndef _AC_H_
#define _AC_H_
/*
 * 	$Id: ac.h,v 1.6 2011/02/18 19:03:36 rec Exp rec $
 *
 * This include file and the associated code file implement the
 * Aho & Corasick string matching algorithm. 
 *
 * The code was originally written by Sean Davis and later
 * refactored by Bob Cousins. This code is public domain.
 *
 * NOTES:
 *    8/94  -  Original Implementation  (Sean Davis)
 *    9/94  -  Redid Implementation  (James Knight)
 *    3/96  -  Modularized the code  (James Knight)
 *    7/96  -  Finished the modularization  (James Knight)
 *    2/11  -  Refactored (Bob Cousins)
 *    4/11  -  Added code (Bob Cousins)
 *
 * For more information on the algorithm, see
 *
 * @article{Aho:1975:ESM:360825.360855,
 *   author = {Aho, Alfred V. and Corasick, Margaret J.},
 *   title = {Efficient string matching: an aid to bibliographic search},
 *   journal = {Commun. ACM},
 *   volume = {18},
 *   issue = {6},
 *   month = {June},
 *   year = {1975},
 *   issn = {0001-0782},
 *   pages = {333--340},
 *   numpages = {8},
 *   url = {http://doi.acm.org/10.1145/360825.360855},
 *   doi = {http://doi.acm.org/10.1145/360825.360855},
 *   acmid = {360855},
 *   publisher = {ACM},
 *   address = {New York, NY, USA},
 *   keywords = {bibliographic search, computational complexity, 
 *    finite state machines, information retrieval, 
 *    keywords and phrases, string pattern matching, text-editing
 *   }
 * } 
 *
 * Usage Notes:
 *
 *     The key is the AC_Struct which is a 'handle' for the algorithm
 *  holding all state associated with one pattern. These structs may
 *  be allocated and initialized via ac_alloc() and freed via ac_free().
 *     Once you have a handle, strings may be added with ac_add_string()
 *  and removed with ac_del_string(). When the handle's configuration
 *  is stable, then it must be prepared with ac_prep(). (If you forget
 *  to do this, ac_search_init() will do it for you.)
 *     Now the handle is ready to search. A search involves two phases.
 *  The first is a call to ac_search_init() which prepares for the 
 *  next search. Then repeated calls to ac_search(). Each call returns
 *  either the next hit or a NULL indicating no additional hits on
 *  the input data.
 *     The handle may be reused by calling ac_search_init() again.
 *     Statistics are gathered automatically. They are reported with
 *  ac_stats() and are zeroed out with ac_clear_stats();
 *
 */
typedef struct ac_priv_tree_node 
  AC_priv_tree_node, 
  *AC_priv_tree;

struct ac_priv_tree_node {
  AC_priv_tree outlink, 
    faillink,
    children,
    sibling;
  int matchid;
  char ch;
};

typedef struct acstructnode AC_Struct, *AC_Handle ;

struct acstructnode {
  char *T;
  AC_priv_tree tree,
    w, 
    output;
  unsigned int N,
    Psize,
    *Plengths;
  int
    c; 

  unsigned long
    prep_new_edges,		/* Statistics from the ac_prep(); */
    prep_old_edges, 
    prep_fail_compares,
    num_compares,		/* Statistics from the ac_search(); */
    num_failures, 
    edges_traversed, 
    outlinks_traversed;

  char
    isstatic,			/* 0 means dynamic */
    ispreprocessed, 		/* 0 or 1 */
    errorflag,			/* 0 or 1 */
    initflag, 			/* 0 or 1 */
    endflag;			/* 0 or 1 */
};

/*
 * ac_alloc
 *
 * Creates a new AC_Struct structure and initializes its fields.
 *
 * Parameters:    none.
 *
 * Returns:  A dynamically allocated AC_Struct structure.
 */
AC_Handle ac_alloc(void);
/*
 * ac_add_string
 *
 * Adds a string to the AC_Struct structure's keyword tree.
 *
 * NOTE:  The `id' value given must be unique to any of the strings
 *        added to the tree, and must be a small integer greater than
 *        0 (since it is used to index an array holding information
 *        about each of the strings).
 *
 *        The best id's to use are to number the strings from 1 to K.
 *
 * Parameters:   node      -  an AC_Struct structure
 *               P         -  the sequence
 *               M         -  the sequence length
 *               id        -  the sequence identifier
 *
 * Returns:  non-zero on success, zero on error.
 */
int ac_add_string(AC_Handle node, char *P, unsigned int M, unsigned int id);
/*
 * ac_del_string
 *
 * Deletes a string from the keyword tree.
 *
 * Parameters:   node  -  an AC_Struct structure
 *               P     -  the sequence to be deleted
 *               M     -  its length
 *               id    -  its identifier
 *
 * Returns:  non-zero on success, zero on error.
 */
int ac_del_string(AC_Handle node, char *P, unsigned int M, unsigned int id);
/*
 * ac_prep
 *
 * Compute the failure and output links for the keyword tree.
 *
 * Parameters:  node  -  an AC_Struct structure
 *
 * Returns: non-zero on success, zero on error.
 */
int ac_prep(AC_Handle node);
/*
 * ac_search_init
 *
 * Initializes the variables used during an Aho-Corasick search.
 * See ac_search for an example of how it should be used.
 *
 * Parameters:  node  -  an AC_Struct structure
 *              T     -  the sequence to be searched
 *              N     -  the length of the sequence
 *
 * Returns:  nothing.
 */
void ac_search_init(AC_Handle node, char *T, unsigned int N);
/*
 * ac_search
 *
 * Scans a text to look for the next occurrence of one of the patterns
 * in the text.  An example of how this search should be used is the
 * following:
 *
 *    s = T;
 *    len = N; 
 *    contflag = 0;
 *    ac_search_init(node, T, N);
 *    while ((s = ac_search(node, &matchlen, &matchid) != NULL) {
 *      >>> Pattern `matchid' matched from `s' to `s + matchlen - 1'. <<<
 *    }
 *
 * where `node', `T' and `N' are assumed to be initialized appropriately.
 *
 * Parameters:  node           -  a preprocessed AC_Struct structure
 *              length_out     -  where to store the new match's length
 *              id_out         -  where to store the identifier of the
 *                                pattern that matched
 *
 * Returns:  0 if done, != 0 if not done.
 *           Returns 's' set to the left end of the text that matches a pattern, or NULL
 *           if no match occurs.  It also stores values in `*length_out',
 *           and `*id_out' giving the match's length and pattern identifier.
 */
int
ac_search(AC_Handle node, 
	  char **s,
	  unsigned int *length_out, 
	  unsigned int *id_out);
/*
 * ac_match
 *
 * Scans a text to look for the next occurrence of one of the patterns
 * in the text.  Unlike ac_search, ac_match requires the match be from
 * the beginning of the text. An example of how this search should be 
 * used is the following:
 *
 *    s = T;
 *    len = N; 
 *    contflag = 0;
 *    ac_search_init(node, T, N);
 *    while ((s = ac_match(node, &matchlen, &matchid) != NULL) {
 *      >>> Pattern `matchid' matched from `s' to `s + matchlen - 1'. <<<
 *    }
 *
 * where `node', `T' and `N' are assumed to be initialized appropriately.
 *
 * Parameters:  node           -  a preprocessed AC_Struct structure
 *              length_out     -  where to store the new match's length
 *              id_out         -  where to store the identifier of the
 *                                pattern that matched
 *
 * Returns:  0 if done, != 0 if not done.
 *           Returns 's' set to the left end of the text that matches 
 *           a pattern, or NULL if no match occurs.  It also stores 
 *           values in `*length_out', and `*id_out' giving the match's 
 *           length and pattern identifier.
 */
int
ac_match(AC_Handle node, 
	 char **s,
	 unsigned int *length_out, 
	 unsigned int *id_out);
/*
 * ac_free
 *
 * Free up the allocated AC_Struct structure.
 *
 * Parameters:   node  -  a AC_Struct structure
 *
 * Returns:  nothing.
 */
void ac_free(AC_Handle node);

/*
 * ac_stats
 *
 * Report statistics on a given AC_Struct
 *
 * Parameters:  file - a FILE * stream file
 *              node  -  a AC_Struct structure
 *
 * Returns:  nothing.
 */
void ac_stats(FILE *f, AC_Handle node);

/*
 * ac_clear_stats
 *
 * Zero out statistics on a given AC_Struct
 *
 * Parameters:  node  -  a AC_Struct structure
 *
 * Returns:  nothing.
 */
void ac_clear_stats(AC_Handle node);
/*
 * ac_print
 *
 * Print a graphviz compatible (DOT format) file of the current handle.
 *
 * Paramters:  p  -  an AC_Handle 
 *             fname - the file name to create
 *
 * Note: If the file cannot be created, a warning is sent to stderr
 *       and the function returns.
 */
void ac_print(AC_Handle p, char *fname);
/*
 * ac_print
 *
 * Generate a C program file with static declarations of the AC state machine.
 *
 * Paramters:  p  -  an AC_Handle 
 *             varname - root name to be used in variable crateion
 *             fname - the file name to create
 *
 * Note: If the file cannot be created, a warning is sent to stderr
 *       and the function returns.
 */
void ac_code(AC_Handle p, char *varname, char *fname);
#endif




















