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/Core/RegularExpression.h"
22 namespace lldb_private {
24 //----------------------------------------------------------------------
25 // Templatized uniqued string map.
27 // This map is useful for mapping unique C string names to values of
28 // type T. Each "const char *" name added must be unique for a given
29 // C string value. ConstString::GetCString() can provide such strings.
30 // Any other string table that has guaranteed unique values can also
32 //----------------------------------------------------------------------
34 class UniqueCStringMap
45 Entry (const char *cstr) :
51 Entry (const char *cstr, const T&v) :
58 operator < (const Entry& rhs) const
60 return cstring < rhs.cstring;
67 //------------------------------------------------------------------
68 // Call this function multiple times to add a bunch of entries to
69 // this map, then later call UniqueCStringMap<T>::Sort() before doing
70 // any searches by name.
71 //------------------------------------------------------------------
73 Append (const char *unique_cstr, const T& value)
75 m_map.push_back (typename UniqueCStringMap<T>::Entry(unique_cstr, value));
79 Append (const Entry &e)
90 //------------------------------------------------------------------
91 // Call this function to always keep the map sorted when putting
92 // entries into the map.
93 //------------------------------------------------------------------
95 Insert (const char *unique_cstr, const T& value)
97 typename UniqueCStringMap<T>::Entry e(unique_cstr, value);
98 m_map.insert (std::upper_bound (m_map.begin(), m_map.end(), e), e);
102 Insert (const Entry &e)
104 m_map.insert (std::upper_bound (m_map.begin(), m_map.end(), e), e);
107 //------------------------------------------------------------------
108 // Get an entries by index in a variety of forms.
110 // The caller is responsible for ensuring that the collection does
111 // not change during while using the returned values.
112 //------------------------------------------------------------------
114 GetValueAtIndex (uint32_t idx, T &value) const
116 if (idx < m_map.size())
118 value = m_map[idx].value;
125 GetCStringAtIndexUnchecked (uint32_t idx) const
127 return m_map[idx].cstring;
130 // Use this function if you have simple types in your map that you
131 // can easily copy when accessing values by index.
133 GetValueAtIndexUnchecked (uint32_t idx) const
135 return m_map[idx].value;
138 // Use this function if you have complex types in your map that you
139 // don't want to copy when accessing values by index.
141 GetValueRefAtIndexUnchecked (uint32_t idx) const
143 return m_map[idx].value;
147 GetCStringAtIndex (uint32_t idx) const
149 return ((idx < m_map.size()) ? m_map[idx].cstring : nullptr);
152 //------------------------------------------------------------------
153 // Find the value for the unique string in the map.
155 // Return the value for \a unique_cstr if one is found, return
156 // \a fail_value otherwise. This method works well for simple type
157 // T values and only if there is a sensible failure value that can
158 // be returned and that won't match any existing values.
159 //------------------------------------------------------------------
161 Find (const char *unique_cstr, T fail_value) const
163 Entry search_entry (unique_cstr);
164 const_iterator end = m_map.end();
165 const_iterator pos = std::lower_bound (m_map.begin(), end, search_entry);
168 if (pos->cstring == unique_cstr)
174 //------------------------------------------------------------------
175 // Get a pointer to the first entry that matches "name". nullptr will
176 // be returned if there is no entry that matches "name".
178 // The caller is responsible for ensuring that the collection does
179 // not change during while using the returned pointer.
180 //------------------------------------------------------------------
182 FindFirstValueForName (const char *unique_cstr) const
184 Entry search_entry (unique_cstr);
185 const_iterator end = m_map.end();
186 const_iterator pos = std::lower_bound (m_map.begin(), end, search_entry);
189 const char *pos_cstr = pos->cstring;
190 if (pos_cstr == unique_cstr)
196 //------------------------------------------------------------------
197 // Get a pointer to the next entry that matches "name" from a
198 // previously returned Entry pointer. nullptr will be returned if there
199 // is no subsequent entry that matches "name".
201 // The caller is responsible for ensuring that the collection does
202 // not change during while using the returned pointer.
203 //------------------------------------------------------------------
205 FindNextValueForName (const Entry *entry_ptr) const
209 const Entry *first_entry = &m_map[0];
210 const Entry *after_last_entry = first_entry + m_map.size();
211 const Entry *next_entry = entry_ptr + 1;
212 if (first_entry <= next_entry && next_entry < after_last_entry)
214 if (next_entry->cstring == entry_ptr->cstring)
222 GetValues (const char *unique_cstr, std::vector<T> &values) const
224 const size_t start_size = values.size();
226 Entry search_entry (unique_cstr);
227 const_iterator pos, end = m_map.end();
228 for (pos = std::lower_bound (m_map.begin(), end, search_entry); pos != end; ++pos)
230 if (pos->cstring == unique_cstr)
231 values.push_back (pos->value);
236 return values.size() - start_size;
240 GetValues (const RegularExpression& regex, std::vector<T> &values) const
242 const size_t start_size = values.size();
244 const_iterator pos, end = m_map.end();
245 for (pos = m_map.begin(); pos != end; ++pos)
247 if (regex.Execute(pos->cstring))
248 values.push_back (pos->value);
251 return values.size() - start_size;
254 //------------------------------------------------------------------
255 // Get the total number of entries in this map.
256 //------------------------------------------------------------------
263 //------------------------------------------------------------------
264 // Returns true if this map is empty.
265 //------------------------------------------------------------------
269 return m_map.empty();
272 //------------------------------------------------------------------
273 // Reserve memory for at least "n" entries in the map. This is
274 // useful to call when you know you will be adding a lot of entries
275 // using UniqueCStringMap::Append() (which should be followed by a
276 // call to UniqueCStringMap::Sort()) or to UniqueCStringMap::Insert().
277 //------------------------------------------------------------------
284 //------------------------------------------------------------------
285 // Sort the unsorted contents in this map. A typical code flow would
287 // size_t approximate_num_entries = ....
288 // UniqueCStringMap<uint32_t> my_map;
289 // my_map.Reserve (approximate_num_entries);
292 // my_map.Append (UniqueCStringMap::Entry(GetName(...), GetValue(...)));
295 //------------------------------------------------------------------
299 std::sort (m_map.begin(), m_map.end());
302 //------------------------------------------------------------------
303 // Since we are using a vector to contain our items it will always
304 // double its memory consumption as things are added to the vector,
305 // so if you intend to keep a UniqueCStringMap around and have
306 // a lot of entries in the map, you will want to call this function
307 // to create a new vector and copy _only_ the exact size needed as
308 // part of the finalization of the string map.
309 //------------------------------------------------------------------
313 if (m_map.size() < m_map.capacity())
315 collection temp (m_map.begin(), m_map.end());
321 Erase (const char *unique_cstr)
323 size_t num_removed = 0;
324 Entry search_entry (unique_cstr);
325 iterator end = m_map.end();
326 iterator begin = m_map.begin();
327 iterator lower_pos = std::lower_bound (begin, end, search_entry);
328 if (lower_pos != end)
330 if (lower_pos->cstring == unique_cstr)
332 iterator upper_pos = std::upper_bound (lower_pos, end, search_entry);
333 if (lower_pos == upper_pos)
335 m_map.erase (lower_pos);
340 num_removed = std::distance (lower_pos, upper_pos);
341 m_map.erase (lower_pos, upper_pos);
349 typedef std::vector<Entry> collection;
350 typedef typename collection::iterator iterator;
351 typedef typename collection::const_iterator const_iterator;
355 } // namespace lldb_private
357 #endif // liblldb_UniqueCStringMap_h_