initial
[prop.git] / include / AD / strings / kmp.h
blob1dad93025b8132c092e472e6d9895eb7ae9ea9dd
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef Knuth_Morris_Pratt_string_matching_h
26 #define Knuth_Morris_Pratt_string_matching_h
29 // String matching with the Knuth-Morris-Pratt algorithm\cite{KMP}
32 #include <AD/strings/strmatch.h>
34 class KMP : public StringMatcher {
36 KMP(const KMP&); // No copy constructor
37 KMP& operator = (const KMP&); // No assignment
39 protected:
41 typedef unsigned char Skip[256];
42 Skip * skip;
43 int patternLength;
45 public:
47 KMP() : skip(0) {}
48 KMP(const char * pattern) : skip(0) { *this = pattern; }
49 ~KMP() { delete [] skip; }
51 // Set new pattern
52 void compile(const char * pattern, int length = -1);
53 void operator = (const char * pattern) { compile(pattern); }
55 // Returns position in pattern is a substring of text
56 // Returns -1 otherwise
57 int Match(const char * text, int length = -1) const;
60 #endif