📄 location.c
字号:
#include <stdio.h>
#include <string.h>
#include "utility.h"
#define HASHSTEP(hash, ch) (hash=(hash<<2)+ch)
long calc_hash(const char* name, long tabsize) //计算Hash值
{
unsigned hash = 0, i;
for(i=0;name[i];i++)
HASHSTEP(hash, name[i]);
hash = hash%tabsize;
return hash;
}
HashNode *add_node(
HashNode **hashtab,
long tabsize,
const char *name,
void *target)
{
unsigned hash;
HashNode **node, *i, *j;
//assert(hashtab&&name&&tabsize);
hash = calc_hash(name,tabsize);
if(!hashtab[hash])
node = &hashtab[hash];
else
{
//check if there is already a same
for(i = hashtab[hash];i;i = i->next)
{
if(!strcmp(i->name, name))
return i;
j=i;
}
node = &(j->next); //success
}
//construct a HashNode
i = (HashNode*)dmalloc(sizeof(HashNode), true);
i->code = hash;
i->val = target;
i->name = newstrcpy(name, UNKNOWN);
i->next = NULL;
*node = i;
return NULL;
}
void remove_node(
HashNode **hashtab,
long tabsize,
const char *name)
{
unsigned hash;
HashNode **node, *i;
hash = calc_hash(name,tabsize);
for(node =&hashtab[hash];*node; node=&((*node)->next))
{
i = *node;
if(!strcmp(i->name, name))
break;
}
if(*node)
{
*node = (*node)->next;
}
else
{
// debug("[remove_node]You try to delete a inexistent node!\n");
}
return ;
}
HashNode *lookup_node(
HashNode **hashtab,
long tabsize,
const char *name)
{
unsigned hash;
HashNode *i;
hash = calc_hash(name, tabsize);
for(i = hashtab[hash];i; i= i->next)
{
if(!strcmp(i->name, name))
return i;
}
return NULL;
}
void push_List(List *list, void* target)
{
ListNode *one = dmalloc(sizeof(ListNode), false);
one->pre=NULL;
one->next=NULL;
one->content = target;
if(!list->head)
list->head = one;
else
{
one->pre = list->tail;
list->tail->next = one;
}
list->tail=one;
}
void *popback_List(
List *list)
{
void *content=NULL;
if(list->tail)
{
content = list->tail->content;
list->tail = list->tail->pre;
}
if(list->tail)
list->tail->next = NULL;
else
list->head = NULL;
return content;
}
void *popfront_List(List* list)
{
void *content=NULL;
if(list->head)
{
content = list->head->content;
list->head = list->head->next;
}
if(list->head)
list->head->pre = NULL;
else
list->tail = NULL;
return content;
}
void insert_List(List *list,
ListNode *lnode,
void *target)
{
ListNode *one = dmalloc(sizeof(ListNode), false);
one->content = target;
one->pre = lnode;
if(lnode == NULL)
{
one->next = list->head;
if(list->head)
{
list->head->pre = one;
list->head = one;
}
else
list->head = list->tail = one;
return ;
}
one->next = lnode->next;
lnode->next = one;
if(one->next)
one->next->pre = one;
if(lnode == list->tail)
list->tail = one;
return ;
}
void appendObject(void **origin, void *extra)
{
Object *i=*origin;
if(!i)
*(Object**)origin = extra;
else
{
for(;i->next;i=i->next);
i->next = extra;
}
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -