StringSet.h 1.8 KB

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