added: `map.copy`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 9 Feb 2024 02:50:26 +0000 (20:50 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 9 Feb 2024 02:50:26 +0000 (20:50 -0600)
core/container/map.onyx

index 7aff718ce0e19481904e4fdc40a619556bfefc84..0efaa9cd1448b91c2ad3f4e4526157ae91224925 100644 (file)
@@ -101,6 +101,18 @@ free :: (use map: &Map) {
     if entries.data != null do array.free(&entries);
 }
 
+#doc """
+    Shallow copies a map using the allocator provided if one is provided, or the allocator on the old map otherwise.
+"""
+copy :: (oldMap: &Map, allocator: ? Allocator = .None) -> Map(oldMap.Key_Type, oldMap.Value_Type) {
+    newMap: typeof *oldMap;
+    newMap.allocator = allocator ?? oldMap.allocator;
+    newMap.hashes = array.copy(oldMap.hashes, newMap.allocator);
+    newMap.entries = array.copy(&oldMap.entries, newMap.allocator);
+
+    return newMap;
+}
+
 #doc """
     Sets the value at the specified key, or creates a new entry
     if the key was not already present.