Halide
string_table.h
Go to the documentation of this file.
1 #ifndef HALIDE_RUNTIME_STRING_TABLE_H
2 #define HALIDE_RUNTIME_STRING_TABLE_H
3 
4 #include "../HalideRuntime.h"
5 #include "block_storage.h"
6 #include "pointer_table.h"
7 #include "string_storage.h"
8 
9 namespace Halide {
10 namespace Runtime {
11 namespace Internal {
12 
13 // Storage class for an array of strings (based on block storage)
14 // -- Intended for building and maintaining tables of strings
15 class StringTable {
16 public:
17  // Disable copy constructors
18  StringTable(const StringTable &) = delete;
19  StringTable &operator=(const StringTable &) = delete;
20 
22  StringTable(void *user_context, size_t capacity, const SystemMemoryAllocatorFns &allocator = StringStorage::default_allocator());
23  StringTable(void *user_context, const char **array, size_t count, const SystemMemoryAllocatorFns &allocator = StringStorage::default_allocator());
24  ~StringTable();
25 
26  void resize(void *user_context, size_t capacity);
27  void destroy(void *user_context);
28  void clear(void *user_context);
29 
30  // fills the contents of the table (copies strings from given array)
31  void fill(void *user_context, const char **array, size_t coun);
32 
33  // assign the entry at given index the given string
34  void assign(void *user_context, size_t index, const char *str, size_t length = 0); // if length is zero, strlen is used
35 
36  // appends the given string to the end of the table
37  void append(void *user_context, const char *str, size_t length = 0); // if length is zero, strlen is used
38 
39  // prepend the given string to the end of the table
40  void prepend(void *user_context, const char *str, size_t length = 0); // if length is zero, strlen is used
41 
42  // parses the given c-string based on given delimiter, stores each substring in the resulting table
43  size_t parse(void *user_context, const char *str, const char *delim);
44 
45  // index-based access operator
46  const char *operator[](size_t index) const;
47 
48  // returns the raw string table pointer
49  const char **data() const;
50 
51  // scans the table for existance of the given string within any entry (linear scan w/string compare!)
52  bool contains(const char *str) const;
53 
54  size_t size() const {
55  return contents.size();
56  }
57 
58 private:
59  PointerTable contents; //< owns string data
60  PointerTable pointers; //< pointers to raw string data
61 };
62 
63 // --
64 
66  : contents(nullptr, 0, sma),
67  pointers(nullptr, 0, sma) {
68  // EMPTY!
69 }
70 
71 StringTable::StringTable(void *user_context, size_t capacity, const SystemMemoryAllocatorFns &sma)
72  : contents(user_context, capacity, sma),
73  pointers(user_context, capacity, sma) {
74  if (capacity) {
75  resize(user_context, capacity);
76  }
77 }
78 
79 StringTable::StringTable(void *user_context, const char **array, size_t count, const SystemMemoryAllocatorFns &sma)
80  : contents(user_context, count, sma),
81  pointers(user_context, count, sma) {
82  fill(user_context, array, count);
83 }
84 
86  destroy(nullptr);
87 }
88 
89 void StringTable::resize(void *user_context, size_t capacity) {
90  pointers.resize(user_context, capacity);
91  while (contents.size() < capacity) {
92  StringStorage *storage_ptr = StringStorage::create(user_context, contents.current_allocator());
93  contents.append(user_context, storage_ptr);
94  }
95 }
96 
97 void StringTable::clear(void *user_context) {
98  for (size_t n = 0; n < contents.size(); ++n) {
99  StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
100  StringStorage::destroy(user_context, storage_ptr);
101  contents.assign(user_context, n, nullptr);
102  }
103  contents.clear(user_context);
104  pointers.clear(user_context);
105 }
106 
107 void StringTable::destroy(void *user_context) {
108  for (size_t n = 0; n < contents.size(); ++n) {
109  StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
110  StringStorage::destroy(user_context, storage_ptr);
111  contents.assign(user_context, n, nullptr);
112  }
113  contents.destroy(user_context);
114  pointers.destroy(user_context);
115 }
116 
117 const char *StringTable::operator[](size_t index) const {
118  if (index < pointers.size()) {
119  return static_cast<const char *>(pointers[index]);
120  }
121  return nullptr;
122 }
123 
124 void StringTable::fill(void *user_context, const char **array, size_t count) {
125  resize(user_context, count);
126  for (size_t n = 0; n < count && n < contents.size(); ++n) {
127  StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
128  storage_ptr->assign(user_context, array[n]);
129  pointers.assign(user_context, n, storage_ptr->data());
130  }
131 }
132 
133 void StringTable::assign(void *user_context, size_t index, const char *str, size_t length) {
134  if (length == 0) {
135  length = strlen(str);
136  }
137  if (index < contents.size()) {
138  StringStorage *storage_ptr = static_cast<StringStorage *>(contents[index]);
139  storage_ptr->assign(user_context, str, length);
140  pointers.assign(user_context, index, storage_ptr->data());
141  }
142 }
143 
144 void StringTable::append(void *user_context, const char *str, size_t length) {
145  StringStorage *storage_ptr = StringStorage::create(user_context, contents.current_allocator());
146  storage_ptr->assign(user_context, str, length);
147  contents.append(user_context, storage_ptr);
148  pointers.append(user_context, storage_ptr->data());
149 }
150 
151 void StringTable::prepend(void *user_context, const char *str, size_t length) {
152  StringStorage *storage_ptr = StringStorage::create(user_context, contents.current_allocator());
153  storage_ptr->assign(user_context, str, length);
154  contents.prepend(user_context, storage_ptr);
155  pointers.prepend(user_context, storage_ptr->data());
156 }
157 
158 size_t StringTable::parse(void *user_context, const char *str, const char *delim) {
159  if (StringUtils::is_empty(str)) {
160  return 0;
161  }
162 
163  size_t delim_length = strlen(delim);
164  size_t total_length = strlen(str);
165  size_t entry_count = StringUtils::count_tokens(str, delim);
166  if (entry_count < 1) {
167  return 0;
168  }
169 
170  resize(user_context, entry_count);
171 
172  // save each entry into the table
173  size_t index = 0;
174  const char *ptr = str;
175  while (!StringUtils::is_empty(ptr) && (index < entry_count)) {
176  size_t ptr_offset = ptr - str;
177  const char *next_delim = strstr(ptr, delim);
178  size_t token_length = (next_delim == nullptr) ? (total_length - ptr_offset) : (next_delim - ptr);
179  if (token_length > 0 && index < contents.size()) {
180  StringStorage *storage_ptr = static_cast<StringStorage *>(contents[index]);
181  storage_ptr->assign(user_context, ptr, token_length);
182  pointers.assign(user_context, index, storage_ptr->data());
183  ++index;
184  }
185  ptr = (next_delim != nullptr) ? (next_delim + delim_length) : nullptr;
186  }
187  return entry_count;
188 }
189 
190 bool StringTable::contains(const char *str) const {
191  if (StringUtils::is_empty(str)) {
192  return false;
193  }
194  for (size_t n = 0; n < contents.size(); ++n) {
195  StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
196  if (storage_ptr->contains(str)) {
197  return true;
198  }
199  }
200 
201  return false;
202 }
203 
204 const char **StringTable::data() const {
205  return reinterpret_cast<const char **>(pointers.data());
206 }
207 
208 // --
209 
210 } // namespace Internal
211 } // namespace Runtime
212 } // namespace Halide
213 
214 #endif // HALIDE_RUNTIME_STRING_STORAGE_H
Halide::Runtime::Internal::PointerTable::size
size_t size() const
Definition: pointer_table.h:297
Halide::Runtime::Internal::StringStorage
Definition: string_storage.h:84
Halide::Runtime::Internal::StringTable::fill
void fill(void *user_context, const char **array, size_t coun)
Definition: string_table.h:124
Halide::Runtime::Internal::StringTable::destroy
void destroy(void *user_context)
Definition: string_table.h:107
Halide::Runtime::Internal::StringTable::operator[]
const char * operator[](size_t index) const
Definition: string_table.h:117
Halide::Runtime::Internal::StringTable::resize
void resize(void *user_context, size_t capacity)
Definition: string_table.h:89
Halide::Runtime::Internal::StringTable::append
void append(void *user_context, const char *str, size_t length=0)
Definition: string_table.h:144
Halide::Runtime::Internal::PointerTable::assign
void assign(void *user_context, size_t index, const void *entry_ptr)
Definition: pointer_table.h:146
Halide::Runtime::Internal::StringTable::clear
void clear(void *user_context)
Definition: string_table.h:97
Halide::Runtime::Internal::StringTable::size
size_t size() const
Definition: string_table.h:54
Halide::Runtime::Internal::PointerTable
Definition: pointer_table.h:14
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
block_storage.h
pointer_table.h
Halide::Runtime::Internal::StringStorage::default_allocator
static const SystemMemoryAllocatorFns & default_allocator()
Definition: string_storage.h:304
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Runtime::Internal::PointerTable::resize
void resize(void *user_context, size_t entry_count, bool realloc=true)
Definition: pointer_table.h:181
Halide::Runtime::Internal::StringTable::operator=
StringTable & operator=(const StringTable &)=delete
Halide::Runtime::Internal::StringStorage::create
static StringStorage * create(void *user_context, const SystemMemoryAllocatorFns &ma)
Definition: string_storage.h:135
Halide::Runtime::Internal::PointerTable::clear
void clear(void *user_context)
Definition: pointer_table.h:169
Halide::Runtime::Internal::StringTable::parse
size_t parse(void *user_context, const char *str, const char *delim)
Definition: string_table.h:158
Halide::Runtime::Internal::PointerTable::append
void append(void *user_context, const void *entry_ptr)
Definition: pointer_table.h:155
Halide::Runtime::Internal::PointerTable::data
void ** data()
Definition: pointer_table.h:311
Halide::Runtime::Internal::StringTable::contains
bool contains(const char *str) const
Definition: string_table.h:190
Halide::Runtime::Internal::StringTable::~StringTable
~StringTable()
Definition: string_table.h:85
string_storage.h
Halide::Runtime::Internal::StringUtils::is_empty
static bool is_empty(const char *str)
Definition: string_storage.h:13
Halide::Runtime::Internal::StringTable::assign
void assign(void *user_context, size_t index, const char *str, size_t length=0)
Definition: string_table.h:133
Halide::Runtime::Internal::strlen
size_t strlen(const char *string)
Halide::Runtime::Internal::StringStorage::destroy
static void destroy(void *user_context, StringStorage *string_storage)
Definition: string_storage.h:149
Halide::Runtime::Internal::StringStorage::data
const char * data() const
Definition: string_storage.h:294
Halide::Runtime::Internal::StringTable::data
const char ** data() const
Definition: string_table.h:204
Halide::Runtime::Internal::StringTable::prepend
void prepend(void *user_context, const char *str, size_t length=0)
Definition: string_table.h:151
Halide::Runtime::Internal::SystemMemoryAllocatorFns
Definition: memory_resources.h:193
strstr
const char * strstr(const char *, const char *)
Halide::Runtime::Internal::StringUtils::count_tokens
static size_t count_tokens(const char *str, const char *delim)
Definition: string_storage.h:24
Halide::Runtime::Internal::StringStorage::assign
void assign(void *user_context, char ch)
Definition: string_storage.h:203
Halide::Runtime::Internal::PointerTable::prepend
void prepend(void *user_context, const void *entry_ptr)
Definition: pointer_table.h:151
Halide::Runtime::Internal::StringTable
Definition: string_table.h:15
Halide::Runtime::Internal::StringTable::StringTable
StringTable(const StringTable &)=delete
Halide::Runtime::Internal::StringStorage::contains
bool contains(const char *str) const
Definition: string_storage.h:164
Halide::Runtime::Internal::PointerTable::current_allocator
const SystemMemoryAllocatorFns & current_allocator() const
Definition: pointer_table.h:353
Halide::Runtime::Internal::PointerTable::destroy
void destroy(void *user_context)
Definition: pointer_table.h:99