StringSet.h 1.8 KB

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