| ... | @@ -133,6 +133,13 @@ pub fn MultiArrayList(comptime T: type) type { | ... | @@ -133,6 +133,13 @@ pub fn MultiArrayList(comptime T: type) type { |
| 133 | }; | 133 | }; |
| 134 | } | 134 | } |
| 135 | | 135 | |
| | 136 | pub fn swap(self: Slice, a: usize, b: usize) void { |
| | 137 | inline for (@typeInfo(Field).@"enum".fields) |field| { |
| | 138 | const its = self.items(@field(Field, field.name)); |
| | 139 | std.mem.swap(@FieldType(T, field.name), &its[a], &its[b]); |
| | 140 | } |
| | 141 | } |
| | 142 | |
| 136 | pub fn toMultiArrayList(self: Slice) Self { | 143 | pub fn toMultiArrayList(self: Slice) Self { |
| 137 | if (self.ptrs.len == 0 or self.capacity == 0) { | 144 | if (self.ptrs.len == 0 or self.capacity == 0) { |
| 138 | return .{}; | 145 | return .{}; |
| ... | @@ -267,6 +274,10 @@ pub fn MultiArrayList(comptime T: type) type { | ... | @@ -267,6 +274,10 @@ pub fn MultiArrayList(comptime T: type) type { |
| 267 | return self.slice().get(index); | 274 | return self.slice().get(index); |
| 268 | } | 275 | } |
| 269 | | 276 | |
| | 277 | pub fn swap(self: Self, a: usize, b: usize) void { |
| | 278 | return self.slice().swap(a, b); |
| | 279 | } |
| | 280 | |
| 270 | /// Extend the list by 1 element. | 281 | /// Extend the list by 1 element. |
| 271 | /// | 282 | /// |
| 272 | /// Allocates more memory as necessary. | 283 | /// Allocates more memory as necessary. |
| ... | @@ -771,6 +782,19 @@ test "basic usage" { | ... | @@ -771,6 +782,19 @@ test "basic usage" { |
| 771 | try testing.expectEqualSlices(u32, list.items(.a), &[_]u32{ 1, 2, 3 }); | 782 | try testing.expectEqualSlices(u32, list.items(.a), &[_]u32{ 1, 2, 3 }); |
| 772 | try testing.expectEqualSlices(u8, list.items(.c), &[_]u8{ 'a', 'b', 'c' }); | 783 | try testing.expectEqualSlices(u8, list.items(.c), &[_]u8{ 'a', 'b', 'c' }); |
| 773 | | 784 | |
| | 785 | list.swap(0, 2); |
| | 786 | try testing.expectEqualSlices(u32, list.items(.a), &[_]u32{ 3, 2, 1 }); |
| | 787 | try testing.expectEqualSlices(u8, list.items(.c), &[_]u8{ 'c', 'b', 'a' }); |
| | 788 | list.swap(2, 1); |
| | 789 | try testing.expectEqualSlices(u32, list.items(.a), &[_]u32{ 3, 1, 2 }); |
| | 790 | try testing.expectEqualSlices(u8, list.items(.c), &[_]u8{ 'c', 'a', 'b' }); |
| | 791 | list.swap(2, 0); |
| | 792 | try testing.expectEqualSlices(u32, list.items(.a), &[_]u32{ 2, 1, 3 }); |
| | 793 | try testing.expectEqualSlices(u8, list.items(.c), &[_]u8{ 'b', 'a', 'c' }); |
| | 794 | list.swap(0, 1); |
| | 795 | try testing.expectEqualSlices(u32, list.items(.a), &[_]u32{ 1, 2, 3 }); |
| | 796 | try testing.expectEqualSlices(u8, list.items(.c), &[_]u8{ 'a', 'b', 'c' }); |
| | 797 | |
| 774 | try testing.expectEqual(@as(usize, 3), list.items(.b).len); | 798 | try testing.expectEqual(@as(usize, 3), list.items(.b).len); |
| 775 | try testing.expectEqualStrings("foobar", list.items(.b)[0]); | 799 | try testing.expectEqualStrings("foobar", list.items(.b)[0]); |
| 776 | try testing.expectEqualStrings("zigzag", list.items(.b)[1]); | 800 | try testing.expectEqualStrings("zigzag", list.items(.b)[1]); |