@@ -16,6 +16,7 @@ import {EnumerableSet} from "./EnumerableSet.sol";
16
16
* - Entries are added, removed, and checked for existence in constant time
17
17
* (O(1)).
18
18
* - Entries are enumerated in O(n). No guarantees are made on the ordering.
19
+ * - Map can be cleared (all entries removed) in O(n).
19
20
*
20
21
* ```solidity
21
22
* contract Example {
@@ -90,6 +91,20 @@ library EnumerableMap {
90
91
return map._keys.remove (key);
91
92
}
92
93
94
+ /**
95
+ * @dev Removes all the entries from a map. O(n).
96
+ *
97
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
98
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
99
+ */
100
+ function clear (Bytes32ToBytes32Map storage map ) internal {
101
+ uint256 len = length (map);
102
+ for (uint256 i = 0 ; i < len; ++ i) {
103
+ delete map._values[map._keys.at (i)];
104
+ }
105
+ map._keys.clear ();
106
+ }
107
+
93
108
/**
94
109
* @dev Returns true if the key is in the map. O(1).
95
110
*/
@@ -185,6 +200,16 @@ library EnumerableMap {
185
200
return remove (map._inner, bytes32 (key));
186
201
}
187
202
203
+ /**
204
+ * @dev Removes all the entries from a map. O(n).
205
+ *
206
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
207
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
208
+ */
209
+ function clear (UintToUintMap storage map ) internal {
210
+ clear (map._inner);
211
+ }
212
+
188
213
/**
189
214
* @dev Returns true if the key is in the map. O(1).
190
215
*/
@@ -278,6 +303,16 @@ library EnumerableMap {
278
303
return remove (map._inner, bytes32 (key));
279
304
}
280
305
306
+ /**
307
+ * @dev Removes all the entries from a map. O(n).
308
+ *
309
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
310
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
311
+ */
312
+ function clear (UintToAddressMap storage map ) internal {
313
+ clear (map._inner);
314
+ }
315
+
281
316
/**
282
317
* @dev Returns true if the key is in the map. O(1).
283
318
*/
@@ -371,6 +406,16 @@ library EnumerableMap {
371
406
return remove (map._inner, bytes32 (key));
372
407
}
373
408
409
+ /**
410
+ * @dev Removes all the entries from a map. O(n).
411
+ *
412
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
413
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
414
+ */
415
+ function clear (UintToBytes32Map storage map ) internal {
416
+ clear (map._inner);
417
+ }
418
+
374
419
/**
375
420
* @dev Returns true if the key is in the map. O(1).
376
421
*/
@@ -464,6 +509,16 @@ library EnumerableMap {
464
509
return remove (map._inner, bytes32 (uint256 (uint160 (key))));
465
510
}
466
511
512
+ /**
513
+ * @dev Removes all the entries from a map. O(n).
514
+ *
515
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
516
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
517
+ */
518
+ function clear (AddressToUintMap storage map ) internal {
519
+ clear (map._inner);
520
+ }
521
+
467
522
/**
468
523
* @dev Returns true if the key is in the map. O(1).
469
524
*/
@@ -557,6 +612,16 @@ library EnumerableMap {
557
612
return remove (map._inner, bytes32 (uint256 (uint160 (key))));
558
613
}
559
614
615
+ /**
616
+ * @dev Removes all the entries from a map. O(n).
617
+ *
618
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
619
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
620
+ */
621
+ function clear (AddressToAddressMap storage map ) internal {
622
+ clear (map._inner);
623
+ }
624
+
560
625
/**
561
626
* @dev Returns true if the key is in the map. O(1).
562
627
*/
@@ -650,6 +715,16 @@ library EnumerableMap {
650
715
return remove (map._inner, bytes32 (uint256 (uint160 (key))));
651
716
}
652
717
718
+ /**
719
+ * @dev Removes all the entries from a map. O(n).
720
+ *
721
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
722
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
723
+ */
724
+ function clear (AddressToBytes32Map storage map ) internal {
725
+ clear (map._inner);
726
+ }
727
+
653
728
/**
654
729
* @dev Returns true if the key is in the map. O(1).
655
730
*/
@@ -743,6 +818,16 @@ library EnumerableMap {
743
818
return remove (map._inner, key);
744
819
}
745
820
821
+ /**
822
+ * @dev Removes all the entries from a map. O(n).
823
+ *
824
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
825
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
826
+ */
827
+ function clear (Bytes32ToUintMap storage map ) internal {
828
+ clear (map._inner);
829
+ }
830
+
746
831
/**
747
832
* @dev Returns true if the key is in the map. O(1).
748
833
*/
@@ -836,6 +921,16 @@ library EnumerableMap {
836
921
return remove (map._inner, key);
837
922
}
838
923
924
+ /**
925
+ * @dev Removes all the entries from a map. O(n).
926
+ *
927
+ * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
928
+ * function uncallable if the map grows to the point where clearing it consumes too much gas to fit in a block.
929
+ */
930
+ function clear (Bytes32ToAddressMap storage map ) internal {
931
+ clear (map._inner);
932
+ }
933
+
839
934
/**
840
935
* @dev Returns true if the key is in the map. O(1).
841
936
*/
0 commit comments