/* duplicate hostent entry * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*. * Dito for protoent and servent. */ static SEGPTR ws_hostent_32_to_16( const struct WS_hostent* he ) { char *p; SEGPTR base; struct ws_hostent16 *p_to; int size = (sizeof(*he) + strlen(he->h_name) + 1 + list_size(he->h_aliases, 0) + list_size(he->h_addr_list, he->h_length)); base = get_buffer_he(size); p_to = MapSL(base); p_to->h_addrtype = he->h_addrtype; p_to->h_length = he->h_length; p = (char *)(p_to + 1); p_to->h_name = base + (p - (char *)p_to); strcpy(p, he->h_name); p += strlen(p) + 1; p_to->h_aliases = base + (p - (char *)p_to); p += list_dup(he->h_aliases, p_to->h_aliases, 0); p_to->h_addr_list = base + (p - (char *)p_to); list_dup(he->h_addr_list, p_to->h_addr_list, he->h_length); return base; }
cDict *dict_new(cList *keys, cList *values) { cDict *cnew; Int i, j, size; if (generic_empty_dict && list_length(keys) == 0) return dict_dup(generic_empty_dict); /* Construct a new dictionary. */ cnew = tmalloc(sizeof(cDict)); cnew->keys = list_dup(keys); cnew->values = list_dup(values); /* Calculate initial size of chain and hash table. */ cnew->hashtab_size = HASHTAB_STARTING_SIZE; while (cnew->hashtab_size < keys->len) { if (cnew->hashtab_size > 4096) cnew->hashtab_size += 4096; else cnew->hashtab_size = cnew->hashtab_size * 2 + MALLOC_DELTA; } /* Initialize chain entries and hash table. */ size = sizeof(Int) * cnew->hashtab_size; cnew->links = tmalloc(size); cnew->hashtab = tmalloc(size); memset(cnew->links, -1, size); memset(cnew->hashtab, -1, size); /* Insert the keys into the hash table, eliminating duplicates. */ i = j = 0; while (i < cnew->keys->len) { if (i != j) { cnew->keys->el[j] = cnew->keys->el[i]; cnew->values->el[j] = cnew->values->el[i]; } if (search(cnew, &keys->el[i]) == F_FAILURE) { insert_key(cnew, j++); } else { data_discard(&cnew->keys->el[i]); data_discard(&cnew->values->el[i]); } i++; } cnew->keys->len = cnew->values->len = j; cnew->refs = 1; if (!generic_empty_dict && list_length(keys) == 0) generic_empty_dict = dict_dup(cnew); return cnew; }
int main(int argc, char *argv[]) { list_type type; type.dup = node_dup; type.free = node_free; type.compare = node_compare; list_t *list = list_create(&type); for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", i); list_add_node_tail(list, value); printf("add %s\n", value); sdsfree(value); } for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", 10 + i); list_add_node_head(list, value); printf("add %s\n", value); sdsfree(value); } list_node *node = list_index(list, 10); sds value = sdsempty(); value = sdscatprintf(value, "%d", 100); list_insert_node(list, node, value, 1); printf("insert %s\n", value); node = list_find(list, value); printf("search: %s\n", (char *)node->value); sdsfree(value); for (int i = -10; i < 10; ++i) { node = list_index(list, i); if (node) { printf("%d: %s\n", i, (char *)node->value); } } list_t *copy = list_dup(list); list_release(list); printf("len: %ld\n", list_len(copy)); list_rotate(copy); list_rotate(copy); list_rotate(copy); list_iter *iter = list_get_iterator(copy, LIST_START_HEAD); while ((node = list_next(iter)) != NULL) { printf("%s\n", (char *)node->value); list_del(copy, node); } list_release_iterator(iter); list_release(copy); printf("len: %ld\n", list_len(copy)); printf("head: %p, tail: %p\n", copy->head, copy->tail); return 0; }
struct expr * cdr(struct expr * e, struct context * ctx) { struct list *nl; struct expr *a, *re; if (!e) return empty_list(); if (list_len(e) < 2) { free_expr(e); return empty_list(); } a = eval(e->v.list->next->v, ctx); if (a->t != LLIST || a->v.list == NULL) { full_free_expr(a); free_expr(e); return empty_list(); } nl = list_dup(a->v.list->next); if (!nl) { re = empty_list(); } else { re = new_expr(LLIST); re->v.list = nl; } full_free_expr(a); free_expr(e); return re; }
static SEGPTR ws_servent_32_to_16( const struct WS_servent *se ) { char *p; SEGPTR base; struct ws_servent16 *p_to; int size = (sizeof(*se) + strlen(se->s_proto) + 1 + strlen(se->s_name) + 1 + list_size(se->s_aliases, 0)); base = get_buffer_se(size); p_to = MapSL(base); p_to->s_port = se->s_port; p = (char *)(p_to + 1); p_to->s_name = base + (p - (char *)p_to); strcpy(p, se->s_name); p += strlen(p) + 1; p_to->s_proto = base + (p - (char *)p_to); strcpy(p, se->s_proto); p += strlen(p) + 1; p_to->s_aliases = base + (p - (char *)p_to); list_dup(se->s_aliases, p_to->s_aliases, 0); return base; }
static SEGPTR ws_protoent_32_to_16( const struct WS_protoent *pe ) { char *p; SEGPTR base; struct ws_protoent16 *p_to; int size = (sizeof(*pe) + strlen(pe->p_name) + 1 + list_size(pe->p_aliases, 0)); base = get_buffer_pe(size); p_to = MapSL(base); p_to->p_proto = pe->p_proto; p = (char *)(p_to + 1); p_to->p_name = base + (p - (char *)p_to); strcpy(p, pe->p_name); p += strlen(p) + 1; p_to->p_aliases = base + (p - (char *)p_to); list_dup(pe->p_aliases, p_to->p_aliases, 0); return base; }
Hash * hash_new_with(cList *keys) { Hash *cnew; Int i, j; /* Construct a new hash */ cnew = EMALLOC(Hash, 1); cnew->keys = list_dup(keys); /* Calculate initial size of chain and hash table. */ cnew->hashtab_size = HASHTAB_STARTING_SIZE; while (cnew->hashtab_size < keys->len) cnew->hashtab_size = cnew->hashtab_size * 2 + MALLOC_DELTA; /* Initialize chain entries and hash table. */ cnew->links = EMALLOC(Int, cnew->hashtab_size); cnew->hashtab = EMALLOC(Int, cnew->hashtab_size); memset(cnew->links, -1, sizeof(Long)*cnew->hashtab_size); memset(cnew->hashtab, -1, sizeof(Long)*cnew->hashtab_size); /* Insert the keys into the hash table, eliminating duplicates. */ i = j = 0; while (i < cnew->keys->len) { if (i != j) cnew->keys->el[j] = cnew->keys->el[i]; if (hash_find(cnew, &keys->el[i]) == F_FAILURE) quickhash_insert_key(cnew, j++); else data_discard(&cnew->keys->el[i]); i++; } cnew->keys->len = j; cnew->refs = 1; return cnew; }
cDict *dict_prep(cDict *dict) { cDict *cnew; if (dict->refs == 1) return dict; /* Duplicate the old dictionary. */ cnew = tmalloc(sizeof(cDict)); cnew->keys = list_dup(dict->keys); cnew->values = list_dup(dict->values); cnew->hashtab_size = dict->hashtab_size; cnew->links = tmalloc(sizeof(Int) * cnew->hashtab_size); cnew->hashtab = tmalloc(sizeof(Int) * cnew->hashtab_size); MEMCPY(cnew->links, dict->links, cnew->hashtab_size); MEMCPY(cnew->hashtab, dict->hashtab, cnew->hashtab_size); dict->refs--; cnew->refs = 1; return cnew; }
static LPARAM copy_he(void *base, int size, const struct WS_hostent *he) { char *p; int needed; struct WS_hostent *to = base; if (!he) return MAKELPARAM( 0, GetLastError() ); needed = sizeof(struct WS_hostent) + strlen(he->h_name) + 1 + list_size(he->h_aliases, 0) + list_size(he->h_addr_list, he->h_length ); if (size < needed) return MAKELPARAM( needed, WSAENOBUFS ); to->h_addrtype = he->h_addrtype; to->h_length = he->h_length; p = (char *)(to + 1); to->h_name = p; strcpy(p, he->h_name); p += strlen(p) + 1; to->h_aliases = (char **)p; p += list_dup(he->h_aliases, p, 0); to->h_addr_list = (char **)p; list_dup(he->h_addr_list, p, he->h_length); return MAKELPARAM( needed, 0 ); }
static LPARAM copy_pe(void *base, int size, const struct WS_protoent* pe) { char *p; int needed; struct WS_protoent *to = base; if (!pe) return MAKELPARAM( 0, GetLastError() ); needed = sizeof(struct WS_protoent) + strlen(pe->p_name) + 1 + list_size(pe->p_aliases, 0); if (size < needed) return MAKELPARAM( needed, WSAENOBUFS ); to->p_proto = pe->p_proto; p = (char *)(to + 1); to->p_name = p; strcpy(p, pe->p_name); p += strlen(p) + 1; to->p_aliases = (char **)p; list_dup(pe->p_aliases, p, 0); return MAKELPARAM( needed, 0 ); }
static LPARAM copy_se(void *base, int size, const struct WS_servent* se) { char *p; int needed; struct WS_servent *to = base; if (!se) return MAKELPARAM( 0, GetLastError() ); needed = sizeof(struct WS_servent) + strlen(se->s_proto) + strlen(se->s_name) + 2 + list_size(se->s_aliases, 0); if (size < needed) return MAKELPARAM( needed, WSAENOBUFS ); to->s_port = se->s_port; p = (char *)(to + 1); to->s_name = p; strcpy(p, se->s_name); p += strlen(p) + 1; to->s_proto = p; strcpy(p, se->s_proto); p += strlen(p) + 1; to->s_aliases = (char **)p; list_dup(se->s_aliases, p, 0); return MAKELPARAM( needed, 0 ); }
cList *list_union(cList * list1, cList * list2) { cData *start, *end, *d; start = list2->el + list2->start; end = start + list2->len; if (list1->len + list2->len < 12) { for (d = start; d < end; d++) { if (list_search(list1, d) == -1) list1 = list_add(list1, d); } } else { Hash *tmp; tmp = hash_new_with(list1); list_discard(list1); for (d = start; d < end; d++) { tmp = hash_add(tmp, d); } list1 = list_dup(tmp->keys); hash_discard(tmp); } return list1; }
static sql_rel * psm_analyze(mvc *sql, dlist *qname, dlist *columns, symbol *sample ) { exp_kind ek = {type_value, card_value, FALSE}; sql_exp *sample_exp = NULL, *call; char *sname = NULL, *tname = NULL; list *tl = sa_list(sql->sa); list *exps = sa_list(sql->sa), *analyze_calls = sa_list(sql->sa); sql_subfunc *f = NULL; if (sample) { sql_subtype *tpe = sql_bind_localtype("lng"); sample_exp = rel_value_exp( sql, NULL, sample, 0, ek); if (sample_exp) sample_exp = rel_check_type(sql, tpe, sample_exp, type_cast); } if (qname) { if (qname->h->next) sname = qname_schema(qname); else sname = qname_table(qname); if (!sname) sname = cur_schema(sql)->base.name; if (qname->h->next) tname = qname_table(qname); } /* call analyze( [schema, [ table ]], opt_sample_size ) */ if (sname) { sql_exp *sname_exp = exp_atom_clob(sql->sa, sname); append(exps, sname_exp); append(tl, exp_subtype(sname_exp)); } if (tname) { sql_exp *tname_exp = exp_atom_clob(sql->sa, tname); append(exps, tname_exp); append(tl, exp_subtype(tname_exp)); if (columns) append(tl, exp_subtype(tname_exp)); } if (!columns) { if (sample_exp) { append(exps, sample_exp); append(tl, exp_subtype(sample_exp)); } f = sql_bind_func_(sql->sa, mvc_bind_schema(sql, "sys"), "analyze", tl, F_PROC); if (!f) return sql_error(sql, 01, "Analyze procedure missing"); call = exp_op(sql->sa, exps, f); append(analyze_calls, call); } else { dnode *n; if (sample_exp) append(tl, exp_subtype(sample_exp)); f = sql_bind_func_(sql->sa, mvc_bind_schema(sql, "sys"), "analyze", tl, F_PROC); if (!f) return sql_error(sql, 01, "Analyze procedure missing"); for( n = columns->h; n; n = n->next) { char *cname = n->data.sval; list *nexps = list_dup(exps, NULL); sql_exp *cname_exp = exp_atom_clob(sql->sa, cname); append(nexps, cname_exp); if (sample_exp) append(nexps, sample_exp); /* call analyze( sname, tname, cname, opt_sample_size ) */ call = exp_op(sql->sa, nexps, f); append(analyze_calls, call); } } return rel_psm_block(sql->sa, analyze_calls); }
cList *dict_keys(cDict *dict) { return list_dup(dict->keys); }
cList *dict_values(cDict *dict) { return list_dup(dict->values); }