配列操作に関する関数
/* 配列への挿入 */
int insert_data(char *key, char *data, int i){
int id, n;
H_DATA *p, *tmp;
id = hash(key)%TABLE_SIZE_V;
p = getData(key, id);
if(i>=DATA_ARRAY_LEN){
printf("Over\n");
return 0;
}
if(p == NULL){
//データが見つからなかった場合(被らなかった場合)
p = (H_DATA *)malloc(sizeof(H_DATA));
p->key = (char *)malloc((strlen(key)+1)*sizeof(char));
if(p == NULL){
printf("no memory!\n");
exit(2);
}
p->list=NULL;
for(n=0; narray_data[n] = NULL;
}
p->array_data[i] = (char *)calloc(strlen(data)+1, sizeof(char));
strcpy(p->key,key);
if(HASH_DEBUG)printf("insert_key : %s\n",p->key);
if(HASH_DEBUG)printf("data : %s\n",data);
strcpy(p->array_data[i], data);
p->list = NULL;
p->next = NULL;
if(table[id]!=NULL){
for(tmp=table[id]; tmp->next; tmp=tmp->next);
tmp->next = p;
}else{
table[id] = p;
}
return 1;
}else {
//データが存在した場合
if(HASH_DEBUG)printf("re insert_data : %s\n",data);
if(HASH_DEBUG)printf("previous _data : %s\n",p->array_data[i]);
if(p->array_data[i] == NULL)
p->array_data[i] = (char *)calloc(strlen(data)+1,sizeof(char));
strcpy(p->array_data[i], data);
return 1;
}
return 0;
}
//指定した要素の値を表示
void printArrayValue(char *key, int i){
H_DATA *p;
int id;
if(i>=DATA_ARRAY_LEN){
printf("'%d' is too large element number to array\n",i);
return;
}
id = hash(key)%TABLE_SIZE_V;
p = getData(key, id);
if(p == NULL){
printf("no such variable %s\n",key);
return;
}else{
printf("%s[%d]=%s\n",key,i,p->array_data[i]);
}
}
//中身がある配列の値を列挙
void printArrayAll(char *key){
H_DATA *p;
int id, i;
id = hash(key)%TABLE_SIZE_V;
p = getData(key, id);
if(p == NULL){
printf("no such variable %s\n",key);
return;
}else{
for(i=0; iarray_data[i])
printf("%s[%d]=%s\n", key,i,p->array_data[i]);
}
}
}
//配列の一要素の削除
int index_delete(char *key, int d){
H_DATA *h_tmp;
int id, i;
id = hash(key)%TABLE_SIZE_V;
h_tmp = getData(key, id);
if(h_tmp == NULL){
printf("array_delete() : no such variable %s\n",key);
return(1);
}
if(h_tmp->array_data[d] != NULL){
free(h_tmp->array_data[d]);
h_tmp->array_data[d] = NULL;
}
//配列に何も入ってなければ配列自体を削除
for(i=0; iarray_data[i]!=NULL)break;
if(i==DATA_ARRAY_LEN)
h_list_del(h_tmp, id, key);
return 0;
}
//配列全体の削除
void array_delete(char *key){
H_DATA *h_tmp;
int id, i;
id = hash(key)%TABLE_SIZE_V;
h_tmp = getData(key, id);
if(h_tmp == NULL){
printf("array_delete() : no such variable %s\n",key);
return;
}
for(i=0; iarray_data[i] != NULL)
free(h_tmp->array_data[i]);
}
h_list_del(h_tmp, id, key);
}
リスト操作に関する関数
/* リストの作成 */
int insert_list(char *key, char *data){
int id;
H_DATA *p, *tmp;
VALUE *d, *d_tmp;
id = hash(key)%TABLE_SIZE_V;
d = (VALUE *)malloc(sizeof(VALUE));
d->list_data = (char *)calloc(strlen(data)+1, sizeof(char));
if(d==NULL){
printf("no memory!\n");
exit(2);
}
d->next = NULL;
strcpy(d->list_data, data);
p = getData(key, id);
if(p == NULL){
p = (H_DATA *)malloc(sizeof(H_DATA));
p->key = (char *)malloc((strlen(key)+1)*sizeof(char));
if(p==NULL){
printf("no memory!\n");
exit(2);
}
strcpy(p->key, key);
p->list = d;
p->next = NULL;
if(table[id]!=NULL){
for(tmp=table[id]; tmp->next; tmp=tmp->next);
tmp->next = p;
}else{
table[id] = p;
}
return 1;
}else{
for(d_tmp=p->list; d_tmp->next; d_tmp=d_tmp->next);
d_tmp->next = d;
return 1;
}
return 0;
}
//リストの全体を表示
void printListValue(char *key){
H_DATA *p;
VALUE *d;
int id;
id = hash(key)%TABLE_SIZE_V;
p = getData(key, id);
if(p==NULL){
printf("no such variable %s\n", key);
return;
}
d = p->list;
printf("%s=%s", p->key, d->list_data);
for(d=d->next; d; d = d->next)
printf("->%s", d->list_data);
printf("\n");
}
//リストの一つのデータの削除
int key_delete(char *key, char *data){
H_DATA *h_tmp;
VALUE *v_data, *v_tmp;
int id;
id = hash(key)%TABLE_SIZE_V;
h_tmp = getData(key, id);
v_data = h_tmp->list;
v_tmp = v_data->next;
if(strcmp(v_data->list_data, data)==0){
h_tmp->list = v_tmp;
free(v_data->list_data);
free(v_data);
if(h_tmp->list == NULL)
h_list_del(h_tmp, id, key);
return 1;
}
for(;v_tmp;v_data=v_data->next, v_tmp=v_tmp->next){
if(strcmp(v_tmp->list_data, data)==0){
v_data->next = v_tmp->next;
free(v_tmp->list_data);
free(v_tmp);
break;
}
}
if(h_tmp->list == NULL)
h_list_del(h_tmp, id, key);
return 1;
}
list_delete() & free_key()
//リスト全体(変数自体)の消去
void list_delete(char *key){
H_DATA *h_tmp;
int id;
id = hash(key)%TABLE_SIZE_V;
h_tmp = getData(key, id);
if(h_tmp==NULL){
printf("no such variable %s\n", key);
return;
}
free_key(h_tmp->list);
h_list_del(h_tmp, id, key);
}
void free_key(VALUE *d){
free(d->list_data);
if(d->next!=NULL)
free_key(d->next);
free(d);
}