File indexing completed on 2026-05-10 08:37:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef LLVM_CLANG_SERIALIZATION_CONTINUOUSRANGEMAP_H
0015 #define LLVM_CLANG_SERIALIZATION_CONTINUOUSRANGEMAP_H
0016
0017 #include "clang/Basic/LLVM.h"
0018 #include "llvm/ADT/STLExtras.h"
0019 #include "llvm/ADT/SmallVector.h"
0020 #include <algorithm>
0021 #include <cassert>
0022 #include <utility>
0023
0024 namespace clang {
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 template <typename Int, typename V, unsigned InitialCapacity>
0037 class ContinuousRangeMap {
0038 public:
0039 using value_type = std::pair<Int, V>;
0040 using reference = value_type &;
0041 using const_reference = const value_type &;
0042 using pointer = value_type *;
0043 using const_pointer = const value_type *;
0044
0045 private:
0046 using Representation = SmallVector<value_type, InitialCapacity>;
0047
0048 Representation Rep;
0049
0050 struct Compare {
0051 bool operator ()(const_reference L, Int R) const {
0052 return L.first < R;
0053 }
0054 bool operator ()(Int L, const_reference R) const {
0055 return L < R.first;
0056 }
0057 bool operator ()(Int L, Int R) const {
0058 return L < R;
0059 }
0060 bool operator ()(const_reference L, const_reference R) const {
0061 return L.first < R.first;
0062 }
0063 };
0064
0065 public:
0066 void insert(const value_type &Val) {
0067 if (!Rep.empty() && Rep.back() == Val)
0068 return;
0069
0070 assert((Rep.empty() || Rep.back().first < Val.first) &&
0071 "Must insert keys in order.");
0072 Rep.push_back(Val);
0073 }
0074
0075 void insertOrReplace(const value_type &Val) {
0076 iterator I = llvm::lower_bound(Rep, Val, Compare());
0077 if (I != Rep.end() && I->first == Val.first) {
0078 I->second = Val.second;
0079 return;
0080 }
0081
0082 Rep.insert(I, Val);
0083 }
0084
0085 using iterator = typename Representation::iterator;
0086 using const_iterator = typename Representation::const_iterator;
0087
0088 iterator begin() { return Rep.begin(); }
0089 iterator end() { return Rep.end(); }
0090 const_iterator begin() const { return Rep.begin(); }
0091 const_iterator end() const { return Rep.end(); }
0092
0093 iterator find(Int K) {
0094 iterator I = llvm::upper_bound(Rep, K, Compare());
0095
0096
0097 if (I == Rep.begin())
0098 return Rep.end();
0099 --I;
0100 return I;
0101 }
0102 const_iterator find(Int K) const {
0103 return const_cast<ContinuousRangeMap*>(this)->find(K);
0104 }
0105
0106 reference back() { return Rep.back(); }
0107 const_reference back() const { return Rep.back(); }
0108
0109
0110
0111 class Builder {
0112 ContinuousRangeMap &Self;
0113
0114 public:
0115 explicit Builder(ContinuousRangeMap &Self) : Self(Self) {}
0116 Builder(const Builder&) = delete;
0117 Builder &operator=(const Builder&) = delete;
0118
0119 ~Builder() {
0120 llvm::sort(Self.Rep, Compare());
0121 Self.Rep.erase(
0122 std::unique(
0123 Self.Rep.begin(), Self.Rep.end(),
0124 [](const_reference A, const_reference B) {
0125
0126
0127 assert((A == B || A.first != B.first) &&
0128 "ContinuousRangeMap::Builder given non-unique keys");
0129 return A == B;
0130 }),
0131 Self.Rep.end());
0132 }
0133
0134 void insert(const value_type &Val) {
0135 Self.Rep.push_back(Val);
0136 }
0137 };
0138
0139 friend class Builder;
0140 };
0141
0142 }
0143
0144 #endif