authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-27 14:03:14-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-27 14:34:26-08:00
logda6e80c30a52fe061d6e534636d373594bf1cd7b
treec53f8ee78a8d719529c23a460ec6ef1a4fd71976
parentc9c7ede2f9be23fd212d968fbc2fdec6ebefdeeb

std.ArrayHashMap: explicit error sets


1 files changed, 26 insertions(+), 24 deletions(-)

lib/std/array_hash_map.zig+26-24
......@@ -604,6 +604,8 @@ pub fn ArrayHashMapUnmanaged(
604604 ordered,
605605 };
606606
607 const Oom = Allocator.Error;
608
607609 /// Convert from an unmanaged map to a managed map. After calling this,
608610 /// the promoted map should no longer be used.
609611 pub fn promote(self: Self, allocator: Allocator) Managed {
......@@ -619,14 +621,14 @@ pub fn ArrayHashMapUnmanaged(
619621 };
620622 }
621623
622 pub fn init(allocator: Allocator, key_list: []const K, value_list: []const V) !Self {
624 pub fn init(allocator: Allocator, key_list: []const K, value_list: []const V) Oom!Self {
623625 var self: Self = .{};
624626 errdefer self.deinit(allocator);
625627 try self.reinit(allocator, key_list, value_list);
626628 return self;
627629 }
628630
629 pub fn reinit(self: *Self, allocator: Allocator, key_list: []const K, value_list: []const V) !void {
631 pub fn reinit(self: *Self, allocator: Allocator, key_list: []const K, value_list: []const V) Oom!void {
630632 try self.entries.resize(allocator, key_list.len);
631633 @memcpy(self.keys(), key_list);
632634 if (@sizeOf(V) != 0) {
......@@ -750,24 +752,24 @@ pub fn ArrayHashMapUnmanaged(
750752 /// Otherwise, puts a new item with undefined value, and
751753 /// the `Entry` pointer points to it. Caller should then initialize
752754 /// the value (but not the key).
753 pub fn getOrPut(self: *Self, allocator: Allocator, key: K) !GetOrPutResult {
755 pub fn getOrPut(self: *Self, allocator: Allocator, key: K) Oom!GetOrPutResult {
754756 if (@sizeOf(Context) != 0)
755757 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call getOrPutContext instead.");
756758 return self.getOrPutContext(allocator, key, undefined);
757759 }
758 pub fn getOrPutContext(self: *Self, allocator: Allocator, key: K, ctx: Context) !GetOrPutResult {
760 pub fn getOrPutContext(self: *Self, allocator: Allocator, key: K, ctx: Context) Oom!GetOrPutResult {
759761 const gop = try self.getOrPutContextAdapted(allocator, key, ctx, ctx);
760762 if (!gop.found_existing) {
761763 gop.key_ptr.* = key;
762764 }
763765 return gop;
764766 }
765 pub fn getOrPutAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype) !GetOrPutResult {
767 pub fn getOrPutAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype) Oom!GetOrPutResult {
766768 if (@sizeOf(Context) != 0)
767769 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call getOrPutContextAdapted instead.");
768770 return self.getOrPutContextAdapted(allocator, key, key_ctx, undefined);
769771 }
770 pub fn getOrPutContextAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype, ctx: Context) !GetOrPutResult {
772 pub fn getOrPutContextAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype, ctx: Context) Oom!GetOrPutResult {
771773 self.ensureTotalCapacityContext(allocator, self.entries.len + 1, ctx) catch |err| {
772774 // "If key exists this function cannot fail."
773775 const index = self.getIndexAdapted(key, key_ctx) orelse return err;
......@@ -848,12 +850,12 @@ pub fn ArrayHashMapUnmanaged(
848850 }
849851 }
850852
851 pub fn getOrPutValue(self: *Self, allocator: Allocator, key: K, value: V) !GetOrPutResult {
853 pub fn getOrPutValue(self: *Self, allocator: Allocator, key: K, value: V) Oom!GetOrPutResult {
852854 if (@sizeOf(Context) != 0)
853855 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call getOrPutValueContext instead.");
854856 return self.getOrPutValueContext(allocator, key, value, undefined);
855857 }
856 pub fn getOrPutValueContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !GetOrPutResult {
858 pub fn getOrPutValueContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Oom!GetOrPutResult {
857859 const res = try self.getOrPutContextAdapted(allocator, key, ctx, ctx);
858860 if (!res.found_existing) {
859861 res.key_ptr.* = key;
......@@ -864,12 +866,12 @@ pub fn ArrayHashMapUnmanaged(
864866
865867 /// Increases capacity, guaranteeing that insertions up until the
866868 /// `expected_count` will not cause an allocation, and therefore cannot fail.
867 pub fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_capacity: usize) !void {
869 pub fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_capacity: usize) Oom!void {
868870 if (@sizeOf(ByIndexContext) != 0)
869871 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureTotalCapacityContext instead.");
870872 return self.ensureTotalCapacityContext(allocator, new_capacity, undefined);
871873 }
872 pub fn ensureTotalCapacityContext(self: *Self, allocator: Allocator, new_capacity: usize, ctx: Context) !void {
874 pub fn ensureTotalCapacityContext(self: *Self, allocator: Allocator, new_capacity: usize, ctx: Context) Oom!void {
873875 self.pointer_stability.lock();
874876 defer self.pointer_stability.unlock();
875877
......@@ -901,7 +903,7 @@ pub fn ArrayHashMapUnmanaged(
901903 self: *Self,
902904 allocator: Allocator,
903905 additional_capacity: usize,
904 ) !void {
906 ) Oom!void {
905907 if (@sizeOf(Context) != 0)
906908 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureTotalCapacityContext instead.");
907909 return self.ensureUnusedCapacityContext(allocator, additional_capacity, undefined);
......@@ -911,7 +913,7 @@ pub fn ArrayHashMapUnmanaged(
911913 allocator: Allocator,
912914 additional_capacity: usize,
913915 ctx: Context,
914 ) !void {
916 ) Oom!void {
915917 return self.ensureTotalCapacityContext(allocator, self.count() + additional_capacity, ctx);
916918 }
917919
......@@ -926,24 +928,24 @@ pub fn ArrayHashMapUnmanaged(
926928
927929 /// Clobbers any existing data. To detect if a put would clobber
928930 /// existing data, see `getOrPut`.
929 pub fn put(self: *Self, allocator: Allocator, key: K, value: V) !void {
931 pub fn put(self: *Self, allocator: Allocator, key: K, value: V) Oom!void {
930932 if (@sizeOf(Context) != 0)
931933 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call putContext instead.");
932934 return self.putContext(allocator, key, value, undefined);
933935 }
934 pub fn putContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !void {
936 pub fn putContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Oom!void {
935937 const result = try self.getOrPutContext(allocator, key, ctx);
936938 result.value_ptr.* = value;
937939 }
938940
939941 /// Inserts a key-value pair into the hash map, asserting that no previous
940942 /// entry with the same key is already present
941 pub fn putNoClobber(self: *Self, allocator: Allocator, key: K, value: V) !void {
943 pub fn putNoClobber(self: *Self, allocator: Allocator, key: K, value: V) Oom!void {
942944 if (@sizeOf(Context) != 0)
943945 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call putNoClobberContext instead.");
944946 return self.putNoClobberContext(allocator, key, value, undefined);
945947 }
946 pub fn putNoClobberContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !void {
948 pub fn putNoClobberContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Oom!void {
947949 const result = try self.getOrPutContext(allocator, key, ctx);
948950 assert(!result.found_existing);
949951 result.value_ptr.* = value;
......@@ -977,12 +979,12 @@ pub fn ArrayHashMapUnmanaged(
977979 }
978980
979981 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
980 pub fn fetchPut(self: *Self, allocator: Allocator, key: K, value: V) !?KV {
982 pub fn fetchPut(self: *Self, allocator: Allocator, key: K, value: V) Oom!?KV {
981983 if (@sizeOf(Context) != 0)
982984 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call fetchPutContext instead.");
983985 return self.fetchPutContext(allocator, key, value, undefined);
984986 }
985 pub fn fetchPutContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !?KV {
987 pub fn fetchPutContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) Oom!?KV {
986988 const gop = try self.getOrPutContext(allocator, key, ctx);
987989 var result: ?KV = null;
988990 if (gop.found_existing) {
......@@ -1269,12 +1271,12 @@ pub fn ArrayHashMapUnmanaged(
12691271 /// Create a copy of the hash map which can be modified separately.
12701272 /// The copy uses the same context as this instance, but is allocated
12711273 /// with the provided allocator.
1272 pub fn clone(self: Self, allocator: Allocator) !Self {
1274 pub fn clone(self: Self, allocator: Allocator) Oom!Self {
12731275 if (@sizeOf(ByIndexContext) != 0)
12741276 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call cloneContext instead.");
12751277 return self.cloneContext(allocator, undefined);
12761278 }
1277 pub fn cloneContext(self: Self, allocator: Allocator, ctx: Context) !Self {
1279 pub fn cloneContext(self: Self, allocator: Allocator, ctx: Context) Oom!Self {
12781280 var other: Self = .{};
12791281 other.entries = try self.entries.clone(allocator);
12801282 errdefer other.entries.deinit(allocator);
......@@ -1308,13 +1310,13 @@ pub fn ArrayHashMapUnmanaged(
13081310 /// directly without going through the methods of this map.
13091311 ///
13101312 /// The time complexity of this operation is O(n).
1311 pub fn reIndex(self: *Self, allocator: Allocator) !void {
1313 pub fn reIndex(self: *Self, allocator: Allocator) Oom!void {
13121314 if (@sizeOf(ByIndexContext) != 0)
13131315 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call reIndexContext instead.");
13141316 return self.reIndexContext(allocator, undefined);
13151317 }
13161318
1317 pub fn reIndexContext(self: *Self, allocator: Allocator, ctx: Context) !void {
1319 pub fn reIndexContext(self: *Self, allocator: Allocator, ctx: Context) Oom!void {
13181320 // Recompute all hashes.
13191321 if (store_hash) {
13201322 for (self.keys(), self.entries.items(.hash)) |key, *hash| {
......@@ -2090,7 +2092,7 @@ const IndexHeader = struct {
20902092 return @as(u32, @intCast(self.length() - 1));
20912093 }
20922094
2093 fn findBitIndex(desired_capacity: usize) !u8 {
2095 fn findBitIndex(desired_capacity: usize) Allocator.Error!u8 {
20942096 if (desired_capacity > max_capacity) return error.OutOfMemory;
20952097 var new_bit_index = @as(u8, @intCast(std.math.log2_int_ceil(usize, desired_capacity)));
20962098 if (desired_capacity > index_capacities[new_bit_index]) new_bit_index += 1;
......@@ -2101,7 +2103,7 @@ const IndexHeader = struct {
21012103
21022104 /// Allocates an index header, and fills the entryIndexes array with empty.
21032105 /// The distance array contents are undefined.
2104 fn alloc(allocator: Allocator, new_bit_index: u8) !*IndexHeader {
2106 fn alloc(allocator: Allocator, new_bit_index: u8) Allocator.Error!*IndexHeader {
21052107 const len = @as(usize, 1) << @as(math.Log2Int(usize), @intCast(new_bit_index));
21062108 const index_size = hash_map.capacityIndexSize(new_bit_index);
21072109 const nbytes = @sizeOf(IndexHeader) + index_size * len;