Skip to content

Commit e2b5c03

Browse files
committed
[frozen-multi-map] Add the ability to "unfreeze" the multi-map so one can add incrementally more elements to the multi-map.
For those unfamiliar, this map is a vector of pairs that we stable sort by the key when we "freeze" it so we can run map operations upon the keys. This allows one to accumulate into the multi-map and then once one has finished accumulating, perform these map operations. While doing some work in the move checker, I thought I would need the ability to incrementally update a multi-map by appending more entires. Due to the design, supporting this is as simple as unfreezing the map. This results in one being no longer able to run map operations without hitting an assert. When one freezes again, the stable sort will put the new entries in the appropriate place in the already sorted initial part of the array. Turns out I didn't need this, but seemed useful, so I am upstreaming it.
1 parent 06a60ff commit e2b5c03

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

include/swift/Basic/FrozenMultiMap.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ class FrozenMultiMap {
122122

123123
bool isFrozen() const { return frozen; }
124124

125-
/// Set this map into its frozen state when we
125+
/// Set this map into its frozen state. This stable sorts our internal array
126+
/// to create our map like context.
127+
///
128+
/// After this, one can only use map like operations and non-mutable vector
129+
/// operations instead of full mutable/non-mutable vector operations.
126130
void setFrozen() {
127131
std::stable_sort(storage.begin(), storage.end(),
128132
[&](const std::pair<Key, Optional<Value>> &lhs,
@@ -134,6 +138,13 @@ class FrozenMultiMap {
134138
frozen = true;
135139
}
136140

141+
/// Unfreeze the map, so one can go back to using mutable vector
142+
/// operations. After one calls this until one freezes the map again, one
143+
/// cannot use map operations.
144+
///
145+
/// This allows one to incrementally update the map.
146+
void unfreeze() { frozen = false; }
147+
137148
/// Reset the frozen multimap in an unfrozen state with its storage cleared.
138149
void reset() {
139150
storage.clear();

0 commit comments

Comments
 (0)