Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 1 | //===- StringSet.h - An efficient set built on StringMap --------*- C++ -*-===// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // StringSet - A set-like wrapper for the StringMap. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_ADT_STRINGSET_H |
| 14 | #define LLVM_ADT_STRINGSET_H |
| 15 | |
| 16 | #include "llvm/ADT/StringMap.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | |
| 18 | namespace llvm { |
| 19 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 20 | /// StringSet - A wrapper for StringMap that provides set-like functionality. |
| 21 | template <class AllocatorTy = MallocAllocator> |
| 22 | class StringSet : public StringMap<NoneType, AllocatorTy> { |
| 23 | using Base = StringMap<NoneType, AllocatorTy>; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 32 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 33 | std::pair<typename Base::iterator, bool> insert(StringRef key) { |
| 34 | return Base::try_emplace(key); |
| 35 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 36 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 37 | template <typename InputIt> |
| 38 | void insert(const InputIt &begin, const InputIt &end) { |
| 39 | for (auto it = begin; it != end; ++it) |
| 40 | insert(*it); |
| 41 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 42 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 43 | template <typename ValueTy> |
| 44 | std::pair<typename Base::iterator, bool> |
| 45 | insert(const StringMapEntry<ValueTy> &mapEntry) { |
| 46 | return insert(mapEntry.getKey()); |
| 47 | } |
| 48 | |
| 49 | /// Check if the set contains the given \c key. |
| 50 | bool contains(StringRef key) const { return Base::FindKey(key) != -1; } |
| 51 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | |
| 53 | } // end namespace llvm |
| 54 | |
| 55 | #endif // LLVM_ADT_STRINGSET_H |