| ... | @@ -40,8 +40,9 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -40,8 +40,9 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 40 | } | 40 | } |
| 41 | | 41 | |
| 42 | /// Free memory used by the dequeue. | 42 | /// Free memory used by the dequeue. |
| 43 | pub fn deinit(self: Self, allocator: Allocator) void { | 43 | pub fn deinit(self: *Self, allocator: Allocator) void { |
| 44 | allocator.free(self.items); | 44 | allocator.free(self.items); |
| | 45 | self.* = undefined; |
| 45 | } | 46 | } |
| 46 | | 47 | |
| 47 | /// Insert a new element, maintaining priority. | 48 | /// Insert a new element, maintaining priority. |
| ... | @@ -77,7 +78,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -77,7 +78,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 77 | return 1 == @clz(index +% 1) & 1; | 78 | return 1 == @clz(index +% 1) & 1; |
| 78 | } | 79 | } |
| 79 | | 80 | |
| 80 | fn nextIsMinLayer(self: Self) bool { | 81 | fn nextIsMinLayer(self: *const Self) bool { |
| 81 | return isMinLayer(self.len); | 82 | return isMinLayer(self.len); |
| 82 | } | 83 | } |
| 83 | | 84 | |
| ... | @@ -86,7 +87,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -86,7 +87,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 86 | min_layer: bool, | 87 | min_layer: bool, |
| 87 | }; | 88 | }; |
| 88 | | 89 | |
| 89 | fn getStartForSiftUp(self: Self, child: T, index: usize) StartIndexAndLayer { | 90 | fn getStartForSiftUp(self: *const Self, child: T, index: usize) StartIndexAndLayer { |
| 90 | const child_index = index; | 91 | const child_index = index; |
| 91 | const parent_index = parentIndex(child_index); | 92 | const parent_index = parentIndex(child_index); |
| 92 | const parent = self.items[parent_index]; | 93 | const parent = self.items[parent_index]; |
| ... | @@ -136,20 +137,20 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -136,20 +137,20 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 136 | | 137 | |
| 137 | /// Look at the smallest element in the dequeue. Returns | 138 | /// Look at the smallest element in the dequeue. Returns |
| 138 | /// `null` if empty. | 139 | /// `null` if empty. |
| 139 | pub fn peekMin(self: *Self) ?T { | 140 | pub fn peekMin(self: *const Self) ?T { |
| 140 | return if (self.len > 0) self.items[0] else null; | 141 | return if (self.len > 0) self.items[0] else null; |
| 141 | } | 142 | } |
| 142 | | 143 | |
| 143 | /// Look at the largest element in the dequeue. Returns | 144 | /// Look at the largest element in the dequeue. Returns |
| 144 | /// `null` if empty. | 145 | /// `null` if empty. |
| 145 | pub fn peekMax(self: *Self) ?T { | 146 | pub fn peekMax(self: *const Self) ?T { |
| 146 | if (self.len == 0) return null; | 147 | if (self.len == 0) return null; |
| 147 | if (self.len == 1) return self.items[0]; | 148 | if (self.len == 1) return self.items[0]; |
| 148 | if (self.len == 2) return self.items[1]; | 149 | if (self.len == 2) return self.items[1]; |
| 149 | return self.bestItemAtIndices(1, 2, .gt).item; | 150 | return self.bestItemAtIndices(1, 2, .gt).item; |
| 150 | } | 151 | } |
| 151 | | 152 | |
| 152 | fn maxIndex(self: Self) ?usize { | 153 | fn maxIndex(self: *const Self) ?usize { |
| 153 | if (self.len == 0) return null; | 154 | if (self.len == 0) return null; |
| 154 | if (self.len == 1) return 0; | 155 | if (self.len == 1) return 0; |
| 155 | if (self.len == 2) return 1; | 156 | if (self.len == 2) return 1; |
| ... | @@ -261,14 +262,14 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -261,14 +262,14 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 261 | index: usize, | 262 | index: usize, |
| 262 | }; | 263 | }; |
| 263 | | 264 | |
| 264 | fn getItem(self: Self, index: usize) ItemAndIndex { | 265 | fn getItem(self: *const Self, index: usize) ItemAndIndex { |
| 265 | return .{ | 266 | return .{ |
| 266 | .item = self.items[index], | 267 | .item = self.items[index], |
| 267 | .index = index, | 268 | .index = index, |
| 268 | }; | 269 | }; |
| 269 | } | 270 | } |
| 270 | | 271 | |
| 271 | fn bestItem(self: Self, item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex { | 272 | fn bestItem(self: *const Self, item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex { |
| 272 | if (compareFn(self.context, item1.item, item2.item) == target_order) { | 273 | if (compareFn(self.context, item1.item, item2.item) == target_order) { |
| 273 | return item1; | 274 | return item1; |
| 274 | } else { | 275 | } else { |
| ... | @@ -276,13 +277,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -276,13 +277,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 276 | } | 277 | } |
| 277 | } | 278 | } |
| 278 | | 279 | |
| 279 | fn bestItemAtIndices(self: Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex { | 280 | fn bestItemAtIndices(self: *const Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex { |
| 280 | const item1 = self.getItem(index1); | 281 | const item1 = self.getItem(index1); |
| 281 | const item2 = self.getItem(index2); | 282 | const item2 = self.getItem(index2); |
| 282 | return self.bestItem(item1, item2, target_order); | 283 | return self.bestItem(item1, item2, target_order); |
| 283 | } | 284 | } |
| 284 | | 285 | |
| 285 | fn bestDescendent(self: Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex { | 286 | fn bestDescendent(self: *const Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex { |
| 286 | const second_child_index = first_child_index + 1; | 287 | const second_child_index = first_child_index + 1; |
| 287 | if (first_grandchild_index >= self.len) { | 288 | if (first_grandchild_index >= self.len) { |
| 288 | // No grandchildren, find the best child (second may not exist) | 289 | // No grandchildren, find the best child (second may not exist) |
| ... | @@ -314,13 +315,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -314,13 +315,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 314 | } | 315 | } |
| 315 | | 316 | |
| 316 | /// Return the number of elements remaining in the dequeue | 317 | /// Return the number of elements remaining in the dequeue |
| 317 | pub fn count(self: Self) usize { | 318 | pub fn count(self: *const Self) usize { |
| 318 | return self.len; | 319 | return self.len; |
| 319 | } | 320 | } |
| 320 | | 321 | |
| 321 | /// Return the number of elements that can be added to the | 322 | /// Return the number of elements that can be added to the |
| 322 | /// dequeue before more memory is allocated. | 323 | /// dequeue before more memory is allocated. |
| 323 | pub fn capacity(self: Self) usize { | 324 | pub fn capacity(self: *const Self) usize { |
| 324 | return self.items.len; | 325 | return self.items.len; |
| 325 | } | 326 | } |
| 326 | | 327 | |