void queue_test1() { Queue queue; queue_init(&queue, sizeof(int)); for (int i = 0; i < 10; i++) { queue_enque(&queue, &i); } for (int i = 0; i < 5; i++) { int item; queue_deque(&queue, &item); printf("%d ", item); } putchar('\n'); for (int i = 15; i < 30; i++) { queue_enque(&queue, &i); } for (int i = 0; i < 15; i++) { int item; queue_deque(&queue, &item); printf("%d ", item); } putchar('\n'); queue_destroy(&queue); }
void test_queue_enque() { struct _queue * q = queue_create(sizeof(int)); int i, size = 3; int items[3] = {0, 1, 2}; int item; for (i=0; i<size; ++i) { assert( q->n_items == i ); queue_enque(q, &items[i]); } queue_deque(q, &item); assert ( item == 0); assert ( q->n_items == 2); queue_deque(q, &item); assert ( item == 1); assert ( q->n_items == 1); queue_deque(q, &item); assert ( item == 2); assert ( q->n_items == 0); assert ( NULL == queue_deque(q, &item) ); assert ( NULL == queue_deque(q, &item) ); queue_remove(q); }
/* Linear time arcs matching for two SORTED arrays of arcs */ void match_full_sorted( struct _arc * a, struct _arc * b, arc_t M, arc_t N, struct _queue * q) { arc_t i = 0; arc_t j = 0; arc_t t; struct _match_item mi; while (i < M && j < N) { if ( a[i].olabel < b[j].ilabel ) ++i; else if ( a[i].olabel > b[j].ilabel ) ++j; else { for (t=j; a[i].olabel == b[t].ilabel; ++t ) { mi.a = a[i]; mi.b = b[t]; if (_match(a, b, i, t)) queue_enque(q, &mi); } ++i; } } }
void match_unsorted( struct _arc * a, struct _arc * b, arc_t M, arc_t N, struct _queue * q) { struct _match_item mi; for (arc_t i = 0; i < M; ++i) for (arc_t j = 0; j < N; ++j) if ( a[i].olabel == b[j].ilabel && _match(a, b, i, j)) { mi.a = a[i]; mi.b = b[j]; queue_enque(q, &mi); } }
// TODO: merge with previous method void match_half_sorted_rev( struct _arc * a, struct _arc * b, arc_t M, arc_t N, struct _queue * q) { arc_t l, h, ll, hh; arc_t i,m; struct _match_item mi; for (i=0; i < N; ++i) { l = 0; h = M-1; while ( l <= h ) { m = (l + h) >> 1; if (b[i].ilabel > a[m].olabel) l = m + 1; else if (b[i].ilabel < a[m].olabel) { if (m == 0) break; // sentinel for unsigned index h h = m - 1; } else { ll = hh = m; while ((ll > l) && (b[i].ilabel == a[ll-1].olabel)) --ll; while ((hh < h) && (b[i].ilabel == a[hh+1].olabel)) ++hh; while (ll <= hh ) { mi.a = a[ll]; mi.b = b[i]; if (_match(a, b, ll, i)) queue_enque(q, &mi); ++ll; } break; } } } }