xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ADT/StringSet.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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(Container && C)32   template <typename Container> explicit StringSet(Container &&C) {
33     for (auto &&Str : C)
34       insert(Str);
35   }
StringSet(AllocatorTy a)36   explicit StringSet(AllocatorTy a) : Base(a) {}
37 
insert(StringRef key)38   std::pair<typename Base::iterator, bool> insert(StringRef key) {
39     return Base::try_emplace(key);
40   }
41 
42   template <typename InputIt>
insert(InputIt begin,InputIt end)43   void insert(InputIt begin, InputIt end) {
44     for (auto it = begin; it != end; ++it)
45       insert(*it);
46   }
47 
48   template <typename ValueTy>
49   std::pair<typename Base::iterator, bool>
insert(const StringMapEntry<ValueTy> & mapEntry)50   insert(const StringMapEntry<ValueTy> &mapEntry) {
51     return insert(mapEntry.getKey());
52   }
53 
54   /// Check if the set contains the given \c key.
contains(StringRef key)55   bool contains(StringRef key) const { return Base::FindKey(key) != -1; }
56 };
57 
58 } // end namespace llvm
59 
60 #endif // LLVM_ADT_STRINGSET_H
61