Halide 22.0.0 Halide compiler and libraries

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
21 explicit StringTable(const SystemMemoryAllocatorFns &allocator = StringStorage::default_allocator());
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 count);
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
65 StringTable::StringTable(const SystemMemoryAllocatorFns &sma)
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
85 StringTable::~StringTable() {
86 destroy(nullptr);
87}
88
89 void StringTable::resize(void *user_context, size_t capacity) {
90 pointers.resize(user_context, capacity);
91 contents.resize(user_context, capacity);
92 for (size_t n = 0; n < contents.size(); ++n) {
93 StringStorage *storage_ptr = StringStorage::create(user_context, contents.current_allocator());
94 contents.assign(user_context, n, storage_ptr);
95 }
96}
97
98 void StringTable::clear(void *user_context) {
99 for (size_t n = 0; n < contents.size(); ++n) {
100 StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
101 StringStorage::destroy(user_context, storage_ptr);
102 contents.assign(user_context, n, nullptr);
103 }
104 contents.clear(user_context);
105 pointers.clear(user_context);
106}
107
108 void StringTable::destroy(void *user_context) {
109 for (size_t n = 0; n < contents.size(); ++n) {
110 StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
111 StringStorage::destroy(user_context, storage_ptr);
112 contents.assign(user_context, n, nullptr);
113 }
114 contents.destroy(user_context);
115 pointers.destroy(user_context);
116}
117
118 const char *StringTable::operator[](size_t index) const {
119 if (index < pointers.size()) {
120 return static_cast<const char *>(pointers[index]);
121 }
122 return nullptr;
123}
124
125 void StringTable::fill(void *user_context, const char **array, size_t count) {
126 resize(user_context, count);
127 for (size_t n = 0; n < count && n < contents.size(); ++n) {
128 StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
129 storage_ptr->assign(user_context, array[n]);
130 pointers.assign(user_context, n, storage_ptr->data());
131 }
132}
133
134 void StringTable::assign(void *user_context, size_t index, const char *str, size_t length) {
135 if (length == 0) {
136 length = strlen(str);
137 }
138 if (index < contents.size()) {
139 StringStorage *storage_ptr = static_cast<StringStorage *>(contents[index]);
140 storage_ptr->assign(user_context, str, length);
141 pointers.assign(user_context, index, storage_ptr->data());
142 }
143}
144
145 void StringTable::append(void *user_context, const char *str, size_t length) {
146 StringStorage *storage_ptr = StringStorage::create(user_context, contents.current_allocator());
147 storage_ptr->assign(user_context, str, length);
148 contents.append(user_context, storage_ptr);
149 pointers.append(user_context, storage_ptr->data());
150}
151
152 void StringTable::prepend(void *user_context, const char *str, size_t length) {
153 StringStorage *storage_ptr = StringStorage::create(user_context, contents.current_allocator());
154 storage_ptr->assign(user_context, str, length);
155 contents.prepend(user_context, storage_ptr);
156 pointers.prepend(user_context, storage_ptr->data());
157}
158
159 size_t StringTable::parse(void *user_context, const char *str, const char *delim) {
160 if (StringUtils::is_empty(str)) {
161 return 0;
162 }
163
164 size_t delim_length = strlen(delim);
165 size_t total_length = strlen(str);
166 size_t entry_count = StringUtils::count_tokens(str, delim);
167 if (entry_count < 1) {
168 return 0;
169 }
170
171 resize(user_context, entry_count);
172
173 // save each entry into the table
174 size_t index = 0;
175 const char *ptr = str;
176 while (!StringUtils::is_empty(ptr) && (index < entry_count)) {
177 size_t ptr_offset = ptr - str;
178 const char *next_delim = strstr(ptr, delim);
179 size_t token_length = (next_delim == nullptr) ? (total_length - ptr_offset) : (next_delim - ptr);
180 if (token_length > 0 && index < contents.size()) {
181 StringStorage *storage_ptr = static_cast<StringStorage *>(contents[index]);
182 storage_ptr->assign(user_context, ptr, token_length);
183 pointers.assign(user_context, index, storage_ptr->data());
184 ++index;
185 }
186 ptr = (next_delim != nullptr) ? (next_delim + delim_length) : nullptr;
187 }
188 return entry_count;
189}
190
191 bool StringTable::contains(const char *str) const {
192 if (StringUtils::is_empty(str)) {
193 return false;
194 }
195 for (size_t n = 0; n < contents.size(); ++n) {
196 StringStorage *storage_ptr = static_cast<StringStorage *>(contents[n]);
197 if (storage_ptr->contains(str)) {
198 return true;
199 }
200 }
201
202 return false;
203}
204
205 const char **StringTable::data() const {
206 return reinterpret_cast<const char **>(pointers.data());
207}
208
209// --
210
211} // namespace Internal
212} // namespace Runtime
213} // namespace Halide
214
215#endif // HALIDE_RUNTIME_STRING_STORAGE_H
block_storage.h
Halide::Runtime::Internal::PointerTable
Definition: pointer_table.h:14
Halide::Runtime::Internal::PointerTable::current_allocator
const SystemMemoryAllocatorFns & current_allocator() const
Definition: pointer_table.h:353
Halide::Runtime::Internal::PointerTable::size
size_t size() const
Definition: pointer_table.h:297
Halide::Runtime::Internal::PointerTable::clear
void clear(void *user_context)
Definition: pointer_table.h:169
Halide::Runtime::Internal::PointerTable::prepend
void prepend(void *user_context, const void *entry_ptr)
Definition: pointer_table.h:151
Halide::Runtime::Internal::PointerTable::destroy
void destroy(void *user_context)
Definition: pointer_table.h:99
Halide::Runtime::Internal::PointerTable::data
void ** data()
Definition: pointer_table.h:311
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::PointerTable::append
void append(void *user_context, const void *entry_ptr)
Definition: pointer_table.h:155
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::StringStorage
Definition: string_storage.h:84
Halide::Runtime::Internal::StringStorage::assign
void assign(void *user_context, char ch)
Definition: string_storage.h:203
Halide::Runtime::Internal::StringStorage::default_allocator
static const SystemMemoryAllocatorFns & default_allocator()
Definition: string_storage.h:304
Halide::Runtime::Internal::StringStorage::data
const char * data() const
Definition: string_storage.h:294
Halide::Runtime::Internal::StringStorage::create
static StringStorage * create(void *user_context, const SystemMemoryAllocatorFns &ma)
Definition: string_storage.h:135
Halide::Runtime::Internal::StringStorage::contains
bool contains(const char *str) const
Definition: string_storage.h:164
Halide::Runtime::Internal::StringStorage::destroy
static void destroy(void *user_context, StringStorage *string_storage)
Definition: string_storage.h:149
Halide::Runtime::Internal::StringTable
Definition: string_table.h:15
Halide::Runtime::Internal::StringTable::append
void append(void *user_context, const char *str, size_t length=0)
Definition: string_table.h:145
Halide::Runtime::Internal::StringTable::~StringTable
~StringTable()
Definition: string_table.h:85
Halide::Runtime::Internal::StringTable::StringTable
StringTable(const StringTable &)=delete
Halide::Runtime::Internal::StringTable::data
const char ** data() const
Definition: string_table.h:205
Halide::Runtime::Internal::StringTable::parse
size_t parse(void *user_context, const char *str, const char *delim)
Definition: string_table.h:159
Halide::Runtime::Internal::StringTable::assign
void assign(void *user_context, size_t index, const char *str, size_t length=0)
Definition: string_table.h:134
Halide::Runtime::Internal::StringTable::size
size_t size() const
Definition: string_table.h:54
Halide::Runtime::Internal::StringTable::destroy
void destroy(void *user_context)
Definition: string_table.h:108
Halide::Runtime::Internal::StringTable::operator[]
const char * operator[](size_t index) const
Definition: string_table.h:118
Halide::Runtime::Internal::StringTable::resize
void resize(void *user_context, size_t capacity)
Definition: string_table.h:89
Halide::Runtime::Internal::StringTable::clear
void clear(void *user_context)
Definition: string_table.h:98
Halide::Runtime::Internal::StringTable::operator=
StringTable & operator=(const StringTable &)=delete
Halide::Runtime::Internal::StringTable::prepend
void prepend(void *user_context, const char *str, size_t length=0)
Definition: string_table.h:152
Halide::Runtime::Internal::StringTable::fill
void fill(void *user_context, const char **array, size_t count)
Definition: string_table.h:125
Halide::Runtime::Internal::StringTable::contains
bool contains(const char *str) const
Definition: string_table.h:191
Halide::Runtime::Internal::strlen
size_t strlen(const char *string)
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
pointer_table.h
strstr
const char * strstr(const char *, const char *)
string_storage.h
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::StringUtils::is_empty
static bool is_empty(const char *str)
Definition: string_storage.h:13
Halide::Runtime::Internal::SystemMemoryAllocatorFns
Definition: memory_resources.h:193
user_context
void * user_context
Definition: vulkan_resources.h:178
allocator
VulkanMemoryAllocator * allocator
Definition: vulkan_resources.h:179