You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.0 KiB

3 years ago
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ref_queue.h"
#include <stdio.h>
#include <stdlib.h>
ref_queue* new_ref_queue(int depth) {
ref_queue* result = malloc(sizeof(ref_queue));
result->data = malloc(sizeof(data_type) * depth);
result->depth = depth;
result->size = 0;
result->start = 0;
result->end = 0;
return result;
}
bool ref_enqueue(ref_queue* q, data_type v) {
if (q->size == q->depth) {
return false;
}
++q->size;
q->data[q->end] = v;
++q->end;
if (q->end == q->depth) {
q->end = 0;
}
// printf("ref_enqueue: %d\n", v);
return true;
}
data_type ref_dequeue(ref_queue* q) {
if (q->size == 0) {
return EMPTY;
}
data_type result = q->data[q->start];
++q->start;
if (q->start == q->depth) {
q->start = 0;
}
--q->size;
// printf("ref_dequeue: %d\n", result);
return result;
}
void print_ref_queue(ref_queue* q) {}