1 //===-- UniqueCStringMap.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_UniqueCStringMap_h_
11 #define liblldb_UniqueCStringMap_h_
18 // Other libraries and framework includes
20 #include "lldb/Utility/ConstString.h"
21 #include "lldb/Utility/RegularExpression.h"
23 namespace lldb_private {
25 //----------------------------------------------------------------------
26 // Templatized uniqued string map.
28 // This map is useful for mapping unique C string names to values of type T.
29 // Each "const char *" name added must be unique for a given
30 // C string value. ConstString::GetCString() can provide such strings.
31 // Any other string table that has guaranteed unique values can also be used.
32 //----------------------------------------------------------------------
33 template <typename T> class UniqueCStringMap {
38 Entry(ConstString cstr) : cstring(cstr), value() {}
40 Entry(ConstString cstr, const T &v) : cstring(cstr), value(v) {}
42 // This is only for uniqueness, not lexicographical ordering, so we can
43 // just compare pointers.
44 bool operator<(const Entry &rhs) const {
45 return cstring.GetCString() < rhs.cstring.GetCString();
52 //------------------------------------------------------------------
53 // Call this function multiple times to add a bunch of entries to this map,
54 // then later call UniqueCStringMap<T>::Sort() before doing any searches by
56 //------------------------------------------------------------------
57 void Append(ConstString unique_cstr, const T &value) {
58 m_map.push_back(typename UniqueCStringMap<T>::Entry(unique_cstr, value));
61 void Append(const Entry &e) { m_map.push_back(e); }
63 void Clear() { m_map.clear(); }
65 //------------------------------------------------------------------
66 // Call this function to always keep the map sorted when putting entries into
68 //------------------------------------------------------------------
69 void Insert(ConstString unique_cstr, const T &value) {
70 typename UniqueCStringMap<T>::Entry e(unique_cstr, value);
71 m_map.insert(std::upper_bound(m_map.begin(), m_map.end(), e), e);
74 void Insert(const Entry &e) {
75 m_map.insert(std::upper_bound(m_map.begin(), m_map.end(), e), e);
78 //------------------------------------------------------------------
79 // Get an entries by index in a variety of forms.
81 // The caller is responsible for ensuring that the collection does not change
82 // during while using the returned values.
83 //------------------------------------------------------------------
84 bool GetValueAtIndex(uint32_t idx, T &value) const {
85 if (idx < m_map.size()) {
86 value = m_map[idx].value;
92 ConstString GetCStringAtIndexUnchecked(uint32_t idx) const {
93 return m_map[idx].cstring;
96 // Use this function if you have simple types in your map that you can easily
97 // copy when accessing values by index.
98 T GetValueAtIndexUnchecked(uint32_t idx) const { return m_map[idx].value; }
100 // Use this function if you have complex types in your map that you don't
101 // want to copy when accessing values by index.
102 const T &GetValueRefAtIndexUnchecked(uint32_t idx) const {
103 return m_map[idx].value;
106 ConstString GetCStringAtIndex(uint32_t idx) const {
107 return ((idx < m_map.size()) ? m_map[idx].cstring : ConstString());
110 //------------------------------------------------------------------
111 // Find the value for the unique string in the map.
113 // Return the value for \a unique_cstr if one is found, return \a fail_value
114 // otherwise. This method works well for simple type
115 // T values and only if there is a sensible failure value that can
116 // be returned and that won't match any existing values.
117 //------------------------------------------------------------------
118 T Find(ConstString unique_cstr, T fail_value) const {
119 Entry search_entry(unique_cstr);
120 const_iterator end = m_map.end();
121 const_iterator pos = std::lower_bound(m_map.begin(), end, search_entry);
123 if (pos->cstring == unique_cstr)
129 //------------------------------------------------------------------
130 // Get a pointer to the first entry that matches "name". nullptr will be
131 // returned if there is no entry that matches "name".
133 // The caller is responsible for ensuring that the collection does not change
134 // during while using the returned pointer.
135 //------------------------------------------------------------------
136 const Entry *FindFirstValueForName(ConstString unique_cstr) const {
137 Entry search_entry(unique_cstr);
138 const_iterator end = m_map.end();
139 const_iterator pos = std::lower_bound(m_map.begin(), end, search_entry);
140 if (pos != end && pos->cstring == unique_cstr)
145 //------------------------------------------------------------------
146 // Get a pointer to the next entry that matches "name" from a previously
147 // returned Entry pointer. nullptr will be returned if there is no subsequent
148 // entry that matches "name".
150 // The caller is responsible for ensuring that the collection does not change
151 // during while using the returned pointer.
152 //------------------------------------------------------------------
153 const Entry *FindNextValueForName(const Entry *entry_ptr) const {
154 if (!m_map.empty()) {
155 const Entry *first_entry = &m_map[0];
156 const Entry *after_last_entry = first_entry + m_map.size();
157 const Entry *next_entry = entry_ptr + 1;
158 if (first_entry <= next_entry && next_entry < after_last_entry) {
159 if (next_entry->cstring == entry_ptr->cstring)
166 size_t GetValues(ConstString unique_cstr, std::vector<T> &values) const {
167 const size_t start_size = values.size();
169 Entry search_entry(unique_cstr);
170 const_iterator pos, end = m_map.end();
171 for (pos = std::lower_bound(m_map.begin(), end, search_entry); pos != end;
173 if (pos->cstring == unique_cstr)
174 values.push_back(pos->value);
179 return values.size() - start_size;
182 size_t GetValues(const RegularExpression ®ex,
183 std::vector<T> &values) const {
184 const size_t start_size = values.size();
186 const_iterator pos, end = m_map.end();
187 for (pos = m_map.begin(); pos != end; ++pos) {
188 if (regex.Execute(pos->cstring.GetCString()))
189 values.push_back(pos->value);
192 return values.size() - start_size;
195 //------------------------------------------------------------------
196 // Get the total number of entries in this map.
197 //------------------------------------------------------------------
198 size_t GetSize() const { return m_map.size(); }
200 //------------------------------------------------------------------
201 // Returns true if this map is empty.
202 //------------------------------------------------------------------
203 bool IsEmpty() const { return m_map.empty(); }
205 //------------------------------------------------------------------
206 // Reserve memory for at least "n" entries in the map. This is useful to call
207 // when you know you will be adding a lot of entries using
208 // UniqueCStringMap::Append() (which should be followed by a call to
209 // UniqueCStringMap::Sort()) or to UniqueCStringMap::Insert().
210 //------------------------------------------------------------------
211 void Reserve(size_t n) { m_map.reserve(n); }
213 //------------------------------------------------------------------
214 // Sort the unsorted contents in this map. A typical code flow would be:
215 // size_t approximate_num_entries = ....
216 // UniqueCStringMap<uint32_t> my_map;
217 // my_map.Reserve (approximate_num_entries);
220 // my_map.Append (UniqueCStringMap::Entry(GetName(...), GetValue(...)));
223 //------------------------------------------------------------------
224 void Sort() { std::sort(m_map.begin(), m_map.end()); }
226 //------------------------------------------------------------------
227 // Since we are using a vector to contain our items it will always double its
228 // memory consumption as things are added to the vector, so if you intend to
229 // keep a UniqueCStringMap around and have a lot of entries in the map, you
230 // will want to call this function to create a new vector and copy _only_ the
231 // exact size needed as part of the finalization of the string map.
232 //------------------------------------------------------------------
234 if (m_map.size() < m_map.capacity()) {
235 collection temp(m_map.begin(), m_map.end());
240 size_t Erase(ConstString unique_cstr) {
241 size_t num_removed = 0;
242 Entry search_entry(unique_cstr);
243 iterator end = m_map.end();
244 iterator begin = m_map.begin();
245 iterator lower_pos = std::lower_bound(begin, end, search_entry);
246 if (lower_pos != end) {
247 if (lower_pos->cstring == unique_cstr) {
248 iterator upper_pos = std::upper_bound(lower_pos, end, search_entry);
249 if (lower_pos == upper_pos) {
250 m_map.erase(lower_pos);
253 num_removed = std::distance(lower_pos, upper_pos);
254 m_map.erase(lower_pos, upper_pos);
262 typedef std::vector<Entry> collection;
263 typedef typename collection::iterator iterator;
264 typedef typename collection::const_iterator const_iterator;
268 } // namespace lldb_private
270 #endif // liblldb_UniqueCStringMap_h_