HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_FSASymbolTable.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_FSASymbolTable.h ( UT Library, C++)
7  *
8  * COMMENTS: Insertion and deletion of symbols is incredibly expensive.
9  * Searching in the table is faster than other symbol tables (on
10  * most architectures).
11  *
12  */
13 
14 #ifndef __UT_FSASymbolTable__
15 #define __UT_FSASymbolTable__
16 
17 #include "UT_API.h"
18 #include "UT_FSA.h"
19 #include "UT_Array.h"
20 #include "UT_DeepString.h"
21 
22 template <typename ITEM_T>
24  struct SymbolStorage
25  {
26  SymbolStorage(const char *str, const ITEM_T &data)
27  : myThing(data)
28  , myString(str) {}
29 
30  ITEM_T myThing;
31  UT_DeepString myString;
32  };
33 
34 public:
35  UT_FSASymbolTable() : myDirty(true) {}
37 
38  // Add a symbol to the table. Returns the string that would be returned
39  // by getStringReference() (this will be valid even after rebuid)
40  const char *addSymbol(const char *symbol, const ITEM_T &data)
41  {
42  myStorage.append(SymbolStorage(symbol, data));
43  myDirty = true;
44  return myStorage.last().myString;
45  }
46  int deleteSymbol(const char *symbol)
47  {
48  for (int i = 0; i < myStorage.entries(); i++)
49  {
50  if (myStorage(i).myString == symbol)
51  {
52  myStorage.removeIndex(i);
53  myDirty = true;
54  return 1;
55  }
56  }
57  return 0;
58  }
59  void clear()
60  {
61  myStorage.setCapacity(0);
62  myTable.clear();
63  myDirty = true;
64  }
65 
66  int findSymbol(const char *symbol, ITEM_T *datap) const
67  {
68  int idx;
69  if (myDirty) rebuild();
70  idx = myTable.findSymbol(symbol);
71  if (idx >= 0)
72  {
73  *datap = myStorage(idx).myThing;
74  return 1;
75  }
76  return 0;
77  }
78  int findSymbol(const UT_StringView &symbol, ITEM_T *datap) const
79  {
80  int idx;
81  if (myDirty) rebuild();
82  idx = myTable.findSymbol(symbol);
83  if (idx >= 0)
84  {
85  *datap = myStorage(idx).myThing;
86  return 1;
87  }
88  return 0;
89  }
90  int findSymbol(const UT_String &str, ITEM_T *datap) const
91  { return findSymbol(str.c_str(), datap); }
92  int findSymbol(const UT_StringRef &str, ITEM_T *datap) const
93  { return findSymbol(UT_StringView(str), datap); }
94 
95  unsigned entries() const { return myStorage.entries(); }
96  bool empty() const { return !myStorage.entries(); }
97 
98  bool contains(const char *symbol) const
99  { return myTable.contains(symbol); }
100  bool count(const char *symbol) const
101  { return myTable.count(symbol); }
102  void extractStrings(UT_StringArray &list) const
103  { myTable.extractStrings(list); }
104 
105  const char *getStringReference(const char *symbol)
106  {
107  int idx;
108  if (myDirty) rebuild();
109  idx = myTable.findSymbol(symbol);
110  return idx >= 0 ? myStorage(idx).myString : 0;
111  }
112 
113  int64 getMemoryUsage(bool inclusive) const
114  {
115  if (myDirty) rebuild();
116  int64 mem = inclusive ? sizeof(*this) : 0;
117  mem += myTable.getMemoryUsage(false);
118  mem += myStorage.getMemoryUsage(false);
119  return mem;
120  }
121 
123  int (*function)(const ITEM_T &, const char *, void *),
124  void *data) const
125  {
126  for (int i = 0; i < myStorage.entries(); i++)
127  {
128  if (!function(myStorage(i).myThing,
129  myStorage(i).myString, data))
130  return 0;
131  }
132  return 1;
133  }
134  int traverse(int (*function)(ITEM_T &, const char *, void *),
135  void *data)
136  {
137  for (int i = 0; i < myStorage.entries(); i++)
138  {
139  if (!function(myStorage(i).myThing,
140  myStorage(i).myString, data))
141  return 0;
142  }
143  return 1;
144  }
145 
146  void rebuild()
147  {
148  ut_FSA_Token *tokens;
149  int i;
150 
151  if (myDirty)
152  {
153  tokens = (ut_FSA_Token *)::malloc(
154  sizeof(ut_FSA_Token)*myStorage.entries());
155  for (i = 0; i < myStorage.entries(); i++)
156  tokens[i].set(i, myStorage(i).myString);
157 
158  myTable.build(myStorage.entries(), tokens, -1);
159  ::free(tokens);
160  myDirty = false;
161  }
162  }
163 
164 private:
165  void rebuild() const { (SYSconst_cast(this))->rebuild(); }
166 
167  UT_FSA myTable;
168  UT_Array<SymbolStorage> myStorage;
169  bool myDirty;
170 };
171 
172 #endif
T & last()
Definition: UT_Array.h:759
const char * getStringReference(const char *symbol)
int findSymbol(const char *symbol) const
unsigned entries() const
int traverseConst(int(*function)(const ITEM_T &, const char *, void *), void *data) const
int findSymbol(const char *symbol, ITEM_T *datap) const
bool empty() const
int traverse(int(*function)(ITEM_T &, const char *, void *), void *data)
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
bool contains(const char *symbol) const
Definition: UT_FSA.h:88
void extractStrings(UT_StringArray &list) const
int deleteSymbol(const char *symbol)
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:620
exint removeIndex(exint index)
Definition: UT_Array.h:338
int count(const char *symbol) const
Definition: UT_FSA.h:90
const char * c_str() const
Definition: UT_String.h:498
int64 getMemoryUsage(bool inclusive) const
void extractStrings(UT_StringArray &list) const
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:40
void setCapacity(exint newcapacity)
Definition: UT_ArrayImpl.h:756
Definition: UT_FSA.h:55
void clear()
bool count(const char *symbol) const
long long int64
Definition: SYS_Types.h:116
exint append()
Definition: UT_Array.h:137
int64 getMemoryUsage(bool inclusive) const
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:611
bool contains(const char *symbol) const
int findSymbol(const UT_StringRef &str, ITEM_T *datap) const
int findSymbol(const UT_String &str, ITEM_T *datap) const
bool build(const UT_StringArray &strings, const UT_IntArray &indices, int notfound_index=-1)
int findSymbol(const UT_StringView &symbol, ITEM_T *datap) const
Definition: format.h:895
const char * addSymbol(const char *symbol, const ITEM_T &data)