|
| 1 | +//===--- FrozenMultiMap.h ----------------------------------*- C++ --------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// \file |
| 14 | +/// |
| 15 | +/// A 2 stage multi-map. Initially the multimap is mutable and can only be |
| 16 | +/// initialized. Once complete, the map is frozen and can be only used for map |
| 17 | +/// operations. It is guaranteed that all values are still in insertion order. |
| 18 | +/// |
| 19 | +/// DISCUSSION: These restrictions flow from the internal implementation of the |
| 20 | +/// multi-map being a pair of keys, values. We form the map property by |
| 21 | +/// performing a stable_sort of the (key, value) in the process of freezing the |
| 22 | +/// map. |
| 23 | +/// |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | + |
| 26 | +#ifndef SWIFT_BASIC_FROZENMULTIMAP_H |
| 27 | +#define SWIFT_BASIC_FROZENMULTIMAP_H |
| 28 | + |
| 29 | +#include "swift/Basic/LLVM.h" |
| 30 | +#include "swift/Basic/STLExtras.h" |
| 31 | +#include "llvm/ADT/SmallVector.h" |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +namespace swift { |
| 35 | + |
| 36 | +template <typename Key, typename Value, |
| 37 | + typename VectorStorage = std::vector<std::pair<Key, Value>>> |
| 38 | +class FrozenMultiMap { |
| 39 | + VectorStorage storage; |
| 40 | + bool frozen = false; |
| 41 | + |
| 42 | +private: |
| 43 | + struct PairToSecondElt; |
| 44 | + |
| 45 | +public: |
| 46 | + using PairToSecondEltRange = |
| 47 | + TransformRange<ArrayRef<std::pair<Key, Value>>, PairToSecondElt>; |
| 48 | + |
| 49 | + FrozenMultiMap() = default; |
| 50 | + |
| 51 | + void insert(const Key &key, const Value &value) { |
| 52 | + assert(!isFrozen() && "Can not insert new keys once map is frozen"); |
| 53 | + storage.emplace_back(key, value); |
| 54 | + } |
| 55 | + |
| 56 | + Optional<PairToSecondEltRange> find(const Key &key) const { |
| 57 | + assert(isFrozen() && |
| 58 | + "Can not perform a find operation until the map is frozen"); |
| 59 | + // Since our array is sorted, we need to first find the first pair with our |
| 60 | + // inst as the first element. |
| 61 | + auto start = std::lower_bound( |
| 62 | + storage.begin(), storage.end(), std::make_pair(key, Value()), |
| 63 | + [&](const std::pair<Key, Value> &p1, const std::pair<Key, Value> &p2) { |
| 64 | + return p1.first < p2.first; |
| 65 | + }); |
| 66 | + if (start == storage.end() || start->first != key) { |
| 67 | + return None; |
| 68 | + } |
| 69 | + |
| 70 | + // Ok, we found our first element. Now scan forward until we find a pair |
| 71 | + // whose instruction is not our own instruction. |
| 72 | + auto end = find_if_not( |
| 73 | + start, storage.end(), |
| 74 | + [&](const std::pair<Key, Value> &pair) { return pair.first == key; }); |
| 75 | + unsigned count = std::distance(start, end); |
| 76 | + ArrayRef<std::pair<Key, Value>> slice(&*start, count); |
| 77 | + return PairToSecondEltRange(slice, PairToSecondElt()); |
| 78 | + } |
| 79 | + |
| 80 | + bool isFrozen() const { return frozen; } |
| 81 | + |
| 82 | + /// Set this map into its frozen state when we |
| 83 | + void setFrozen() { |
| 84 | + std::stable_sort(storage.begin(), storage.end(), |
| 85 | + [&](const std::pair<Key, Value> &lhs, |
| 86 | + const std::pair<Key, Value> &rhs) { |
| 87 | + // Only compare the first entry so that we preserve |
| 88 | + // insertion order. |
| 89 | + return lhs.first < rhs.first; |
| 90 | + }); |
| 91 | + frozen = true; |
| 92 | + } |
| 93 | + |
| 94 | + unsigned size() const { return storage.size(); } |
| 95 | + bool empty() const { return storage.empty(); } |
| 96 | + |
| 97 | + struct iterator : std::iterator<std::forward_iterator_tag, |
| 98 | + std::pair<Key, ArrayRef<Value>>> { |
| 99 | + using base_iterator = typename decltype(storage)::iterator; |
| 100 | + |
| 101 | + FrozenMultiMap ↦ |
| 102 | + base_iterator baseIter; |
| 103 | + Optional<std::pair<Key, PairToSecondEltRange>> currentValue; |
| 104 | + |
| 105 | + iterator(FrozenMultiMap &map, base_iterator iter) |
| 106 | + : map(map), baseIter(iter), currentValue() { |
| 107 | + updateCurrentValue(); |
| 108 | + } |
| 109 | + |
| 110 | + void updateCurrentValue() { |
| 111 | + base_iterator end = map.storage.end(); |
| 112 | + |
| 113 | + // If we are end, set currentValue to be None. |
| 114 | + if (baseIter == end) { |
| 115 | + currentValue = None; |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // Otherwise, determine the next range that we are visiting. |
| 120 | + auto rangeEnd = std::find_if_not(std::next(baseIter), end, |
| 121 | + [&](const std::pair<Key, Value> &elt) { |
| 122 | + return elt.first == baseIter->first; |
| 123 | + }); |
| 124 | + unsigned count = std::distance(baseIter, rangeEnd); |
| 125 | + ArrayRef<std::pair<Key, Value>> slice(&*baseIter, count); |
| 126 | + currentValue = {baseIter->first, |
| 127 | + PairToSecondEltRange(slice, PairToSecondElt())}; |
| 128 | + } |
| 129 | + |
| 130 | + iterator &operator++() { |
| 131 | + baseIter = std::find_if_not(std::next(baseIter), map.storage.end(), |
| 132 | + [&](const std::pair<Key, Value> &elt) { |
| 133 | + return elt.first == baseIter->first; |
| 134 | + }); |
| 135 | + updateCurrentValue(); |
| 136 | + return *this; |
| 137 | + } |
| 138 | + |
| 139 | + iterator operator++(int) { |
| 140 | + auto tmp = *this; |
| 141 | + baseIter = std::find_if_not(std::next(baseIter), map.storage.end(), |
| 142 | + [&](const std::pair<Key, Value> &elt) { |
| 143 | + return elt.first == baseIter->first; |
| 144 | + }); |
| 145 | + updateCurrentValue(); |
| 146 | + return tmp; |
| 147 | + } |
| 148 | + |
| 149 | + std::pair<Key, PairToSecondEltRange> operator*() const { |
| 150 | + return *currentValue; |
| 151 | + } |
| 152 | + |
| 153 | + bool operator==(const iterator &RHS) const { |
| 154 | + return baseIter == RHS.baseIter; |
| 155 | + } |
| 156 | + |
| 157 | + bool operator!=(const iterator &RHS) const { |
| 158 | + return baseIter != RHS.baseIter; |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + /// Return a range of (key, ArrayRef<Value>) pairs. The keys are guaranteed to |
| 163 | + /// be in key sorted order and the ArrayRef<Value> are in insertion order. |
| 164 | + llvm::iterator_range<iterator> getRange() const { |
| 165 | + assert(isFrozen() && |
| 166 | + "Can not create range until data structure is frozen?!"); |
| 167 | + auto *self = const_cast<FrozenMultiMap *>(this); |
| 168 | + iterator iter1 = iterator(*self, self->storage.begin()); |
| 169 | + iterator iter2 = iterator(*self, self->storage.end()); |
| 170 | + return llvm::make_range(iter1, iter2); |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | +template <typename Key, typename Value, typename Storage> |
| 175 | +struct FrozenMultiMap<Key, Value, Storage>::PairToSecondElt { |
| 176 | + PairToSecondElt() {} |
| 177 | + |
| 178 | + Value operator()(const std::pair<Key, Value> &pair) const { |
| 179 | + return pair.second; |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +template <typename Key, typename Value, unsigned SmallSize> |
| 184 | +using SmallFrozenMultiMap = |
| 185 | + FrozenMultiMap<Key, Value, SmallVector<std::pair<Key, Value>, SmallSize>>; |
| 186 | + |
| 187 | +} // namespace swift |
| 188 | + |
| 189 | +#endif |
0 commit comments