1 //===- StringSet.h - An efficient set built on StringMap --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 ///  StringSet - A set-like wrapper for the StringMap.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ADT_STRINGSET_H
15 #define LLVM_ADT_STRINGSET_H
16 
17 #include "llvm/ADT/StringMap.h"
18 
19 namespace llvm {
20 
21 /// StringSet - A wrapper for StringMap that provides set-like functionality.
22 template <class AllocatorTy = MallocAllocator>
23 class StringSet : public StringMap<std::nullopt_t, AllocatorTy> {
24   using Base = StringMap<std::nullopt_t, AllocatorTy>;
25 
26 public:
27   StringSet() = default;
StringSet(std::initializer_list<StringRef> initializer)28   StringSet(std::initializer_list<StringRef> initializer) {
29     for (StringRef str : initializer)
30       insert(str);
31   }
StringSet(AllocatorTy a)32   explicit StringSet(AllocatorTy a) : Base(a) {}
33 
insert(StringRef key)34   std::pair<typename Base::iterator, bool> insert(StringRef key) {
35     return Base::try_emplace(key);
36   }
37 
38   template <typename InputIt>
insert(InputIt begin,InputIt end)39   void insert(InputIt begin, InputIt end) {
40     for (auto it = begin; it != end; ++it)
41       insert(*it);
42   }
43 
44   template <typename ValueTy>
45   std::pair<typename Base::iterator, bool>
insert(const StringMapEntry<ValueTy> & mapEntry)46   insert(const StringMapEntry<ValueTy> &mapEntry) {
47     return insert(mapEntry.getKey());
48   }
49 
50   /// Check if the set contains the given \c key.
contains(StringRef key)51   bool contains(StringRef key) const { return Base::FindKey(key) != -1; }
52 };
53 
54 } // end namespace llvm
55 
56 #endif // LLVM_ADT_STRINGSET_H
57