Exemplo n.º 1
0
void consolidate(struct priority_queue *pq){
	int i,x,d;
	
	struct node *t1,*t2,*temp;
	x = pq->n_nodes * (ceil(log(pq->init_n)/log((1+sqrt(5))/2)));
	struct node *array[x];

	for(i = 0; i < x; i++)
		array[i] = NULL;
	
	
	t1 = pq->root_list;
	temp = t1->right;

	for(i = 1; i <= pq->n_nodes; i++){	
		d = t1->degree;
		while(array[d] != 0){
			t2 = array[d];
			if(t1->key < t2->key)	
				t1 = link_node(t1,t2);
			else
				t1 = link_node(t2,t1);
			array[d] = 0;
			d = d+1;
		}
		array[d] = t1;
		t1 = temp;
		temp = temp->right;
	}
	pq->root_list = NULL;
	pq->n_nodes = 0;
	for(i = 0; i < x; i++){
		if(array[i] != 0){
			if(pq->root_list == NULL){
				pq->root_list = array[i];
				pq->root_list->left = pq->root_list->right = pq->root_list;
				pq->n_nodes = 1;
			}
			else{	
				array[i]->left = pq->root_list;
				array[i]->right = pq->root_list->right;
				pq->root_list->right->left = array[i];
				pq->root_list->right = array[i];
				pq->n_nodes += 1;
				if(array[i]->key < pq->root_list->key)
					pq->root_list = array[i] ;
			}
		}
	}
}
/*!\fn int list_push_sorted( LIST *list , void *ptr , int (*comparator)( const void *a , const void *b ) , void (*destructor)( void *ptr ) )
 *\brief Add a pointer sorted in the list , starting by the end of the list.
 *\param list An initilized list container. A null value will cause an error and a _log message.
 *\param ptr The pointer you wan to put in the list. A null value will cause an error and a _log message.
 *\param comparator A pointer to a function which take two void * pointers and return an int.
 *\param destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't.
 *\return TRUE or FALSE. Check error messages in DEBUG mode.
 */
int list_push_sorted( LIST *list , void *ptr , int (*comparator)( const void *a , const void *b ) , void (*destructor)( void *ptr ) )
{
   LIST_NODE *nodeptr = NULL ;

   n_assert( list , n_log( LOG_ERR , "invalid list: NULL" ); return FALSE );
   n_assert( ptr  , n_log( LOG_ERR , "invalid ptr: NULL" );  return FALSE ); 

   if( list -> nb_max_items > 0 && ( list -> nb_items >= list -> nb_max_items ) )
   {
      n_log( LOG_ERR , "list is full" );
      return FALSE ;
   }

   if( list -> end )
   {
      nodeptr = list -> end ;
      while( nodeptr && ( comparator( ptr , nodeptr -> ptr ) < 0 ) )
         nodeptr = nodeptr -> prev ;

      if( !nodeptr )
      {
         /* It's the lower ranked element in the sort */
         list_unshift( list , ptr , destructor );
      }   
      else
      {
         /* we have a match inside the list. let's insert the datas */
         LIST_NODE *node_next = nodeptr -> next  ;
         LIST_NODE *newnode = new_list_node( ptr , destructor );
         n_assert( newnode , n_log( LOG_ERR , "Couldn't allocate new node" ); return FALSE );

         if( node_next )
         {
            link_node( newnode , node_next );
         }   
         else 
            list -> end = newnode ;

         link_node( nodeptr , newnode );
         list -> nb_items ++ ;
      }
   }
Exemplo n.º 3
0
//插入樹結點
struct node * insert_node(struct node *current_node, short value, struct header_table *htable, int counter) {		
	if(current_node->child == NULL) {
		//list *new_list 	      = listCreate(0);		
		current_node->child   = listCreate();
		current_node 		  = link_node(current_node, value);	
		add_header_table(htable, current_node, value);
		current_node->counter = counter;
	}
	else {
		node_tmp = node_get(current_node->child, value, node_tmp);
		if(node_tmp != NULL) {
			current_node 		  = node_tmp;
			current_node->counter = current_node->counter + 1;
		}
		else {
			current_node = link_node(current_node, value);
			add_header_table(htable, current_node, value);
			current_node->counter = counter;
		}
	}
		
	return current_node;
}
Exemplo n.º 4
0
struct usbdev *
create_device(struct usbmgr_device_descriptor desc)
{
	struct usbdev *dev;

	dev = (struct usbdev *)Malloc(sizeof(struct usbdev));
	if (dev == NULL)
		return NULL;
	INIT_NODE(&(dev->link));
	dev->desc = desc;
	dev->status = USBMGR_ST_UNKNOWN;
	dev->type = USBMGR_TYPE_NONE;
	link_node(&device_ring,&(dev->link));

	return dev;
}
Exemplo n.º 5
0
struct node * insert_sub_node(struct node *current_node, short value, struct node *tmp, struct header_table *htable) {		
	if(current_node->child == NULL) {
		list *new_list 	      = listCreate();				
		//current_node->child   = new_list;
		//current_node->no = 100;
		//current_node 		  = link_node(current_node, value);	
		//add_header_table(htable, current_node, value);
		
	}
	else {
		tmp = node_get(current_node->child, value, tmp);
		if(tmp != NULL) {
			current_node 		  = tmp;
			current_node->counter = current_node->counter + 1;
		}
		else {
			current_node = link_node(current_node, value);
			add_header_table(htable, current_node, value);
		}
	}
		
	return current_node;
}
Exemplo n.º 6
0
/**
 * @brief
 *	add node to json list
 *
 * @param[in] ntype - node type
 * @param[in] vtype - value type
 * @param[in] key - node key
 * @param[in] value - value for node
 *
 * @return 	structure handle
 * @retval	structure handle to JsonNode list	success
 * @retval	NULL					error
 *
 */
JsonNode*
add_json_node(JsonNodeType ntype, JsonValueType vtype, JsonEscapeType esc_type, char *key, void *value) {
	int 	  rc = 0;
	char	  *ptr = NULL;
	char 	  *pc  = NULL;
	JsonNode  *node = NULL;

	node = create_json_node();
	if (node == NULL) {
		fprintf(stderr, "Json Node: out of memory\n");
		return NULL;
	}
	node->node_type = ntype;
	if (key != NULL) {
		ptr = strdup((char *)key);
		if (ptr == NULL) {
			fprintf(stderr, "Json Node: out of memory\n");
			return NULL;
		}
		node->key = ptr;
	}
	if (vtype == JSON_NULL && value != NULL) {
		(void)strtod(value, &pc);
		while (pc) {
			if (isspace(*pc))
				pc++;
			else
				break;
		}
		if (strcmp(pc, "") == 0) {
			node->value_type = JSON_NUMERIC;
			ptr = strdup(value);
			if (ptr == NULL)
				return NULL;
			node->value.string = ptr;
		} else
			node->value_type = JSON_STRING;
   	} else {
		node->value_type = vtype;
		if (node->value_type == JSON_INT)
			node->value.inumber = *((long int *)value);
		else if (node->value_type == JSON_FLOAT)
			node->value.fnumber = *((double *)value);
    	}
            
	if (node->value_type == JSON_STRING) {
		if (value != NULL) {
			ptr = strdup_escape(esc_type, value);
			if (ptr == NULL)
				return NULL;
		}
		node->value.string = ptr;
	}
	rc = link_node(node);
	if (rc) {
		free(node);
		fprintf(stderr, "Json link: out of memory\n");
		return NULL;
	}
	return node;
}
Exemplo n.º 7
0
/**
 * @brief
 *	add node to json list
 *
 * @param[in] ntype - node type
 * @param[in] vtype - value type
 * @param[in] key - node key
 * @param[in] value - value for node
 *
 * @return 	structure handle
 * @retval	structure handle to JsonNode list	success
 * @retval	NULL					error
 *
 */
JsonNode*
add_json_node(JsonNodeType ntype, JsonValueType vtype, JsonEscapeType esc_type, char *key, void *value) {
	int 	  rc = 0;
	char	  *ptr = NULL;
	char 	  *pc  = NULL;
	double    val  = 0;
	long int  ivalue = 0;
	JsonNode  *node = NULL;

	node = create_json_node();
	if (node == NULL) {
		fprintf(stderr, "Json Node: out of memory\n");
		return NULL;
	}
	node->node_type = ntype;
	if (key != NULL) {
		ptr = strdup((char *)key);
		if (ptr == NULL) {
			fprintf(stderr, "Json Node: out of memory\n");
			return NULL;
		}
		node->key = ptr;
	}
	if (vtype == JSON_NULL && value != NULL) {
		val = strtod(value, &pc);
		while (pc) {
			if (isspace(*pc))
				pc++;
			else
				break;
		}
		if (strcmp(pc, "") == 0) {
			ivalue = (long int) val;
			if (val == ivalue) {/* This checks if value have any non zero fractional part after decimal. If not then value has to be represented as integer otherwise as float. */
				node->value_type = JSON_INT;
				node->value.inumber = ivalue;
			} else {
				node->value_type = JSON_FLOAT;
				node->value.fnumber = val;
			}
		} else
			node->value_type = JSON_STRING;
	}
	else {
		node->value_type = vtype;
		if (node->value_type == JSON_INT)
			node->value.inumber = *((long int *)value);
		else if (node->value_type == JSON_FLOAT)
			node->value.fnumber = *((double *)value);
	}


	if (node->value_type == JSON_STRING) {
		if (value != NULL) {
			ptr = strdup_escape(esc_type, value);
			if (ptr == NULL)
				return NULL;
		}
		node->value.string = ptr;
	}
	rc = link_node(node);
	if (rc) {
		free(node);
		fprintf(stderr, "Json link: out of memory\n");
		return NULL;
	}
	return node;
}