StringSet.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // StringSet - A set-like wrapper for the StringMap.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_ADT_STRINGSET_H
  13. #define LLVM_ADT_STRINGSET_H
  14. #include "llvm/ADT/StringMap.h"
  15. namespace llvm {
  16. /// StringSet - A wrapper for StringMap that provides set-like functionality.
  17. template <class AllocatorTy = MallocAllocator>
  18. class StringSet : public StringMap<NoneType, AllocatorTy> {
  19. using Base = StringMap<NoneType, AllocatorTy>;
  20. public:
  21. StringSet() = default;
  22. StringSet(std::initializer_list<StringRef> initializer) {
  23. for (StringRef str : initializer)
  24. insert(str);
  25. }
  26. explicit StringSet(AllocatorTy a) : Base(a) {}
  27. std::pair<typename Base::iterator, bool> insert(StringRef key) {
  28. return Base::try_emplace(key);
  29. }
  30. template <typename InputIt>
  31. void insert(const InputIt &begin, const InputIt &end) {
  32. for (auto it = begin; it != end; ++it)
  33. insert(*it);
  34. }
  35. template <typename ValueTy>
  36. std::pair<typename Base::iterator, bool>
  37. insert(const StringMapEntry<ValueTy> &mapEntry) {
  38. return insert(mapEntry.getKey());
  39. }
  40. /// Check if the set contains the given \c key.
  41. bool contains(StringRef key) const { return Base::FindKey(key) != -1; }
  42. };
  43. } // end namespace llvm
  44. #endif // LLVM_ADT_STRINGSET_H