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/ADL.h" 18 #include "llvm/ADT/STLForwardCompat.h" 19 #include "llvm/ADT/StringMap.h" 20 21 namespace llvm { 22 23 /// StringSet - A wrapper for StringMap that provides set-like functionality. 24 template <class AllocatorTy = MallocAllocator> 25 class StringSet : public StringMap<std::nullopt_t, AllocatorTy> { 26 using Base = StringMap<std::nullopt_t, AllocatorTy>; 27 28 public: 29 StringSet() = default; StringSet(std::initializer_list<StringRef> initializer)30 StringSet(std::initializer_list<StringRef> initializer) { 31 for (StringRef str : initializer) 32 insert(str); 33 } StringSet(llvm::from_range_t,Range && R)34 template <typename Range> StringSet(llvm::from_range_t, Range &&R) { 35 insert(adl_begin(R), adl_end(R)); 36 } StringSet(AllocatorTy a)37 explicit StringSet(AllocatorTy a) : Base(a) {} 38 insert(StringRef key)39 std::pair<typename Base::iterator, bool> insert(StringRef key) { 40 return Base::try_emplace(key); 41 } 42 43 template <typename InputIt> insert(InputIt begin,InputIt end)44 void insert(InputIt begin, InputIt end) { 45 for (auto it = begin; it != end; ++it) 46 insert(*it); 47 } 48 insert_range(Range && R)49 template <typename Range> void insert_range(Range &&R) { 50 insert(adl_begin(R), adl_end(R)); 51 } 52 53 template <typename ValueTy> 54 std::pair<typename Base::iterator, bool> insert(const StringMapEntry<ValueTy> & mapEntry)55 insert(const StringMapEntry<ValueTy> &mapEntry) { 56 return insert(mapEntry.getKey()); 57 } 58 59 /// Check if the set contains the given \c key. contains(StringRef key)60 bool contains(StringRef key) const { return Base::FindKey(key) != -1; } 61 }; 62 63 } // end namespace llvm 64 65 #endif // LLVM_ADT_STRINGSET_H 66