use * for pointer type instead of &
See #770 To help automatically translate code, see the zig-fmt-pointer-reform-2 branch. This will convert all & into *. Due to the syntax ambiguity (which is why we are making this change), even address-of & will turn into *, so you'll have to manually fix thes instances. You will be guaranteed to get compile errors for them - expected 'type', found 'foo'
This commit is contained in:
@@ -87,49 +87,49 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
const ShelfIndex = std.math.Log2Int(usize);
|
||||
|
||||
prealloc_segment: [prealloc_item_count]T,
|
||||
dynamic_segments: []&T,
|
||||
allocator: &Allocator,
|
||||
dynamic_segments: []*T,
|
||||
allocator: *Allocator,
|
||||
len: usize,
|
||||
|
||||
pub const prealloc_count = prealloc_item_count;
|
||||
|
||||
/// Deinitialize with `deinit`
|
||||
pub fn init(allocator: &Allocator) Self {
|
||||
pub fn init(allocator: *Allocator) Self {
|
||||
return Self{
|
||||
.allocator = allocator,
|
||||
.len = 0,
|
||||
.prealloc_segment = undefined,
|
||||
.dynamic_segments = []&T{},
|
||||
.dynamic_segments = []*T{},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: &Self) void {
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.freeShelves(ShelfIndex(self.dynamic_segments.len), 0);
|
||||
self.allocator.free(self.dynamic_segments);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn at(self: &Self, i: usize) &T {
|
||||
pub fn at(self: *Self, i: usize) *T {
|
||||
assert(i < self.len);
|
||||
return self.uncheckedAt(i);
|
||||
}
|
||||
|
||||
pub fn count(self: &const Self) usize {
|
||||
pub fn count(self: *const Self) usize {
|
||||
return self.len;
|
||||
}
|
||||
|
||||
pub fn push(self: &Self, item: &const T) !void {
|
||||
pub fn push(self: *Self, item: *const T) !void {
|
||||
const new_item_ptr = try self.addOne();
|
||||
new_item_ptr.* = item.*;
|
||||
}
|
||||
|
||||
pub fn pushMany(self: &Self, items: []const T) !void {
|
||||
pub fn pushMany(self: *Self, items: []const T) !void {
|
||||
for (items) |item| {
|
||||
try self.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop(self: &Self) ?T {
|
||||
pub fn pop(self: *Self) ?T {
|
||||
if (self.len == 0) return null;
|
||||
|
||||
const index = self.len - 1;
|
||||
@@ -138,7 +138,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn addOne(self: &Self) !&T {
|
||||
pub fn addOne(self: *Self) !*T {
|
||||
const new_length = self.len + 1;
|
||||
try self.growCapacity(new_length);
|
||||
const result = self.uncheckedAt(self.len);
|
||||
@@ -147,7 +147,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
}
|
||||
|
||||
/// Grows or shrinks capacity to match usage.
|
||||
pub fn setCapacity(self: &Self, new_capacity: usize) !void {
|
||||
pub fn setCapacity(self: *Self, new_capacity: usize) !void {
|
||||
if (new_capacity <= usize(1) << (prealloc_exp + self.dynamic_segments.len)) {
|
||||
return self.shrinkCapacity(new_capacity);
|
||||
} else {
|
||||
@@ -156,15 +156,15 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
}
|
||||
|
||||
/// Only grows capacity, or retains current capacity
|
||||
pub fn growCapacity(self: &Self, new_capacity: usize) !void {
|
||||
pub fn growCapacity(self: *Self, new_capacity: usize) !void {
|
||||
const new_cap_shelf_count = shelfCount(new_capacity);
|
||||
const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
|
||||
if (new_cap_shelf_count > old_shelf_count) {
|
||||
self.dynamic_segments = try self.allocator.realloc(&T, self.dynamic_segments, new_cap_shelf_count);
|
||||
self.dynamic_segments = try self.allocator.realloc(*T, self.dynamic_segments, new_cap_shelf_count);
|
||||
var i = old_shelf_count;
|
||||
errdefer {
|
||||
self.freeShelves(i, old_shelf_count);
|
||||
self.dynamic_segments = self.allocator.shrink(&T, self.dynamic_segments, old_shelf_count);
|
||||
self.dynamic_segments = self.allocator.shrink(*T, self.dynamic_segments, old_shelf_count);
|
||||
}
|
||||
while (i < new_cap_shelf_count) : (i += 1) {
|
||||
self.dynamic_segments[i] = (try self.allocator.alloc(T, shelfSize(i))).ptr;
|
||||
@@ -173,12 +173,12 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
}
|
||||
|
||||
/// Only shrinks capacity or retains current capacity
|
||||
pub fn shrinkCapacity(self: &Self, new_capacity: usize) void {
|
||||
pub fn shrinkCapacity(self: *Self, new_capacity: usize) void {
|
||||
if (new_capacity <= prealloc_item_count) {
|
||||
const len = ShelfIndex(self.dynamic_segments.len);
|
||||
self.freeShelves(len, 0);
|
||||
self.allocator.free(self.dynamic_segments);
|
||||
self.dynamic_segments = []&T{};
|
||||
self.dynamic_segments = []*T{};
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -190,10 +190,10 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
}
|
||||
|
||||
self.freeShelves(old_shelf_count, new_cap_shelf_count);
|
||||
self.dynamic_segments = self.allocator.shrink(&T, self.dynamic_segments, new_cap_shelf_count);
|
||||
self.dynamic_segments = self.allocator.shrink(*T, self.dynamic_segments, new_cap_shelf_count);
|
||||
}
|
||||
|
||||
pub fn uncheckedAt(self: &Self, index: usize) &T {
|
||||
pub fn uncheckedAt(self: *Self, index: usize) *T {
|
||||
if (index < prealloc_item_count) {
|
||||
return &self.prealloc_segment[index];
|
||||
}
|
||||
@@ -230,7 +230,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
return list_index + prealloc_item_count - (usize(1) << ((prealloc_exp + 1) + shelf_index));
|
||||
}
|
||||
|
||||
fn freeShelves(self: &Self, from_count: ShelfIndex, to_count: ShelfIndex) void {
|
||||
fn freeShelves(self: *Self, from_count: ShelfIndex, to_count: ShelfIndex) void {
|
||||
var i = from_count;
|
||||
while (i != to_count) {
|
||||
i -= 1;
|
||||
@@ -239,13 +239,13 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
}
|
||||
|
||||
pub const Iterator = struct {
|
||||
list: &Self,
|
||||
list: *Self,
|
||||
index: usize,
|
||||
box_index: usize,
|
||||
shelf_index: ShelfIndex,
|
||||
shelf_size: usize,
|
||||
|
||||
pub fn next(it: &Iterator) ?&T {
|
||||
pub fn next(it: *Iterator) ?*T {
|
||||
if (it.index >= it.list.len) return null;
|
||||
if (it.index < prealloc_item_count) {
|
||||
const ptr = &it.list.prealloc_segment[it.index];
|
||||
@@ -269,7 +269,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
return ptr;
|
||||
}
|
||||
|
||||
pub fn prev(it: &Iterator) ?&T {
|
||||
pub fn prev(it: *Iterator) ?*T {
|
||||
if (it.index == 0) return null;
|
||||
|
||||
it.index -= 1;
|
||||
@@ -286,7 +286,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
return &it.list.dynamic_segments[it.shelf_index][it.box_index];
|
||||
}
|
||||
|
||||
pub fn peek(it: &Iterator) ?&T {
|
||||
pub fn peek(it: *Iterator) ?*T {
|
||||
if (it.index >= it.list.len)
|
||||
return null;
|
||||
if (it.index < prealloc_item_count)
|
||||
@@ -295,7 +295,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
return &it.list.dynamic_segments[it.shelf_index][it.box_index];
|
||||
}
|
||||
|
||||
pub fn set(it: &Iterator, index: usize) void {
|
||||
pub fn set(it: *Iterator, index: usize) void {
|
||||
it.index = index;
|
||||
if (index < prealloc_item_count) return;
|
||||
it.shelf_index = shelfIndex(index);
|
||||
@@ -304,7 +304,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
|
||||
}
|
||||
};
|
||||
|
||||
pub fn iterator(self: &Self, start_index: usize) Iterator {
|
||||
pub fn iterator(self: *Self, start_index: usize) Iterator {
|
||||
var it = Iterator{
|
||||
.list = self,
|
||||
.index = undefined,
|
||||
@@ -331,7 +331,7 @@ test "std.SegmentedList" {
|
||||
try testSegmentedList(16, a);
|
||||
}
|
||||
|
||||
fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void {
|
||||
fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
|
||||
var list = SegmentedList(i32, prealloc).init(allocator);
|
||||
defer list.deinit();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user