| ... | ... | @@ -18,12 +18,12 @@ const Allocator = std.mem.Allocator; |
| 18 | 18 | // ... |
| 19 | 19 | // |
| 20 | 20 | // warehouse indexes: |
| 21 | | // shelf 0: 0 |
| 22 | | // shelf 1: 0 1 |
| 23 | | // shelf 2: 0 1 2 3 |
| 24 | | // shelf 3: 0 1 2 3 4 5 6 7 |
| 25 | | // shelf 4: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 26 | | // shelf 5: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
| 21 | // shelf 0: 0 |
| 22 | // shelf 1: 0 1 |
| 23 | // shelf 2: 0 1 2 3 |
| 24 | // shelf 3: 0 1 2 3 4 5 6 7 |
| 25 | // shelf 4: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 26 | // shelf 5: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
| 27 | 27 | // ... |
| 28 | 28 | // |
| 29 | 29 | // With this arrangement, here are the equations to get the shelf index and |
| ... | ... | @@ -66,6 +66,8 @@ const Allocator = std.mem.Allocator; |
| 66 | 66 | /// |
| 67 | 67 | /// Because it never has to copy elements from an old location to a new location, it does not require |
| 68 | 68 | /// its elements to be copyable, and it avoids wasting memory when backed by an ArenaAllocator. |
| 69 | /// Note that the push() and pop() convenience methods perform a copy, but you can instead use |
| 70 | /// addOne(), at(), setCapacity(), and shrinkCapacity() to avoid copying items. |
| 69 | 71 | /// |
| 70 | 72 | /// This data structure has O(1) push and O(1) pop. |
| 71 | 73 | /// |
| ... | ... | @@ -74,8 +76,10 @@ const Allocator = std.mem.Allocator; |
| 74 | 76 | pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type { |
| 75 | 77 | return struct { |
| 76 | 78 | const Self = this; |
| 77 | | const prealloc_base = blk: { |
| 79 | const prealloc_exp = blk: { |
| 80 | // we don't use the prealloc_exp constant when prealloc_item_count is 0. |
| 78 | 81 | assert(prealloc_item_count != 0); |
| 82 | |
| 79 | 83 | const value = std.math.log2_int(usize, prealloc_item_count); |
| 80 | 84 | assert((1 << value) == prealloc_item_count); // prealloc_item_count must be a power of 2 |
| 81 | 85 | break :blk @typeOf(1)(value); |
| ... | ... | @@ -190,28 +194,28 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type |
| 190 | 194 | if (prealloc_item_count == 0) { |
| 191 | 195 | return std.math.log2_int_ceil(usize, box_count + 1); |
| 192 | 196 | } |
| 193 | | return std.math.log2_int_ceil(usize, box_count + prealloc_item_count) - prealloc_base - 1; |
| 197 | return std.math.log2_int_ceil(usize, box_count + prealloc_item_count) - prealloc_exp - 1; |
| 194 | 198 | } |
| 195 | 199 | |
| 196 | 200 | fn shelfSize(shelf_index: ShelfIndex) usize { |
| 197 | 201 | if (prealloc_item_count == 0) { |
| 198 | 202 | return usize(1) << shelf_index; |
| 199 | 203 | } |
| 200 | | return usize(1) << (shelf_index + (prealloc_base + 1)); |
| 204 | return usize(1) << (shelf_index + (prealloc_exp + 1)); |
| 201 | 205 | } |
| 202 | 206 | |
| 203 | 207 | fn shelfIndex(list_index: usize) ShelfIndex { |
| 204 | 208 | if (prealloc_item_count == 0) { |
| 205 | 209 | return std.math.log2_int(usize, list_index + 1); |
| 206 | 210 | } |
| 207 | | return std.math.log2_int(usize, list_index + prealloc_item_count) - prealloc_base - 1; |
| 211 | return std.math.log2_int(usize, list_index + prealloc_item_count) - prealloc_exp - 1; |
| 208 | 212 | } |
| 209 | 213 | |
| 210 | 214 | fn boxIndex(list_index: usize, shelf_index: ShelfIndex) usize { |
| 211 | 215 | if (prealloc_item_count == 0) { |
| 212 | 216 | return (list_index + 1) - (usize(1) << shelf_index); |
| 213 | 217 | } |
| 214 | | return list_index + prealloc_item_count - (usize(1) << ((prealloc_base + 1) + shelf_index)); |
| 218 | return list_index + prealloc_item_count - (usize(1) << ((prealloc_exp + 1) + shelf_index)); |
| 215 | 219 | } |
| 216 | 220 | |
| 217 | 221 | fn freeShelves(self: &Self, from_count: ShelfIndex, to_count: ShelfIndex) void { |