cList *dict_key_value_pair(cDict *dict, Int i) { cList *l; if (i >= dict->keys->len) return NULL; l = list_new(2); l->len = 2; data_dup(&l->el[0], &dict->keys->el[i]); data_dup(&l->el[1], &dict->values->el[i]); return l; }
int bstree_insert_node(BStree bst, int key, char *data, int a){ //Printing error if the seeked array location is not possible if (a>bst->size){ printf("Array out of bound error\n"); return a; } // if the seeked location is free then save it else if (bst->is_free[a] == 1){ //dup method bst->tree_nodes->data = data_dup(data); //Making new node Node node = {key, data}; //Saving node to tree bst->tree_nodes = &node; //Position in is free array marked filled bst->is_free[a] = 0; } else if(bst->tree_nodes[a].key == key ){ return a;//Do nothing already found } //If the holding key is larger than inspection key then go right else if(bst->tree_nodes[a].key > key){ a = a*2 + 1; bstree_insert_node(bst, key, data, a); } //Otherwise the holding key is smaller than inspection key then go left else{ a = 2*a; bstree_insert_node(bst, key, data, a); } return 0; }
/* Error-checking on pos is the job of the calling function. */ cList *list_insert(cList * list, Int pos, cData * elem) { list = list_prep(list, list->start, list->len + 1); pos += list->start; MEMMOVE(list->el + pos + 1, list->el + pos, list->len - 1 - pos); data_dup(&list->el[pos], elem); return list; }
struct entry_t *entry_dup(struct entry_t *entry){ struct entry_t *entryCopy = (struct entry_t *) malloc (sizeof(struct entry_t)); entryCopy->key = strdup(entry->key); entryCopy->timestamp = entry->timestamp; entryCopy->value = data_dup(entry->value); return entryCopy; }
void dup_handled (cData *dest, cData *source) { HandledFrob *s = HANDLED_FROB(source), *d = TMALLOC(HandledFrob, 1); d->cclass = s->cclass; d->handler = ident_dup(s->handler); data_dup (&d->rep, &s->rep); dest->u.instance = d; }
/* Error-checking on pos is the job of the calling function. */ cList *list_replace(cList * list, Int pos, cData * elem) { /* list_prep needed here only for multiply referenced lists */ if (list->refs > 1) list = list_prep(list, list->start, list->len); pos += list->start; data_discard(&list->el[pos]); data_dup(&list->el[pos], elem); return list; }
Long dict_find(cDict *dict, cData *key, cData *ret) { Int pos; pos = search(dict, key); if (pos == F_FAILURE) return keynf_id; data_dup(ret, &dict->values->el[pos]); return NOT_AN_IDENT; }
cList *list_append(cList * list1, cList * list2) { Int i; cData *p, *q; list1 = list_prep(list1, list1->start, list1->len + list2->len); p = list1->el + list1->start + list1->len - list2->len; q = list2->el + list2->start; for (i = 0; i < list2->len; i++) data_dup(&p[i], &q[i]); return list1; }
cList *list_prep(cList * list, Int start, Int len) { cList *cnew; Int i, resize, size; /* Figure out if we need to resize the list or move its contents. Moving * contents takes precedence. */ #if DISABLED resize = (len - start) * 4 < list->size; resize = resize && list->size > STARTING_SIZE; resize = resize || (list->size < len); #endif resize = list->size < len + start; /* Move the list contents into a new list. */ if ((list->refs > 1) || (resize && start > 0)) { cnew = list_new(len); cnew->len = len; len = (list->len < len) ? list->len : len; for (i = 0; i < len; i++) data_dup(&cnew->el[i], &list->el[start + i]); list_discard(list); return cnew; } /* Resize the list. We can assume that list->start == start == 0. */ else if (resize) { for (; list->len > len; list->len--) data_discard(&list->el[list->len - 1]); list->len = len; size = len; list = (cList *) erealloc(list, sizeof(cList) + (size * sizeof(cData))); list->size = size; return list; } else { for (; list->start < start; list->start++, list->len--) data_discard(&list->el[list->start]); for (; list->len > len; list->len--) data_discard(&list->el[list->start + list->len - 1]); list->start = start; list->len = len; return list; } }
unsigned char * data_dup_len(const unsigned char * data, unsigned int len) { if (!data) return NULL; if (len <= 0) { return data_dup(data); } else { unsigned char * dup_data; dup_data = (unsigned char *)malloc(len * sizeof(unsigned char)); memcpy(dup_data, data, len); return dup_data; } }
int testDup() { int result, data_size = strlen("1234567890abc")+1; char *data_s = strdup("1234567890abc"); struct data_t *data = data_create2(data_size,data_s); struct data_t *data2 = data_dup(data); result = (data->data != data2->data) && (data->datasize == data2->datasize) && (memcmp(data->data, data2->data, data->datasize) == 0); data_destroy(data); data_destroy(data2); printf("Modulo data -> teste data_dup: %s\n",result?"passou":"nao passou"); return result; }
/* which is 1 for max, -1 for min. */ INTERNAL void find_extreme(Int which) { Int arg_start, num_args, i, type; cData *args, *extreme, d; arg_start = arg_starts[--arg_pos]; args = &stack[arg_start]; num_args = stack_pos - arg_start; if (!num_args) { cthrow(numargs_id, "Called with no arguments, requires at least one."); return; } type = args[0].type; if (type != INTEGER && type != STRING && type != FLOAT) { cthrow(type_id, "First argument (%D) not an integer, float or string.", &args[0]); return; } extreme = &args[0]; for (i = 1; i < num_args; i++) { if (args[i].type != type) { cthrow(type_id, "Arguments are not all of same type."); return; } if (data_cmp(&args[i], extreme) * which > 0) extreme = &args[i]; } /* Replace args[0] with extreme, and pop other arguments. */ data_dup(&d, extreme); data_discard(&args[0]); args[0] = d; pop(num_args - 1); }
cList *list_add(cList * list, cData * elem) { list = list_prep(list, list->start, list->len + 1); data_dup(&list->el[list->start + list->len - 1], elem); return list; }