| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/priority_queue.h> |
| 7 |
|
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#define PARENT_OF(index) (((index) & 1) ? (index) >> 1 : (index) > 1 ? ((index) - 2) >> 1 : 0) |
| 11 |
#define LEFT_OF(index) (((index) << 1) + 1) |
| 12 |
#define RIGHT_OF(index) (((index) << 1) + 2) |
| 13 |
|
| 14 |
static void s_swap(struct aws_priority_queue *queue, size_t a, size_t b) { |
| 15 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 16 |
AWS_PRECONDITION(a < queue->container.length); |
| 17 |
AWS_PRECONDITION(b < queue->container.length); |
| 18 |
AWS_PRECONDITION(aws_priority_queue_backpointer_index_valid(queue, a)); |
| 19 |
AWS_PRECONDITION(aws_priority_queue_backpointer_index_valid(queue, b)); |
| 20 |
|
| 21 |
aws_array_list_swap(&queue->container, a, b); |
| 22 |
|
| 23 |
|
| 24 |
if (!AWS_IS_ZEROED(queue->backpointers)) { |
| 25 |
AWS_ASSERT(queue->backpointers.length > a); |
| 26 |
AWS_ASSERT(queue->backpointers.length > b); |
| 27 |
|
| 28 |
struct aws_priority_queue_node **bp_a = &((struct aws_priority_queue_node **)queue->backpointers.data)[a]; |
| 29 |
struct aws_priority_queue_node **bp_b = &((struct aws_priority_queue_node **)queue->backpointers.data)[b]; |
| 30 |
|
| 31 |
struct aws_priority_queue_node *tmp = *bp_a; |
| 32 |
*bp_a = *bp_b; |
| 33 |
*bp_b = tmp; |
| 34 |
|
| 35 |
if (*bp_a) { |
| 36 |
(*bp_a)->current_index = a; |
| 37 |
} |
| 38 |
|
| 39 |
if (*bp_b) { |
| 40 |
(*bp_b)->current_index = b; |
| 41 |
} |
| 42 |
} |
| 43 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 44 |
AWS_POSTCONDITION(aws_priority_queue_backpointer_index_valid(queue, a)); |
| 45 |
AWS_POSTCONDITION(aws_priority_queue_backpointer_index_valid(queue, b)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static bool s_sift_down(struct aws_priority_queue *queue, size_t root) { |
| 51 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 52 |
AWS_PRECONDITION(root < queue->container.length); |
| 53 |
|
| 54 |
bool did_move = false; |
| 55 |
|
| 56 |
size_t len = aws_array_list_length(&queue->container); |
| 57 |
|
| 58 |
while (LEFT_OF(root) < len) { |
| 59 |
size_t left = LEFT_OF(root); |
| 60 |
size_t right = RIGHT_OF(root); |
| 61 |
size_t first = root; |
| 62 |
void *first_item = NULL; |
| 63 |
void *other_item = NULL; |
| 64 |
|
| 65 |
aws_array_list_get_at_ptr(&queue->container, &first_item, root); |
| 66 |
aws_array_list_get_at_ptr(&queue->container, &other_item, left); |
| 67 |
|
| 68 |
if (queue->pred(first_item, other_item) > 0) { |
| 69 |
first = left; |
| 70 |
first_item = other_item; |
| 71 |
} |
| 72 |
|
| 73 |
if (right < len) { |
| 74 |
aws_array_list_get_at_ptr(&queue->container, &other_item, right); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if (queue->pred(first_item, other_item) > 0) { |
| 79 |
first = right; |
| 80 |
first_item = other_item; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (first != root) { |
| 85 |
s_swap(queue, first, root); |
| 86 |
did_move = true; |
| 87 |
root = first; |
| 88 |
} else { |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 94 |
return did_move; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
static bool s_sift_up(struct aws_priority_queue *queue, size_t index) { |
| 99 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 100 |
AWS_PRECONDITION(index < queue->container.length); |
| 101 |
|
| 102 |
bool did_move = false; |
| 103 |
|
| 104 |
void *parent_item = NULL; |
| 105 |
void *child_item = NULL; |
| 106 |
size_t parent = PARENT_OF(index); |
| 107 |
while (index) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
if (aws_array_list_get_at_ptr(&queue->container, &parent_item, parent) || |
| 114 |
aws_array_list_get_at_ptr(&queue->container, &child_item, index)) { |
| 115 |
abort(); |
| 116 |
} |
| 117 |
|
| 118 |
if (queue->pred(parent_item, child_item) > 0) { |
| 119 |
s_swap(queue, index, parent); |
| 120 |
did_move = true; |
| 121 |
index = parent; |
| 122 |
parent = PARENT_OF(index); |
| 123 |
} else { |
| 124 |
break; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 129 |
return did_move; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static void s_sift_either(struct aws_priority_queue *queue, size_t index) { |
| 137 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 138 |
AWS_PRECONDITION(index < queue->container.length); |
| 139 |
|
| 140 |
if (!index || !s_sift_up(queue, index)) { |
| 141 |
s_sift_down(queue, index); |
| 142 |
} |
| 143 |
|
| 144 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 145 |
} |
| 146 |
|
| 147 |
int aws_priority_queue_init_dynamic( |
| 148 |
struct aws_priority_queue *queue, |
| 149 |
struct aws_allocator *alloc, |
| 150 |
size_t default_size, |
| 151 |
size_t item_size, |
| 152 |
aws_priority_queue_compare_fn *pred) { |
| 153 |
|
| 154 |
AWS_FATAL_PRECONDITION(queue != NULL); |
| 155 |
AWS_FATAL_PRECONDITION(alloc != NULL); |
| 156 |
AWS_FATAL_PRECONDITION(item_size > 0); |
| 157 |
|
| 158 |
queue->pred = pred; |
| 159 |
AWS_ZERO_STRUCT(queue->backpointers); |
| 160 |
|
| 161 |
int ret = aws_array_list_init_dynamic(&queue->container, alloc, default_size, item_size); |
| 162 |
if (ret == AWS_OP_SUCCESS) { |
| 163 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 164 |
} else { |
| 165 |
AWS_POSTCONDITION(AWS_IS_ZEROED(queue->container)); |
| 166 |
AWS_POSTCONDITION(AWS_IS_ZEROED(queue->backpointers)); |
| 167 |
} |
| 168 |
return ret; |
| 169 |
} |
| 170 |
|
| 171 |
void aws_priority_queue_init_static( |
| 172 |
struct aws_priority_queue *queue, |
| 173 |
void *heap, |
| 174 |
size_t item_count, |
| 175 |
size_t item_size, |
| 176 |
aws_priority_queue_compare_fn *pred) { |
| 177 |
|
| 178 |
AWS_FATAL_PRECONDITION(queue != NULL); |
| 179 |
AWS_FATAL_PRECONDITION(heap != NULL); |
| 180 |
AWS_FATAL_PRECONDITION(item_count > 0); |
| 181 |
AWS_FATAL_PRECONDITION(item_size > 0); |
| 182 |
|
| 183 |
queue->pred = pred; |
| 184 |
AWS_ZERO_STRUCT(queue->backpointers); |
| 185 |
|
| 186 |
aws_array_list_init_static(&queue->container, heap, item_count, item_size); |
| 187 |
|
| 188 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 189 |
} |
| 190 |
|
| 191 |
bool aws_priority_queue_backpointer_index_valid(const struct aws_priority_queue *const queue, size_t index) { |
| 192 |
if (AWS_IS_ZEROED(queue->backpointers)) { |
| 193 |
return true; |
| 194 |
} |
| 195 |
if (index < queue->backpointers.length) { |
| 196 |
struct aws_priority_queue_node *node = ((struct aws_priority_queue_node **)queue->backpointers.data)[index]; |
| 197 |
return (node == NULL) || AWS_MEM_IS_WRITABLE(node, sizeof(struct aws_priority_queue_node)); |
| 198 |
} |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
bool aws_priority_queue_backpointers_valid_deep(const struct aws_priority_queue *const queue) { |
| 203 |
if (!queue) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
for (size_t i = 0; i < queue->backpointers.length; i++) { |
| 207 |
if (!aws_priority_queue_backpointer_index_valid(queue, i)) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
bool aws_priority_queue_backpointers_valid(const struct aws_priority_queue *const queue) { |
| 215 |
if (!queue) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
bool backpointer_list_is_valid = |
| 221 |
(aws_array_list_is_valid(&queue->backpointers) && (queue->backpointers.current_size != 0) && |
| 222 |
(queue->backpointers.data != NULL)); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
bool backpointer_list_item_size = queue->backpointers.item_size == sizeof(struct aws_priority_queue_node *); |
| 229 |
bool lists_equal_lengths = queue->backpointers.length == queue->container.length; |
| 230 |
bool backpointers_non_zero_current_size = queue->backpointers.current_size > 0; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
#if (AWS_DEEP_CHECKS == 1) |
| 235 |
bool backpointers_valid_deep = aws_priority_queue_backpointers_valid_deep(queue); |
| 236 |
#else |
| 237 |
bool backpointers_valid_deep = true; |
| 238 |
#endif |
| 239 |
bool backpointers_zero = |
| 240 |
(queue->backpointers.current_size == 0 && queue->backpointers.length == 0 && queue->backpointers.data == NULL); |
| 241 |
bool backpointer_struct_is_valid = |
| 242 |
backpointers_zero || (backpointer_list_item_size && lists_equal_lengths && backpointers_non_zero_current_size && |
| 243 |
backpointers_valid_deep); |
| 244 |
|
| 245 |
return ((backpointer_list_is_valid && backpointer_struct_is_valid) || AWS_IS_ZEROED(queue->backpointers)); |
| 246 |
} |
| 247 |
|
| 248 |
bool aws_priority_queue_is_valid(const struct aws_priority_queue *const queue) { |
| 249 |
|
| 250 |
if (!queue) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
bool pred_is_valid = (queue->pred != NULL); |
| 254 |
bool container_is_valid = aws_array_list_is_valid(&queue->container); |
| 255 |
|
| 256 |
bool backpointers_valid = aws_priority_queue_backpointers_valid(queue); |
| 257 |
return pred_is_valid && container_is_valid && backpointers_valid; |
| 258 |
} |
| 259 |
|
| 260 |
void aws_priority_queue_clean_up(struct aws_priority_queue *queue) { |
| 261 |
aws_array_list_clean_up(&queue->container); |
| 262 |
if (!AWS_IS_ZEROED(queue->backpointers)) { |
| 263 |
aws_array_list_clean_up(&queue->backpointers); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
int aws_priority_queue_push(struct aws_priority_queue *queue, void *item) { |
| 268 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 269 |
AWS_PRECONDITION(item && AWS_MEM_IS_READABLE(item, queue->container.item_size)); |
| 270 |
int rval = aws_priority_queue_push_ref(queue, item, NULL); |
| 271 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 272 |
return rval; |
| 273 |
} |
| 274 |
|
| 275 |
int aws_priority_queue_push_ref( |
| 276 |
struct aws_priority_queue *queue, |
| 277 |
void *item, |
| 278 |
struct aws_priority_queue_node *backpointer) { |
| 279 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 280 |
AWS_PRECONDITION(item && AWS_MEM_IS_READABLE(item, queue->container.item_size)); |
| 281 |
|
| 282 |
int err = aws_array_list_push_back(&queue->container, item); |
| 283 |
if (err) { |
| 284 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 285 |
return err; |
| 286 |
} |
| 287 |
size_t index = aws_array_list_length(&queue->container) - 1; |
| 288 |
|
| 289 |
if (backpointer && !queue->backpointers.alloc) { |
| 290 |
if (!queue->container.alloc) { |
| 291 |
aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 292 |
goto backpointer_update_failed; |
| 293 |
} |
| 294 |
|
| 295 |
if (aws_array_list_init_dynamic( |
| 296 |
&queue->backpointers, queue->container.alloc, index + 1, sizeof(struct aws_priority_queue_node *))) { |
| 297 |
goto backpointer_update_failed; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
memset(queue->backpointers.data, 0, queue->backpointers.current_size); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
if (!AWS_IS_ZEROED(queue->backpointers)) { |
| 310 |
if (aws_array_list_set_at(&queue->backpointers, &backpointer, index)) { |
| 311 |
goto backpointer_update_failed; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if (backpointer) { |
| 316 |
backpointer->current_index = index; |
| 317 |
} |
| 318 |
|
| 319 |
s_sift_up(queue, aws_array_list_length(&queue->container) - 1); |
| 320 |
|
| 321 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 322 |
return AWS_OP_SUCCESS; |
| 323 |
|
| 324 |
backpointer_update_failed: |
| 325 |
|
| 326 |
aws_array_list_pop_back(&queue->container); |
| 327 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 328 |
return AWS_OP_ERR; |
| 329 |
} |
| 330 |
|
| 331 |
static int s_remove_node(struct aws_priority_queue *queue, void *item, size_t item_index) { |
| 332 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 333 |
AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size)); |
| 334 |
if (aws_array_list_get_at(&queue->container, item, item_index)) { |
| 335 |
|
| 336 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 337 |
return AWS_OP_ERR; |
| 338 |
} |
| 339 |
|
| 340 |
size_t swap_with = aws_array_list_length(&queue->container) - 1; |
| 341 |
struct aws_priority_queue_node *backpointer = NULL; |
| 342 |
|
| 343 |
if (item_index != swap_with) { |
| 344 |
s_swap(queue, item_index, swap_with); |
| 345 |
} |
| 346 |
|
| 347 |
aws_array_list_pop_back(&queue->container); |
| 348 |
|
| 349 |
if (!AWS_IS_ZEROED(queue->backpointers)) { |
| 350 |
aws_array_list_get_at(&queue->backpointers, &backpointer, swap_with); |
| 351 |
if (backpointer) { |
| 352 |
backpointer->current_index = SIZE_MAX; |
| 353 |
} |
| 354 |
aws_array_list_pop_back(&queue->backpointers); |
| 355 |
} |
| 356 |
|
| 357 |
if (item_index != swap_with) { |
| 358 |
s_sift_either(queue, item_index); |
| 359 |
} |
| 360 |
|
| 361 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 362 |
return AWS_OP_SUCCESS; |
| 363 |
} |
| 364 |
|
| 365 |
int aws_priority_queue_remove( |
| 366 |
struct aws_priority_queue *queue, |
| 367 |
void *item, |
| 368 |
const struct aws_priority_queue_node *node) { |
| 369 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 370 |
AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size)); |
| 371 |
AWS_PRECONDITION(node && AWS_MEM_IS_READABLE(node, sizeof(struct aws_priority_queue_node))); |
| 372 |
AWS_ERROR_PRECONDITION( |
| 373 |
node->current_index < aws_array_list_length(&queue->container), AWS_ERROR_PRIORITY_QUEUE_BAD_NODE); |
| 374 |
AWS_ERROR_PRECONDITION(queue->backpointers.data, AWS_ERROR_PRIORITY_QUEUE_BAD_NODE); |
| 375 |
|
| 376 |
int rval = s_remove_node(queue, item, node->current_index); |
| 377 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 378 |
return rval; |
| 379 |
} |
| 380 |
|
| 381 |
int aws_priority_queue_pop(struct aws_priority_queue *queue, void *item) { |
| 382 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 383 |
AWS_PRECONDITION(item && AWS_MEM_IS_WRITABLE(item, queue->container.item_size)); |
| 384 |
AWS_ERROR_PRECONDITION(aws_array_list_length(&queue->container) != 0, AWS_ERROR_PRIORITY_QUEUE_EMPTY); |
| 385 |
|
| 386 |
int rval = s_remove_node(queue, item, 0); |
| 387 |
AWS_POSTCONDITION(aws_priority_queue_is_valid(queue)); |
| 388 |
return rval; |
| 389 |
} |
| 390 |
|
| 391 |
int aws_priority_queue_top(const struct aws_priority_queue *queue, void **item) { |
| 392 |
AWS_ERROR_PRECONDITION(aws_array_list_length(&queue->container) != 0, AWS_ERROR_PRIORITY_QUEUE_EMPTY); |
| 393 |
return aws_array_list_get_at_ptr(&queue->container, item, 0); |
| 394 |
} |
| 395 |
|
| 396 |
size_t aws_priority_queue_size(const struct aws_priority_queue *queue) { |
| 397 |
return aws_array_list_length(&queue->container); |
| 398 |
} |
| 399 |
|
| 400 |
size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue) { |
| 401 |
return aws_array_list_capacity(&queue->container); |
| 402 |
} |
| 403 |
|
| 404 |
void aws_priority_queue_clear(struct aws_priority_queue *queue) { |
| 405 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 406 |
size_t backpointer_count = aws_array_list_length(&queue->backpointers); |
| 407 |
for (size_t i = 0; i < backpointer_count; ++i) { |
| 408 |
struct aws_priority_queue_node *node = NULL; |
| 409 |
aws_array_list_get_at(&queue->backpointers, &node, i); |
| 410 |
if (node != NULL) { |
| 411 |
node->current_index = SIZE_MAX; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
aws_array_list_clear(&queue->backpointers); |
| 416 |
aws_array_list_clear(&queue->container); |
| 417 |
AWS_PRECONDITION(aws_priority_queue_is_valid(queue)); |
| 418 |
} |
| 419 |
|
| 420 |
void aws_priority_queue_node_init(struct aws_priority_queue_node *node) { |
| 421 |
node->current_index = SIZE_MAX; |
| 422 |
} |
| 423 |
|
| 424 |
bool aws_priority_queue_node_is_in_queue(const struct aws_priority_queue_node *node) { |
| 425 |
return node->current_index != SIZE_MAX; |
| 426 |
} |