/** * Returns a deep copy of the specified deque. A deep copy is a copy of * both the deque structure and the data it holds. * * @note The new deque is allocated using the original deque's allocators * and also inherits the configuration of the original deque. * * @param[in] deque the deque to be copied * @param[in] cp the copy function that returns a copy of a deque element * * @return a deep copy of the specified deque */ Deque* deque_copy_deep(Deque *deque, void *(*cp) (void*)) { Deque *copy = deque->mem_alloc(sizeof(Deque)); copy->size = deque->size; copy->capacity = deque->capacity; copy->mem_alloc = deque->mem_alloc; copy->mem_calloc = deque->mem_calloc; copy->mem_free = deque->mem_free; copy->buffer = copy->mem_alloc(copy->capacity * sizeof(void*)); copy_buffer(deque, copy->buffer, cp); copy->first = 0; copy->last = copy->size; // XXX return copy; }
/** * Returns a new empty deque based on the specified DequeConf object. * * The deque is allocated using the allocators specified the DequeConf object * The allocation may fail if the underlying allocator fails. * * @param[in] conf Deque configuration object. All fields must be initialized * appropriate values. * * @return a new deque if the allocation was successful, or NULL if not. */ Deque *deque_new_conf(DequeConf *conf) { Deque *deque = conf->mem_calloc(1, sizeof(Deque)); if (deque == NULL) return NULL; deque->mem_alloc = conf->mem_alloc; deque->mem_calloc = conf->mem_calloc; deque->mem_free = conf->mem_free; deque->capacity = upper_pow_two(conf->capacity); deque->first = 0; deque->last = 0; deque->size = 0; deque->buffer = deque->mem_alloc(deque->capacity * sizeof(void*)); return deque; }
/** * Returns a shallow copy of the specified deque. A shallow copy is a copy of * the deque structure, but not the elements it holds. * * @note The new deque is allocated using the original deque's allocators * and also inherits the configuration of the original deque. * * @param[in] deque the deque to be copied * * @return a shallow copy of the specified deque */ Deque* deque_copy_shallow(Deque *deque) { Deque *copy = deque->mem_alloc(sizeof(Deque)); copy->size = deque->size; copy->capacity = deque->capacity; copy->mem_alloc = deque->mem_alloc; copy->mem_calloc = deque->mem_calloc; copy->mem_free = deque->mem_free; copy->buffer = copy->mem_alloc(copy->capacity * sizeof(void*)); copy_buffer(deque, copy->buffer, NULL); copy->first = 0; copy->last = copy->size; return copy; }