1 /*************************************************************************/
2 /* Copyright (c) 2004                                                    */
3 /* Daniel Sleator, David Temperley, and John Lafferty                    */
4 /* All rights reserved                                                   */
5 /*                                                                       */
6 /* Use of the link grammar parsing system is subject to the terms of the */
7 /* license set forth in the LICENSE file included with this software.    */
8 /* This license allows free redistribution and use in source and binary  */
9 /* forms, with or without modification, subject to certain conditions.   */
10 /*                                                                       */
11 /*************************************************************************/
12 
13 #ifndef _FAST_MATCH_H_
14 #define _FAST_MATCH_H_
15 
16 #include <stddef.h> // for size_t
17 #include "api-types.h"
18 #include "link-includes.h" // for Sentence
19 #include "memory-pool.h"
20 
21 typedef struct Match_node_struct Match_node;
22 struct Match_node_struct
23 {
24 	Match_node * next;
25 	Disjunct * d;
26 };
27 
28 typedef struct fast_matcher_s fast_matcher_t;
29 struct fast_matcher_s
30 {
31 	size_t size;
32 	unsigned int *l_table_size;  /* the sizes of the hash tables */
33 	unsigned int *r_table_size;
34 
35 	/* the beginnings of the hash tables */
36 	Match_node *** l_table;
37 	Match_node *** r_table;
38 
39 	/* I'll pedantically maintain my own array of these cells */
40 	Disjunct ** match_list;      /* match-list stack */
41 	size_t match_list_end;       /* index to the match-list stack end */
42 	size_t match_list_size;      /* number of allocated elements */
43 };
44 
45 /* See the source file for documentation. */
46 fast_matcher_t* alloc_fast_matcher(const Sentence, unsigned int *[]);
47 void free_fast_matcher(Sentence sent, fast_matcher_t*);
48 
49 size_t form_match_list(fast_matcher_t *, int, Connector *, int, Connector *, int);
50 
51 /**
52  * Return the match-list element at the given index.
53  */
get_match_list_element(fast_matcher_t * ctxt,size_t mli)54 static inline Disjunct *get_match_list_element(fast_matcher_t *ctxt, size_t mli)
55 {
56 	return ctxt->match_list[mli];
57 }
58 
59 /**
60  * Pop up the match-list stack
61  */
pop_match_list(fast_matcher_t * ctxt,size_t match_list_last)62 static inline void pop_match_list(fast_matcher_t *ctxt, size_t match_list_last)
63 {
64 	ctxt->match_list_end = match_list_last;
65 }
66 
67 /**
68  * Return true iff there is no match-list (not even en empty one).
69  */
is_no_match_list(fast_matcher_t * ctxt,size_t match_list_start)70 static inline bool is_no_match_list(fast_matcher_t *ctxt, size_t match_list_start)
71 {
72 	return ctxt->match_list_end == match_list_start;
73 }
74 
75 /**
76  * Return Get the match-list current position.
77  */
get_match_list_position(fast_matcher_t * ctxt)78 static inline size_t get_match_list_position(fast_matcher_t *ctxt)
79 {
80 	return ctxt->match_list_end;
81 }
82 
83 /**
84  * Return the match-list at a given position.
85  */
get_match_list(fast_matcher_t * ctxt,size_t pos)86 static inline Disjunct **get_match_list(fast_matcher_t *ctxt, size_t pos)
87 {
88 	return &ctxt->match_list[pos];
89 }
90 #endif
91