LLVM 18.0.0git
StringSet.h
Go to the documentation of this file.
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/StringMap.h"
18
19namespace llvm {
20
21/// StringSet - A wrapper for StringMap that provides set-like functionality.
22template <class AllocatorTy = MallocAllocator>
23class StringSet : public StringMap<std::nullopt_t, AllocatorTy> {
25
26public:
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
34 std::pair<typename Base::iterator, bool> insert(StringRef key) {
35 return Base::try_emplace(key);
36 }
37
38 template <typename InputIt>
39 void insert(InputIt begin, InputIt end) {
40 for (auto it = begin; it != end; ++it)
41 insert(*it);
42 }
43
44 template <typename ValueTy>
45 std::pair<typename Base::iterator, bool>
47 return insert(mapEntry.getKey());
48 }
49
50 /// Check if the set contains the given \c key.
51 bool contains(StringRef key) const { return Base::FindKey(key) != -1; }
52};
53
54} // end namespace llvm
55
56#endif // LLVM_ADT_STRINGSET_H
This file defines the StringMap class.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef getKey() const
int FindKey(StringRef Key) const
FindKey - Look up the bucket that contains the specified key.
Definition: StringMap.cpp:142
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:112
iterator end()
Definition: StringMap.h:205
iterator begin()
Definition: StringMap.h:204
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:341
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
StringSet()=default
StringSet(AllocatorTy a)
Definition: StringSet.h:32
std::pair< typename Base::iterator, bool > insert(const StringMapEntry< ValueTy > &mapEntry)
Definition: StringSet.h:46
void insert(InputIt begin, InputIt end)
Definition: StringSet.h:39
StringSet(std::initializer_list< StringRef > initializer)
Definition: StringSet.h:28
bool contains(StringRef key) const
Check if the set contains the given key.
Definition: StringSet.h:51
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18