Пример #1
0
/**
 * Shrink deque at begin.
 */
void _deque_shrink_at_begin(deque_t* pdeq_deque, size_t t_shrinksize)
{
    deque_iterator_t it_oldbegin;
    deque_iterator_t it_newbegin;
    deque_iterator_t it_iter;
    _mappointer_t    ppby_map = NULL;
    bool_t           b_result = false;

    assert(pdeq_deque != NULL);
    assert(_deque_is_inited(pdeq_deque));

    it_oldbegin = deque_begin(pdeq_deque);
    t_shrinksize = t_shrinksize < deque_size(pdeq_deque) ? t_shrinksize : deque_size(pdeq_deque);
    it_newbegin = iterator_next_n(deque_begin(pdeq_deque), t_shrinksize);
    assert(_deque_iterator_belong_to_deque(pdeq_deque, it_newbegin));

    /* destroy all elements */
    for(it_iter = it_oldbegin; !iterator_equal(it_iter, it_newbegin); it_iter = iterator_next(it_iter))
    {
        b_result = _GET_DEQUE_TYPE_SIZE(pdeq_deque);
        _GET_DEQUE_TYPE_DESTROY_FUNCTION(pdeq_deque)(_deque_iterator_get_pointer_auxiliary(it_iter), &b_result);
        assert(b_result);
    }
    pdeq_deque->_t_start = it_newbegin;

    for(ppby_map = _GET_DEQUE_MAP_POINTER(pdeq_deque->_t_start) - 1; ppby_map >= _GET_DEQUE_MAP_POINTER(it_oldbegin); --ppby_map)
    {
        _alloc_deallocate(&pdeq_deque->_t_allocator, *ppby_map, _GET_DEQUE_TYPE_SIZE(pdeq_deque), _DEQUE_ELEM_COUNT);
        *ppby_map = NULL;
    }
}
Пример #2
0
TEST_C(DequeTests, DequeFilter3)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);
    deque_add_last(deque, &e);
    deque_add_last(deque, &f);
    CHECK_EQUAL_C_INT(6, deque_size(deque));

    Deque *filter = NULL;
    deque_filter(deque, pred3, &filter);
    const void * const* buff = deque_get_buffer(filter);

    CHECK_EQUAL_C_INT(1, deque_size(filter));
    CHECK_EQUAL_C_POINTER(buff[0], &f);

    free(filter);
};
Пример #3
0
TEST_C(DequeTests, DequeFilterMut1)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);
    deque_add_last(deque, &d);
    deque_add_last(deque, &e);
    deque_add_last(deque, &f);
    CHECK_EQUAL_C_INT(6, deque_size(deque));

    deque_filter_mut(deque, pred1);
    CHECK_EQUAL_C_INT(3, deque_size(deque));

    int *removed = NULL;
    deque_remove_first(deque, (void*) &removed);
    CHECK_EQUAL_C_INT(a, *removed);

    deque_remove_first(deque, (void*) &removed);
    CHECK_EQUAL_C_INT(b, *removed);

    deque_remove_first(deque, (void*) &removed);
    CHECK_EQUAL_C_INT(c, *removed);
};
Пример #4
0
TEST_C(DequeTests, DequeZipIterRemove)
{
    deque_add(deque, "a");
    deque_add(deque, "b");
    deque_add(deque, "c");
    deque_add(deque, "d");

    Deque *d2;
    deque_new(&d2);

    deque_add(d2, "e");
    deque_add(d2, "f");
    deque_add(d2, "g");

    DequeZipIter zip;
    deque_zip_iter_init(&zip, deque, d2);

    void *e1, *e2;
    void *r1, *r2;
    while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) {
        if (strcmp((char*) e1, "b") == 0)
            deque_zip_iter_remove(&zip, &r1, &r2);
    }
    CHECK_EQUAL_C_STRING("b", (char*)r1);
    CHECK_EQUAL_C_STRING("f", (char*)r2);
    CHECK_EQUAL_C_INT(0, deque_contains(deque, "b"));
    CHECK_EQUAL_C_INT(0, deque_contains(deque, "f"));
    CHECK_EQUAL_C_INT(3, deque_size(deque));
    CHECK_EQUAL_C_INT(2, deque_size(d2));
    deque_destroy(d2);
};
Пример #5
0
void pop_back_check_constant(size_t item_size, deque_t* d, int num, int val)
{
  int i;
  int j;
  ssize_t sz = deque_size(item_size, d);
  assert(item_size == 4);
  assert(sz >= num);
  for( i = 0; i < num; i++ ) {
    assert(deque_size(item_size, d) > 0);
    j = 0;
    deque_it_get_cur(4, deque_last(item_size, d), &j);
    assert( j == val );
    deque_pop_back(4, d);
  }
}
Пример #6
0
void pop_front_check_counting(size_t item_size, deque_t* d, int num)
{
  int i;
  int j;
  ssize_t sz = deque_size(item_size, d);
  assert(item_size == 4);
  assert(sz >= num);
  for( i = 0; i < num; i++ ) {
    assert(deque_size(item_size, d) > 0);
    j = 0;
    deque_it_get_cur(4, deque_begin(d), &j);
    assert( j == i );
    deque_pop_front(4, d);
  }
}
Пример #7
0
void pop_back_check_counting_reverse(size_t item_size, deque_t* d, int num)
{
  int i;
  int j;
  ssize_t sz = deque_size(item_size, d);
  assert(item_size == 4);
  assert(sz >= num);
  for( i = num - 1; i >= 0; i-- ) {
    assert(deque_size(item_size, d) > 0);
    j = 0;
    deque_it_get_cur(4, deque_last(item_size, d), &j);
    assert( j == i );
    deque_pop_back(4, d);
  }
}
Пример #8
0
void iterate_counting_reverse(size_t item_size, deque_t* d, int num)
{
  deque_iterator_t cur;
  deque_iterator_t first = deque_begin(d);
  deque_iterator_t last;
  deque_iterator_t test;
  int i = 0;
  int j;

  assert(item_size == 4);

  if( num == 0 ) return;
  if( deque_size(item_size, d) == 0 ) return;

  last = deque_last(item_size, d);
  cur = last;
  do {
    // check that the value is right.
    deque_it_get_cur(item_size, cur, &j);
    assert( j == i );

    // check that advancing from end -i elements takes us here.
    test = last;
    deque_it_forward_n(item_size, &test, - i);
    assert( deque_it_equals( test, cur ) );

    // advance to the next element, cheking that
    // back_one is the same as forward_n(-1)
    test = cur;
    deque_it_back_one(item_size, &cur);
    deque_it_forward_n(item_size, &test, -1);
    assert( deque_it_equals( test, cur ) );
    i++;
  } while ( i < num && ! deque_it_equals(cur,first) ); 
}
Пример #9
0
void pop_back_check_counting(size_t item_size, deque_t* d, int num)
{
  int i;
  int j;
  ssize_t sz = deque_size(item_size, d);
  assert(item_size == 4);
  assert(sz >= num);
  for( i = 0; i < num; i++ ) {
    assert(deque_size(item_size, d) > 0);
    j = 0;
    deque_it_get_cur(4, deque_last(item_size, d), &j);
    //printf("Got %i\n", j);
    assert( j == i );
    deque_pop_back(4, d);
  }
}
Пример #10
0
static void
deque_test_enqueue_dequeue(deque_t q,
                           void (*in)(deque_t, element_t),
                           element_t (*out)(deque_t),
                           int rand_n, double rand_d)
{
    int i;
    double* d;

    SHOW_FUNC_INFO(in, out);
    srand(time(0));
    for (i = 0; i < 10; ++i) {
        d = (double*)malloc(sizeof(*d));
        *d = rand() % rand_n * rand_d;
        in(q, d);
        fprintf(stdout, "[%02d] deque enqueue element : %lf\n", i, *d);
    }
    fprintf(stdout, "deque size is : %d ===>\n", deque_size(q));
    d = deque_peek_front(q);
    fprintf(stdout, "\tfront element of deque : %lf\n", *d);
    d = deque_peek_rear(q);
    fprintf(stdout, "\trear element of deque: %lf\n", *d);
    while (!deque_empty(q)) {
        d = out(q);
        fprintf(stdout, "deque dequeue element : %lf\n", *d);
        free(d);
    }
}
Пример #11
0
TEST_C(DequeTests, DequeCopyDeep)
{
    int *a = malloc(sizeof(int));
    int *b = malloc(sizeof(int));
    int *c = malloc(sizeof(int));

    *a = 1;
    *b = 2;
    *c = 3;

    deque_add_last(deque, a);
    deque_add_last(deque, b);
    deque_add_last(deque, c);

    Deque *copy;
    deque_copy_deep(deque, cpy, &copy);

    int size = deque_size(copy);
    CHECK_EQUAL_C_INT(3, size);

    int *ca;
    deque_get_at(copy, 0, (void*)&ca);
    int *cb;
    deque_get_at(copy, 1, (void*)&cb);
    int *cc;
    deque_get_at(copy, 2, (void*)&cc);

    CHECK_EQUAL_C_INT(1, *ca);
    CHECK_EQUAL_C_INT(2, *cb);
    CHECK_EQUAL_C_INT(3, *cc);
    deque_destroy_cb(copy, free);
    free(a);
    free(b);
    free(c);
}
Пример #12
0
TEST_C(DequeTests, DequeCopyShallow)
{
    int a = 1;
    int b = 2;
    int c = 3;

    deque_add_last(deque, &a);
    deque_add_last(deque, &b);
    deque_add_last(deque, &c);

    Deque *copy;
    deque_copy_shallow(deque, &copy);

    int size = deque_size(copy);
    CHECK_EQUAL_C_INT(3, size);

    int *ca;
    deque_get_at(copy, 0, (void*)&ca);

    int *cb;
    deque_get_at(copy, 1, (void*)&cb);

    int *cc;
    deque_get_at(copy, 2, (void*)&cc);

    CHECK_EQUAL_C_INT(a, *ca);
    CHECK_EQUAL_C_INT(b, *cb);
    CHECK_EQUAL_C_INT(c, *cc);
    deque_destroy(copy);
};
Пример #13
0
int deque_pop_front( deque dq, TYPE *item){
  if( deque_empty( dq) ){
    return DEQUE_EMPTY;
  }
  *item = dq->cbuffer[dq->front];
  if(dq->front == dq->cbuffersize -1){
    dq->front = 0;
  }else{
    dq->front++;
  }
  if( (double) deque_size(dq) / (double) dq->cbuffersize < 0.25 &&
      deque_size( dq) > MINSIZE ){
    if( deque_shrink( dq) == DEQUE_MEM_ERROR ){
      return DEQUE_MEM_ERROR;
    }
  }
  return DEQUE_SUCCESS;
}
Пример #14
0
int deque_pop_back( deque dq, TYPE *item){
  if( deque_empty( dq) ){
    return DEQUE_EMPTY;
  }
  if(dq->back == 0 ){ 
    dq->back = dq->cbuffersize-1;
  }else{
    dq->back--;
  }
  *item = dq->cbuffer[dq->back];
  if( (double) deque_size(dq) / (double) dq->cbuffersize < 0.25  &&
      deque_size( dq) > MINSIZE ){
    if( deque_shrink( dq) == DEQUE_MEM_ERROR ){
      return DEQUE_MEM_ERROR;
    }
  }
  return DEQUE_SUCCESS;
}
Пример #15
0
/**
 * Return the number of elements in a queue.
 */
size_t queue_size(const queue_t* cpque_queue)
{
    assert(cpque_queue != NULL);

#ifdef CSTL_QUEUE_LIST_SEQUENCE
    return list_size(&cpque_queue->_t_sequence);
#else
    return deque_size(&cpque_queue->_t_sequence);
#endif
}
Пример #16
0
TEST_C(DequeTests, DequeZipIterAdd)
{
    deque_add(deque, "a");
    deque_add(deque, "b");
    deque_add(deque, "c");
    deque_add(deque, "d");

    Deque *d2;
    deque_new(&d2);

    deque_add(d2, "e");
    deque_add(d2, "f");
    deque_add(d2, "g");

    char *h = "h";
    char *i = "i";

    DequeZipIter zip;
    deque_zip_iter_init(&zip, deque, d2);

    void *e1, *e2;
    while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) {
        if (strcmp((char*) e1, "b") == 0)
            deque_zip_iter_add(&zip, h, i);
    }

    size_t index;
    deque_index_of(deque, "h", &index);

    CHECK_EQUAL_C_INT(2, index);

    deque_index_of(deque, "i", &index);
    CHECK_EQUAL_C_INT(2, index);

    deque_index_of(deque, "c", &index);
    CHECK_EQUAL_C_INT(3, index);
    CHECK_EQUAL_C_INT(1, deque_contains(deque, "h"));
    CHECK_EQUAL_C_INT(1, deque_contains(d2, "i"));
    CHECK_EQUAL_C_INT(5, deque_size(deque));
    CHECK_EQUAL_C_INT(4, deque_size(d2));
    deque_destroy(d2);
};
Пример #17
0
TEST_C(DequeTests, DequeIteratorAdd)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    int g = 7;

    deque_add(deque, &a);
    deque_add(deque, &b);
    deque_add(deque, &c);
    deque_add(deque, &d);
    deque_add(deque, &e);
    deque_add(deque, &f);

    DequeIter iter;
    deque_iter_init(&iter, deque);

    size_t i = 0;

    int *el;

    CHECK_EQUAL_C_INT(6, deque_size(deque));

    while (deque_iter_next(&iter, (void*) &el) != CC_ITER_END) {
        if (*el == d)
            deque_iter_add(&iter, &g);
        if (i >= 3) {
          CHECK_EQUAL_C_INT(i, deque_iter_index(&iter) - 1);
        }
        i++;
    }
    CHECK_EQUAL_C_INT(7, deque_size(deque));

    void *ret;
    deque_get_at(deque, 4, &ret);
    CHECK_EQUAL_C_INT(g, *(int*)ret);
};
Пример #18
0
/**
 * Return the number of elements in a stack.
 */
size_t stack_size(const cstl_stack_t* cpsk_stack)
{
    assert(cpsk_stack != NULL);

#if defined (CSTL_STACK_VECTOR_SEQUENCE)
    return vector_size(&cpsk_stack->_t_sequence);
#elif defined (CSTL_STACK_LIST_SEQUENCE)
    return list_size(&cpsk_stack->_t_sequence);
#else
    return deque_size(&cpsk_stack->_t_sequence);
#endif
}
Пример #19
0
int deque_get_item( const deque dq, int pos, TYPE *item){
  int newpos;
  if( deque_empty( dq) ){
    return DEQUE_EMPTY;
  }
  if( pos >= deque_size( dq) ){
    return DEQUE_OUT_OF_BOUNDS;
  }
  newpos =  (dq->front+pos) % dq->cbuffersize;
  *item = dq->cbuffer[newpos];
  return DEQUE_SUCCESS;
}
Пример #20
0
void qbuffer_extend_front(qbuffer_t* buf)
{
  if( deque_size(sizeof(qbuffer_part_t), &buf->deque) > 0 ) {
    // Get the first part.
    qbuffer_part_t* qbp = (qbuffer_part_t*) deque_it_get_cur_ptr( sizeof(qbuffer_part_t), deque_begin(& buf->deque));
    if( (qbp->flags & QB_PART_FLAGS_EXTENDABLE_TO_ENTIRE_BYTES) &&
        qbp->skip_bytes > 0 ) {
      qbp->len_bytes = qbp->bytes->len;
      qbp->skip_bytes = 0;
      buf->offset_start = qbp->end_offset - qbp->len_bytes;
    }
  }
}
Пример #21
0
void qbuffer_extend_back(qbuffer_t* buf)
{
  if( deque_size(sizeof(qbuffer_part_t), &buf->deque) > 0 ) {
    // Get the last part.
    qbuffer_part_t* qbp = (qbuffer_part_t*) deque_it_get_cur_ptr( sizeof(qbuffer_part_t), deque_last(sizeof(qbuffer_part_t), &buf->deque));
    if( (qbp->flags & QB_PART_FLAGS_EXTENDABLE_TO_ENTIRE_BYTES) &&
        qbp->len_bytes < qbp->bytes->len ) {
      qbp->end_offset = (qbp->end_offset - qbp->len_bytes) + qbp->bytes->len;
      qbp->len_bytes = qbp->bytes->len;
      buf->offset_end = qbp->end_offset;
    }
  }
}
Пример #22
0
int deque_push_front( deque dq, TYPE item){
  if( deque_size( dq) == dq->cbuffersize-1 ){
    if( deque_grow( dq) == DEQUE_MEM_ERROR ){
      return DEQUE_MEM_ERROR;
    }
  }
  if(dq->front == 0 ){ 
    dq->front = dq->cbuffersize-1;
  }else{
    dq->front--;
  }
  dq->cbuffer[dq->front] = item;
  return DEQUE_SUCCESS;
}
Пример #23
0
int deque_push_back( deque dq, TYPE item){
  if( deque_size( dq) == dq->cbuffersize-1 ){
    if( deque_grow( dq) == DEQUE_MEM_ERROR ){
      return DEQUE_MEM_ERROR;
    }
  }
  dq->cbuffer[dq->back] = item;
  if(dq->back == dq->cbuffersize -1){
    dq->back = 0;
  }else{
    dq->back++;
  }
  return DEQUE_SUCCESS;
}
Пример #24
0
TEST_C(DequeTests, DequeSize)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;

    deque_add(deque, &a);
    deque_add(deque, &b);
    deque_add(deque, &c);
    deque_add(deque, &d);

    size_t size = deque_size(deque);
    CHECK_EQUAL_C_INT(4, size);
};
Пример #25
0
TEST_C(DequeTests, DequeIteratorRemove)
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    deque_add(deque, &a);
    deque_add(deque, &b);
    deque_add(deque, &c);
    deque_add(deque, &d);
    deque_add(deque, &e);
    deque_add(deque, &f);

    DequeIter iter;
    deque_iter_init(&iter, deque);

    size_t i = 0;
    void *el;
    while (deque_iter_next(&iter, &el) != CC_ITER_END) {
        if (i == 3)
            deque_iter_remove(&iter, NULL);

        if (i > 2) {
          CHECK_EQUAL_C_INT(5, deque_size(deque));
        } else {
          CHECK_EQUAL_C_INT(6, deque_size(deque));
        }
        if (i >= 3) {
          CHECK_EQUAL_C_INT(i-1, deque_iter_index(&iter));
        }
        i++;
    }
};
Пример #26
0
int main(void)
{
    deque_t *deque;
    int errcode,integer,integer_;
    size_t counter = 0;
    deque = deque_initialize(sizeof(int),DEFAULT_RELEASE_FUNCTION);
    if(!deque){
        fputs("Error!\n",stderr);
        return -1;
    }
    while(counter != 64){
        integer = counter;
        errcode = deque_push_front(deque,&integer);
        if(errcode){
            deque_release(deque);
            fputs("Error!\n",stderr);
            return -1;
        }
        integer = counter+10;
        errcode = deque_push_back(deque,&integer);
        if(errcode){
            deque_release(deque);
            fputs("Error!\n",stderr);
            return -1;
        }
        counter++;
    }
    counter = 0;
    while(counter != deque_size(deque)){
        errcode = deque_refer_from_front(deque,counter,&integer);
        if(errcode){
            deque_release(deque);
            fputs("Error!\n",stderr);
            return -1;
        }
        errcode = deque_refer_from_back(deque,counter,&integer_);
        if(errcode){
            deque_release(deque);
            fputs("Error!\n",stderr);
            return -1;
        }
        fprintf(stdout,"%03d : %03d,%03d\n",counter,integer,integer_);
        counter++;
    }
    deque_release(deque);
    return 0;
}
Пример #27
0
static int deque_basic() {
  Deque d[1];
  deque_init(d, sizeof(int), 10);

  *(int*)deque_pushback(d) = 2;
  *(int*)deque_pushback(d) = 3;
  *(int*)deque_pushfront(d) = 1;
  *(int*)deque_pushback(d) = 4;
  *(int*)deque_pushfront(d) = 0;

  for (int i = 0; i <= 4; i++) {
    int val = *(int*)deque_front(d);
    deque_popfront(d);
    assertEqual(i, val);
    assertEqual(4-i, deque_size(d));
  }

  deque_close(d);
  return 0;
}
Пример #28
0
void qbuffer_trim_back(qbuffer_t* buf, int64_t remove_bytes)
{
  int64_t new_end = buf->offset_end - remove_bytes;

  if( remove_bytes == 0 ) return;
  assert( remove_bytes > 0 );
  assert( new_end >= buf->offset_start );

  // Go through the deque removing entire parts.
  while( deque_size(sizeof(qbuffer_part_t), &buf->deque) > 0 ) {
    // Get the last part.
    qbuffer_part_t* qbp = (qbuffer_part_t*) deque_it_get_cur_ptr( sizeof(qbuffer_part_t), deque_last(sizeof(qbuffer_part_t), &buf->deque));

    if( qbp->end_offset > new_end ) {
      // we might remove it entirely, or maybe
      // we just adjust its length and skip.
      if( qbp->end_offset - qbp->len_bytes >= new_end ) {
        qbytes_t* bytes = qbp->bytes;
        // starts entirely after new_end, remove the chunk.
        // Remove it from the deque
        deque_pop_front(sizeof(qbuffer_part_t), &buf->deque);
        // release the bytes.
        qbytes_release(bytes);
      } else {
        // Keep only a part of this chunk.
        int64_t remove_here = qbp->end_offset - new_end;
        qbp->len_bytes -= remove_here;
        qbp->end_offset -= remove_here;
        break; // this is the last one.
      }
    } else {
      break; // we're past
    }
  }

  // Now set the new offset.
  buf->offset_end = new_end;

  // invalidate cached entries.
  qbuffer_clear_cached(buf);
}
Пример #29
0
void qbuffer_trim_front(qbuffer_t* buf, int64_t remove_bytes)
{
  int64_t new_start = buf->offset_start + remove_bytes;

  if( remove_bytes == 0 ) return;
  
  assert( remove_bytes > 0 );
  assert( new_start <= buf->offset_end );

  while( deque_size(sizeof(qbuffer_part_t), &buf->deque) > 0 ) {
    // Get the first part.
    qbuffer_part_t* qbp = (qbuffer_part_t*) deque_it_get_cur_ptr( sizeof(qbuffer_part_t), deque_begin(& buf->deque));

    if( qbp->end_offset - qbp->len_bytes < new_start ) {
      // we might remove it entirely, or maybe
      // we just adjust its length and skip.
      if( qbp->end_offset <= new_start ) {
        qbytes_t* bytes = qbp->bytes;
        // ends entirely before new_start, remove the chunk.
        // Remove it from the deque
        deque_pop_front(sizeof(qbuffer_part_t), &buf->deque);
        // release the bytes.
        qbytes_release(bytes);
      } else {
        // Keep only a part of this chunk.
        int64_t remove_here = new_start - (qbp->end_offset - qbp->len_bytes);
        qbp->skip_bytes += remove_here;
        qbp->len_bytes -= remove_here;
        break; // this is the last one.
      }
    } else {
      break; // we're past
    }
  }

  // Now set the new offset.
  buf->offset_start = new_start;

  // invalidate cached entries.
  qbuffer_clear_cached(buf);
}
Пример #30
0
TEST_C(DequeTests, DequeRemoveAll)
{
    int a = 1;
    int b = 2;
    int c = 3;

    deque_add(deque, &a);
    deque_add(deque, &b);
    deque_add(deque, &c);

    deque_remove_all(deque);

    void *first;
    int stat1 = deque_get_first(deque, &first);
    void *last;
    int stat2 = deque_get_last(deque, &last);

    CHECK_EQUAL_C_INT(CC_ERR_OUT_OF_RANGE, stat1);
    CHECK_EQUAL_C_INT(CC_ERR_OUT_OF_RANGE, stat2);

    CHECK_EQUAL_C_INT(0, deque_size(deque));
};