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
29 // type T. 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
33 //----------------------------------------------------------------------
34 template <typename T> class UniqueCStringMap {
39 Entry(ConstString cstr) : cstring(cstr), value() {}
41 Entry(ConstString cstr, const T &v) : cstring(cstr), value(v) {}
43 // This is only for uniqueness, not lexicographical ordering, so we can
44 // just compare pointers.
45 bool operator<(const Entry &rhs) const {
46 return cstring.GetCString() < rhs.cstring.GetCString();
53 //------------------------------------------------------------------
54 // Call this function multiple times to add a bunch of entries to
55 // this map, then later call UniqueCStringMap<T>::Sort() before doing
56 // any searches by name.
57 //------------------------------------------------------------------
58 void Append(ConstString unique_cstr, const T &value) {
59 m_map.push_back(typename UniqueCStringMap<T>::Entry(unique_cstr, value));
62 void Append(const Entry &e) { m_map.push_back(e); }
64 void Clear() { m_map.clear(); }
66 //------------------------------------------------------------------
67 // Call this function to always keep the map sorted when putting
68 // entries into the map.
69 //------------------------------------------------------------------
70 void Insert(ConstString unique_cstr, const T &value) {
71 typename UniqueCStringMap<T>::Entry e(unique_cstr, value);
72 m_map.insert(std::upper_bound(m_map.begin(), m_map.end(), e), e);
75 void Insert(const Entry &e) {
76 m_map.insert(std::upper_bound(m_map.begin(), m_map.end(), e), e);
79 //------------------------------------------------------------------
80 // Get an entries by index in a variety of forms.
82 // The caller is responsible for ensuring that the collection does
83 // not change during while using the returned values.
84 //------------------------------------------------------------------
85 bool GetValueAtIndex(uint32_t idx, T &value) const {
86 if (idx < m_map.size()) {
87 value = m_map[idx].value;
93 ConstString GetCStringAtIndexUnchecked(uint32_t idx) const {
94 return m_map[idx].cstring;
97 // Use this function if you have simple types in your map that you
98 // can easily copy when accessing values by index.
99 T GetValueAtIndexUnchecked(uint32_t idx) const { return m_map[idx].value; }
101 // Use this function if you have complex types in your map that you
102 // don't want to copy when accessing values by index.
103 const T &GetValueRefAtIndexUnchecked(uint32_t idx) const {
104 return m_map[idx].value;
107 ConstString GetCStringAtIndex(uint32_t idx) const {
108 return ((idx < m_map.size()) ? m_map[idx].cstring : ConstString());
111 //------------------------------------------------------------------
112 // Find the value for the unique string in the map.
114 // Return the value for \a unique_cstr if one is found, return
115 // \a fail_value otherwise. This method works well for simple type
116 // T values and only if there is a sensible failure value that can
117 // be returned and that won't match any existing values.
118 //------------------------------------------------------------------
119 T Find(ConstString unique_cstr, T fail_value) const {
120 Entry search_entry(unique_cstr);
121 const_iterator end = m_map.end();
122 const_iterator pos = std::lower_bound(m_map.begin(), end, search_entry);
124 if (pos->cstring == unique_cstr)
130 //------------------------------------------------------------------
131 // Get a pointer to the first entry that matches "name". nullptr will
132 // be returned if there is no entry that matches "name".
134 // The caller is responsible for ensuring that the collection does
135 // not change during while using the returned pointer.
136 //------------------------------------------------------------------
137 const Entry *FindFirstValueForName(ConstString unique_cstr) const {
138 Entry search_entry(unique_cstr);
139 const_iterator end = m_map.end();
140 const_iterator pos = std::lower_bound(m_map.begin(), end, search_entry);
141 if (pos != end && pos->cstring == unique_cstr)
146 //------------------------------------------------------------------
147 // Get a pointer to the next entry that matches "name" from a
148 // previously returned Entry pointer. nullptr will be returned if there
149 // is no subsequent entry that matches "name".
151 // The caller is responsible for ensuring that the collection does
152 // not change during while using the returned pointer.
153 //------------------------------------------------------------------
154 const Entry *FindNextValueForName(const Entry *entry_ptr) const {
155 if (!m_map.empty()) {
156 const Entry *first_entry = &m_map[0];
157 const Entry *after_last_entry = first_entry + m_map.size();
158 const Entry *next_entry = entry_ptr + 1;
159 if (first_entry <= next_entry && next_entry < after_last_entry) {
160 if (next_entry->cstring == entry_ptr->cstring)
167 size_t GetValues(ConstString unique_cstr, std::vector<T> &values) const {
168 const size_t start_size = values.size();
170 Entry search_entry(unique_cstr);
171 const_iterator pos, end = m_map.end();
172 for (pos = std::lower_bound(m_map.begin(), end, search_entry); pos != end;
174 if (pos->cstring == unique_cstr)
175 values.push_back(pos->value);
180 return values.size() - start_size;
183 size_t GetValues(const RegularExpression ®ex,
184 std::vector<T> &values) const {
185 const size_t start_size = values.size();
187 const_iterator pos, end = m_map.end();
188 for (pos = m_map.begin(); pos != end; ++pos) {
189 if (regex.Execute(pos->cstring.GetCString()))
190 values.push_back(pos->value);
193 return values.size() - start_size;
196 //------------------------------------------------------------------
197 // Get the total number of entries in this map.
198 //------------------------------------------------------------------
199 size_t GetSize() const { return m_map.size(); }
201 //------------------------------------------------------------------
202 // Returns true if this map is empty.
203 //------------------------------------------------------------------
204 bool IsEmpty() const { return m_map.empty(); }
206 //------------------------------------------------------------------
207 // Reserve memory for at least "n" entries in the map. This is
208 // useful to call when you know you will be adding a lot of entries
209 // using UniqueCStringMap::Append() (which should be followed by a
210 // call to UniqueCStringMap::Sort()) or to UniqueCStringMap::Insert().
211 //------------------------------------------------------------------
212 void Reserve(size_t n) { m_map.reserve(n); }
214 //------------------------------------------------------------------
215 // Sort the unsorted contents in this map. A typical code flow would
217 // size_t approximate_num_entries = ....
218 // UniqueCStringMap<uint32_t> my_map;
219 // my_map.Reserve (approximate_num_entries);
222 // my_map.Append (UniqueCStringMap::Entry(GetName(...), GetValue(...)));
225 //------------------------------------------------------------------
226 void Sort() { std::sort(m_map.begin(), m_map.end()); }
228 //------------------------------------------------------------------
229 // Since we are using a vector to contain our items it will always
230 // double its memory consumption as things are added to the vector,
231 // so if you intend to keep a UniqueCStringMap around and have
232 // a lot of entries in the map, you will want to call this function
233 // to create a new vector and copy _only_ the exact size needed as
234 // part of the finalization of the string map.
235 //------------------------------------------------------------------
237 if (m_map.size() < m_map.capacity()) {
238 collection temp(m_map.begin(), m_map.end());
243 size_t Erase(ConstString unique_cstr) {
244 size_t num_removed = 0;
245 Entry search_entry(unique_cstr);
246 iterator end = m_map.end();
247 iterator begin = m_map.begin();
248 iterator lower_pos = std::lower_bound(begin, end, search_entry);
249 if (lower_pos != end) {
250 if (lower_pos->cstring == unique_cstr) {
251 iterator upper_pos = std::upper_bound(lower_pos, end, search_entry);
252 if (lower_pos == upper_pos) {
253 m_map.erase(lower_pos);
256 num_removed = std::distance(lower_pos, upper_pos);
257 m_map.erase(lower_pos, upper_pos);
265 typedef std::vector<Entry> collection;
266 typedef typename collection::iterator iterator;
267 typedef typename collection::const_iterator const_iterator;
271 } // namespace lldb_private
273 #endif // liblldb_UniqueCStringMap_h_