/********************************************************************** * FUNCTION NAME: * add_node * * DESCRIPTION: * This function add node to the tree * * INTERFACE: * GLOBAL DATA: * None * * INPUT: * parent --parent node pointer * new_node--node which want to be added * * OUTPUT: * None * * INPUT/OUTPUT: * None * * AUTHOR: * Fu Pei * * RETURN VALUE: * * if add node failed return the error type otherwise return PROCESS_SUCCESS * * NOTES: * *********************************************************************/ int add_element(node_t* parent,node_t* new_node) { int ret = PROCESS_SUCCESS; int type = 0; node_t *root = NULL; node_t* chld = NULL; if ((NULL == parent) || (NULL == new_node)) { ret = ADD_ELEM_FAILED; } /*get root element*/ root = get_root(parent); if (root == &(xml_node_array[0])) { type = FIRST_XML; } else { type = SECOND_XML; } /*doesn't have child*/ if (parent->child == NULL) { /*alloc memory*/ ret = malloc_node(1,&(parent->child),type); /*can we add node to tree*/ if ((ret != ADD_ELEM_FAILED) && (ret != OUT_OF_MEMORY) ) { parent->child->state = ALLOCTE; copy_node_data(parent->child,new_node); } } else { /*get last children*/ node_t* last_child = parent->child; while (last_child) { last_child = last_child->sibl; } /*alloc memory*/ ret = malloc_node(1,&(last_child),type); /*can we add node to tree*/ if ((ret != ADD_ELEM_FAILED) && (ret != OUT_OF_MEMORY) ) { last_child->state = ALLOCTE; copy_node_data(last_child,new_node); } } return ret; }
void add_item(struct list* _hashmap[], int _data) { int hash_code = hash(_data); struct list* list = _hashmap[hash_code]; struct node* node = malloc_node(_data); add_node(list, node); }
Node* tree_insert(Node* &T, Interval key) { // 分配新节点 Node *n = malloc_node(key); // 如果当前是空树,则n节点作为根,刷成黑色即满足红黑性质 if (NIL == T){ SET_BLACK(n); T = n; return n; } // 如果当前不是空树,则先找到插入位置 Node *p = T; Node *q; while(NIL != p) { // 调整路径上节点的max值 if (MAX(p) < HIGH(key)) MAX(p) = HIGH(key); q = p; if (LT(key,KEY(p))) p = LEFT(p); else p = RIGHT(p); } // 找到了插入位置,n节点的父亲为q PARENT(n) = q; if (LT(key, KEY(q))) { LEFT(q) = n; } else { RIGHT(q) = n; } // 设置n节点为红色 // 这样只可能违背红黑性质4(性质4的调整应该比性质5简单) SET_RED(n); // 从n节点开始调整,使之符合红黑性质4 // 调整的每一步,都不会破坏其他红黑性质 rb_insert_fixup(T, n); return n; }
Node* tree_insert(Node* &T, int key) { // 分配新节点 Node *n = malloc_node(key); // 如果当前是空树,则n节点作为根,刷成黑色即满足红黑性质 if (NIL == T){ SET_BLACK(n); T = n; return n; } // 如果当前不是空树,则先找到插入位置 Node *p = T; Node *q; while(NIL != p) { // 每经过一个节点,其size加一 SIZE(p)++; q = p; if (key < KEY(p)) p = LEFT(p); else p = RIGHT(p); } // 找到了插入位置,n节点的父亲为q PARENT(n) = q; if (key < KEY(q)) { LEFT(q) = n; } else { RIGHT(q) = n; } // 设置n节点为红色 // 这样只可能违背红黑性质4(性质4的调整应该比性质5简单) SET_RED(n); // 从n节点开始调整,使之符合红黑性质4 // 调整的每一步,都不会破坏其他红黑性质 rb_insert_fixup(T, n); return n; }
struct HashIdx::VElt *HashIdx::insertobj(const char *key, int length, Db *dbptr,short numOfPartitions) { if (key == NULL || length == 0) { return NULL; } struct VElt *finger = malloc_node(); #ifdef VALIDATE if (finger == NULL) { printf("HashIdx:insertobj: ERROR, malloc noded failed with key %d and length %d", (int) *key, length); return NULL; } #endif int Mindex; if(bernsteinHash==true) Mindex=TCUtility::bernsteinHash(key,length,MAXRES); else Mindex = h_hash(key, length); init_res_node(finger, NULL, NULL, key, length,dbptr,numOfPartitions); #ifdef VERBOSE printf("HashIdx:insertobj: hash index %d, length %d, pval %.20f\n",Mindex, length, pval); #endif EnterCriticalSection(hashtable[Mindex].concurrent); if (hashtable[Mindex].ptr == NULL) hashtable[Mindex].ptr = finger; else { finger->flink = hashtable[Mindex].ptr; hashtable[Mindex].ptr -> blink = finger; hashtable[Mindex].ptr = finger; } LeaveCriticalSection(hashtable[Mindex].concurrent); return finger; }
perfmgr_db_err_t perfmgr_db_create_entry(perfmgr_db_t * db, uint64_t guid, boolean_t esp0, uint8_t num_ports, char *name) { perfmgr_db_err_t rc = PERFMGR_EVENT_DB_SUCCESS; cl_plock_excl_acquire(&db->lock); if (!get(db, guid)) { db_node_t *pc_node = malloc_node(guid, esp0, num_ports, name); if (!pc_node) { rc = PERFMGR_EVENT_DB_NOMEM; goto Exit; } if (insert(db, pc_node)) { free_node(pc_node); rc = PERFMGR_EVENT_DB_FAIL; goto Exit; } } Exit: cl_plock_release(&db->lock); return rc; }
struct cqueue* create_queue_node(void* element) { struct cqueue* tmp = malloc_node(); tmp->e = element; return tmp; }