HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_FSA.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_FSA.h ( UT Library, C++)
7  *
8  * COMMENTS: Finite State Automata String Table
9  * This table takes a fixed set of strings and creates a FSA which is used
10  * for scanning the table. The integer value associated with each string
11  * can be found quite quickly. As well, a search for a string not in the
12  * table is especially efficient.
13  *
14  * The string table used to build the FSA can be destroyed after the table
15  * is constructed, unless, on the odd chance, you want to find out what
16  * strings are stored in the table.
17  */
18 
19 #ifndef __UT_FSA__
20 #define __UT_FSA__
21 
22 #include "UT_API.h"
23 #include "UT_NonCopyable.h"
24 #include "UT_String.h"
25 #include "UT_StringHolder.h"
26 #include "UT_StringView.h"
27 #include "UT_ValArray.h"
28 #include "UT_VectorTypes.h"
29 #include <SYS/SYS_Types.h>
30 
31 class UT_StringArray;
32 
34 {
35 public:
36  ut_FSA_Token(int id=0, const char *token=0)
37  {
38  myId = id;
39  myToken = token;
40  }
41 
42  void set(int id, const char *token)
43  {
44  myToken = token;
45  myId = id;
46  }
47  int getId() const { return myId; }
48  const char *getToken() const { return myToken; }
49 private:
50  const char *myToken;
51  int myId;
52 };
53 
55 {
56 public:
57  UT_FSA();
58  ~UT_FSA();
59 
61 
62  bool build(const UT_StringArray &strings, const UT_IntArray &indices,
63  int notfound_index = -1);
64  bool build(int ntokens, const ut_FSA_Token tokens[],
65  int notfound_index=-1);
66  void clear();
67 
68  /// Return the size of the symbol table
69  exint entries() const { return myEntries; }
70 
71  bool empty() const { return (myEntries == 0); }
72 
73  /// @{
74  /// Find a symbol in the map, returning the integer value associated with
75  /// the key.
76  int findSymbol(const char *symbol) const;
77  int findSymbol(const UT_StringView &symbol) const;
78  int findSymbol(const UT_String &symbol) const
79  { return findSymbol(symbol.c_str()); }
80  int findSymbol(const UT_StringRef &symbol) const
81  { return findSymbol(UT_StringView(symbol)); }
82  /// @}
83 
84  /// Return the integer associated with entries not in the map
85  int getNotFound() const { return myNotFound; }
86 
87  /// @{
88  /// Return whether a symbol exists
89  bool contains(const char *symbol) const
90  { return findSymbol(symbol) != myNotFound; }
91  int count(const char *symbol) const
92  { return contains(symbol) ? 1 : 0; }
93  /// @}
94 
95  int64 getMemoryUsage(bool inclusive) const;
96 
97  void dumpTables() const;
98 
99  // This method is used for validation. It is kind of expensive since it
100  // iterates over the entire space of the FSA for extraction.
101  void extractStrings(UT_StringArray &list) const;
102 
103  /// @private
104  /// Used internally
105  void resolveTree(const UT_IntArray &nexts,
106  const UT_IntArray &states,
107  const UT_IntArray &accepts,
108  const UT_ValArray<const char *> &remainders);
109 
110 private:
111  int *myAccept; // Acceptance states
112  int *myNexts; // Transition table
113  int *myValidate; // Verification states
114  char **myRemainder; // Remaining, unique text
115  int myNSize, myVSize, myASize, myRSize; // Sizes of arrays
116  int myEntries;
117  int myNotFound;
118 };
119 
120 #endif
const char * getToken() const
Definition: UT_FSA.h:48
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
int findSymbol(const UT_String &symbol) const
Definition: UT_FSA.h:78
exint entries() const
Return the size of the symbol table.
Definition: UT_FSA.h:69
int getNotFound() const
Return the integer associated with entries not in the map.
Definition: UT_FSA.h:85
bool contains(const char *symbol) const
Definition: UT_FSA.h:89
int64 exint
Definition: SYS_Types.h:125
int findSymbol(const UT_StringRef &symbol) const
Definition: UT_FSA.h:80
#define UT_API
Definition: UT_API.h:14
int count(const char *symbol) const
Definition: UT_FSA.h:91
const char * c_str() const
Definition: UT_String.h:508
ut_FSA_Token(int id=0, const char *token=0)
Definition: UT_FSA.h:36
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:39
Definition: UT_FSA.h:54
int getId() const
Definition: UT_FSA.h:47
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
GLuint id
Definition: glcorearb.h:655
void set(int id, const char *token)
Definition: UT_FSA.h:42
GLsizei const GLchar *const * strings
Definition: glcorearb.h:1933
bool empty() const
Definition: UT_FSA.h:71
bool OIIO_UTIL_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?