]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/UniqueCStringMap.h
Merge CK as of commit 24d26965d1a28039062ba3bcf9433b623f3d2c5e, to get
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / UniqueCStringMap.h
1 //===-- UniqueCStringMap.h --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_UniqueCStringMap_h_
11 #define liblldb_UniqueCStringMap_h_
12
13 // C Includes
14 // C++ Includes
15 #include <algorithm>
16 #include <vector>
17
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/Core/RegularExpression.h"
21
22 namespace lldb_private {
23
24 //----------------------------------------------------------------------
25 // Templatized uniqued string map.
26 //
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
31 // be used.
32 //----------------------------------------------------------------------
33 template <typename T>
34 class UniqueCStringMap
35 {
36 public:
37     struct Entry
38     {
39         Entry () :
40             cstring(nullptr),
41             value()
42         {
43         }
44
45         Entry (const char *cstr) :
46             cstring(cstr),
47             value()
48         {
49         }
50
51         Entry (const char *cstr, const T&v) :
52             cstring(cstr),
53             value(v)
54         {
55         }
56
57         bool
58         operator < (const Entry& rhs) const
59         {
60             return cstring < rhs.cstring;
61         }
62
63         const char* cstring;
64         T value;
65     };
66
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     //------------------------------------------------------------------
72     void
73     Append (const char *unique_cstr, const T& value)
74     {
75         m_map.push_back (typename UniqueCStringMap<T>::Entry(unique_cstr, value));
76     }
77
78     void
79     Append (const Entry &e)
80     {
81         m_map.push_back (e);
82     }
83
84     void
85     Clear ()
86     {
87         m_map.clear();
88     }
89
90     //------------------------------------------------------------------
91     // Call this function to always keep the map sorted when putting
92     // entries into the map.
93     //------------------------------------------------------------------
94     void
95     Insert (const char *unique_cstr, const T& value)
96     {
97         typename UniqueCStringMap<T>::Entry e(unique_cstr, value);
98         m_map.insert (std::upper_bound (m_map.begin(), m_map.end(), e), e);
99     }
100
101     void
102     Insert (const Entry &e)
103     {
104         m_map.insert (std::upper_bound (m_map.begin(), m_map.end(), e), e);
105     }
106
107     //------------------------------------------------------------------
108     // Get an entries by index in a variety of forms.
109     //
110     // The caller is responsible for ensuring that the collection does
111     // not change during while using the returned values.
112     //------------------------------------------------------------------
113     bool
114     GetValueAtIndex (uint32_t idx, T &value) const
115     {
116         if (idx < m_map.size())
117         {
118             value = m_map[idx].value;
119             return true;
120         }
121         return false;
122     }
123
124     const char *
125     GetCStringAtIndexUnchecked (uint32_t idx) const
126     {
127         return m_map[idx].cstring;
128     }
129     
130     // Use this function if you have simple types in your map that you
131     // can easily copy when accessing values by index.
132     T
133     GetValueAtIndexUnchecked (uint32_t idx) const
134     {
135         return m_map[idx].value;        
136     }
137
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.
140     const T &
141     GetValueRefAtIndexUnchecked (uint32_t idx) const
142     {
143         return m_map[idx].value;
144     }
145
146     const char *
147     GetCStringAtIndex (uint32_t idx) const
148     {
149         return ((idx < m_map.size()) ? m_map[idx].cstring : nullptr);
150     }
151
152     //------------------------------------------------------------------
153     // Find the value for the unique string in the map.
154     //
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     //------------------------------------------------------------------
160     T
161     Find (const char *unique_cstr, T fail_value) const
162     {
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);
166         if (pos != end)
167         {
168             if (pos->cstring == unique_cstr)
169                 return pos->value;
170         }
171         return fail_value;
172     }
173
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".
177     //
178     // The caller is responsible for ensuring that the collection does
179     // not change during while using the returned pointer.
180     //------------------------------------------------------------------
181     const Entry *
182     FindFirstValueForName (const char *unique_cstr) const
183     {
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);
187         if (pos != end)
188         {
189             const char *pos_cstr = pos->cstring;
190             if (pos_cstr == unique_cstr)
191                 return &(*pos);
192         }
193         return nullptr;
194     }
195
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".
200     //
201     // The caller is responsible for ensuring that the collection does
202     // not change during while using the returned pointer.
203     //------------------------------------------------------------------
204     const Entry *
205     FindNextValueForName (const Entry *entry_ptr) const
206     {
207         if (!m_map.empty())
208         {
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)
213             {
214                 if (next_entry->cstring == entry_ptr->cstring)
215                     return next_entry;
216             }
217         }
218         return nullptr;
219     }
220
221     size_t
222     GetValues (const char *unique_cstr, std::vector<T> &values) const
223     {
224         const size_t start_size = values.size();
225
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)
229         {
230             if (pos->cstring == unique_cstr)
231                 values.push_back (pos->value);
232             else
233                 break;
234         }
235
236         return values.size() - start_size;
237     }
238     
239     size_t
240     GetValues (const RegularExpression& regex, std::vector<T> &values) const
241     {
242         const size_t start_size = values.size();
243
244         const_iterator pos, end = m_map.end();
245         for (pos = m_map.begin(); pos != end; ++pos)
246         {
247             if (regex.Execute(pos->cstring))
248                 values.push_back (pos->value);
249         }
250
251         return values.size() - start_size;
252     }
253
254     //------------------------------------------------------------------
255     // Get the total number of entries in this map.
256     //------------------------------------------------------------------
257     size_t
258     GetSize () const
259     {
260         return m_map.size();
261     }
262
263     //------------------------------------------------------------------
264     // Returns true if this map is empty.
265     //------------------------------------------------------------------
266     bool
267     IsEmpty() const
268     {
269         return m_map.empty();
270     }
271
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     //------------------------------------------------------------------
278     void
279     Reserve (size_t n)
280     {
281         m_map.reserve (n);
282     }
283
284     //------------------------------------------------------------------
285     // Sort the unsorted contents in this map. A typical code flow would
286     // be:
287     // size_t approximate_num_entries = ....
288     // UniqueCStringMap<uint32_t> my_map;
289     // my_map.Reserve (approximate_num_entries);
290     // for (...)
291     // {
292     //      my_map.Append (UniqueCStringMap::Entry(GetName(...), GetValue(...)));
293     // }
294     // my_map.Sort();
295     //------------------------------------------------------------------
296     void
297     Sort ()
298     {
299         std::sort (m_map.begin(), m_map.end());
300     }
301     
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     //------------------------------------------------------------------
310     void
311     SizeToFit ()
312     {
313         if (m_map.size() < m_map.capacity())
314         {
315             collection temp (m_map.begin(), m_map.end());
316             m_map.swap(temp);
317         }
318     }
319
320     size_t
321     Erase (const char *unique_cstr)
322     {
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)
329         {
330             if (lower_pos->cstring == unique_cstr)
331             {
332                 iterator upper_pos = std::upper_bound (lower_pos, end, search_entry);
333                 if (lower_pos == upper_pos)
334                 {
335                     m_map.erase (lower_pos);
336                     num_removed = 1;
337                 }
338                 else
339                 {
340                     num_removed = std::distance (lower_pos, upper_pos);
341                     m_map.erase (lower_pos, upper_pos);
342                 }
343             }
344         }
345         return num_removed;
346     }
347
348 protected:
349     typedef std::vector<Entry> collection;
350     typedef typename collection::iterator iterator;
351     typedef typename collection::const_iterator const_iterator;
352     collection m_map;
353 };
354
355 } // namespace lldb_private
356
357 #endif // liblldb_UniqueCStringMap_h_